You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by po...@apache.org on 2020/12/27 13:52:59 UTC

[airflow-cancel-workflow-runs] 43/44: Fixed too-agressive (cross-branches) cancelling

This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow-cancel-workflow-runs.git

commit f4a33154219b13dbb1e171695d6f03810f3a7b47
Author: Jarek Potiuk <ja...@potiuk.com>
AuthorDate: Sun Nov 1 19:01:45 2020 +0100

    Fixed too-agressive (cross-branches) cancelling
    
    There was an error introduced in v4_3 that cancelled cross-PR builds
    
    This change fixes it
---
 dist/index.js | 31 ++++++++++++++++++++-----------
 src/main.ts   | 46 +++++++++++++++++++++++++++-------------------
 2 files changed, 47 insertions(+), 30 deletions(-)

diff --git a/dist/index.js b/dist/index.js
index a46b432..e6422c4 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1491,13 +1491,22 @@ 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.
  *
@@ -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 = [];
@@ -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. ` +
@@ -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;
                         }
                     }
                 }
@@ -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) {
@@ -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);
     }
 }
 /**
@@ -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?
@@ -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`);
@@ -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`);
diff --git a/src/main.ts b/src/main.ts
index 7ee681b..cd99a35 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -53,9 +53,11 @@ 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}`
@@ -63,6 +65,20 @@ function getSourceGroupId(triggeringRunInfo: TriggeringRunInfo): string {
 }
 
 /**
+ * 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.
  *
@@ -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 = []
@@ -233,7 +250,6 @@ 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') {
@@ -241,6 +257,7 @@ async function jobsMatchingNames(
               `    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. ` +
@@ -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
         }
       }
     }
@@ -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
     )
@@ -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)
   }
 }
 
@@ -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
   )
@@ -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
     )
@@ -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`