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.8 #221

Closed
wants to merge 7 commits into from
Closed
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
25 changes: 25 additions & 0 deletions __tests__/api-helper2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,28 @@ describe('ApiHelper with params', () => {
});
});
});

describe('ApiHelper without logger', () => {
disableNetConnect(nock);
testEnv();

const helper = new ApiHelper(octokit, context, undefined, {branch: 'test-branch', sender: 'test-sender', refForUpdate: 'test-ref', suppressBPError: true});

describe('updateRef', () => {
it('should not output warning', async() => {
const mockStdout = spyOnStdout();
nock('https://api.github.com')
.patch('/repos/hello/world/git/refs/' + encodeURIComponent('test-ref'), body => {
expect(body).toHaveProperty('sha');
return body;
})
.reply(403, {
'message': 'Required status check "Test" is expected.',
});

await helper.updateRef(createCommitResponse, 'test-ref', false);

stdoutCalledWith(mockStdout, []);
});
});
});
32 changes: 29 additions & 3 deletions __tests__/context-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { testEnv, getContext } from '@technote-space/github-action-test-helper';
import { Logger, ContextHelper } from '../src';

const {isRelease, isPush, isPr, isIssue, isCron, isCustomEvent, getGitUrl, getRepository, getTagName, getSender, showActionInfo} = ContextHelper;
const {isRelease, isPush, isPr, isIssue, isCron, isCustomEvent, isCreateTag, getGitUrl, getRepository, getTagName, getSender, showActionInfo} = ContextHelper;

