UNPKG

2.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Context = void 0;
4const fs_1 = require("fs");
5const os_1 = require("os");
6class Context {
7 /**
8 * Hydrate the context from the environment
9 */
10 constructor() {
11 var _a, _b, _c;
12 this.payload = {};
13 if (process.env.GITHUB_EVENT_PATH) {
14 if (fs_1.existsSync(process.env.GITHUB_EVENT_PATH)) {
15 this.payload = JSON.parse(fs_1.readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' }));
16 }
17 else {
18 const path = process.env.GITHUB_EVENT_PATH;
19 process.stdout.write(`GITHUB_EVENT_PATH ${path} does not exist${os_1.EOL}`);
20 }
21 }
22 this.eventName = process.env.GITHUB_EVENT_NAME;
23 this.sha = process.env.GITHUB_SHA;
24 this.ref = process.env.GITHUB_REF;
25 this.workflow = process.env.GITHUB_WORKFLOW;
26 this.action = process.env.GITHUB_ACTION;
27 this.actor = process.env.GITHUB_ACTOR;
28 this.job = process.env.GITHUB_JOB;
29 this.runNumber = parseInt(process.env.GITHUB_RUN_NUMBER, 10);
30 this.runId = parseInt(process.env.GITHUB_RUN_ID, 10);
31 this.apiUrl = (_a = process.env.GITHUB_API_URL) !== null && _a !== void 0 ? _a : `https://api.github.com`;
32 this.serverUrl = (_b = process.env.GITHUB_SERVER_URL) !== null && _b !== void 0 ? _b : `https://github.com`;
33 this.graphqlUrl = (_c = process.env.GITHUB_GRAPHQL_URL) !== null && _c !== void 0 ? _c : `https://api.github.com/graphql`;
34 }
35 get issue() {
36 const payload = this.payload;
37 return Object.assign(Object.assign({}, this.repo), { number: (payload.issue || payload.pull_request || payload).number });
38 }
39 get repo() {
40 if (process.env.GITHUB_REPOSITORY) {
41 const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
42 return { owner, repo };
43 }
44 if (this.payload.repository) {
45 return {
46 owner: this.payload.repository.owner.login,
47 repo: this.payload.repository.name
48 };
49 }
50 throw new Error("context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'");
51 }
52}
53exports.Context = Context;
54//# sourceMappingURL=context.js.map
\No newline at end of file