From e05b62fb2a3ec75fd26c54340bf4f62a02da9bb5 Mon Sep 17 00:00:00 2001 From: Sara Tan Date: Fri, 16 Apr 2021 22:03:30 -0700 Subject: [PATCH] Upgrade octokit types (#141) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package.json | 2 +- src/autoupdater.ts | 31 ++++++++------ test/autoupdate.test.ts | 94 ++++++++++++++++++++++++++++++++++++----- yarn.lock | 12 ++++++ 4 files changed, 113 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index edf92242..dd095551 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "dependencies": { "@actions/core": "^1.2.6", "@actions/github": "^4.0.0", - "@octokit/types": "^5.5.0", + "@octokit/types": "^6.13.0", "@types/node": "^14.14.37", "@vercel/ncc": "^0.27.0", "typescript": "^4.2.4" diff --git a/src/autoupdater.ts b/src/autoupdater.ts index ec45b380..84caec72 100644 --- a/src/autoupdater.ts +++ b/src/autoupdater.ts @@ -3,14 +3,10 @@ import { GitHub } from '@actions/github/lib/utils'; import * as ghCore from '@actions/core'; import * as octokit from '@octokit/types'; import { ConfigLoader } from './config-loader'; +import { Endpoints } from '@octokit/types'; -interface MergeOpts { - owner: string; - repo: string; - base: string; - head: string; - commit_message?: string; -} +type PullRequestResponse = Endpoints['GET /repos/{owner}/{repo}/pulls/{pull_number}']['response']; +type MergeParameters = Endpoints['POST /repos/{owner}/{repo}/merges']['parameters']; export class AutoUpdater { eventData: any; @@ -51,7 +47,7 @@ export class AutoUpdater { let pullsPage: octokit.OctokitResponse; for await (pullsPage of this.octokit.paginate.iterator(paginatorOpts)) { - let pull: octokit.PullsUpdateResponseData; + let pull: PullRequestResponse['data']; for (pull of pullsPage.data) { ghCore.startGroup(`PR-${pull.number}`); const isUpdated = await this.update(pull); @@ -87,7 +83,7 @@ export class AutoUpdater { return isUpdated; } - async update(pull: octokit.PullsUpdateResponseData): Promise { + async update(pull: PullRequestResponse['data']): Promise { const { ref } = pull.head; ghCore.info(`Evaluating pull request #${pull.number}...`); @@ -110,7 +106,7 @@ export class AutoUpdater { } const mergeMsg = this.config.mergeMsg(); - const mergeOpts: octokit.RequestParameters & MergeOpts = { + const mergeOpts: MergeParameters = { owner: pull.head.repo.owner.login, repo: pull.head.repo.name, // We want to merge the base branch into this one. @@ -135,7 +131,7 @@ export class AutoUpdater { return true; } - async prNeedsUpdate(pull: octokit.PullsUpdateResponseData): Promise { + async prNeedsUpdate(pull: PullRequestResponse['data']): Promise { if (pull.merged === true) { ghCore.warning('Skipping pull request, already merged.'); return false; @@ -173,6 +169,10 @@ export class AutoUpdater { const excludedLabels = this.config.excludedLabels(); if (excludedLabels.length > 0) { for (const label of pull.labels) { + if (label.name === undefined) { + ghCore.warning(`Label name is undefined, continuing.`); + continue; + } if (excludedLabels.includes(label.name)) { ghCore.info( `Pull request has excluded label '${label.name}', skipping update.`, @@ -208,6 +208,11 @@ export class AutoUpdater { } for (const label of pull.labels) { + if (label.name === undefined) { + ghCore.warning(`Label name is undefined, continuing.`); + continue; + } + if (labels.includes(label.name)) { ghCore.info( `Pull request has label '${label.name}' and PR branch is behind base branch.`, @@ -247,9 +252,7 @@ export class AutoUpdater { return true; } - async merge( - mergeOpts: octokit.RequestParameters & MergeOpts, - ): Promise { + async merge(mergeOpts: MergeParameters): Promise { const sleep = (timeMs: number) => { return new Promise((resolve) => { setTimeout(resolve, timeMs); diff --git a/test/autoupdate.test.ts b/test/autoupdate.test.ts index 4c3d1de9..d4e8566b 100644 --- a/test/autoupdate.test.ts +++ b/test/autoupdate.test.ts @@ -7,7 +7,9 @@ if ('GITHUB_TOKEN' in process.env) { import nock from 'nock'; import config from '../src/config-loader'; import { AutoUpdater } from '../src/autoupdater'; -import { PullsUpdateResponseData } from '@octokit/types'; +import { Endpoints } from '@octokit/types'; + +type PullRequestResponse = Endpoints['GET /repos/{owner}/{repo}/pulls/{pull_number}']['response']; jest.mock('../src/config-loader'); @@ -30,6 +32,30 @@ const dummyEvent = { name: repo, }, }; +const invalidLabelPull = { + number: 1, + merged: false, + state: 'open', + labels: [ + { + id: 1, + }, + ], + base: { + ref: base, + label: base, + }, + head: { + label: head, + ref: head, + repo: { + name: repo, + owner: { + login: owner, + }, + }, + }, +}; const validPull = { number: 1, merged: false, @@ -69,7 +95,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (pull as unknown) as PullsUpdateResponseData, + (pull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(false); }); @@ -82,7 +108,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (pull as unknown) as PullsUpdateResponseData, + (pull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(false); }); @@ -97,7 +123,7 @@ describe('test `prNeedsUpdate`', () => { }); const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (pull as unknown) as PullsUpdateResponseData, + (pull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(false); }); @@ -111,7 +137,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (validPull as unknown) as PullsUpdateResponseData, + (validPull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(false); @@ -130,7 +156,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (validPull as unknown) as PullsUpdateResponseData, + (validPull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(true); @@ -187,7 +213,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (validPull as unknown) as PullsUpdateResponseData, + (validPull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(false); @@ -220,6 +246,52 @@ describe('test `prNeedsUpdate`', () => { expect(config.excludedLabels).toHaveBeenCalled(); }); + test('pull request has labels with no name', async () => { + (config.pullRequestFilter as jest.Mock).mockReturnValue('labelled'); + (config.pullRequestLabels as jest.Mock).mockReturnValue(['one', 'two']); + (config.excludedLabels as jest.Mock).mockReturnValue([]); + + const scope = nock('https://api.github.com:443') + .get(`/repos/${owner}/${repo}/compare/${head}...${base}`) + .reply(200, { + behind_by: 1, + }); + + const updater = new AutoUpdater(config, {}); + const needsUpdate = await updater.prNeedsUpdate( + (invalidLabelPull as unknown) as PullRequestResponse['data'], + ); + + expect(needsUpdate).toEqual(false); + expect(scope.isDone()).toEqual(true); + expect(config.pullRequestFilter).toHaveBeenCalled(); + expect(config.pullRequestLabels).toHaveBeenCalled(); + expect(config.excludedLabels).toHaveBeenCalled(); + }); + + test('pull request has labels with no name - excluded labels checked', async () => { + (config.pullRequestFilter as jest.Mock).mockReturnValue('labelled'); + (config.pullRequestLabels as jest.Mock).mockReturnValue([]); + (config.excludedLabels as jest.Mock).mockReturnValue(['one', 'two']); + + const scope = nock('https://api.github.com:443') + .get(`/repos/${owner}/${repo}/compare/${head}...${base}`) + .reply(200, { + behind_by: 1, + }); + + const updater = new AutoUpdater(config, {}); + const needsUpdate = await updater.prNeedsUpdate( + (invalidLabelPull as unknown) as PullRequestResponse['data'], + ); + + expect(needsUpdate).toEqual(false); + expect(scope.isDone()).toEqual(true); + expect(config.pullRequestFilter).toHaveBeenCalled(); + expect(config.pullRequestLabels).toHaveBeenCalled(); + expect(config.excludedLabels).toHaveBeenCalled(); + }); + test('pull request labels do not match', async () => { (config.pullRequestFilter as jest.Mock).mockReturnValue('labelled'); (config.pullRequestLabels as jest.Mock).mockReturnValue(['three', 'four']); @@ -233,7 +305,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (validPull as unknown) as PullsUpdateResponseData, + (validPull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(false); @@ -286,7 +358,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (validPull as unknown) as PullsUpdateResponseData, + (validPull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(true); @@ -314,7 +386,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (validPull as unknown) as PullsUpdateResponseData, + (validPull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(false); @@ -336,7 +408,7 @@ describe('test `prNeedsUpdate`', () => { const updater = new AutoUpdater(config, {}); const needsUpdate = await updater.prNeedsUpdate( - (validPull as unknown) as PullsUpdateResponseData, + (validPull as unknown) as PullRequestResponse['data'], ); expect(needsUpdate).toEqual(true); diff --git a/yarn.lock b/yarn.lock index d975af68..9cd8dbe0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -560,6 +560,11 @@ "@octokit/types" "^5.0.0" universal-user-agent "^6.0.0" +"@octokit/openapi-types@^6.0.0": + version "6.0.0" + resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-6.0.0.tgz#7da8d7d5a72d3282c1a3ff9f951c8133a707480d" + integrity sha512-CnDdK7ivHkBtJYzWzZm7gEkanA7gKH6a09Eguz7flHw//GacPJLmkHA3f3N++MJmlxD1Fl+mB7B32EEpSCwztQ== + "@octokit/plugin-paginate-rest@^2.2.3": version "2.4.0" resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-2.4.0.tgz#92f951ddc8a1cd505353fa07650752ca25ed7e93" @@ -605,6 +610,13 @@ dependencies: "@types/node" ">= 8" +"@octokit/types@^6.13.0": + version "6.13.0" + resolved "https://registry.yarnpkg.com/@octokit/types/-/types-6.13.0.tgz#779e5b7566c8dde68f2f6273861dd2f0409480d0" + integrity sha512-W2J9qlVIU11jMwKHUp5/rbVUeErqelCsO5vW5PKNb7wAXQVUz87Rc+imjlEvpvbH8yUb+KHmv8NEjVZdsdpyxA== + dependencies: + "@octokit/openapi-types" "^6.0.0" + "@sinonjs/commons@^1.7.0": version "1.8.1" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.1.tgz#e7df00f98a203324f6dc7cc606cad9d4a8ab2217"