From 8248bc1feff049e98c0e6a96889b147199c38203 Mon Sep 17 00:00:00 2001 From: Jarek Potiuk Date: Sun, 4 Apr 2021 20:10:42 +0200 Subject: [PATCH] Protects against null head_repository (#14) --- dist/index.js | 25 ++++++++++++++++++++++++- src/main.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 4afd5db..5d7621f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1505,7 +1505,9 @@ function getCommonGroupIdFromTriggeringRunInfo(triggeringRunInfo, sourceWorkflow * @returns the unique string id for the group */ function getCommonGroupIdFromRunItem(runItem) { - return `:${retrieveWorkflowIdFromUrl(runItem.workflow_url)}:${runItem.head_repository.full_name}:${runItem.head_branch}:${runItem.event}`; + return `:${retrieveWorkflowIdFromUrl(runItem.workflow_url)}:${runItem.head_repository !== null + ? runItem.head_repository.full_name + : 'UNKNOWN_REPO'}:${runItem.head_branch}:${runItem.event}`; } /** * Creates query parameters selecting all runs that share the same source group as we have. This can @@ -1755,6 +1757,10 @@ function getWorkflowRuns(repositoryInfo, statusValues, cancelMode, createListRun * @return true if we determine that the run Id should be cancelled */ function checkCandidateForCancellingDuplicate(runItem, cancelFutureDuplicates, triggeringRunInfo, sourceWorkflowId, mapOfWorkflowRunCandidates) { + if (runItem.head_repository === null) { + core.warning(`\nThe run number: ${runItem.run_number} is weird. It's 'head_repository' is null.\n`); + return; + } const runHeadRepo = runItem.head_repository.full_name; if (triggeringRunInfo.headRepo !== undefined && runHeadRepo !== triggeringRunInfo.headRepo) { @@ -2010,6 +2016,10 @@ function findPullRequest(repositoryInfo, headRepo, headBranch, headSha) { */ function findPullRequestForRunItem(repositoryInfo, runItem) { return __awaiter(this, void 0, void 0, function* () { + if (runItem.head_repository === null) { + core.warning(`\nThe run number: ${runItem.run_number} is weird. It's 'head_repository' is null.\n`); + return undefined; + } const pullRequest = yield findPullRequest(repositoryInfo, runItem.head_repository.owner.login, runItem.head_branch, runItem.head_sha); if (pullRequest) { return pullRequest.number; @@ -2205,6 +2215,19 @@ function getTriggeringRunInfo(repositoryInfo, runId) { run_id: runId }); const sourceRun = reply.data; + if (sourceRun.head_repository === null) { + core.warning(`\nThe run number: ${sourceRun.run_number} is weird. It's 'head_repository' is null.\n`); + return { + workflowId: retrieveWorkflowIdFromUrl(reply.data.workflow_url), + runId, + headRepo: 'UNKNOWN_REPO', + headBranch: reply.data.head_branch, + headSha: reply.data.head_sha, + mergeCommitSha: null, + pullRequest: null, + eventName: reply.data.event + }; + } core.info(`Source workflow: Head repo: ${sourceRun.head_repository.full_name}, ` + `Head branch: ${sourceRun.head_branch} ` + `Event: ${sourceRun.event}, Head sha: ${sourceRun.head_sha}, url: ${sourceRun.url}`); diff --git a/src/main.ts b/src/main.ts index 109dd86..1077969 100644 --- a/src/main.ts +++ b/src/main.ts @@ -76,7 +76,9 @@ function getCommonGroupIdFromRunItem( runItem: rest.ActionsListWorkflowRunsResponseWorkflowRunsItem ): string { return `:${retrieveWorkflowIdFromUrl(runItem.workflow_url)}:${ - runItem.head_repository.full_name + runItem.head_repository !== null + ? runItem.head_repository.full_name + : 'UNKNOWN_REPO' }:${runItem.head_branch}:${runItem.event}` } @@ -373,6 +375,12 @@ function checkCandidateForCancellingDuplicate( rest.ActionsListWorkflowRunsResponseWorkflowRunsItem[] > ): void { + if (runItem.head_repository === null) { + core.warning( + `\nThe run number: ${runItem.run_number} is weird. It's 'head_repository' is null.\n` + ) + return + } const runHeadRepo = runItem.head_repository.full_name if ( triggeringRunInfo.headRepo !== undefined && @@ -793,6 +801,12 @@ async function findPullRequestForRunItem( repositoryInfo: RepositoryInfo, runItem: rest.ActionsListWorkflowRunsResponseWorkflowRunsItem ): Promise { + if (runItem.head_repository === null) { + core.warning( + `\nThe run number: ${runItem.run_number} is weird. It's 'head_repository' is null.\n` + ) + return undefined + } const pullRequest = await findPullRequest( repositoryInfo, runItem.head_repository.owner.login, @@ -1118,6 +1132,21 @@ async function getTriggeringRunInfo( run_id: runId }) const sourceRun = reply.data + if (sourceRun.head_repository === null) { + core.warning( + `\nThe run number: ${sourceRun.run_number} is weird. It's 'head_repository' is null.\n` + ) + return { + workflowId: retrieveWorkflowIdFromUrl(reply.data.workflow_url), + runId, + headRepo: 'UNKNOWN_REPO', + headBranch: reply.data.head_branch, + headSha: reply.data.head_sha, + mergeCommitSha: null, + pullRequest: null, + eventName: reply.data.event + } + } core.info( `Source workflow: Head repo: ${sourceRun.head_repository.full_name}, ` + `Head branch: ${sourceRun.head_branch} ` +