mirror of
https://github.com/tj-actions/changed-files
synced 2025-01-05 09:35:54 +00:00
feat: add support for passing branch name to the base_sha and sha inputs (#1742)
This commit is contained in:
parent
6784c9e1ee
commit
2139fa1b6f
5 changed files with 77 additions and 12 deletions
17
.github/workflows/test.yml
vendored
17
.github/workflows/test.yml
vendored
|
@ -286,11 +286,15 @@ jobs:
|
||||||
shell:
|
shell:
|
||||||
bash
|
bash
|
||||||
|
|
||||||
test-unset-github-output-env:
|
test-using-branch-names-for-base-sha-and-sha-inputs:
|
||||||
name: Test unset GITHUB_OUTPUT env
|
name: Test using branch names for base_sha and sha inputs
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
needs: build
|
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:
|
steps:
|
||||||
- name: Checkout branch
|
- name: Checkout branch
|
||||||
|
@ -304,12 +308,11 @@ jobs:
|
||||||
with:
|
with:
|
||||||
name: build-assets
|
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
|
id: changed-files
|
||||||
continue-on-error: true
|
|
||||||
uses: ./
|
uses: ./
|
||||||
env:
|
with:
|
||||||
GITHUB_OUTPUT: ""
|
base_sha: main
|
||||||
|
|
||||||
- name: Show output
|
- name: Show output
|
||||||
run: |
|
run: |
|
||||||
|
|
BIN
dist/index.js
generated
vendored
BIN
dist/index.js
generated
vendored
Binary file not shown.
BIN
dist/index.js.map
generated
vendored
BIN
dist/index.js.map
generated
vendored
Binary file not shown.
|
@ -5,6 +5,7 @@ import {Env} from './env'
|
||||||
import {Inputs} from './inputs'
|
import {Inputs} from './inputs'
|
||||||
import {
|
import {
|
||||||
canDiffCommits,
|
canDiffCommits,
|
||||||
|
cleanShaInput,
|
||||||
getCurrentBranchName,
|
getCurrentBranchName,
|
||||||
getHeadSha,
|
getHeadSha,
|
||||||
getParentSha,
|
getParentSha,
|
||||||
|
@ -23,7 +24,11 @@ const getCurrentSHA = async ({
|
||||||
inputs: Inputs
|
inputs: Inputs
|
||||||
workingDirectory: string
|
workingDirectory: string
|
||||||
}): Promise<string> => {
|
}): Promise<string> => {
|
||||||
let currentSha = inputs.sha
|
let currentSha = await cleanShaInput({
|
||||||
|
sha: inputs.sha,
|
||||||
|
cwd: workingDirectory,
|
||||||
|
token: inputs.token
|
||||||
|
})
|
||||||
core.debug('Getting current SHA...')
|
core.debug('Getting current SHA...')
|
||||||
|
|
||||||
if (inputs.until) {
|
if (inputs.until) {
|
||||||
|
@ -162,7 +167,11 @@ export const getSHAForNonPullRequestEvent = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentSha = await getCurrentSHA({inputs, workingDirectory})
|
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 diff = '..'
|
||||||
const currentBranchName = await getCurrentBranchName({cwd: workingDirectory})
|
const currentBranchName = await getCurrentBranchName({cwd: workingDirectory})
|
||||||
|
|
||||||
|
@ -186,7 +195,6 @@ export const getSHAForNonPullRequestEvent = async (
|
||||||
throw new Error('Similar commit hashes detected.')
|
throw new Error('Similar commit hashes detected.')
|
||||||
}
|
}
|
||||||
|
|
||||||
await verifyCommitSha({sha: previousSha, cwd: workingDirectory})
|
|
||||||
core.debug(`Previous SHA: ${previousSha}`)
|
core.debug(`Previous SHA: ${previousSha}`)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -390,7 +398,11 @@ export const getSHAForPullRequestEvent = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
const currentSha = await getCurrentSHA({inputs, workingDirectory})
|
const currentSha = await getCurrentSHA({inputs, workingDirectory})
|
||||||
let previousSha = inputs.baseSha
|
let previousSha = await cleanShaInput({
|
||||||
|
sha: inputs.baseSha,
|
||||||
|
cwd: workingDirectory,
|
||||||
|
token: inputs.token
|
||||||
|
})
|
||||||
let diff = '...'
|
let diff = '...'
|
||||||
|
|
||||||
if (previousSha && currentSha && currentBranch && targetBranch) {
|
if (previousSha && currentSha && currentBranch && targetBranch) {
|
||||||
|
@ -404,7 +416,6 @@ export const getSHAForPullRequestEvent = async (
|
||||||
throw new Error('Similar commit hashes detected.')
|
throw new Error('Similar commit hashes detected.')
|
||||||
}
|
}
|
||||||
|
|
||||||
await verifyCommitSha({sha: previousSha, cwd: workingDirectory})
|
|
||||||
core.debug(`Previous SHA: ${previousSha}`)
|
core.debug(`Previous SHA: ${previousSha}`)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
51
src/utils.ts
51
src/utils.ts
|
@ -1,6 +1,7 @@
|
||||||
/*global AsyncIterableIterator*/
|
/*global AsyncIterableIterator*/
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import * as exec from '@actions/exec'
|
import * as exec from '@actions/exec'
|
||||||
|
import * as github from '@actions/github'
|
||||||
import {createReadStream, promises as fs} from 'fs'
|
import {createReadStream, promises as fs} from 'fs'
|
||||||
import {readFile} from 'fs/promises'
|
import {readFile} from 'fs/promises'
|
||||||
import {flattenDeep} from 'lodash'
|
import {flattenDeep} from 'lodash'
|
||||||
|
@ -771,6 +772,56 @@ export const verifyCommitSha = async ({
|
||||||
return exitCode
|
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 ({
|
export const getPreviousGitTag = async ({
|
||||||
cwd
|
cwd
|
||||||
}: {
|
}: {
|
||||||
|
|
Loading…
Reference in a new issue