Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
SHSharkar committed Nov 15, 2023
1 parent ec95bf1 commit e174018
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ uncaughtExceptions.log
.vscode
src/*.json
.idea
test.ts
test.ts
/.npmrc
4 changes: 2 additions & 2 deletions out/cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -34168,9 +34168,9 @@ Z2(
async () => {
await checkIsLatestVersion();
if (await isHookCalled()) {
prepareCommitMessageHook();
await prepareCommitMessageHook();
} else {
commit(extraArgs);
await commit(extraArgs);
}
},
extraArgs
Expand Down
20 changes: 11 additions & 9 deletions out/github-action.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -55842,11 +55842,10 @@ async function improveMessagesInChunks(diffsAndSHAs) {
}
var getDiffsBySHAs = async (SHAs) => {
const diffPromises = SHAs.map((sha) => getCommitDiff(sha));
const diffs = await Promise.all(diffPromises).catch((error) => {
return await Promise.all(diffPromises).catch((error) => {
$e(`Error in Promise.all(getCommitDiffs(SHAs)): ${error}.`);
throw error;
});
return diffs;
};
async function improveCommitMessages(commitsToImprove) {
if (commitsToImprove.length) {
Expand All @@ -55861,10 +55860,7 @@ async function improveCommitMessages(commitsToImprove) {
$e("Done.");
const improvedMessagesWithSHAs = await improveMessagesInChunks(diffsWithSHAs);
console.log(`Improved ${improvedMessagesWithSHAs.length} commits: `, improvedMessagesWithSHAs);
const messagesChanged = improvedMessagesWithSHAs.some(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ sha, msg }, index) => msg !== commitsToImprove[index].message
);
const messagesChanged = improvedMessagesWithSHAs.some(({ msg }, index) => msg !== commitsToImprove[index].message);
if (!messagesChanged) {
console.log("No changes in commit messages detected, skipping rebase");
return;
Expand Down Expand Up @@ -55916,11 +55912,17 @@ async function run() {
);
}
} catch (error) {
const err = error?.message || error;
import_core20.default.setFailed(err);
if (error instanceof Error) {
import_core20.default.setFailed(error.message);
} else {
import_core20.default.setFailed(String(error) || "An unknown error occurred");
}
}
}
run();
run().catch((error) => {
console.error("An error occurred:", error);
import_core20.default.setFailed(error instanceof Error ? error.message : "An unknown error occurred");
});
/*! Bundled license information:

undici/lib/fetch/body.js:
Expand Down
5 changes: 3 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios';
import chalk from 'chalk';
import { execa } from 'execa';
import OpenAI from 'openai';
import { ChatCompletionMessageParam } from 'openai/resources';

import { CONFIG_MODES, DEFAULT_MODEL_TOKEN_LIMIT, getConfig } from './commands/config';
import { GenerateCommitMessageErrorEnum } from './generateCommitMessageFromGitDiff';
Expand Down Expand Up @@ -33,8 +34,8 @@ class OpenAi {
constructor() {}

public generateCommitMessage = async (
messages: Array<{ role: string; content: string }>,
): Promise<string | undefined> => {
messages: Array<ChatCompletionMessageParam>,
): Promise<string | null | undefined> => {
const params = {
model: MODEL,
messages,
Expand Down
6 changes: 4 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { cli } from 'cleye';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import packageJSON from '../package.json';
import { commit } from './commands/commit';
import { commitlintConfigCommand } from './commands/commitlint';
Expand All @@ -25,9 +27,9 @@ cli(
await checkIsLatestVersion();

if (await isHookCalled()) {
prepareCommitMessageHook();
await prepareCommitMessageHook();
} else {
commit(extraArgs);
await commit(extraArgs);
}
},
extraArgs,
Expand Down
26 changes: 14 additions & 12 deletions src/github-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function improveMessagesInChunks(diffsAndSHAs: DiffAndSHA[]) {
improvedMessagesAndSHAs.push(...chunkOfImprovedMessagesBySha);

// sometimes openAI errors with 429 code (too many requests),
// so lets sleep a bit
// so let's sleep a bit
const sleepFor = 1000 * randomIntFromInterval(1, 5) + 100 * randomIntFromInterval(1, 5);

outro(`Improved ${chunkOfPromises.length} messages. Sleeping for ${sleepFor}`);
Expand All @@ -94,12 +94,10 @@ async function improveMessagesInChunks(diffsAndSHAs: DiffAndSHA[]) {
const getDiffsBySHAs = async (SHAs: string[]) => {
const diffPromises = SHAs.map((sha) => getCommitDiff(sha));

const diffs = await Promise.all(diffPromises).catch((error) => {
return await Promise.all(diffPromises).catch((error) => {
outro(`Error in Promise.all(getCommitDiffs(SHAs)): ${error}.`);
throw error;
});

return diffs;
};

async function improveCommitMessages(commitsToImprove: { id: string; message: string }[]): Promise<void> {
Expand All @@ -121,10 +119,7 @@ async function improveCommitMessages(commitsToImprove: { id: string; message: st
console.log(`Improved ${improvedMessagesWithSHAs.length} commits: `, improvedMessagesWithSHAs);

// Check if there are actually any changes in the commit messages
const messagesChanged = improvedMessagesWithSHAs.some(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ sha, msg }, index) => msg !== commitsToImprove[index].message,
);
const messagesChanged = improvedMessagesWithSHAs.some(({ msg }, index) => msg !== commitsToImprove[index].message);

if (!messagesChanged) {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -197,10 +192,17 @@ async function run() {
`GitWiz was called on ${github.context.payload.action}. GitWiz is supposed to be used on "push" action.`,
);
}
} catch (error: any) {
const err = error?.message || error;
core.setFailed(err);
} catch (error: unknown) {
if (error instanceof Error) {
core.setFailed(error.message);
} else {
core.setFailed(String(error) || 'An unknown error occurred');
}
}
}

run();
run().catch((error) => {
// eslint-disable-next-line no-console
console.error('An error occurred:', error);
core.setFailed(error instanceof Error ? error.message : 'An unknown error occurred');
});
6 changes: 4 additions & 2 deletions src/utils/tokenCount.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import cl100k_base from '@dqbd/tiktoken/encoders/cl100k_base.json';
import { Tiktoken } from '@dqbd/tiktoken/lite';
import OpenAI from 'openai';
import ChatCompletionContentPart = OpenAI.ChatCompletionContentPart;

export function tokenCount(content: string): number {
export function tokenCount(content: string | null | Array<ChatCompletionContentPart>): number {
const encoding = new Tiktoken(cl100k_base.bpe_ranks, cl100k_base.special_tokens, cl100k_base.pat_str);
const tokens = encoding.encode(content);
const tokens = encoding.encode(<string>content);
encoding.free();
return tokens.length;
}

0 comments on commit e174018

Please sign in to comment.