diff --git a/dist/index.js b/dist/index.js index 9f81ae5..8877106 100644 --- a/dist/index.js +++ b/dist/index.js @@ -28941,6 +28941,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); const core = __importStar(__nccwpck_require__(2186)); const github = __importStar(__nccwpck_require__(5438)); const utils_1 = __nccwpck_require__(1314); +const PR_WORKFLOW_ID = '.github/workflows/PR.yml'; /** * API wrapper for the fork action and related usages. */ @@ -28973,6 +28974,8 @@ class Api { * @returns {Promise} */ async fork({ ref, workflow_id, inputs, prefix, jobs, head_sha }) { + // Ensure jobs have not been triggerd by the PR workflow + await this.ensureJobs(head_sha, jobs.map(job => `${prefix}${job}`)); // If the workflow has already been dispatched, create // checks from the exist one. let run = await this.latestRun(workflow_id, head_sha); @@ -29044,6 +29047,22 @@ class Api { } } } + /** + * Ensure jobs have not been triggered yet + * + * NOTE: this function only works for `gear-tech/gear` + */ + async ensureJobs(head_sha, filter) { + const run = await this.latestRun(PR_WORKFLOW_ID, head_sha); + if (!run) + return; + const jobs = await this.getJobs(run.id, filter, false); + if (jobs.length > 0) { + const processed = jobs.map(j => j.name).join(','); + core.info(`[${processed}] have been processed in the PR workflow`); + process.exit(0); + } + } /** * Create check with provided arguments. * @@ -29104,24 +29123,21 @@ class Api { * @param {string[]} filter - Job names to be filtered out. * @returns {Promise} - Jobs of a workflow run. */ - async getJobs(run_id, filter) { + async getJobs(run_id, filter, strict = true) { const { data: { jobs } } = await this.octokit.rest.actions.listJobsForWorkflowRun({ owner: this.owner, repo: this.repo, run_id }); - if (jobs.length === 0) { - core.setFailed(`No workflow is found from ${run_id}`); - process.exit(1); - } - // Check if forked jobs are processed. const forkedJobs = jobs.filter(job => filter.includes(job.name)); + if (!strict) + return forkedJobs; if (forkedJobs.length < filter.length) { core.info(`Waiting for ${filter} ...`); await (0, utils_1.wait)(5000); return await this.getJobs(run_id, filter); } - return jobs.filter(job => filter.includes(job.name)); + return forkedJobs; } /** * List workflow runs for specifed workflow and branch. diff --git a/src/api.ts b/src/api.ts index 9de848d..2189e25 100644 --- a/src/api.ts +++ b/src/api.ts @@ -17,6 +17,8 @@ import { } from '@/types'; import { wait } from '@/utils'; +const PR_WORKFLOW_ID = '.github/workflows/PR.yml'; + /** * API wrapper for the fork action and related usages. */ @@ -59,6 +61,12 @@ export default class Api { jobs, head_sha }: ForkOptions): Promise { + // Ensure jobs have not been triggerd by the PR workflow + await this.ensureJobs( + head_sha, + jobs.map(job => `${prefix}${job}`) + ); + // If the workflow has already been dispatched, create // checks from the exist one. let run = await this.latestRun(workflow_id, head_sha); @@ -151,6 +159,23 @@ export default class Api { } } + /** + * Ensure jobs have not been triggered yet + * + * NOTE: this function only works for `gear-tech/gear` + */ + async ensureJobs(head_sha: string, filter: string[]): Promise { + const run = await this.latestRun(PR_WORKFLOW_ID, head_sha); + if (!run) return; + + const jobs = await this.getJobs(run.id, filter, false); + if (jobs.length > 0) { + const processed = jobs.map(j => j.name).join(','); + core.info(`[${processed}] have been processed in the PR workflow`); + process.exit(0); + } + } + /** * Create check with provided arguments. * @@ -225,7 +250,11 @@ export default class Api { * @param {string[]} filter - Job names to be filtered out. * @returns {Promise} - Jobs of a workflow run. */ - async getJobs(run_id: number, filter: string[]): Promise { + async getJobs( + run_id: number, + filter: string[], + strict = true + ): Promise { const { data: { jobs } } = await this.octokit.rest.actions.listJobsForWorkflowRun({ @@ -234,20 +263,15 @@ export default class Api { run_id }); - if (jobs.length === 0) { - core.setFailed(`No workflow is found from ${run_id}`); - process.exit(1); - } - - // Check if forked jobs are processed. const forkedJobs = jobs.filter(job => filter.includes(job.name)); + if (!strict) return forkedJobs; if (forkedJobs.length < filter.length) { core.info(`Waiting for ${filter} ...`); await wait(5000); return await this.getJobs(run_id, filter); } - return jobs.filter(job => filter.includes(job.name)); + return forkedJobs; } /**