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 db63ce1a50987abd5ce462a79f12d93080c69160 Author: Jason T. Greene <[email protected]> AuthorDate: Tue Feb 4 00:40:12 2020 -0600 Fix pagination --- .github/workflows/another.yml | 13 ---------- __tests__/main.test.ts | 11 ++++---- dist/index.js | 59 +++++++++++++++++++++++++++++-------------- src/main.ts | 19 ++++++++------ 4 files changed, 56 insertions(+), 46 deletions(-) diff --git a/.github/workflows/another.yml b/.github/workflows/another.yml deleted file mode 100644 index e0d2524..0000000 --- a/.github/workflows/another.yml +++ /dev/null @@ -1,13 +0,0 @@ -name: "build-test" -on: # rebuild any PRs and main branch changes - pull_request: - push: - -jobs: - build: # make sure build/ci work properly - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - run: | - echo Hello - sleep 60 diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index bb16f9d..efa2802 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -7,17 +7,16 @@ 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['RUN_ID'] = '33782469'; -// process.env['GITHUB_REPOSITORY'] = 'protean-project/quarkus-ci'; -// process.env['GITHUB_REF'] = 'master'; +// 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' // const options: cp.ExecSyncOptions = { // env: process.env // } // try { // console.log(cp.execSync(`node ${ip}`, options).toString()) // } catch (error) { -// console.log("Error stdout = " + error.stdout.toString()); +// console.log('Error stdout = ' + error.stdout.toString()) // } - // }) diff --git a/dist/index.js b/dist/index.js index 397e2b1..88b1aee 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1448,6 +1448,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; +var __asyncValues = (this && this.__asyncValues) || function (o) { + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); + var m = o[Symbol.asyncIterator], i; + return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); + function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } + function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } +}; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; @@ -1459,6 +1466,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const github = __importStar(__webpack_require__(469)); const core = __importStar(__webpack_require__(393)); function run() { + var e_1, _a; return __awaiter(this, void 0, void 0, function* () { try { const token = core.getInput('token'); @@ -1477,30 +1485,43 @@ function run() { const listRuns = octokit.actions.listRepoWorkflowRuns.endpoint.merge({ owner, repo, - branch, + // branch, event: 'push' }); - // eslint-disable-next-line @typescript-eslint/no-explicit-any - yield octokit.paginate(listRuns).then((runs) => __awaiter(this, void 0, void 0, function* () { - let matched = false; - let workflow = ''; - for (const element of runs) { - core.info(`${element.id} : ${element.workflow_url} : ${element.status} : ${element.run_number}`); - if (!matched) { - if (element.id.toString() === selfRunId) { - matched = true; - workflow = element.workflow_url; + let matched = false; + let workflow = ''; + let count = 0; + try { + for (var _b = __asyncValues(octokit.paginate.iterator(listRuns)), _c; _c = yield _b.next(), !_c.done;) { + const item = _c.value; + // 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; + for (const element of elements) { + core.info(`${element.id} : ${element.workflow_url} : ${element.status} : ${element.run_number}`); + if (!matched) { + if (element.id.toString() === selfRunId) { + matched = true; + workflow = element.workflow_url; + } + // Skip everything up to and matching this run + continue; + } + // Only cancel jobs with the same workflow + if (workflow === element.workflow_url && + element.status.toString() !== 'completed') { + Promise.resolve(cancelRun(octokit, owner, repo, element.id)); } - // Skip everything up to and matching this run - continue; - } - // Only cancel jobs with the same workflow - if (workflow === element.workflow_url && - element.status.toString() !== 'completed') { - yield cancelRun(octokit, owner, repo, element.id); } } - })); + } + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b); + } + finally { if (e_1) throw e_1.error; } + } } catch (error) { core.setFailed(error.message); diff --git a/src/main.ts b/src/main.ts index e79b04a..44db6fe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -25,15 +25,18 @@ async function run(): Promise<void> { const listRuns = octokit.actions.listRepoWorkflowRuns.endpoint.merge({ owner, repo, - branch, + // branch, event: 'push' }) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - await octokit.paginate(listRuns).then(async (runs: any[]) => { - let matched = false - let workflow = '' - for (const element of runs) { + let matched = false + let workflow = '' + let count = 0 + 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 + for (const element of elements) { core.info( `${element.id} : ${element.workflow_url} : ${element.status} : ${element.run_number}` ) @@ -52,10 +55,10 @@ async function run(): Promise<void> { workflow === element.workflow_url && element.status.toString() !== 'completed' ) { - await cancelRun(octokit, owner, repo, element.id) + Promise.resolve(cancelRun(octokit, owner, repo, element.id)) } } - }) + } } catch (error) { core.setFailed(error.message) }
