Skip to content

Commit

Permalink
feat: add support for passing branch name to the base_sha and sha inp…
Browse files Browse the repository at this point in the history
…uts (#1742)
  • Loading branch information
jackton1 committed Nov 27, 2023
1 parent 6784c9e commit 2139fa1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 19 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/test.yml
Expand Up @@ -286,11 +286,15 @@ jobs:
shell:
bash

test-unset-github-output-env:
name: Test unset GITHUB_OUTPUT env
test-using-branch-names-for-base-sha-and-sha-inputs:
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 All @@ -304,12 +308,11 @@ jobs:
with:
name: build-assets

- name: Run changed-files with unset GITHUB_OUTPUT env
- name: Run changed-files with main as the base_sha
id: changed-files
continue-on-error: true
uses: ./
env:
GITHUB_OUTPUT: ""
with:
base_sha: main

- name: Show output
run: |
Expand Down
59 changes: 53 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

21 changes: 16 additions & 5 deletions src/commitSha.ts
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,11 @@ const getCurrentSHA = async ({
inputs: Inputs
workingDirectory: string
}): Promise<string> => {
let currentSha = inputs.sha
let currentSha = await cleanShaInput({
sha: inputs.sha,
cwd: workingDirectory,
token: inputs.token
})
core.debug('Getting current SHA...')

if (inputs.until) {
Expand Down Expand Up @@ -162,7 +167,11 @@ export const getSHAForNonPullRequestEvent = async (
}

const currentSha = await getCurrentSHA({inputs, workingDirectory})
let previousSha = inputs.baseSha
let previousSha = await cleanShaInput({
sha: inputs.baseSha,
cwd: workingDirectory,
token: inputs.token
})
const diff = '..'
const currentBranchName = await getCurrentBranchName({cwd: workingDirectory})

Expand All @@ -186,7 +195,6 @@ export const getSHAForNonPullRequestEvent = async (
throw new Error('Similar commit hashes detected.')
}

await verifyCommitSha({sha: previousSha, cwd: workingDirectory})
core.debug(`Previous SHA: ${previousSha}`)

return {
Expand Down Expand Up @@ -390,7 +398,11 @@ export const getSHAForPullRequestEvent = async (
}

const currentSha = await getCurrentSHA({inputs, workingDirectory})
let previousSha = inputs.baseSha
let previousSha = await cleanShaInput({
sha: inputs.baseSha,
cwd: workingDirectory,
token: inputs.token
})
let diff = '...'

if (previousSha && currentSha && currentBranch && targetBranch) {
Expand All @@ -404,7 +416,6 @@ export const getSHAForPullRequestEvent = async (
throw new Error('Similar commit hashes detected.')
}

await verifyCommitSha({sha: previousSha, cwd: workingDirectory})
core.debug(`Previous SHA: ${previousSha}`)

return {
Expand Down
51 changes: 51 additions & 0 deletions src/utils.ts
@@ -1,6 +1,7 @@
/*global AsyncIterableIterator*/
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as github from '@actions/github'
import {createReadStream, promises as fs} from 'fs'
import {readFile} from 'fs/promises'
import {flattenDeep} from 'lodash'
Expand Down Expand Up @@ -771,6 +772,56 @@ 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.
* @param cwd The working directory.
* @param token The GitHub token.
* @returns The cleaned SHA string.
*/
export const cleanShaInput = async ({
sha,
cwd,
token
}: {
sha: string
cwd: string
token: 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}^{commit}`],
{
cwd,
ignoreReturnCode: true,
silent: !core.isDebug()
}
)

if (exitCode !== 0) {
const octokit = github.getOctokit(token)
// If it's not a valid commit sha, assume it's a branch name and get the HEAD sha
const {data: refData} = await octokit.rest.git.getRef({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
ref: `heads/${sha}`
})

return refData.object.sha
}

return stdout.trim()
}
export const getPreviousGitTag = async ({
cwd
}: {
Expand Down

0 comments on commit 2139fa1

Please sign in to comment.