diff --git a/.github/workflows/greetings.yml b/.github/workflows/greetings.yml index d3ebb714e66..12b4c79031f 100644 --- a/.github/workflows/greetings.yml +++ b/.github/workflows/greetings.yml @@ -7,6 +7,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/first-interaction@v1 + continue-on-error: true with: repo-token: ${{ secrets.GITHUB_TOKEN }} issue-message: "Thanks for reporting this issue, don't forget to star this project if you haven't already to help us reach a wider audience." diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 612b9badb05..f1ffad983c1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -290,7 +290,11 @@ jobs: name: Test using branch names for base_sha and sha inputs runs-on: ubuntu-latest needs: build - if: needs.build.outputs.files_changed != 'true' + if: | + ( + github.event_name == 'push' || + github.event_name == 'pull_request' + ) && needs.build.outputs.files_changed != 'true' steps: - name: Checkout branch diff --git a/src/commitSha.ts b/src/commitSha.ts index 17ca5bcbc38..b333339fbaa 100644 --- a/src/commitSha.ts +++ b/src/commitSha.ts @@ -5,6 +5,7 @@ import {Env} from './env' import {Inputs} from './inputs' import { canDiffCommits, + cleanShaInput, getCurrentBranchName, getHeadSha, getParentSha, @@ -23,7 +24,7 @@ const getCurrentSHA = async ({ inputs: Inputs workingDirectory: string }): Promise => { - let currentSha = inputs.sha + let currentSha = await cleanShaInput(inputs.sha) core.debug('Getting current SHA...') if (inputs.until) { @@ -162,7 +163,7 @@ export const getSHAForNonPullRequestEvent = async ( } const currentSha = await getCurrentSHA({inputs, workingDirectory}) - let previousSha = inputs.baseSha + let previousSha = await cleanShaInput(inputs.baseSha) const diff = '..' const currentBranchName = await getCurrentBranchName({cwd: workingDirectory}) @@ -390,7 +391,7 @@ export const getSHAForPullRequestEvent = async ( } const currentSha = await getCurrentSHA({inputs, workingDirectory}) - let previousSha = inputs.baseSha + let previousSha = await cleanShaInput(inputs.baseSha) let diff = '...' if (previousSha && currentSha && currentBranch && targetBranch) { diff --git a/src/utils.ts b/src/utils.ts index 1fb756c2f59..ed1cba3612e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -771,6 +771,52 @@ export const verifyCommitSha = async ({ return exitCode } +/** + * Clean the sha from the input which could be a branch name or a commit sha. + * + * If the input is a valid commit sha, return it as is. + * + * If the input is a branch name, get the HEAD sha of that branch and return it. + * + * @param sha The input string, which could be a branch name or a commit sha. + * @returns The cleaned SHA string. + * @throws Error If the input is not a valid commit sha or a branch name. + */ +export const cleanShaInput = async (sha: string): Promise => { + // Check if the input is a valid commit sha + if (!sha) { + return sha + } + // Check if the input is a valid commit sha + const {stdout, exitCode} = await exec.getExecOutput( + 'git', + ['rev-parse', '--verify', sha], + { + ignoreReturnCode: true, + silent: !core.isDebug() + } + ) + + if (exitCode !== 0) { + // If it's not a valid commit sha, assume it's a branch name and get the HEAD sha + const {stdout: stdout2, exitCode: exitCode2} = await exec.getExecOutput( + 'git', + ['rev-parse', '--verify', `refs/heads/${sha}`], + { + ignoreReturnCode: true, + silent: !core.isDebug() + } + ) + + if (exitCode2 !== 0) { + throw new Error(`Unable to locate the commit sha: ${sha}`) + } + + return stdout2.trim() + } + + return stdout.trim() +} export const getPreviousGitTag = async ({ cwd }: {