Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for passing branch name to the base_sha and sha inputs #1740

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/greetings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/commitSha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Env} from './env'
import {Inputs} from './inputs'
import {
canDiffCommits,
cleanShaInput,
getCurrentBranchName,
getHeadSha,
getParentSha,
Expand All @@ -23,7 +24,7 @@ const getCurrentSHA = async ({
inputs: Inputs
workingDirectory: string
}): Promise<string> => {
let currentSha = inputs.sha
let currentSha = await cleanShaInput(inputs.sha)
core.debug('Getting current SHA...')

if (inputs.until) {
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
// 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
}: {
Expand Down