Skip to content

Commit 8034586

Browse files
Merge pull request #235 from technote-space/release/v1.1.3
release/v1.1.3
2 parents 6081615 + b573144 commit 8034586

File tree

6 files changed

+178
-88
lines changed

6 files changed

+178
-88
lines changed

__tests__/api-helper2.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,71 @@ describe('ApiHelper with params', () => {
211211
expect(fn).not.toBeCalled();
212212
});
213213
});
214+
215+
216+
describe('getTags', () => {
217+
it('should get tags', async() => {
218+
nock('https://api.github.com')
219+
.persist()
220+
.get('/repos/hello/world/git/matching-refs/tags/')
221+
.reply(200, () => getApiFixture(rootDir, 'repos.git.matching-refs'));
222+
223+
expect(await helper.getTags()).toEqual([
224+
'v1.0.0',
225+
'v2.0.0',
226+
'v1.2.3',
227+
]);
228+
});
229+
230+
it('should get tags', async() => {
231+
nock('https://api.github.com')
232+
.persist()
233+
.get('/repos/hello/world/git/matching-refs/tags/')
234+
.reply(200, () => []);
235+
236+
expect(await helper.getTags()).toEqual([]);
237+
});
238+
});
239+
240+
describe('getLastTag', () => {
241+
it('should get tags', async() => {
242+
nock('https://api.github.com')
243+
.persist()
244+
.get('/repos/hello/world/git/matching-refs/tags/')
245+
.reply(200, () => getApiFixture(rootDir, 'repos.git.matching-refs'));
246+
247+
expect(await helper.getLastTag()).toBe('v2.0.0');
248+
});
249+
250+
it('should get tags', async() => {
251+
nock('https://api.github.com')
252+
.persist()
253+
.get('/repos/hello/world/git/matching-refs/tags/')
254+
.reply(200, () => []);
255+
256+
expect(await helper.getLastTag()).toBe('v0.0.0');
257+
});
258+
});
259+
260+
describe('getNewPatchVersion', () => {
261+
it('should get tags', async() => {
262+
nock('https://api.github.com')
263+
.persist()
264+
.get('/repos/hello/world/git/matching-refs/tags/')
265+
.reply(200, () => getApiFixture(rootDir, 'repos.git.matching-refs'));
266+
267+
expect(await helper.getNewPatchVersion()).toBe('v2.0.1');
268+
});
269+
270+
it('should get tags', async() => {
271+
nock('https://api.github.com')
272+
.persist()
273+
.get('/repos/hello/world/git/matching-refs/tags/')
274+
.reply(200, () => []);
275+
276+
expect(await helper.getNewPatchVersion()).toBe('v0.0.1');
277+
});
278+
});
214279
});
215280

216281
describe('ApiHelper without logger', () => {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"ref": "refs/tags/v1.0.0",
4+
"node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlLWE=",
5+
"url": "https://api.github.com/repos/octocat/Hello-World/git/refs/tags/v1.0.0",
6+
"object": {
7+
"type": "commit",
8+
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
9+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
10+
}
11+
},
12+
{
13+
"ref": "refs/tags/v2.0.0",
14+
"node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlLWI=",
15+
"url": "https://api.github.com/repos/octocat/Hello-World/git/refs/tags/v2.0.0",
16+
"object": {
17+
"type": "commit",
18+
"sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac",
19+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac"
20+
}
21+
},
22+
{
23+
"ref": "refs/tags/v1.2.3",
24+
"node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlLWI=",
25+
"url": "https://api.github.com/repos/octocat/Hello-World/git/refs/tags/v1.2.3",
26+
"object": {
27+
"type": "commit",
28+
"sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac",
29+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac"
30+
}
31+
}
32+
]

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/github-action-helper",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"description": "Helper to filter GitHub Action.",
55
"author": {
66
"name": "Technote",
@@ -32,9 +32,9 @@
3232
"sprintf-js": "^1.1.2"
3333
},
3434
"devDependencies": {
35-
"@technote-space/github-action-test-helper": "^0.2.1",
35+
"@technote-space/github-action-test-helper": "^0.2.2",
3636
"@types/jest": "^25.1.2",
37-
"@types/node": "^13.7.0",
37+
"@types/node": "^13.7.1",
3838
"@typescript-eslint/eslint-plugin": "^2.19.2",
3939
"@typescript-eslint/parser": "^2.19.2",
4040
"eslint": "^6.8.0",

src/api-helper.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Context } from '@actions/github/lib/context';
44
import { Octokit } from '@octokit/rest';
55
import { exportVariable } from '@actions/core';
66
import { Logger } from './index';
7-
import { getRefForUpdate, isPrRef, getBranch } from './utils';
7+
import { getRefForUpdate, isPrRef, getBranch, trimRef, versionCompare, generateNewPatchVersion } from './utils';
88
import { getSender } from './context-helper';
99

