1 | "use strict";
|
2 |
|
3 | const log = require("npmlog");
|
4 | const childProcess = require("@lerna/child-process");
|
5 | const { Octokit } = require("@octokit/rest");
|
6 | const parseGitUrl = require("git-url-parse");
|
7 |
|
8 | exports.createGitHubClient = createGitHubClient;
|
9 | exports.parseGitRepo = parseGitRepo;
|
10 |
|
11 | function createGitHubClient() {
|
12 | log.silly("createGitHubClient");
|
13 |
|
14 | const { GH_TOKEN, GHE_API_URL, GHE_VERSION } = process.env;
|
15 |
|
16 | if (!GH_TOKEN) {
|
17 | throw new Error("A GH_TOKEN environment variable is required.");
|
18 | }
|
19 |
|
20 | if (GHE_VERSION) {
|
21 |
|
22 | Octokit.plugin(require(`@octokit/plugin-enterprise-rest/ghe-${GHE_VERSION}`));
|
23 | }
|
24 |
|
25 | const options = {
|
26 | auth: `token ${GH_TOKEN}`,
|
27 | };
|
28 |
|
29 | if (GHE_API_URL) {
|
30 | options.baseUrl = GHE_API_URL;
|
31 | }
|
32 |
|
33 | return new Octokit(options);
|
34 | }
|
35 |
|
36 | function parseGitRepo(remote = "origin", opts) {
|
37 | log.silly("parseGitRepo");
|
38 |
|
39 | const args = ["config", "--get", `remote.${remote}.url`];
|
40 |
|
41 | log.verbose("git", args);
|
42 |
|
43 | const url = childProcess.execSync("git", args, opts);
|
44 |
|
45 | if (!url) {
|
46 | throw new Error(`Git remote URL could not be found using "${remote}".`);
|
47 | }
|
48 |
|
49 | return parseGitUrl(url);
|
50 | }
|