Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update @actions/github #54

Merged
merged 7 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"test:watch": "jest --watchAll"
},
"dependencies": {
"@actions/github": "^4.0.0",
"@actions/core": "^1.2.6",
"@actions/github": "^2.0.0",
"@octokit/types": "^5.5.0",
"@types/node": "^12.12.6",
"@zeit/ncc": "^0.22.3",
Expand Down
49 changes: 33 additions & 16 deletions src/autoupdater.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import * as github from '@actions/github';
import { GitHub } from '@actions/github/lib/utils';
import * as ghCore from '@actions/core';
import Octokit from '@octokit/rest';
import * as octokit from '@octokit/types';
import { ConfigLoader } from './config-loader';

interface MergeOpts {
owner: string;
repo: string;
base: string;
head: string;
commit_message?: string;
}

export class AutoUpdater {
eventData: any;
config: ConfigLoader;
octokit: github.GitHub;
octokit: InstanceType<typeof GitHub>;

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
constructor(config: ConfigLoader, eventData: any) {
constructor(
config: ConfigLoader,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
eventData: any,
) {
this.eventData = eventData;
this.config = config;
this.octokit = new github.GitHub(this.config.githubToken());
this.octokit = github.getOctokit(this.config.githubToken());
}

async handlePush(): Promise<number> {
Expand All @@ -36,10 +48,11 @@ export class AutoUpdater {
sort: 'updated',
direction: 'desc',
});
for await (const pullsPage of this.octokit.paginate.iterator(
paginatorOpts,
)) {
for (const pull of pullsPage.data) {

let pullsPage: octokit.OctokitResponse<any>;
for await (pullsPage of this.octokit.paginate.iterator(paginatorOpts)) {
let pull: octokit.PullsUpdateResponseData;
for (pull of pullsPage.data) {
ghCore.startGroup(`PR-${pull.number}`);
const isUpdated = await this.update(pull);
ghCore.endGroup();
Expand Down Expand Up @@ -74,7 +87,7 @@ export class AutoUpdater {
return isUpdated;
}

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

Expand All @@ -97,7 +110,7 @@ export class AutoUpdater {
}

const mergeMsg = this.config.mergeMsg();
const mergeOpts: Octokit.RequestOptions & Octokit.ReposMergeParams = {
const mergeOpts: octokit.RequestParameters & MergeOpts = {
owner: pull.head.repo.owner.login,
repo: pull.head.repo.name,
// We want to merge the base branch into this one.
Expand All @@ -114,8 +127,10 @@ export class AutoUpdater {
return true;
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async prNeedsUpdate(pull: any): Promise<boolean> {
async prNeedsUpdate(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
pull: any,
): Promise<boolean> {
if (pull.merged === true) {
ghCore.warning('Skipping pull request, already merged.');
return false;
Expand Down Expand Up @@ -206,7 +221,7 @@ export class AutoUpdater {
}

async merge(
mergeOpts: Octokit.RequestOptions & Octokit.ReposMergeParams,
mergeOpts: octokit.RequestParameters & MergeOpts,
): Promise<boolean> {
const sleep = (timeMs: number) => {
return new Promise((resolve) => {
Expand All @@ -215,13 +230,15 @@ export class AutoUpdater {
};

const doMerge = async () => {
const mergeResp = await this.octokit.repos.merge(mergeOpts);
const mergeResp: octokit.OctokitResponse<any> = await this.octokit.repos.merge(
mergeOpts,
);

// See https://developer.github.com/v3/repos/merging/#perform-a-merge
const { status } = mergeResp;
if (status === 200) {
ghCore.info(
`Branch update succesful, new branch HEAD: ${mergeResp.data.sha}.`,
`Branch update successful, new branch HEAD: ${mergeResp.data.sha}.`,
);
} else if (status === 204) {
ghCore.info(
Expand Down
7 changes: 5 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ export class Router {
eventData: any;
updater: AutoUpdater;

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
constructor(config: ConfigLoader, eventData: any) {
constructor(
config: ConfigLoader,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
eventData: any,
) {
this.updater = new AutoUpdater(config, eventData);
}

Expand Down
1 change: 1 addition & 0 deletions test/autoupdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jest.mock('../src/config-loader');

beforeEach(() => {
jest.resetAllMocks();
jest.spyOn(config, 'githubToken').mockImplementation(() => 'test-token');
});

const owner = 'chinthakagodawita';
Expand Down
Loading