1010
type PullsUpdateParams = {
@@ -100,8 +100,7 @@ export default class ApiHelper {
100100
*/
101101
private createBlob = async(rootDir: string, filepath: string): Promise<{ path: string; sha: string }> => {
102102
const blob = await this.octokit.git.createBlob({
103-
owner: this.context.repo.owner,
104-
repo: this.context.repo.repo,
103+
...this.context.repo,
105104
content: Buffer.from(fs.readFileSync(path.resolve(rootDir, filepath), 'utf8')).toString('base64'),
106105
encoding: 'base64',
107106
});
@@ -121,8 +120,7 @@ export default class ApiHelper {
121120
* @return {Promise<Octokit.Response<Octokit.GitGetCommitResponse>>} commit
122121
*/
123122
private getCommit = async(): Promise<Octokit.Response<Octokit.GitGetCommitResponse>> => this.octokit.git.getCommit({
124-
owner: this.context.repo.owner,
125-
repo: this.context.repo.repo,
123+
...this.context.repo,
126124
'commit_sha': this.getCommitSha(),
127125
});
128126

@@ -133,8 +131,7 @@ export default class ApiHelper {
133131
const key = parseInt(this.context.payload.number, 10);
134132
if (!(key in this.prCache)) {
135133
this.prCache[key] = await this.octokit.pulls.get({
136-
owner: this.context.repo.owner,
137-
repo: this.context.repo.repo,
134+
...this.context.repo,
138135
'pull_number': this.context.payload.number,
139136
});
140137
}
@@ -154,8 +151,7 @@ export default class ApiHelper {
154151
* @return {Promise<Octokit.Response<Octokit.GitCreateTreeResponse>>} tree
155152
*/
156153
public createTree = async(blobs: { path: string; sha: string }[]): Promise<Octokit.Response<Octokit.GitCreateTreeResponse>> => this.octokit.git.createTree({
157-
owner: this.context.repo.owner,
158-
repo: this.context.repo.repo,
154+
...this.context.repo,
159155
'base_tree': (await this.getCommit()).data.tree.sha,
160156
tree: Object.values(blobs).map(blob => ({
161157
path: blob.path,
@@ -171,8 +167,7 @@ export default class ApiHelper {
171167
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>>} commit
172168
*/
173169
public createCommit = async(commitMessage: string, tree: Octokit.Response<Octokit.GitCreateTreeResponse>): Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> => this.octokit.git.createCommit({
174-
owner: this.context.repo.owner,
175-
repo: this.context.repo.repo,
170+
...this.context.repo,
176171
tree: tree.data.sha,
177172
parents: [this.getCommitSha()],
178173
message: commitMessage,
@@ -185,8 +180,7 @@ export default class ApiHelper {
185180
private getRef = async(refName: string): Promise<Octokit.AnyResponse | null> => {
186181
try {
187182
return await this.octokit.git.getRef({
188-
owner: this.context.repo.owner,
189-
repo: this.context.repo.repo,
183+
...this.context.repo,
190184
ref: refName,
191185
});
192186
} catch (error) {
@@ -203,8 +197,7 @@ export default class ApiHelper {
203197
public updateRef = async(commit: Octokit.Response<Octokit.GitCreateCommitResponse>, refName: string, force: boolean): Promise<boolean> => {
204198
try {
205199
await this.octokit.git.updateRef({
206-
owner: this.context.repo.owner,
207-
repo: this.context.repo.repo,
200+
...this.context.repo,
208201
ref: refName,
209202
sha: commit.data.sha,
210203
force,
@@ -229,8 +222,7 @@ export default class ApiHelper {
229222
*/
230223
public createRef = async(commit: Octokit.Response<Octokit.GitCreateCommitResponse>, refName: string): Promise<void> => {
231224
await this.octokit.git.createRef({
232-
owner: this.context.repo.owner,
233-
repo: this.context.repo.repo,
225+
...this.context.repo,
234226
ref: refName,
235227
sha: commit.data.sha,
236228
});
@@ -242,8 +234,7 @@ export default class ApiHelper {
242234
*/
243235
public deleteRef = async(refName: string): Promise<void> => {
244236
await this.octokit.git.deleteRef({
245-
owner: this.context.repo.owner,
246-
repo: this.context.repo.repo,
237+
...this.context.repo,
247238
ref: refName,
248239
});
249240
};
@@ -254,8 +245,7 @@ export default class ApiHelper {
254245
*/
255246
public findPullRequest = async(branchName: string): Promise<Octokit.PullsListResponseItem | null> => {
256247
const response = await this.octokit.pulls.list({
257-
owner: this.context.repo.owner,
258-
repo: this.context.repo.repo,
248+
...this.context.repo,
259249
head: `${this.context.repo.owner}:${getBranch(branchName, false)}`,
260250
});
261251
if (response.data.length) {
@@ -274,8 +264,7 @@ export default class ApiHelper {
274264
sort: 'created',
275265
direction: 'asc',
276266
}, params, {
277-
owner: this.context.repo.owner,
278-
repo: this.context.repo.repo,
267+
...this.context.repo,
279268
})),
280269
);
281270

@@ -285,8 +274,7 @@ export default class ApiHelper {
285274
* @return {Promise<Octokit.Response<Octokit.PullsCreateResponse>>} pull
286275
*/
287276
public pullsCreate = async(branchName: string, detail: PullsCreateParams): Promise<Octokit.Response<Octokit.PullsCreateResponse>> => this.octokit.pulls.create({
288-
owner: this.context.repo.owner,
289-
repo: this.context.repo.repo,
277+
...this.context.repo,
290278
head: `${this.context.repo.owner}:${getBranch(branchName, false)}`,
291279
base: (await this.getRefForUpdate(false)).replace(/^heads\//, ''),
292280
...detail,
@@ -298,8 +286,7 @@ export default class ApiHelper {
298286
* @return {Promise<Octokit.Response<Octokit.PullsUpdateResponse>>} pull
299287
*/
300288
public pullsUpdate = async(number: number, detail: PullsUpdateParams): Promise<Octokit.Response<Octokit.PullsUpdateResponse>> => this.octokit.pulls.update({
301-
owner: this.context.repo.owner,
302-
repo: this.context.repo.repo,
289+
...this.context.repo,
303290
'pull_number': number,
304291
base: (await this.getRefForUpdate(false)).replace(/^heads\//, ''),
305292
state: 'open',
@@ -379,8 +366,7 @@ export default class ApiHelper {
379366
}
380367

381368
await this.octokit.issues.createComment({
382-
owner: this.context.repo.owner,
383-
repo: this.context.repo.repo,
369+
...this.context.repo,
384370
'issue_number': pullRequest.number,
385371
body,
386372
});
@@ -528,9 +514,30 @@ export default class ApiHelper {
528514
};
529515
};
530516

531-
// eslint-disable-next-line camelcase
532-
public getDefaultBranch = async(): Promise<string> => this.context.payload.repository?.default_branch ?? (await this.octokit.repos.get({
533-
owner: this.context.repo.owner,
534-
repo: this.context.repo.repo,
517+
/**
518+
* @return {Promise<string>} default branch
519+
*/
520+
public getDefaultBranch = async(): Promise<string> => this.context.payload.repository?.default_branch ?? (await this.octokit.repos.get({ // eslint-disable-line camelcase
521+
...this.context.repo,
535522
})).data.default_branch;
523+
524+
/**
525+
* @return {Promise<Array<string>>} tags
526+
*/
527+
public getTags = async(): Promise<Array<string>> => (await this.octokit.paginate(
528+
this.octokit.git.listMatchingRefs.endpoint.merge({
529+
...this.context.repo,
530+
ref: 'tags/',
531+
}),
532+
)).map((item: Octokit.GitListMatchingRefsResponseItem): string => trimRef(item.ref));
533+
534+
/**
535+
* @return {Promise<string>} tag
536+
*/
537+
public getLastTag = async(): Promise<string> => 'v' + ((await this.getTags()).filter(tag => /^v?\d+(\.\d+)*$/.test(tag)).sort(versionCompare).reverse()[0]?.replace(/^v/, '') ?? '0.0.0');
538+
539+
/**
540+
* @return {Promise<string>} tag
541+
*/
542+
public getNewPatchVersion = async(): Promise<string> => generateNewPatchVersion(await this.getLastTag());
536543
}

src/git-helper.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,8 @@ export default class GitHelper {
545545
if (!isCloned(workDir)) {
546546
throw new Error('Not a git repository');
547547
}
548-
const tags = (await this.getTags(workDir)).filter(tag => /^v?\d+(\.\d+)*$/.test(tag));
549-
const compareTag = (tag1: string, tag2: string): number => versionCompare(tag1, tag2);
550-
return 'v' + (tags.slice().sort(compareTag).reverse()[0]?.replace(/^v/, '') ?? '0.0.0');
548+
549+
return 'v' + ((await this.getTags(workDir)).filter(tag => /^v?\d+(\.\d+)*$/.test(tag)).sort(versionCompare).reverse()[0]?.replace(/^v/, '') ?? '0.0.0');
551550
};
552551

553552
/**

0 commit comments

Comments
 (0)