3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-02-21 05:28:14 +00:00

feat: add support for forcing the use of GitHub’s REST API (#1849)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Tonye Jack 2024-01-13 01:48:42 -07:00 committed by GitHub
parent a57f4dcc81
commit 0ca1c07b47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 13 deletions

View file

@ -36,3 +36,32 @@ jobs:
- name: List all changed files
run: |
echo '${{ needs.changed-files.outputs.all_changed_files }}'
changed-files-rest-api:
name: Get changed files using REST API
runs-on: ubuntu-latest
outputs:
all_changed_files: ${{ steps.changed-files.outputs.all_changed_files }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
continue-on-error: ${{ github.event_name == 'push' }}
uses: ./
with:
use_rest_api: true
- name: List all changed files
run: echo '${{ steps.changed-files.outputs.all_changed_files }}'
view-changed-files-rest-api:
name: View all changed files using REST API
runs-on: ubuntu-latest
needs: [changed-files-rest-api]
steps:
- name: List all changed files
run: |
echo '${{ needs.changed-files-rest-api.outputs.all_changed_files }}'

View file

@ -1,4 +1,4 @@
name: Workflow Run Exmaple
name: Workflow Run Example
on:
workflow_run:
workflows: [Matrix Test]

View file

@ -203,6 +203,10 @@ inputs:
description: "Github API URL."
required: false
default: ${{ github.api_url }}
use_rest_api:
description: "Force the use of Github's REST API even when a local copy of the repository exists"
required: false
default: "false"
fail_on_initial_diff_error:
description: "Fail when the initial diff fails."
required: false

26
dist/index.js generated vendored
View file

@ -1553,6 +1553,9 @@ const getInputs = () => {
const negationPatternsFirst = core.getBooleanInput('negation_patterns_first', {
required: false
});
const useRestApi = core.getBooleanInput('use_rest_api', {
required: false
});
const inputs = {
files,
filesSeparator,
@ -1605,7 +1608,8 @@ const getInputs = () => {
outputRenamedFilesAsDeletedAndAdded,
token,
apiUrl,
negationPatternsFirst
negationPatternsFirst,
useRestApi
};
if (fetchDepth) {
inputs.fetchDepth = Math.max(parseInt(fetchDepth, 10), 2);
@ -1793,7 +1797,7 @@ const getChangedFilesFromRESTAPI = ({ inputs, filePatterns, yamlFilePatterns })
});
});
function run() {
var _a;
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
core.startGroup('changed-files');
const env = yield (0, env_1.getEnv)();
@ -1815,21 +1819,31 @@ function run() {
workingDirectory
});
core.debug(`Yaml file patterns: ${JSON.stringify(yamlFilePatterns)}`);
if (inputs.useRestApi && !((_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number)) {
throw new Error("Only pull_request* events are supported when using GitHub's REST API.");
}
if (inputs.token &&
((_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number) &&
!hasGitDirectory) {
((_b = github.context.payload.pull_request) === null || _b === void 0 ? void 0 : _b.number) &&
(!hasGitDirectory || inputs.useRestApi)) {
core.info("Using GitHub's REST API to get changed files");
const unsupportedInputs = [
'sha',
'baseSha',
'since',
'until',
'path',
'quotePath',
'diffRelative',
'sinceLastRemoteCommit',
'recoverDeletedFiles',
'recoverDeletedFilesToDestination',
'recoverFiles',
'recoverFilesSeparator',
'recoverFilesIgnore',
'recoverFilesIgnoreSeparator',
'includeAllOldNewRenamedFiles',
'oldNewSeparator',
'oldNewFilesSeparator',
'skipInitialFetch',
'fetchSubmoduleHistory',
'dirNamesDeletedFilesIncludeOnlyDeletedDirs'
@ -1848,8 +1862,7 @@ function run() {
else {
if (!hasGitDirectory) {
core.info(`Running on a ${github.context.eventName} event...`);
core.setFailed("Can't find local .git directory. Please run actions/checkout before this action. If you intend to use Github's REST API note that only pull_request* events are supported.");
return;
throw new Error("Can't find local .git directory. Please run actions/checkout before this action (Make sure the path specified in the 'path' input is correct). If you intend to use Github's REST API note that only pull_request* events are supported.");
}
core.info('Using local .git directory');
yield getChangedFilesFromLocalGitHistory({
@ -1868,6 +1881,7 @@ if (!process.env.TESTING) {
// eslint-disable-next-line github/no-then
run().catch(e => {
core.setFailed(e.message || e);
process.exit(1);
});
}

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -53,6 +53,7 @@ export type Inputs = {
failOnInitialDiffError: boolean
failOnSubmoduleDiffError: boolean
negationPatternsFirst: boolean
useRestApi: boolean
}
export const getInputs = (): Inputs => {
@ -228,6 +229,10 @@ export const getInputs = (): Inputs => {
}
)
const useRestApi = core.getBooleanInput('use_rest_api', {
required: false
})
const inputs: Inputs = {
files,
filesSeparator,
@ -280,7 +285,8 @@ export const getInputs = (): Inputs => {
outputRenamedFilesAsDeletedAndAdded,
token,
apiUrl,
negationPatternsFirst
negationPatternsFirst,
useRestApi
}
if (fetchDepth) {

View file

@ -243,10 +243,16 @@ export async function run(): Promise<void> {
})
core.debug(`Yaml file patterns: ${JSON.stringify(yamlFilePatterns)}`)
if (inputs.useRestApi && !github.context.payload.pull_request?.number) {
throw new Error(
"Only pull_request* events are supported when using GitHub's REST API."
)
}
if (
inputs.token &&
github.context.payload.pull_request?.number &&
!hasGitDirectory
(!hasGitDirectory || inputs.useRestApi)
) {
core.info("Using GitHub's REST API to get changed files")
const unsupportedInputs: (keyof Inputs)[] = [
@ -254,12 +260,19 @@ export async function run(): Promise<void> {
'baseSha',
'since',
'until',
'path',
'quotePath',
'diffRelative',
'sinceLastRemoteCommit',
'recoverDeletedFiles',
'recoverDeletedFilesToDestination',
'recoverFiles',
'recoverFilesSeparator',
'recoverFilesIgnore',
'recoverFilesIgnoreSeparator',
'includeAllOldNewRenamedFiles',
'oldNewSeparator',
'oldNewFilesSeparator',
'skipInitialFetch',
'fetchSubmoduleHistory',
'dirNamesDeletedFilesIncludeOnlyDeletedDirs'
@ -280,10 +293,9 @@ export async function run(): Promise<void> {
} else {
if (!hasGitDirectory) {
core.info(`Running on a ${github.context.eventName} event...`)
core.setFailed(
"Can't find local .git directory. Please run actions/checkout before this action. If you intend to use Github's REST API note that only pull_request* events are supported."
throw new Error(
"Can't find local .git directory. Please run actions/checkout before this action (Make sure the path specified in the 'path' input is correct). If you intend to use Github's REST API note that only pull_request* events are supported."
)
return
}
core.info('Using local .git directory')
@ -302,5 +314,6 @@ if (!process.env.TESTING) {
// eslint-disable-next-line github/no-then
run().catch(e => {
core.setFailed(e.message || e)
process.exit(1)
})
}