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

release/v0.8.3 #210

Merged
merged 11 commits into from
Feb 4, 2020
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/node_modules
/coverage
/dist
.eslintcache
11 changes: 5 additions & 6 deletions __tests__/api-helper1.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-magic-numbers */
import nock from 'nock';
import path from 'path';
import { GitHub } from '@actions/github' ;
import { GitCreateTreeResponse, GitCreateCommitResponse } from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import {
disableNetConnect,
testEnv,
Expand Down Expand Up @@ -30,10 +29,10 @@ const context = getContext({
number: 123,
},
});
const octokit = new GitHub('test-token');
const octokit = new Octokit({auth: 'token test-token'});
const logger = new Logger();

const createCommitResponse = createResponse<GitCreateCommitResponse>({
const createCommitResponse = createResponse<Octokit.GitCreateCommitResponse>({
author: {
date: '',
email: '',
Expand Down Expand Up @@ -160,7 +159,7 @@ describe('ApiHelper', () => {
return getApiFixture(rootDir, 'repos.git.commits');
});

const commit = await helper.createCommit('test commit message', createResponse<GitCreateTreeResponse>({
const commit = await helper.createCommit('test commit message', createResponse<Octokit.GitCreateTreeResponse>({
sha: 'tree-sha',
tree: [],
url: '',
Expand Down Expand Up @@ -197,7 +196,7 @@ describe('ApiHelper', () => {
},
},
}), logger);
const commit = await helper.createCommit('test commit message', createResponse<GitCreateTreeResponse>({
const commit = await helper.createCommit('test commit message', createResponse<Octokit.GitCreateTreeResponse>({
sha: 'tree-sha',
tree: [],
url: '',
Expand Down
7 changes: 3 additions & 4 deletions __tests__/api-helper2.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable no-magic-numbers */
import nock from 'nock';
import path from 'path';
import { GitHub } from '@actions/github' ;
import { GitCreateCommitResponse } from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import {
disableNetConnect,
testEnv,
Expand Down Expand Up @@ -30,10 +29,10 @@ const context = getContext({
number: 123,
},
});
const octokit = new GitHub('test-token');
const octokit = new Octokit({auth: 'token test-token'});
const logger = new Logger();

const createCommitResponse = createResponse<GitCreateCommitResponse>({
const createCommitResponse = createResponse<Octokit.GitCreateCommitResponse>({
author: {
date: '',
email: '',
Expand Down
22 changes: 13 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "@technote-space/github-action-helper",
"version": "0.8.2",
"version": "0.8.3",
"description": "Helper to filter GitHub Action.",
"author": "Technote <[email protected]> (https://technote.space)",
"author": {
"name": "Technote",
"email": "[email protected]",
"url": "https://technote.space"
},
"license": "MIT",
"keywords": [
"github",
Expand All @@ -28,16 +32,16 @@
"sprintf-js": "^1.1.2"
},
"devDependencies": {
"@technote-space/github-action-test-helper": "^0.1.4",
"@types/jest": "^24.9.1",
"@types/node": "^13.5.0",
"@typescript-eslint/eslint-plugin": "^2.17.0",
"@typescript-eslint/parser": "^2.17.0",
"@technote-space/github-action-test-helper": "^0.1.5",
"@types/jest": "^25.1.1",
"@types/node": "^13.7.0",
"@typescript-eslint/eslint-plugin": "^2.19.0",
"@typescript-eslint/parser": "^2.19.0",
"eslint": "^6.8.0",
"jest": "^25.1.0",
"jest-circus": "^25.1.0",
"nock": "^11.7.2",
"ts-jest": "^25.0.0",
"ts-jest": "^25.2.0",
"typescript": "^3.7.5"
},
"publishConfig": {
Expand All @@ -46,7 +50,7 @@
"scripts": {
"build": "tsc",
"test": "yarn lint && yarn cover",
"lint": "eslint 'src/**/*.ts' '__tests__/**/*.ts'",
"lint": "eslint 'src/**/*.ts' '__tests__/**/*.ts' --cache",
"lint:fix": "eslint --fix 'src/**/*.ts' '__tests__/**/*.ts'",
"cover": "jest --coverage",
"update": "ncu -u && yarn install && yarn upgrade && yarn audit"
Expand Down
69 changes: 29 additions & 40 deletions src/api-helper.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import fs from 'fs';
import path from 'path';
import { GitHub } from '@actions/github/lib/github';
import { Context } from '@actions/github/lib/context';
import {
Response,
AnyResponse,
GitCreateTreeResponse,
GitCreateCommitResponse,
GitGetCommitResponse,
PullsGetResponse,
PullsListResponseItem,
PullsCreateResponse,
PullsUpdateResponse,
} from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import { exportVariable } from '@actions/core';
import { Logger } from './index';
import { getRefForUpdate, isPrRef, getBranch } from './utils';
Expand Down Expand Up @@ -55,10 +44,10 @@ export default class ApiHelper {
private readonly sender?: string | undefined = undefined;
private readonly suppressBPError?: boolean | undefined = undefined;
private readonly refForUpdate?: string | undefined = undefined;
private prCache: { [key: number]: Response<PullsGetResponse> } = {};
private prCache: { [key: number]: Octokit.Response<Octokit.PullsGetResponse> } = {};

/**
* @param {GitHub} octokit octokit
* @param {Octokit} octokit octokit
* @param {Context} context context
* @param {Logger} logger logger
* @param {object} options options
Expand All @@ -68,7 +57,7 @@ export default class ApiHelper {
* @param {boolean|undefined} options.suppressBPError suppress branch protection error?
*/
constructor(
private readonly octokit: GitHub,
private readonly octokit: Octokit,
private readonly context: Context,
private readonly logger: Logger,
options?: { branch?: string; sender?: string; refForUpdate?: string; suppressBPError?: boolean },
Expand Down Expand Up @@ -120,18 +109,18 @@ export default class ApiHelper {
private getCommitSha = (): string => isPrRef(this.context) && this.context.payload.pull_request ? this.context.payload.pull_request.head.sha : this.context.sha;

/**
* @return {Promise<Response<GitGetCommitResponse>>} commit
* @return {Promise<Octokit.Response<Octokit.GitGetCommitResponse>>} commit
*/
private getCommit = async(): Promise<Response<GitGetCommitResponse>> => this.octokit.git.getCommit({
private getCommit = async(): Promise<Octokit.Response<Octokit.GitGetCommitResponse>> => this.octokit.git.getCommit({
owner: this.context.repo.owner,
repo: this.context.repo.repo,
'commit_sha': this.getCommitSha(),
});

/**
* @return {Promise<Response<PullsGetResponse>>} commit
* @return {Promise<Octokit.Response<Octokit.PullsGetResponse>>} commit
*/
private getPR = async(): Promise<Response<PullsGetResponse>> => {
private getPR = async(): Promise<Octokit.Response<Octokit.PullsGetResponse>> => {
const key = parseInt(this.context.payload.number, 10);
if (!(key in this.prCache)) {
this.prCache[key] = await this.octokit.pulls.get({
Expand All @@ -153,9 +142,9 @@ export default class ApiHelper {

/**
* @param {{ path: string, sha: string }[]} blobs blobs
* @return {Promise<Response<GitCreateTreeResponse>>} tree
* @return {Promise<Octokit.Response<Octokit.GitCreateTreeResponse>>} tree
*/
public createTree = async(blobs: { path: string; sha: string }[]): Promise<Response<GitCreateTreeResponse>> => this.octokit.git.createTree({
public createTree = async(blobs: { path: string; sha: string }[]): Promise<Octokit.Response<Octokit.GitCreateTreeResponse>> => this.octokit.git.createTree({
owner: this.context.repo.owner,
repo: this.context.repo.repo,
'base_tree': (await this.getCommit()).data.tree.sha,
Expand All @@ -169,10 +158,10 @@ export default class ApiHelper {

/**
* @param {string} commitMessage commit message
* @param {Response<GitCreateTreeResponse>} tree tree
* @return {Promise<Response<GitCreateCommitResponse>>} commit
* @param {Octokit.Response<Octokit.GitCreateTreeResponse>} tree tree
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>>} commit
*/
public createCommit = async(commitMessage: string, tree: Response<GitCreateTreeResponse>): Promise<Response<GitCreateCommitResponse>> => this.octokit.git.createCommit({
public createCommit = async(commitMessage: string, tree: Octokit.Response<Octokit.GitCreateTreeResponse>): Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> => this.octokit.git.createCommit({
owner: this.context.repo.owner,
repo: this.context.repo.repo,
tree: tree.data.sha,
Expand All @@ -182,9 +171,9 @@ export default class ApiHelper {

/**
* @param {string} refName refName
* @return {Promise<AnyResponse|null>} refName
* @return {Promise<Octokit.AnyResponse|null>} refName
*/
private getRef = async(refName: string): Promise<AnyResponse | null> => {
private getRef = async(refName: string): Promise<Octokit.AnyResponse | null> => {
try {
return await this.octokit.git.getRef({
owner: this.context.repo.owner,
Expand All @@ -197,12 +186,12 @@ export default class ApiHelper {
};

/**
* @param {Response<GitCreateCommitResponse>} commit commit
* @param {Octokit.Response<Octokit.GitCreateCommitResponse>} commit commit
* @param {string} refName refName
* @param {boolean} force force
* @return {Promise<void>} void
*/
public updateRef = async(commit: Response<GitCreateCommitResponse>, refName: string, force: boolean): Promise<boolean> => {
public updateRef = async(commit: Octokit.Response<Octokit.GitCreateCommitResponse>, refName: string, force: boolean): Promise<boolean> => {
try {
await this.octokit.git.updateRef({
owner: this.context.repo.owner,
Expand All @@ -225,11 +214,11 @@ export default class ApiHelper {
};

/**
* @param {Response<GitCreateCommitResponse>} commit commit
* @param {Octokit.Response<Octokit.GitCreateCommitResponse>} commit commit
* @param {string} refName refName
* @return {Promise<void>} void
*/
public createRef = async(commit: Response<GitCreateCommitResponse>, refName: string): Promise<void> => {
public createRef = async(commit: Octokit.Response<Octokit.GitCreateCommitResponse>, refName: string): Promise<void> => {
await this.octokit.git.createRef({
owner: this.context.repo.owner,
repo: this.context.repo.repo,
Expand All @@ -252,9 +241,9 @@ export default class ApiHelper {

/**
* @param {string} branchName branch name
* @return {Promise<PullsListResponseItem>} pull request
* @return {Promise<Octokit.PullsListResponseItem>} pull request
*/
public findPullRequest = async(branchName: string): Promise<PullsListResponseItem | null> => {
public findPullRequest = async(branchName: string): Promise<Octokit.PullsListResponseItem | null> => {
const response = await this.octokit.pulls.list({
owner: this.context.repo.owner,
repo: this.context.repo.repo,
Expand All @@ -269,9 +258,9 @@ export default class ApiHelper {

/**
* @param {PullsListParams} params params
* @return {AsyncIterable<PullsListResponseItem>} pull request list
* @return {AsyncIterable<Octokit.PullsListResponseItem>} pull request list
*/
public async* pullsList(params: PullsListParams): AsyncIterable<PullsListResponseItem> {
public async* pullsList(params: PullsListParams): AsyncIterable<Octokit.PullsListResponseItem> {
const perPage = 100;
let page = 1;
while (true) {
Expand All @@ -295,9 +284,9 @@ export default class ApiHelper {
/**
* @param {string} branchName branch name
* @param {PullsCreateParams} detail detail
* @return {Promise<PullsCreateResponse>} pull
* @return {Promise<Octokit.Response<Octokit.PullsCreateResponse>>} pull
*/
public pullsCreate = async(branchName: string, detail: PullsCreateParams): Promise<Response<PullsCreateResponse>> => this.octokit.pulls.create({
public pullsCreate = async(branchName: string, detail: PullsCreateParams): Promise<Octokit.Response<Octokit.PullsCreateResponse>> => this.octokit.pulls.create({
owner: this.context.repo.owner,
repo: this.context.repo.repo,
head: `${this.context.repo.owner}:${getBranch(branchName, false)}`,
Expand All @@ -308,9 +297,9 @@ export default class ApiHelper {
/**
* @param {number} number pull number
* @param {PullsUpdateParams} detail detail
* @return {Promise<PullsUpdateResponse>} pull
* @return {Promise<Octokit.Response<Octokit.PullsUpdateResponse>>} pull
*/
public pullsUpdate = async(number: number, detail: PullsUpdateParams): Promise<Response<PullsUpdateResponse>> => this.octokit.pulls.update({
public pullsUpdate = async(number: number, detail: PullsUpdateParams): Promise<Octokit.Response<Octokit.PullsUpdateResponse>> => this.octokit.pulls.update({
owner: this.context.repo.owner,
repo: this.context.repo.repo,
'pull_number': number,
Expand Down Expand Up @@ -418,9 +407,9 @@ export default class ApiHelper {
* @param {string} rootDir root dir
* @param {string} commitMessage commit message
* @param {string[]} files files
* @return {Promise<Response<GitCreateCommitResponse>>} commit
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>>} commit
*/
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<Response<GitCreateCommitResponse>> => {
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> => {
this.logger.startProcess('Creating blobs...');
const blobs = await this.filesToBlobs(rootDir, files);

Expand Down
Loading