Skip to content

Commit

Permalink
Merge pull request #19 from technote-space/release/v0.1.1
Browse files Browse the repository at this point in the history
Release/v0.1.1
  • Loading branch information
technote-space committed Sep 24, 2019
2 parents fccdc71 + 57f8215 commit 17673e0
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/issue_opened.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
steps:
- name: Assign issues to project
if: github.event.action == 'opened'
uses: alex-page/github-project-automation-plus@master
uses: alex-page/github-project-automation-plus@v0.0.3
with:
project: Backlog
column: To do
Expand Down
188 changes: 117 additions & 71 deletions __tests__/api-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,34 @@ describe('ApiHelper', () => {
});

describe('updateRef', () => {
const response = createResponse<GitCreateCommitResponse>({
author: {
date: '',
email: '',
name: '',
},
committer: {
date: '',
email: '',
name: '',
},
message: '',
'node_id': '',
parents: [],
sha: '',
tree: {
sha: '',
url: '',
},
url: '',
verification: {
payload: null,
reason: '',
signature: null,
verified: true,
},
});

it('should update ref', async() => {
const fn1 = jest.fn();
const fn2 = jest.fn();
Expand All @@ -158,71 +186,28 @@ describe('ApiHelper', () => {
return getApiFixture(path.resolve(__dirname, 'fixtures'), 'repos.git.refs');
});

await helper.updateRef(createResponse<GitCreateCommitResponse>({
author: {
date: '',
email: '',
name: '',
},
committer: {
date: '',
email: '',
name: '',
},
message: '',
'node_id': '',
parents: [],
sha: '',
tree: {
sha: '',
url: '',
},
url: '',
verification: {
payload: null,
reason: '',
signature: null,
verified: true,
},
}), octokit, context);
await helper.updateRef(response, octokit, context);

expect(fn1).toBeCalledTimes(1);
expect(fn2).toBeCalledTimes(1);
});
});

describe('checkProtected', () => {
it('should return true', async() => {
const fn1 = jest.fn();
nock('https://api.github.com')
.persist()
.get('/repos/hello/world/branches/test/protection')
.reply(200, () => {
fn1();
return getApiFixture(path.resolve(__dirname, 'fixtures'), 'repos.branches.protection');
});

expect(await helper.checkProtected(octokit, context)).toBeTruthy();
expect(fn1).toBeCalledTimes(1);
});

it('should return false', async() => {
const fn1 = jest.fn();
it('should output warning', async() => {
nock('https://api.github.com')
.persist()
.get('/repos/hello/world/branches/test/protection')
.reply(404, (uri, body) => {
fn1();
.patch('/repos/hello/world/git/refs/' + encodeURIComponent('heads/test'), body => {
expect(body).toHaveProperty('sha');
return body;
})
.reply(403, {
'message': 'Required status check "Test" is expected.',
});

expect(await helper.checkProtected(octokit, context)).toBeFalsy();
expect(fn1).toBeCalledTimes(1);
await expect(helper.updateRef(response, octokit, context)).rejects.toThrow('Required status check "Test" is expected.');
});
});

describe('commit', () => {
it('should not commit 1', async() => {
it('should not commit', async() => {
const mockStdout = spyOnStdout();

expect(await helper.commit(path.resolve(__dirname, '..'), 'test commit message', [], octokit, context)).toBeFalsy();
Expand All @@ -232,27 +217,11 @@ describe('ApiHelper', () => {
]);
});

it('should not commit 2', async() => {
const mockStdout = spyOnStdout();
nock('https://api.github.com')
.persist()
.get('/repos/hello/world/branches/test/protection')
.reply(200, () => getApiFixture(path.resolve(__dirname, 'fixtures'), 'repos.branches.protection'));

expect(await helper.commit(path.resolve(__dirname, 'fixtures'), 'test commit message', ['build1.json', 'build2.json'], octokit, context)).toBeFalsy();

stdoutCalledWith(mockStdout, [
'##[warning]Branch [test] is protected.',
]);
});

it('should commit', async() => {
const mockStdout = spyOnStdout();
process.env.GITHUB_SHA = 'sha';
nock('https://api.github.com')
.persist()
.get('/repos/hello/world/branches/test/protection')
.reply(404)
.post('/repos/hello/world/git/blobs')
.reply(201, () => {
return getApiFixture(path.resolve(__dirname, 'fixtures'), 'repos.git.blobs');
Expand Down Expand Up @@ -337,7 +306,86 @@ describe('ApiHelper with params', () => {
testEnv();
testLogger();

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

describe('updateRef', () => {
const response = createResponse<GitCreateCommitResponse>({
author: {
date: '',
email: '',
name: '',
},
committer: {
date: '',
email: '',
name: '',
},
message: '',
'node_id': '',
parents: [],
sha: '',
tree: {
sha: '',
url: '',
},
url: '',
verification: {
payload: null,
reason: '',
signature: null,
verified: true,
},
});

it('should output warning 1', 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(response, octokit, context);

stdoutCalledWith(mockStdout, [
'##[warning]Branch [test-branch] is protected.',
]);
});

it('should output warning 2', 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': '5 of 5 required status checks are expected.',
});

await helper.updateRef(response, octokit, context);

stdoutCalledWith(mockStdout, [
'##[warning]Branch [test-branch] is protected.',
]);
});

it('should throw error', async() => {
nock('https://api.github.com')
.patch('/repos/hello/world/git/refs/' + encodeURIComponent('test-ref'), body => {
expect(body).toHaveProperty('sha');
return body;
})
.reply(404, {
'message': 'Not Found',
});

await expect(helper.updateRef(response, octokit, context)).rejects.toThrow('Not Found');
});
});

describe('commit', () => {
it('should commit', async() => {
Expand All @@ -346,8 +394,6 @@ describe('ApiHelper with params', () => {
const mockStdout = spyOnStdout();
nock('https://api.github.com')
.persist()
.get('/repos/hello/world/branches/test/protection')
.reply(404)
.post('/repos/hello/world/git/blobs')
.reply(201, () => {
return getApiFixture(path.resolve(__dirname, 'fixtures'), 'repos.git.blobs');
Expand Down
105 changes: 0 additions & 105 deletions __tests__/fixtures/repos.branches.protection.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"devDependencies": {
"@technote-space/github-action-test-helper": "^0.0.5",
"@types/jest": "^24.0.18",
"@types/node": "^12.7.5",
"@types/node": "^12.7.6",
"@typescript-eslint/eslint-plugin": "^2.3.1",
"@typescript-eslint/parser": "^2.3.1",
"eslint": "^6.4.0",
Expand Down

0 comments on commit 17673e0

Please sign in to comment.