UNPKG

1.15 kBJavaScriptView Raw
1"use strict";
2
3const log = require("npmlog");
4const childProcess = require("@lerna/child-process");
5const { Octokit } = require("@octokit/rest");
6const parseGitUrl = require("git-url-parse");
7
8exports.createGitHubClient = createGitHubClient;
9exports.parseGitRepo = parseGitRepo;
10
11function 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 // eslint-disable-next-line
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
36function 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}