Skip to content

Commit

Permalink
Fixed too-agressive (cross-branches) cancelling
Browse files Browse the repository at this point in the history
There was an error introduced in v4_3 that cancelled cross-PR builds

This change fixes it
  • Loading branch information
potiuk committed Nov 1, 2020
1 parent 169f169 commit f4a3315
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
31 changes: 20 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1491,12 +1491,21 @@ var CancelMode;
* Converts the source of a run object into a string that can be used as map key in maps where we keep
* arrays of runs per source group
* @param triggeringRunInfo the object identifying the triggering workflow
* @returns the unique string id for the source group
* @returns the unique string id for the group
*/
function getSourceGroupId(triggeringRunInfo) {
function getCommonGroupIdFromTriggeringRunInfo(triggeringRunInfo) {
return (`:${triggeringRunInfo.workflowId}:${triggeringRunInfo.headRepo}` +
`:${triggeringRunInfo.headBranch}:${triggeringRunInfo.eventName}`);
}
/**
* Converts the source of a run object into a string that can be used as map key in maps where we keep
* arrays of runs per group
* @param runItem Item the run item to retrieve the group from
* @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}`;
}
/**
* Creates query parameters selecting all runs that share the same source group as we have. This can
* be used to select duplicates of my own run.
Expand Down Expand Up @@ -1594,6 +1603,7 @@ function matchInArray(stringToMatch, regexps) {
* @param mapOfWorkflowRunCandidates map of workflow runs to add the run item to
*/
function addWorkflowRunToMap(key, runItem, mapOfWorkflowRunCandidates) {
core.info(`\nAdding the run: ${runItem.id} to candidates with ${key} key.\n`);
let arrayOfRuns = mapOfWorkflowRunCandidates.get(key);
if (arrayOfRuns === undefined) {
arrayOfRuns = [];
Expand Down Expand Up @@ -1632,12 +1642,12 @@ function jobsMatchingNames(repositoryInfo, runId, jobNameRegexps, checkIfFailed)
const [jobMatched, jobMatches] = matchInArray(job.name, jobNameRegexps);
if (jobMatched) {
allMatches.push(...jobMatches);
matched = true;
if (checkIfFailed) {
// Only fail the build if one of the matching jobs fail
if (job.conclusion === 'failure') {
core.info(` The Job ${job.name} matches one of the ${jobNameRegexps} regexps and it failed.` +
` It will be added to the candidates.`);
matched = true;
}
else {
core.info(` The Job ${job.name} matches one of the ${jobNameRegexps} regexps but it did not fail. ` +
Expand All @@ -1648,6 +1658,7 @@ function jobsMatchingNames(repositoryInfo, runId, jobNameRegexps, checkIfFailed)
// Fail the build if any of the job names match
core.info(` The Job ${job.name} matches one of the ${jobNameRegexps} regexps. ` +
`It will be added to the candidates.`);
matched = true;
}
}
}
Expand Down Expand Up @@ -1748,7 +1759,7 @@ function checkCandidateForCancellingDuplicate(runItem, cancelFutureDuplicates, t
}
if (cancelFutureDuplicates) {
core.info(`\nCancel Future Duplicates: Returning run id that might be duplicate or my own run: ${runItem.id}.\n`);
addWorkflowRunToMap(getSourceGroupId(triggeringRunInfo), runItem, mapOfWorkflowRunCandidates);
addWorkflowRunToMap(getCommonGroupIdFromTriggeringRunInfo(triggeringRunInfo), runItem, mapOfWorkflowRunCandidates);
}
else {
if (runItem.id === triggeringRunInfo.runId) {
Expand All @@ -1769,8 +1780,7 @@ function checkCandidateForCancellingDuplicate(runItem, cancelFutureDuplicates, t
*/
function checkCandidateForCancellingSelf(runItem, triggeringRunInfo, mapOfWorkflowRunCandidates) {
if (runItem.id === triggeringRunInfo.runId) {
core.info(`\nAdding the "source" run: ${runItem.id} to candidates.\n`);
addWorkflowRunToMap(getSourceGroupId(triggeringRunInfo), runItem, mapOfWorkflowRunCandidates);
addWorkflowRunToMap('selfRun', runItem, mapOfWorkflowRunCandidates);
}
}
/**
Expand All @@ -1780,8 +1790,7 @@ function checkCandidateForCancellingSelf(runItem, triggeringRunInfo, mapOfWorkfl
* @param mapOfWorkflowRunCandidates - map of the workflow runs to add candidates to
*/
function checkCandidateForAllDuplicates(runItem, triggeringRunInfo, mapOfWorkflowRunCandidates) {
core.info(`\nAdding the run: ${runItem.id} to candidates.\n`);
addWorkflowRunToMap(getSourceGroupId(triggeringRunInfo), runItem, mapOfWorkflowRunCandidates);
addWorkflowRunToMap(getCommonGroupIdFromRunItem(runItem), runItem, mapOfWorkflowRunCandidates);
}
/**
* Should the run is candidate for cancelling in naming job cancelling mode?
Expand All @@ -1797,8 +1806,8 @@ function checkCandidateForCancellingNamedJobs(repositoryInfo, runItem, jobNamesR
// Cancel all jobs that have failed jobs (no matter when started)
const [matched, allMatches] = yield jobsMatchingNames(repositoryInfo, runItem.id, jobNamesRegexps, false);
if (matched) {
core.info(`\nSome jobs have matching names in ${runItem.id}: ${allMatches}. Adding it candidates.\n`);
addWorkflowRunToMap(getSourceGroupId(triggeringRunInfo), runItem, mapOfWorkflowRunCandidates);
core.info(`\nSome jobs have matching names in ${runItem.id}: ${allMatches}. Adding it as candidate.\n`);
addWorkflowRunToMap('allMatchingNamedJobs', runItem, mapOfWorkflowRunCandidates);
}
else {
core.info(`\nNone of the jobs match name in ${runItem.id}.\n`);
Expand All @@ -1820,7 +1829,7 @@ function checkCandidateForCancellingFailedJobs(repositoryInfo, runItem, jobNames
const [matched, allMatches] = yield jobsMatchingNames(repositoryInfo, runItem.id, jobNamesRegexps, true);
if (matched) {
core.info(`\nSome matching named jobs failed in ${runItem.id}: ${allMatches}. Adding it to candidates.\n`);
addWorkflowRunToMap(getSourceGroupId(triggeringRunInfo), runItem, mapOfWorkflowRunCandidates);
addWorkflowRunToMap('failedJobs', runItem, mapOfWorkflowRunCandidates);
}
else {
core.info(`\nNone of the matching jobs failed in ${runItem.id}. Not adding as candidate to cancel.\n`);
Expand Down
46 changes: 27 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,31 @@ interface TriggeringRunInfo {
* Converts the source of a run object into a string that can be used as map key in maps where we keep
* arrays of runs per source group
* @param triggeringRunInfo the object identifying the triggering workflow
* @returns the unique string id for the source group
* @returns the unique string id for the group
*/
function getSourceGroupId(triggeringRunInfo: TriggeringRunInfo): string {
function getCommonGroupIdFromTriggeringRunInfo(
triggeringRunInfo: TriggeringRunInfo
): string {
return (
`:${triggeringRunInfo.workflowId}:${triggeringRunInfo.headRepo}` +
`:${triggeringRunInfo.headBranch}:${triggeringRunInfo.eventName}`
)
}

/**
* Converts the source of a run object into a string that can be used as map key in maps where we keep
* arrays of runs per group
* @param runItem Item the run item to retrieve the group from
* @returns the unique string id for the group
*/
function getCommonGroupIdFromRunItem(
runItem: rest.ActionsListWorkflowRunsResponseWorkflowRunsItem
): string {
return `:${retrieveWorkflowIdFromUrl(runItem.workflow_url)}:${
runItem.head_repository.full_name
}:${runItem.head_branch}:${runItem.event}`
}

/**
* Creates query parameters selecting all runs that share the same source group as we have. This can
* be used to select duplicates of my own run.
Expand Down Expand Up @@ -190,6 +206,7 @@ function addWorkflowRunToMap(
rest.ActionsListWorkflowRunsResponseWorkflowRunsItem[]
>
): void {
core.info(`\nAdding the run: ${runItem.id} to candidates with ${key} key.\n`)
let arrayOfRuns = mapOfWorkflowRunCandidates.get(key)
if (arrayOfRuns === undefined) {
arrayOfRuns = []
Expand Down Expand Up @@ -233,14 +250,14 @@ async function jobsMatchingNames(
const [jobMatched, jobMatches] = matchInArray(job.name, jobNameRegexps)
if (jobMatched) {
allMatches.push(...jobMatches)
matched = true
if (checkIfFailed) {
// Only fail the build if one of the matching jobs fail
if (job.conclusion === 'failure') {
core.info(
` The Job ${job.name} matches one of the ${jobNameRegexps} regexps and it failed.` +
` It will be added to the candidates.`
)
matched = true
} else {
core.info(
` The Job ${job.name} matches one of the ${jobNameRegexps} regexps but it did not fail. ` +
Expand All @@ -253,6 +270,7 @@ async function jobsMatchingNames(
` The Job ${job.name} matches one of the ${jobNameRegexps} regexps. ` +
`It will be added to the candidates.`
)
matched = true
}
}
}
Expand Down Expand Up @@ -362,7 +380,7 @@ function checkCandidateForCancellingDuplicate(
`\nCancel Future Duplicates: Returning run id that might be duplicate or my own run: ${runItem.id}.\n`
)
addWorkflowRunToMap(
getSourceGroupId(triggeringRunInfo),
getCommonGroupIdFromTriggeringRunInfo(triggeringRunInfo),
runItem,
mapOfWorkflowRunCandidates
)
Expand Down Expand Up @@ -394,12 +412,7 @@ function checkCandidateForCancellingSelf(
>
): void {
if (runItem.id === triggeringRunInfo.runId) {
core.info(`\nAdding the "source" run: ${runItem.id} to candidates.\n`)
addWorkflowRunToMap(
getSourceGroupId(triggeringRunInfo),
runItem,
mapOfWorkflowRunCandidates
)
addWorkflowRunToMap('selfRun', runItem, mapOfWorkflowRunCandidates)
}
}

Expand All @@ -417,9 +430,8 @@ function checkCandidateForAllDuplicates(
rest.ActionsListWorkflowRunsResponseWorkflowRunsItem[]
>
): void {
core.info(`\nAdding the run: ${runItem.id} to candidates.\n`)
addWorkflowRunToMap(
getSourceGroupId(triggeringRunInfo),
getCommonGroupIdFromRunItem(runItem),
runItem,
mapOfWorkflowRunCandidates
)
Expand Down Expand Up @@ -453,10 +465,10 @@ async function checkCandidateForCancellingNamedJobs(
)
if (matched) {
core.info(
`\nSome jobs have matching names in ${runItem.id}: ${allMatches}. Adding it candidates.\n`
`\nSome jobs have matching names in ${runItem.id}: ${allMatches}. Adding it as candidate.\n`
)
addWorkflowRunToMap(
getSourceGroupId(triggeringRunInfo),
'allMatchingNamedJobs',
runItem,
mapOfWorkflowRunCandidates
)
Expand Down Expand Up @@ -495,11 +507,7 @@ async function checkCandidateForCancellingFailedJobs(
core.info(
`\nSome matching named jobs failed in ${runItem.id}: ${allMatches}. Adding it to candidates.\n`
)
addWorkflowRunToMap(
getSourceGroupId(triggeringRunInfo),
runItem,
mapOfWorkflowRunCandidates
)
addWorkflowRunToMap('failedJobs', runItem, mapOfWorkflowRunCandidates)
} else {
core.info(
`\nNone of the matching jobs failed in ${runItem.id}. Not adding as candidate to cancel.\n`
Expand Down

0 comments on commit f4a3315

Please sign in to comment.