diff --git a/out/github-action.cjs b/out/github-action.cjs index 7e08289..8354fb0 100644 --- a/out/github-action.cjs +++ b/out/github-action.cjs @@ -27914,31 +27914,42 @@ async function getCommitDiff(commitSha) { async function improveCommitMessagesWithRebase(commits) { let commitsToImprove = pattern ? commits.filter(({ commit }) => new RegExp(pattern).test(commit.message)) : commits; if (!commitsToImprove.length) { - ce('No commits with a message "oc" found.'); - ce( - 'If you want OpenCommit Action to generate a commit message for you \u2014 commit the message as two letters: "oc"' - ); + ce("No new commits found."); return; } - ce(`Found ${commitsToImprove.length} commits, improving`); + ce(`Found ${commitsToImprove.length} commits to improve.`); const commitShas = commitsToImprove.map((commit) => commit.sha); const diffPromises = commitShas.map((sha) => getCommitDiff(sha)); - const commitDiffBySha = await Promise.all(diffPromises).then( - (results) => results.reduce((acc, result) => { - acc[result.sha] = result.diff; - return acc; - }, {}) + ce("Fetching commit diffs by SHAs."); + const commitDiffs = await Promise.all( + diffPromises ).catch((error) => { ce(`error in Promise.all(getCommitDiffs(SHAs)): ${error}`); throw error; }); - ce("Starting interactive rebase: `$ rebase -i`."); + ce("Done."); + ce("Improving commit messages by diffs."); + const improvePromises = commitDiffs.map( + (commit) => generateCommitMessageByDiff(commit.diff) + ); + const improvedMessagesBySha = await Promise.all(improvePromises).then((results) => { + return results.reduce((acc, improvedMsg, i2) => { + acc[commitDiffs[i2].sha] = improvedMsg; + return acc; + }, {}); + }).catch((error) => { + ce(`error in Promise.all(getCommitDiffs(SHAs)): ${error}`); + throw error; + }); + ce("Done."); + ce( + `Starting interactive rebase: "$ rebase -i ${commitsToImprove[0].parents[0].sha}".` + ); await execa("git", ["rebase", "-i", commitsToImprove[0].parents[0].sha]); for (const commit of commitsToImprove) { try { - const commitDiff = commitDiffBySha[commit.sha]; - const improvedMessage = await generateCommitMessageByDiff(commitDiff); - await execa("git", ["commit", "--amend", "-m", improvedMessage]); + const commitDiff = improvedMessagesBySha[commit.sha]; + await execa("git", ["commit", "--amend", "-m", commitDiff]); await execa("git", ["rebase", "--continue"]); } catch (error) { throw error; @@ -27950,7 +27961,7 @@ async function improveCommitMessagesWithRebase(commits) { } ce("Force pushing interactively rebased commits into remote origin."); await execa("git", ["push", "origin", `+${context.ref}`]); - ce("Done \u{1F64F}"); + ce("Done \u23F1\uFE0F"); } async function run(retries = 3) { ae("OpenCommit \u2014 improving commit messages with GPT"); @@ -27969,12 +27980,11 @@ async function run(retries = 3) { pull_number: payload.pull_request.number }); const commits = commitsResponse.data; - ce("testing outro"); await improveCommitMessagesWithRebase(commits); } else { - ce("wrong action"); + ce("Wrong action."); import_core4.default.error( - `OpenCommit was called on ${import_github.default.context.payload.action}. OpenCommit is not supposed to be used on actions other from "pull_request.opened" and "push".` + `OpenCommit was called on ${import_github.default.context.payload.action}. OpenCommit is not supposed to be used on actions other from "pull_request.opened" and "pull_request.synchronize".` ); } } catch (error) { diff --git a/src/github-action.ts b/src/github-action.ts index 229a53c..2e588f2 100644 --- a/src/github-action.ts +++ b/src/github-action.ts @@ -24,7 +24,7 @@ type CommitsArray = CommitsData['data']; type SHA = string; type Diff = string; -type DiffBySHA = Record; +type MessageBySha = Record; async function getCommitDiff(commitSha: string) { const diffResponse = await octokit.request( @@ -47,40 +47,52 @@ async function improveCommitMessagesWithRebase(commits: CommitsArray) { : commits; if (!commitsToImprove.length) { - outro('No commits with a message "oc" found.'); - outro( - 'If you want OpenCommit Action to generate a commit message for you — commit the message as two letters: "oc"' - ); + outro('No new commits found.'); + return; } - outro(`Found ${commitsToImprove.length} commits, improving`); + outro(`Found ${commitsToImprove.length} commits to improve.`); const commitShas = commitsToImprove.map((commit) => commit.sha); const diffPromises = commitShas.map((sha) => getCommitDiff(sha)); - const commitDiffBySha: DiffBySHA = await Promise.all(diffPromises) - .then((results) => - results.reduce((acc, result) => { - acc[result.sha] = result.diff; + outro('Fetching commit diffs by SHAs.'); + const commitDiffs: { sha: string; diff: string }[] = await Promise.all( + diffPromises + ).catch((error) => { + outro(`error in Promise.all(getCommitDiffs(SHAs)): ${error}`); + throw error; + }); + outro('Done.'); + + outro('Improving commit messages by diffs.'); + const improvePromises = commitDiffs.map((commit) => + generateCommitMessageByDiff(commit.diff) + ); + const improvedMessagesBySha: MessageBySha = await Promise.all(improvePromises) + .then((results) => { + return results.reduce((acc, improvedMsg, i) => { + acc[commitDiffs[i].sha] = improvedMsg; return acc; - }, {} as DiffBySHA) - ) + }, {} as MessageBySha); + }) .catch((error) => { outro(`error in Promise.all(getCommitDiffs(SHAs)): ${error}`); throw error; }); + outro('Done.'); - outro('Starting interactive rebase: `$ rebase -i`.'); + outro( + `Starting interactive rebase: "$ rebase -i ${commitsToImprove[0].parents[0].sha}".` + ); await execa('git', ['rebase', '-i', commitsToImprove[0].parents[0].sha]); for (const commit of commitsToImprove) { try { - const commitDiff = commitDiffBySha[commit.sha]; + const commitDiff = improvedMessagesBySha[commit.sha]; - const improvedMessage = await generateCommitMessageByDiff(commitDiff); - - await execa('git', ['commit', '--amend', '-m', improvedMessage]); + await execa('git', ['commit', '--amend', '-m', commitDiff]); await execa('git', ['rebase', '--continue']); } catch (error) { throw error; @@ -96,7 +108,7 @@ async function improveCommitMessagesWithRebase(commits: CommitsArray) { // Force push the rebased commits await execa('git', ['push', 'origin', `+${context.ref}`]); - outro('Done 🙏'); + outro('Done ⏱️'); } async function run(retries = 3) { @@ -120,13 +132,11 @@ async function run(retries = 3) { const commits = commitsResponse.data; - outro('testing outro'); - await improveCommitMessagesWithRebase(commits); } else { - outro('wrong action'); + outro('Wrong action.'); core.error( - `OpenCommit was called on ${github.context.payload.action}. OpenCommit is not supposed to be used on actions other from "pull_request.opened" and "push".` + `OpenCommit was called on ${github.context.payload.action}. OpenCommit is not supposed to be used on actions other from "pull_request.opened" and "pull_request.synchronize".` ); } } catch (error: any) {