-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
100 lines (88 loc) · 2.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import { branch as getBranch, repository as getRepository, token as getToken } from '../index.js';
import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js';
import { Command } from 'commander';
import { Octokit } from '@octokit/rest';
import { Ora } from 'ora';
import { RequestError } from '@octokit/request-error';
interface IGitHubCommitArgs {
path?: string;
token?: string;
branch?: string;
spinner?: Ora;
}
/**
* Execute the `github-commit` command.
*
* @param {string} [args.path] Path to a git directory.
* @param {string} [args.token] GitHub personal access token.
* @param {string} [args.branch] GitHub branch.
* @param {string} [args.spinner] Terminal spinner.
*
* @returns {Promise<string>} The latest commit SHA provided by a CI
* environment variable or remote GitHub commits for the given git repository.
*/
export const execute = async (args: IGitHubCommitArgs = {}): Promise<string | undefined> => {
let retVal: string | undefined =
process.env.CIRCLE_SHA1 || process.env.TRAVIS_PULL_REQUEST_SHA || process.env.TRAVIS_COMMIT;
if (!retVal) {
try {
const auth = args.token || (await getToken(args.spinner));
const github = new Octokit({ auth });
const repository = (await getRepository(args.path, args.spinner))!;
const sha = args.branch || (await getBranch(args.path, args.spinner));
/* https://octokit.github.io/rest.js/v17#repos-list-commits */
const commits = await github.repos.listCommits({
owner: repository.owner,
repo: repository.repo,
sha
});
/* eslint-disable-next-line @typescript-eslint/no-unnecessary-condition */
if (commits?.data) {
retVal = commits.data[0].sha || undefined;
}
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
if ((error as RequestError).status !== 404) {
handleErrorMessage(error, 'github-commit', args.spinner);
throw error;
}
}
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('github-commit');
return command
.description('output latest GitHub commit SHA for the repo branch')
.arguments('[path]')
.option('-b, --branch <branch>', 'GitHub branch name')
.option('-t, --token <token>', 'access token')
.action(async path => {
try {
spinner.start();
const options = command.opts();
const commit = await execute({
path,
branch: options.branch,
token: options.token,
spinner
});
if (commit) {
handleSuccessMessage(commit, spinner);
} else {
throw new Error();
}
} catch {
spinner.fail('Commit not found');
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};