Skip to content

Commit

Permalink
Upgrade octokit types (#141)
Browse files Browse the repository at this point in the history
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
anarast and dependabot[bot] authored Apr 17, 2021
1 parent 02ad0ce commit e05b62f
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 17 additions & 14 deletions src/autoupdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +47,7 @@ export class AutoUpdater {

let pullsPage: octokit.OctokitResponse<any>;
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);
Expand Down Expand Up @@ -87,7 +83,7 @@ export class AutoUpdater {
return isUpdated;
}

async update(pull: octokit.PullsUpdateResponseData): Promise<boolean> {
async update(pull: PullRequestResponse['data']): Promise<boolean> {
const { ref } = pull.head;
ghCore.info(`Evaluating pull request #${pull.number}...`);

Expand All @@ -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.
Expand All @@ -135,7 +131,7 @@ export class AutoUpdater {
return true;
}

async prNeedsUpdate(pull: octokit.PullsUpdateResponseData): Promise<boolean> {
async prNeedsUpdate(pull: PullRequestResponse['data']): Promise<boolean> {
if (pull.merged === true) {
ghCore.warning('Skipping pull request, already merged.');
return false;
Expand Down Expand Up @@ -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.`,
Expand Down Expand Up @@ -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.`,
Expand Down Expand Up @@ -247,9 +252,7 @@ export class AutoUpdater {
return true;
}

async merge(
mergeOpts: octokit.RequestParameters & MergeOpts,
): Promise<boolean> {
async merge(mergeOpts: MergeParameters): Promise<boolean> {
const sleep = (timeMs: number) => {
return new Promise((resolve) => {
setTimeout(resolve, timeMs);
Expand Down
94 changes: 83 additions & 11 deletions test/autoupdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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,
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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']);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e05b62f

Please sign in to comment.