UNPKG

6.7 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.Context = void 0;
7const path_1 = __importDefault(require("path"));
8const deepmerge_1 = __importDefault(require("deepmerge"));
9const alias_log_1 = require("./helpers/alias-log");
10/**
11 * The context of the event that was triggered, including the payload and
12 * helpers for extracting information can be passed to GitHub API calls.
13 *
14 * ```js
15 * module.exports = app => {
16 * app.on('push', context => {
17 * context.log.info('Code was pushed to the repo, what should we do with it?');
18 * });
19 * };
20 * ```
21 *
22 * @property {octokit} octokit - An Octokit instance
23 * @property {payload} payload - The webhook event payload
24 * @property {logger} log - A logger
25 */
26class Context {
27 constructor(event, octokit, log) {
28 this.name = event.name;
29 this.id = event.id;
30 this.payload = event.payload;
31 this.octokit = octokit;
32 this.log = alias_log_1.aliasLog(log);
33 }
34 /**
35 * Return the `owner` and `repo` params for making API requests against a
36 * repository.
37 *
38 * ```js
39 * const params = context.repo({path: '.github/config.yml'})
40 * // Returns: {owner: 'username', repo: 'reponame', path: '.github/config.yml'}
41 * ```
42 *
43 * @param object - Params to be merged with the repo params.
44 *
45 */
46 repo(object) {
47 const repo = this.payload.repository;
48 if (!repo) {
49 throw new Error("context.repo() is not supported for this webhook event.");
50 }
51 return Object.assign({
52 owner: repo.owner.login || repo.owner.name,
53 repo: repo.name,
54 }, object);
55 }
56 /**
57 * Return the `owner`, `repo`, and `issue_number` params for making API requests
58 * against an issue. The object passed in will be merged with the repo params.
59 *
60 *
61 * ```js
62 * const params = context.issue({body: 'Hello World!'})
63 * // Returns: {owner: 'username', repo: 'reponame', issue_number: 123, body: 'Hello World!'}
64 * ```
65 *
66 * @param object - Params to be merged with the issue params.
67 */
68 issue(object) {
69 const payload = this.payload;
70 return Object.assign({
71 issue_number: (payload.issue || payload.pull_request || payload).number,
72 }, this.repo(object));
73 }
74 /**
75 * Return the `owner`, `repo`, and `issue_number` params for making API requests
76 * against an issue. The object passed in will be merged with the repo params.
77 *
78 *
79 * ```js
80 * const params = context.pullRequest({body: 'Hello World!'})
81 * // Returns: {owner: 'username', repo: 'reponame', pull_number: 123, body: 'Hello World!'}
82 * ```
83 *
84 * @param object - Params to be merged with the pull request params.
85 */
86 pullRequest(object) {
87 const payload = this.payload;
88 return Object.assign({
89 pull_number: (payload.issue || payload.pull_request || payload).number,
90 }, this.repo(object));
91 }
92 /**
93 * Returns a boolean if the actor on the event was a bot.
94 * @type {boolean}
95 */
96 get isBot() {
97 return this.payload.sender.type === "Bot";
98 }
99 /**
100 * Reads the app configuration from the given YAML file in the `.github`
101 * directory of the repository.
102 *
103 * For example, given a file named `.github/config.yml`:
104 *
105 * ```yml
106 * close: true
107 * comment: Check the specs on the rotary girder.
108 * ```
109 *
110 * Your app can read that file from the target repository:
111 *
112 * ```js
113 * // Load config from .github/config.yml in the repository
114 * const config = await context.config('config.yml')
115 *
116 * if (config.close) {
117 * context.octokit.issues.comment(context.issue({body: config.comment}))
118 * context.octokit.issues.edit(context.issue({state: 'closed'}))
119 * }
120 * ```
121 *
122 * You can also use a `defaultConfig` object:
123 *
124 * ```js
125 * // Load config from .github/config.yml in the repository and combine with default config
126 * const config = await context.config('config.yml', {comment: 'Make sure to check all the specs.'})
127 *
128 * if (config.close) {
129 * context.octokit.issues.comment(context.issue({body: config.comment}));
130 * context.octokit.issues.edit(context.issue({state: 'closed'}))
131 * }
132 * ```
133 *
134 * Config files can also specify a base that they extend. `deepMergeOptions` can be used
135 * to configure how the target config, extended base, and default configs are merged.
136 *
137 * For security reasons, configuration is only loaded from the repository's default branch,
138 * changes made in pull requests from different branches or forks are ignored.
139 *
140 * If you need more lower-level control over reading and merging configuration files,
141 * you can `context.octokit.config.get(options)`, see https://github.com/probot/octokit-plugin-config.
142 *
143 * @param fileName - Name of the YAML file in the `.github` directory
144 * @param defaultConfig - An object of default config options
145 * @param deepMergeOptions - Controls merging configs (from the [deepmerge](https://github.com/TehShrike/deepmerge) module)
146 * @return Configuration object read from the file
147 */
148 async config(fileName, defaultConfig, deepMergeOptions) {
149 const params = this.repo({
150 path: path_1.default.posix.join(".github", fileName),
151 defaults(configs) {
152 const result = deepmerge_1.default.all([defaultConfig || {}, ...configs], deepMergeOptions);
153 return result;
154 },
155 });
156 // @ts-ignore
157 const { config, files } = await this.octokit.config.get(params);
158 // if no default config is set, and no config files are found, return null
159 if (!defaultConfig && !files.find((file) => file.config !== null)) {
160 return null;
161 }
162 return config;
163 }
164 /**
165 * @deprecated `context.event` is deprecated, use `context.name` instead.
166 */
167 get event() {
168 this.log.warn(`[probot] "context.event" is deprecated. Use "context.name" instead.`);
169 return this.name;
170 }
171 /**
172 * @deprecated `context.github` is deprecated. Use `context.octokit` instead.
173 */
174 get github() {
175 this.log.warn(`[probot] "context.github" is deprecated. Use "context.octokit" instead.`);
176 return this.octokit;
177 }
178}
179exports.Context = Context;
180//# sourceMappingURL=context.js.map
\No newline at end of file