describe('isRelease', () => {
it('should return true', () => {
Expand Down Expand Up @@ -93,6 +93,32 @@ describe('isCustomEvent', () => {
});
});

describe('isCreateTag', () => {
it('should return true', () => {
expect(isCreateTag(getContext({
eventName: 'create',
payload: {
'ref_type': 'tag',
},
}))).toBe(true);
});

it('should return false 1', () => {
expect(isCreateTag(getContext({
eventName: 'create',
payload: {
'ref_type': 'branch',
},
}))).toBe(false);
});

it('should return false 2', () => {
expect(isCreateTag(getContext({
eventName: 'release',
}))).toBe(false);
});
});

describe('getGitUrl', () => {
testEnv();

Expand Down Expand Up @@ -221,7 +247,7 @@ describe('showActionInfo', () => {
stdoutCalledWith(mockStdout, [
'',
'==================================================',
'Version: v1.2.3',
'Version: v1.2.3(undefined)',
'Event: push',
'Action: rerequested',
'sha: test-sha',
Expand Down Expand Up @@ -262,7 +288,7 @@ describe('showActionInfo', () => {
stdoutCalledWith(mockStdout, [
'',
'==================================================',
'Version: hello/[email protected]',
'Version: hello/[email protected](162553222be3497f057501e028b47afc64944d84)',
'Event: push',
'Action: rerequested',
'sha: test-sha',
Expand Down
28 changes: 14 additions & 14 deletions __tests__/fixtures/build2.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"owner": "hello",
"repo": "world",
"sha": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2",
"ref": "release/v1.2.3",
"tagName": "v1.2.3",
"branch": "gh-actions",
"tags": [
"v1.2.3",
"v1",
"v1.2"
],
"updated_at": "2020-01-01T01:23:45.000Z"
}
{
"owner": "hello",
"repo": "world",
"sha": "162553222be3497f057501e028b47afc64944d84",
"ref": "release/v1.2.3",
"tagName": "v1.2.3",
"branch": "gh-actions",
"tags": [
"v1.2.3",
"v1",
"v1.2"
],
"updated_at": "2020-01-01T01:23:45.000Z"
}
28 changes: 14 additions & 14 deletions __tests__/fixtures/test/build.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"owner": "hello",
"repo": "world",
"sha": "ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2",
"ref": "release/v1.2.3",
"tagName": "v1.2.3",
"branch": "gh-actions",
"tags": [
"v1.2.3",
"v1",
"v1.2"
],
"updated_at": "2020-01-01T01:23:45.000Z"
}
{
"owner": "hello",
"repo": "world",
"sha": "162553222be3497f057501e028b47afc64944d84",
"ref": "release/v1.2.3",
"tagName": "v1.2.3",
"branch": "gh-actions",
"tags": [
"v1.2.3",
"v1",
"v1.2"
],
"updated_at": "2020-01-01T01:23:45.000Z"
}
2 changes: 1 addition & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ describe('getBuildInfo', () => {
expect(getBuildInfo(path.resolve(__dirname, 'fixtures', 'build2.json'))).toEqual({
'owner': 'hello',
'repo': 'world',
'sha': 'ed968e840d10d2d313a870bc131a4e2c311d7ad09bdf32b3418147221f51a6e2',
'sha': '162553222be3497f057501e028b47afc64944d84',
'ref': 'release/v1.2.3',
'tagName': 'v1.2.3',
'branch': 'gh-actions',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@technote-space/github-action-helper",
"version": "0.8.7",
"version": "0.8.8",
"description": "Helper to filter GitHub Action.",
"author": {
"name": "Technote",
Expand Down Expand Up @@ -32,7 +32,7 @@
"sprintf-js": "^1.1.2"
},
"devDependencies": {
"@technote-space/github-action-test-helper": "^0.2.0",
"@technote-space/github-action-test-helper": "^0.2.1",
"@types/jest": "^25.1.2",
"@types/node": "^13.7.0",
"@typescript-eslint/eslint-plugin": "^2.19.0",
Expand Down
73 changes: 44 additions & 29 deletions src/api-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class ApiHelper {
constructor(
private readonly octokit: Octokit,
private readonly context: Context,
private readonly logger: Logger,
private readonly logger?: Logger,
options?: { branch?: string; sender?: string; refForUpdate?: string; suppressBPError?: boolean },
) {
this.branch = options?.branch;
Expand All @@ -68,6 +68,15 @@ export default class ApiHelper {
this.suppressBPError = options?.suppressBPError;
}

/**
* @param {function} caller caller
*/
private callLogger = (caller: (logger: Logger) => void): void => {
if (this.logger) {
caller(this.logger);
}
};

/**
* @return {string|boolean} sender
*/
Expand Down Expand Up @@ -204,7 +213,7 @@ export default class ApiHelper {
return true;
} catch (error) {
if (this.suppressBPError === true && this.isProtectedBranchError(error)) {
this.logger.warn('Branch is protected.');
this.callLogger(logger => logger.warn('Branch is protected.'));
} else {
throw error;
}
Expand Down Expand Up @@ -319,6 +328,18 @@ export default class ApiHelper {
return {branchName, headName, refName};
};

/**
* @param {string} createBranchName branch name
* @param {PullsCreateParams} detail detail
* @return {Promise<PullsInfo>} info
*/
private createPulls = async(createBranchName: string, detail: PullsCreateParams): Promise<PullsInfo> => {
this.callLogger(async logger => logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
const created = await this.pullsCreate(createBranchName, detail);
this.callLogger(logger => logger.endProcess());
return Object.assign({isPrCreated: true}, created.data);
};

/**
* @param {string} createBranchName branch name
* @param {PullsCreateParams} detail detail
Expand All @@ -327,16 +348,13 @@ export default class ApiHelper {
public pullsCreateOrUpdate = async(createBranchName: string, detail: PullsCreateParams): Promise<PullsInfo> => {
const pullRequest = await this.findPullRequest(createBranchName);
if (pullRequest) {
this.logger.startProcess('Updating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
this.callLogger(async logger => logger.startProcess('Updating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
const updated = await this.pullsUpdate(pullRequest.number, detail);
this.logger.endProcess();
this.callLogger(logger => logger.endProcess());
return Object.assign({isPrCreated: false}, updated.data);
} else {
this.logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
const created = await this.pullsCreate(createBranchName, detail);
this.logger.endProcess();
return Object.assign({isPrCreated: true}, created.data);
}

return this.createPulls(createBranchName, detail);
};

/**
Expand All @@ -347,16 +365,13 @@ export default class ApiHelper {
public pullsCreateOrComment = async(createBranchName: string, detail: PullsCreateParams): Promise<PullsInfo> => {
const pullRequest = await this.findPullRequest(createBranchName);
if (pullRequest) {
this.logger.startProcess('Creating comment to PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
this.callLogger(async logger => logger.startProcess('Creating comment to PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
await this.createCommentToPr(createBranchName, detail.body);
this.logger.endProcess();
this.callLogger(logger => logger.endProcess());
return Object.assign({isPrCreated: false}, pullRequest);
} else {
this.logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false));
const created = await this.pullsCreate(createBranchName, detail);
this.logger.endProcess();
return Object.assign({isPrCreated: true}, created.data);
}

return this.createPulls(createBranchName, detail);
};

/**
Expand Down Expand Up @@ -396,7 +411,7 @@ export default class ApiHelper {
*/
private checkDiff = (files: string[]): boolean => {
if (!files.length) {
this.logger.info('There is no diff.');
this.callLogger(logger => logger.info('There is no diff.'));
return false;
}

Expand All @@ -410,13 +425,13 @@ export default class ApiHelper {
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>>} commit
*/
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> => {
this.logger.startProcess('Creating blobs...');
this.callLogger(logger => logger.startProcess('Creating blobs...'));
const blobs = await this.filesToBlobs(rootDir, files);

this.logger.startProcess('Creating tree...');
this.callLogger(logger => logger.startProcess('Creating tree...'));
const tree = await this.createTree(blobs);

this.logger.startProcess('Creating commit... [%s]', tree.data.sha);
this.callLogger(logger => logger.startProcess('Creating commit... [%s]', tree.data.sha));
return this.createCommit(commitMessage, tree);
};

Expand All @@ -434,13 +449,13 @@ export default class ApiHelper {
const commit = await this.prepareCommit(rootDir, commitMessage, files);
const ref = await this.getRefForUpdate(true);

this.logger.startProcess('Updating ref... [%s] [%s]', ref, commit.data.sha);
this.callLogger(logger => logger.startProcess('Updating ref... [%s] [%s]', ref, commit.data.sha));
if (await this.updateRef(commit, ref, false)) {
process.env.GITHUB_SHA = commit.data.sha;
exportVariable('GITHUB_SHA', commit.data.sha);
}

this.logger.endProcess();
this.callLogger(logger => logger.endProcess());
return true;
};

Expand All @@ -461,10 +476,10 @@ export default class ApiHelper {
const commit = await this.prepareCommit(rootDir, commitMessage, files);
const ref = await this.getRef(headName);
if (null === ref) {
this.logger.startProcess('Creating reference... [%s] [%s]', refName, commit.data.sha);
this.callLogger(logger => logger.startProcess('Creating reference... [%s] [%s]', refName, commit.data.sha));
await this.createRef(commit, refName);
} else {
this.logger.startProcess('Updating reference... [%s] [%s]', refName, commit.data.sha);
this.callLogger(logger => logger.startProcess('Updating reference... [%s] [%s]', refName, commit.data.sha));
await this.updateRef(commit, headName, true);
}

Expand All @@ -479,7 +494,7 @@ export default class ApiHelper {
const {branchName, headName, refName} = this.getBranchInfo(createBranchName);
const pullRequest = await this.findPullRequest(branchName);
if (pullRequest) {
this.logger.startProcess('Closing PullRequest... [%s]', branchName);
this.callLogger(logger => logger.startProcess('Closing PullRequest... [%s]', branchName));
if (message) {
await this.createCommentToPr(branchName, message);
}
Expand All @@ -489,18 +504,18 @@ export default class ApiHelper {
base: undefined,
});
} else {
this.logger.info('There is no PullRequest named [%s]', branchName);
this.callLogger(logger => logger.info('There is no PullRequest named [%s]', branchName));

const ref = await this.getRef(headName);
if (!ref) {
this.logger.info('There is no reference named [%s]', refName);
this.callLogger(logger => logger.info('There is no reference named [%s]', refName));
return;
}
}

this.logger.startProcess('Deleting reference... [%s]', refName);
this.callLogger(logger => logger.startProcess('Deleting reference... [%s]', refName));
await this.deleteRef(headName);
this.logger.endProcess();
this.callLogger(logger => logger.endProcess());
};

/**
Expand Down
6 changes: 4 additions & 2 deletions src/context-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const isCron = (context: Context): boolean => 'schedule' === context.even

export const isCustomEvent = (context: Context): boolean => 'repository_dispatch' === context.eventName;

export const isCreateTag = (context: Context): boolean => 'create' === context.eventName && 'tag' === context.payload.ref_type;

export const getTagName = (context: Context): string => isRelease(context) ? context.payload.release.tag_name : (/^refs\/tags\//.test(context.ref) ? context.ref.replace(/^refs\/tags\//, '') : '');

export const getSender = (context: Context): string | false => context.payload.sender && context.payload.sender.type === 'User' ? context.payload.sender.login : false;
Expand All @@ -36,9 +38,9 @@ export const showActionInfo = (rootDir: string, logger: Logger, context: Context
logger.log(separator);
if (false !== info) {
if ('owner' in info) {
logger.log('Version: %s/%s@%s', info.owner, info.repo, info.tagName);
logger.log('Version: %s/%s@%s(%s)', info.owner, info.repo, info.tagName, info.sha);
} else {
logger.log('Version: %s', info.tagName);
logger.log('Version: %s(%s)', info.tagName, info.sha);
}
}
logger.log('Event: %s', context.eventName);
Expand Down
Loading