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 40bee3e7736ef97b456abb188f3e8a027aa48cce
Author: Jason T. Greene <[email protected]>
AuthorDate: Thu Feb 6 13:22:34 2020 -0600

    Add support for pull requests
---
 __tests__/main.test.ts | 15 +++++++++++----
 src/main.ts            | 27 +++++++++++++++++----------
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts
index efa2802..e237067 100644
--- a/__tests__/main.test.ts
+++ b/__tests__/main.test.ts
@@ -7,10 +7,17 @@ test('no op', () => {})
 // shows how the runner will run a javascript action with env / stdout protocol
 // test('test runs', () => {
 //   const ip = path.join(__dirname, '..', 'lib', 'main.js')
-//   process.env['INPUT_TOKEN'] = ''
-//   process.env['GITHUB_RUN_ID'] = '33782469'
-//   process.env['GITHUB_REPOSITORY'] = 'n1hility/cancel-previous-runs'
-//   process.env['GITHUB_REF'] = 'refs/heads/master'
+//     process.env['INPUT_TOKEN'] = ''
+//     process.env['GITHUB_RUN_ID'] = '35588693' //'33782469'
+//     process.env['GITHUB_REPOSITORY'] = ''
+//     process.env['GITHUB_HEAD_REF'] = 'refs/heads/n1hility-patch-5'
+//     process.env['GITHUB_EVENT_NAME'] = 'pull_request'
+
+// //   process.env['GITHUB_RUN_ID'] = '35599067'
+// //   process.env['GITHUB_REPOSITORY'] = ''
+// //   process.env['GITHUB_REF'] = 'refs/heads/master'
+// //   process.env['GITHUB_EVENT_NAME'] = 'push'
+
 //   const options: cp.ExecSyncOptions = {
 //     env: process.env
 //   }
diff --git a/src/main.ts b/src/main.ts
index 12547c2..c99a264 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -13,13 +13,15 @@ async function run(): Promise<void> {
     const branchPrefix = 'refs/heads/'
     const tagPrefix = 'refs/tags/'
 
-    if (eventName !== 'push') {
-      core.info('Skipping non-push event')
+    if (!['push', 'pull_request'].includes(eventName)) {
+      core.info('Skipping unsupported event')
       return
     }
 
-    let branch = getRequiredEnv('GITHUB_REF')
-    if (!branch.startsWith(branchPrefix)) {
+    const pullRequest = 'pull_request' === eventName
+
+    let branch = getRequiredEnv(pullRequest ? 'GITHUB_HEAD_REF' : 'GITHUB_REF')
+    if (!pullRequest && !branch.startsWith(branchPrefix)) {
       if (branch.startsWith(tagPrefix)) {
         core.info(`Skipping tag build`)
         return
@@ -38,16 +40,19 @@ async function run(): Promise<void> {
       owner,
       repo,
       branch,
-      event: 'push'
+      event: pullRequest ? 'pull_request' : 'push'
     })
 
     let matched = false
     let workflow = ''
-    let count = 0
+    let headRepoName = ''
     for await (const item of octokit.paginate.iterator(listRuns)) {
       // There is some sort of bug where the pagination URLs point to a
-      // different URL with a different data format
-      const elements = ++count < 2 ? item.data : item.data.workflow_runs
+      // different endpoint URL which trips up the resulting representation
+      // In that case, fallback to the actual REST 'workflow_runs' property
+      const elements =
+        item.data.length === undefined ? item.data.workflow_runs : item.data
+
       for (const element of elements) {
         core.info(
           `${element.id} : ${element.workflow_url} : ${element.status} : 
${element.run_number}`
@@ -57,6 +62,7 @@ async function run(): Promise<void> {
           if (element.id.toString() === selfRunId) {
             matched = true
             workflow = element.workflow_url
+            headRepoName = pullRequest ? element.head_repository.full_name : ''
           }
           // Skip everything up to and matching this run
           continue
@@ -65,9 +71,10 @@ async function run(): Promise<void> {
         // Only cancel jobs with the same workflow
         if (
           workflow === element.workflow_url &&
-          element.status.toString() !== 'completed'
+          element.status.toString() !== 'completed' &&
+          (!pullRequest || headRepoName === element.head_repository.full_name)
         ) {
-          Promise.resolve(cancelRun(octokit, owner, repo, element.id))
+          await cancelRun(octokit, owner, repo, element.id)
         }
       }
     }

Reply via email to