From 1a1dab144c3664091639181e7f4ba41790112c62 Mon Sep 17 00:00:00 2001 From: David Murphy Date: Tue, 29 Nov 2022 19:58:51 +0000 Subject: [PATCH] feat(close-pr-support): Enable commit detection when tag search fails by failing back to looking at commits in closed & merged PR --- src/action.ts | 1 + src/utils.ts | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/action.ts b/src/action.ts index 2a073169e..4d3f8f098 100644 --- a/src/action.ts +++ b/src/action.ts @@ -122,6 +122,7 @@ export default async function main() { core.setOutput('previous_tag', previousTag.name); commits = await getCommits(previousTag.commit.sha, commitRef); + core.debug('We found ' + commits.length + ' commits to consider!'); let bump = await analyzeCommits( { diff --git a/src/utils.ts b/src/utils.ts index 05b1247f3..ac3eb89c5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,6 +5,7 @@ import DEFAULT_RELEASE_TYPES from '@semantic-release/commit-analyzer/lib/default import { compareCommits, listTags } from './github'; import { defaultChangelogRules } from './defaults'; import { Await } from './ts'; +import { context } from '@actions/github'; type Tags = Await>; @@ -30,21 +31,60 @@ export async function getValidTags( return validTags; } +interface FinalCommit { + sha: string | null; + commit: { + message: string; + }; +} export async function getCommits( baseRef: string, headRef: string ): Promise<{ message: string; hash: string | null }[]> { - const commits = await compareCommits(baseRef, headRef); + let commits: Array; + commits = await compareCommits(baseRef, headRef); + core.info('We found ' + commits.length + ' commits using classic compare!'); + if (commits.length < 1) { + core.info( + 'We did not find enough commits, attempting to scan closed PR method.' + ); + commits = getClosedPRCommits(); + } + if (commits.length == 0) { + return []; + } return commits - .filter((commit) => !!commit.commit.message) - .map((commit) => ({ + .filter((commit: FinalCommit) => !!commit.commit.message) + .map((commit: FinalCommit) => ({ message: commit.commit.message, hash: commit.sha, })); } +function getClosedPRCommits() { + let commits = Array(); + if (!('pull_request' in context.payload)) { + core.debug('We are in a closed PR context continuing.'); + core.debug(JSON.stringify(context.payload.commits)); + let pr_commit_count = context.payload.commits.length; + core.info( + 'We found ' + pr_commit_count + ' commits from the Closed PR method.' + ); + commits = context.payload.commits + .filter((commit: FinalCommit) => !!commit.commit.message) + .filter((commit: FinalCommit) => ({ + message: commit.commit.message, + hash: commit.sha, + })); + core.debug( + 'After processing we are going to present ' + commits.length + ' commits!' + ); + } + return commits; +} + export function getBranchFromRef(ref: string) { return ref.replace('refs/heads/', ''); }