Skip to content

Commit db2eb2c

Browse files
feat: update library usage
BREAKING CHANGE: returns response data
1 parent 3f29c2b commit db2eb2c

File tree

1 file changed

+88
-69
lines changed

1 file changed

+88
-69
lines changed

src/api-helper.ts

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import fs from 'fs';
22
import path from 'path';
33
import {Context} from '@actions/github/lib/context';
4-
import {Octokit} from '@octokit/rest';
5-
import {Octokit as OctokitCore} from '@octokit/core';
4+
import {GitHub} from '@actions/github/lib/utils';
5+
import {PaginateInterface} from '@octokit/plugin-paginate-rest';
6+
import {RestEndpointMethods} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types';
7+
import {
8+
OctokitResponse,
9+
GitGetCommitResponseData,
10+
PullsGetResponseData,
11+
GitCreateTreeResponseData,
12+
GitCreateCommitResponseData,
13+
GitGetRefResponseData,
14+
PullsListResponseData,
15+
PullsCreateResponseData,
16+
PullsUpdateResponseData,
17+
GitListMatchingRefsResponseData,
18+
} from '@octokit/types';
619
import {exportVariable} from '@actions/core';
720
import {Logger} from './index';
821
import {getRefForUpdate, isPrRef, getBranch, trimRef, versionCompare, generateNewPatchVersion, generateNewMajorVersion, generateNewMinorVersion} from './utils';
@@ -42,14 +55,14 @@ type PullsListParams = {
4255
*/
4356
export default class ApiHelper {
4457

45-
private readonly branch?: string | undefined = undefined;
46-
private readonly sender?: string | undefined = undefined;
47-
private readonly suppressBPError?: boolean | undefined = undefined;
48-
private readonly refForUpdate?: string | undefined = undefined;
49-
private prCache: { [key: number]: Octokit.Response<Octokit.PullsGetResponse> } = {};
58+
private readonly branch?: string | undefined = undefined;
59+
private readonly sender?: string | undefined = undefined;
60+
private readonly suppressBPError?: boolean | undefined = undefined;
61+
private readonly refForUpdate?: string | undefined = undefined;
62+
private prCache: { [key: number]: PullsGetResponseData } = {};
5063

5164
/**
52-
* @param {Octokit} octokit octokit
65+
* @param {RestEndpointMethods} octokit octokit
5366
* @param {Context} context context
5467
* @param {Logger} logger logger
5568
* @param {object} options options
@@ -59,7 +72,7 @@ export default class ApiHelper {
5972
* @param {boolean|undefined} options.suppressBPError suppress branch protection error?
6073
*/
6174
constructor(
62-
private readonly octokit: OctokitCore,
75+
private readonly octokit: InstanceType<typeof GitHub>,
6376
private readonly context: Context,
6477
private readonly logger?: Logger,
6578
options?: { branch?: string; sender?: string; refForUpdate?: string; suppressBPError?: boolean },
@@ -70,6 +83,12 @@ export default class ApiHelper {
7083
this.suppressBPError = options?.suppressBPError;
7184
}
7285

86+
/**
87+
* @param {OctokitResponse} response response
88+
* @return {any} data
89+
*/
90+
private getResponseData = async <T>(response: Promise<OctokitResponse<T>>): Promise<T> => (await response).data;
91+
7392
/**
7493
* @param {function} caller caller
7594
*/
@@ -90,7 +109,7 @@ export default class ApiHelper {
90109
*/
91110
public getRefForUpdate = async(encode: boolean): Promise<string> => {
92111
const ref = this.refForUpdate ? this.refForUpdate : (
93-
isPrRef(this.context) ? ('heads/' + (await this.getPR()).data.head.ref) : getRefForUpdate(this.context)
112+
isPrRef(this.context) ? ('heads/' + (await this.getPR()).head.ref) : getRefForUpdate(this.context)
94113
);
95114
return encode ? encodeURIComponent(ref) : ref;
96115
};
@@ -101,7 +120,7 @@ export default class ApiHelper {
101120
* @return {Promise<{ path: string, sha: string }>} blob
102121
*/
103122
private createBlob = async(rootDir: string, filepath: string): Promise<{ path: string; sha: string }> => {
104-
const blob = await this.octokit.git.createBlob({
123+
const blob = await (this.octokit as RestEndpointMethods).git.createBlob({
105124
...this.context.repo,
106125
content: Buffer.from(fs.readFileSync(path.resolve(rootDir, filepath), 'utf8')).toString('base64'),
107126
encoding: 'base64',
@@ -119,23 +138,23 @@ export default class ApiHelper {
119138
private getCommitSha = (): string => isPrRef(this.context) && this.context.payload.pull_request ? this.context.payload.pull_request.head.sha : this.context.sha;
120139

121140
/**
122-
* @return {Promise<Octokit.Response<Octokit.GitGetCommitResponse>>} commit
141+
* @return {Promise<GitGetCommitResponseData>} commit
123142
*/
124-
private getCommit = async(): Promise<Octokit.Response<Octokit.GitGetCommitResponse>> => this.octokit.git.getCommit({
143+
private getCommit = async(): Promise<GitGetCommitResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.getCommit({
125144
...this.context.repo,
126145
'commit_sha': this.getCommitSha(),
127-
});
146+
}));
128147

129148
/**
130-
* @return {Promise<Octokit.Response<Octokit.PullsGetResponse>>} commit
149+
* @return {Promise<PullsGetResponseData>} commit
131150
*/
132-
private getPR = async(): Promise<Octokit.Response<Octokit.PullsGetResponse>> => {
151+
private getPR = async(): Promise<PullsGetResponseData> => {
133152
const key = parseInt(this.context.payload.number, 10);
134153
if (!(key in this.prCache)) {
135-
this.prCache[key] = await this.octokit.pulls.get({
154+
this.prCache[key] = await this.getResponseData((this.octokit as RestEndpointMethods).pulls.get({
136155
...this.context.repo,
137156
'pull_number': this.context.payload.number,
138-
});
157+
}));
139158
}
140159

141160
return this.prCache[key];
@@ -150,58 +169,58 @@ export default class ApiHelper {
150169

151170
/**
152171
* @param {{ path: string, sha: string }[]} blobs blobs
153-
* @return {Promise<Octokit.Response<Octokit.GitCreateTreeResponse>>} tree
172+
* @return {Promise<GitCreateTreeResponseData>} tree
154173
*/
155-
public createTree = async(blobs: { path: string; sha: string }[]): Promise<Octokit.Response<Octokit.GitCreateTreeResponse>> => this.octokit.git.createTree({
174+
public createTree = async(blobs: { path: string; sha: string }[]): Promise<GitCreateTreeResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.createTree({
156175
...this.context.repo,
157-
'base_tree': (await this.getCommit()).data.tree.sha,
176+
'base_tree': (await this.getCommit()).tree.sha,
158177
tree: Object.values(blobs).map(blob => ({
159178
path: blob.path,
160179
type: 'blob',
161180
mode: '100644',
162181
sha: blob.sha,
163182
})),
164-
});
183+
}));
165184

166185
/**
167186
* @param {string} commitMessage commit message
168-
* @param {Octokit.Response<Octokit.GitCreateTreeResponse>} tree tree
169-
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>>} commit
187+
* @param {GitCreateTreeResponseData} tree tree
188+
* @return {Promise<GitCreateCommitResponseData>} commit
170189
*/
171-
public createCommit = async(commitMessage: string, tree: Octokit.Response<Octokit.GitCreateTreeResponse>): Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> => this.octokit.git.createCommit({
190+
public createCommit = async(commitMessage: string, tree: GitCreateTreeResponseData): Promise<GitCreateCommitResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).git.createCommit({
172191
...this.context.repo,
173-
tree: tree.data.sha,
192+
tree: tree.sha,
174193
parents: [this.getCommitSha()],
175194
message: commitMessage,
176-
});
195+
}));
177196

178197
/**
179198
* @param {string} refName refName
180-
* @return {Promise<Octokit.AnyResponse|null>} refName
199+
* @return {Promise<GitGetRefResponseData|null>} refName
181200
*/
182-
private getRef = async(refName: string): Promise<Octokit.AnyResponse | null> => {
201+
private getRef = async(refName: string): Promise<GitGetRefResponseData | null> => {
183202
try {
184-
return await this.octokit.git.getRef({
203+
return await this.getResponseData((this.octokit as RestEndpointMethods).git.getRef({
185204
...this.context.repo,
186205
ref: refName,
187-
});
206+
}));
188207
} catch (error) {
189208
return null;
190209
}
191210
};
192211

193212
/**
194-
* @param {Octokit.Response<Octokit.GitCreateCommitResponse>} commit commit
213+
* @param {GitCreateCommitResponseData} commit commit
195214
* @param {string} refName refName
196215
* @param {boolean} force force
197-
* @return {Promise<void>} void
216+
* @return {Promise<boolean>} updated?
198217
*/
199-
public updateRef = async(commit: Octokit.Response<Octokit.GitCreateCommitResponse>, refName: string, force: boolean): Promise<boolean> => {
218+
public updateRef = async(commit: GitCreateCommitResponseData, refName: string, force: boolean): Promise<boolean> => {
200219
try {
201-
await this.octokit.git.updateRef({
220+
await (this.octokit as RestEndpointMethods).git.updateRef({
202221
...this.context.repo,
203222
ref: refName,
204-
sha: commit.data.sha,
223+
sha: commit.sha,
205224
force,
206225
});
207226

@@ -218,15 +237,15 @@ export default class ApiHelper {
218237
};
219238

220239
/**
221-
* @param {Octokit.Response<Octokit.GitCreateCommitResponse>} commit commit
240+
* @param {GitCreateCommitResponseData} commit commit
222241
* @param {string} refName refName
223242
* @return {Promise<void>} void
224243
*/
225-
public createRef = async(commit: Octokit.Response<Octokit.GitCreateCommitResponse>, refName: string): Promise<void> => {
226-
await this.octokit.git.createRef({
244+
public createRef = async(commit: GitCreateCommitResponseData, refName: string): Promise<void> => {
245+
await (this.octokit as RestEndpointMethods).git.createRef({
227246
...this.context.repo,
228247
ref: refName,
229-
sha: commit.data.sha,
248+
sha: commit.sha,
230249
});
231250
};
232251

@@ -235,18 +254,18 @@ export default class ApiHelper {
235254
* @return {Promise<void>} void
236255
*/
237256
public deleteRef = async(refName: string): Promise<void> => {
238-
await this.octokit.git.deleteRef({
257+
await (this.octokit as RestEndpointMethods).git.deleteRef({
239258
...this.context.repo,
240259
ref: refName,
241260
});
242261
};
243262

244263
/**
245264
* @param {string} branchName branch name
246-
* @return {Promise<Octokit.PullsListResponseItem>} pull request
265+
* @return {Promise<PullsListResponseData | null>} pull request
247266
*/
248-
public findPullRequest = async(branchName: string): Promise<Octokit.PullsListResponseItem | null> => {
249-
const response = await this.octokit.pulls.list({
267+
public findPullRequest = async(branchName: string): Promise<PullsListResponseData[number] | null> => {
268+
const response = await (this.octokit as RestEndpointMethods).pulls.list({
250269
...this.context.repo,
251270
head: `${this.context.repo.owner}:${getBranch(branchName, false)}`,
252271
});
@@ -259,10 +278,10 @@ export default class ApiHelper {
259278

260279
/**
261280
* @param {PullsListParams} params params
262-
* @return {AsyncIterable<Octokit.PullsListResponseItem>} pull request list
281+
* @return {AsyncIterable<PullsListResponseData>} pull request list
263282
*/
264-
public pullsList = (params: PullsListParams): Promise<Octokit.PullsListResponse> => this.octokit.paginate(
265-
this.octokit.pulls.list.endpoint.merge(Object.assign({
283+
public pullsList = (params: PullsListParams): Promise<PullsListResponseData> => (this.octokit.paginate as PaginateInterface)(
284+
(this.octokit as RestEndpointMethods).pulls.list.endpoint.merge(Object.assign({
266285
sort: 'created',
267286
direction: 'asc',
268287
}, params, {
@@ -273,27 +292,27 @@ export default class ApiHelper {
273292
/**
274293
* @param {string} branchName branch name
275294
* @param {PullsCreateParams} detail detail
276-
* @return {Promise<Octokit.Response<Octokit.PullsCreateResponse>>} pull
295+
* @return {Promise<PullsCreateResponseData>} pull
277296
*/
278-
public pullsCreate = async(branchName: string, detail: PullsCreateParams): Promise<Octokit.Response<Octokit.PullsCreateResponse>> => this.octokit.pulls.create({
297+
public pullsCreate = async(branchName: string, detail: PullsCreateParams): Promise<PullsCreateResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).pulls.create({
279298
...this.context.repo,
280299
head: `${this.context.repo.owner}:${getBranch(branchName, false)}`,
281300
base: (await this.getRefForUpdate(false)).replace(/^heads\//, ''),
282301
...detail,
283-
});
302+
}));
284303

285304
/**
286305
* @param {number} number pull number
287306
* @param {PullsUpdateParams} detail detail
288-
* @return {Promise<Octokit.Response<Octokit.PullsUpdateResponse>>} pull
307+
* @return {Promise<PullsUpdateResponseData>} pull
289308
*/
290-
public pullsUpdate = async(number: number, detail: PullsUpdateParams): Promise<Octokit.Response<Octokit.PullsUpdateResponse>> => this.octokit.pulls.update({
309+
public pullsUpdate = async(number: number, detail: PullsUpdateParams): Promise<PullsUpdateResponseData> => this.getResponseData((this.octokit as RestEndpointMethods).pulls.update({
291310
...this.context.repo,
292311
'pull_number': number,
293312
base: (await this.getRefForUpdate(false)).replace(/^heads\//, ''),
294313
state: 'open',
295314
...detail,
296-
});
315+
}));
297316

298317
/**
299318
* @param {string} branch branch
@@ -315,7 +334,7 @@ export default class ApiHelper {
315334
this.callLogger(async logger => logger.startProcess('Creating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
316335
const created = await this.pullsCreate(createBranchName, detail);
317336
this.callLogger(logger => logger.endProcess());
318-
return Object.assign({isPrCreated: true}, created.data);
337+
return Object.assign({isPrCreated: true}, created);
319338
};
320339

321340
/**
@@ -329,7 +348,7 @@ export default class ApiHelper {
329348
this.callLogger(async logger => logger.startProcess('Updating PullRequest... [%s] -> [%s]', getBranch(createBranchName, false), await this.getRefForUpdate(false)));
330349
const updated = await this.pullsUpdate(pullRequest.number, detail);
331350
this.callLogger(logger => logger.endProcess());
332-
return Object.assign({isPrCreated: false}, updated.data);
351+
return Object.assign({isPrCreated: false}, updated);
333352
}
334353

335354
return this.createPulls(createBranchName, detail);
@@ -367,7 +386,7 @@ export default class ApiHelper {
367386
return false;
368387
}
369388

370-
await this.octokit.issues.createComment({
389+
await (this.octokit as RestEndpointMethods).issues.createComment({
371390
...this.context.repo,
372391
'issue_number': pullRequest.number,
373392
body,
@@ -399,16 +418,16 @@ export default class ApiHelper {
399418
* @param {string} rootDir root dir
400419
* @param {string} commitMessage commit message
401420
* @param {string[]} files files
402-
* @return {Promise<Octokit.Response<Octokit.GitCreateCommitResponse>>} commit
421+
* @return {Promise<GitCreateCommitResponseData>} commit
403422
*/
404-
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<Octokit.Response<Octokit.GitCreateCommitResponse>> => {
423+
private prepareCommit = async(rootDir: string, commitMessage: string, files: string[]): Promise<GitCreateCommitResponseData> => {
405424
this.callLogger(logger => logger.startProcess('Creating blobs...'));
406425
const blobs = await this.filesToBlobs(rootDir, files);
407426

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

411-
this.callLogger(logger => logger.startProcess('Creating commit... [%s]', tree.data.sha));
430+
this.callLogger(logger => logger.startProcess('Creating commit... [%s]', tree.sha));
412431
return this.createCommit(commitMessage, tree);
413432
};
414433

@@ -426,10 +445,10 @@ export default class ApiHelper {
426445
const commit = await this.prepareCommit(rootDir, commitMessage, files);
427446
const ref = await this.getRefForUpdate(true);
428447

429-
this.callLogger(logger => logger.startProcess('Updating ref... [%s] [%s]', ref, commit.data.sha));
448+
this.callLogger(logger => logger.startProcess('Updating ref... [%s] [%s]', ref, commit.sha));
430449
if (await this.updateRef(commit, ref, false)) {
431-
process.env.GITHUB_SHA = commit.data.sha;
432-
exportVariable('GITHUB_SHA', commit.data.sha);
450+
process.env.GITHUB_SHA = commit.sha;
451+
exportVariable('GITHUB_SHA', commit.sha);
433452
}
434453

435454
this.callLogger(logger => logger.endProcess());
@@ -453,10 +472,10 @@ export default class ApiHelper {
453472
const commit = await this.prepareCommit(rootDir, commitMessage, files);
454473
const ref = await this.getRef(headName);
455474
if (null === ref) {
456-
this.callLogger(logger => logger.startProcess('Creating reference... [%s] [%s]', refName, commit.data.sha));
475+
this.callLogger(logger => logger.startProcess('Creating reference... [%s] [%s]', refName, commit.sha));
457476
await this.createRef(commit, refName);
458477
} else {
459-
this.callLogger(logger => logger.startProcess('Updating reference... [%s] [%s]', refName, commit.data.sha));
478+
this.callLogger(logger => logger.startProcess('Updating reference... [%s] [%s]', refName, commit.sha));
460479
await this.updateRef(commit, headName, true);
461480
}
462481

@@ -504,7 +523,7 @@ export default class ApiHelper {
504523
throw new Error('Sender is not valid.');
505524
}
506525

507-
const {data: user} = await this.octokit.users.getByUsername({
526+
const {data: user} = await (this.octokit as RestEndpointMethods).users.getByUsername({
508527
username: sender,
509528
});
510529

@@ -519,19 +538,19 @@ export default class ApiHelper {
519538
/**
520539
* @return {Promise<string>} default branch
521540
*/
522-
public getDefaultBranch = async(): Promise<string> => this.context.payload.repository?.default_branch ?? (await this.octokit.repos.get({ // eslint-disable-line camelcase
541+
public getDefaultBranch = async(): Promise<string> => this.context.payload.repository?.default_branch ?? (await (this.octokit as RestEndpointMethods).repos.get({ // eslint-disable-line camelcase
523542
...this.context.repo,
524543
})).data.default_branch;
525544

526545
/**
527546
* @return {Promise<Array<string>>} tags
528547
*/
529-
public getTags = async(): Promise<Array<string>> => (await this.octokit.paginate(
530-
this.octokit.git.listMatchingRefs.endpoint.merge({
548+
public getTags = async(): Promise<Array<string>> => (await (this.octokit.paginate as PaginateInterface)(
549+
(this.octokit as RestEndpointMethods).git.listMatchingRefs.endpoint.merge({
531550
...this.context.repo,
532551
ref: 'tags/',
533552
}),
534-
)).map((item: Octokit.GitListMatchingRefsResponseItem): string => trimRef(item.ref));
553+
)).map((item): string => trimRef((item as GitListMatchingRefsResponseData[number]).ref));
535554

536555
/**
537556
* @return {Promise<string>} tag

0 commit comments

Comments
 (0)