/
context.ts
257 lines (235 loc) 路 7.12 KB
/
context.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import path from "path";
import { EventPayloads, WebhookEvent } from "@octokit/webhooks";
import merge from "deepmerge";
import type { Logger } from "pino";
import { ProbotOctokit } from "./octokit/probot-octokit";
import { aliasLog } from "./helpers/alias-log";
import { DeprecatedLogger } from "./types";
import { WebhookEvents } from "@octokit/webhooks";
export type MergeOptions = merge.Options;
export interface WebhookPayloadWithRepository {
[key: string]: any;
repository?: EventPayloads.PayloadRepository;
issue?: {
[key: string]: any;
number: number;
html_url?: string;
body?: string;
};
pull_request?: {
[key: string]: any;
number: number;
html_url?: string;
body?: string;
};
sender?: {
[key: string]: any;
type: string;
};
action?: string;
installation?: {
id: number;
[key: string]: any;
};
}
/**
* The context of the event that was triggered, including the payload and
* helpers for extracting information can be passed to GitHub API calls.
*
* ```js
* module.exports = app => {
* app.on('push', context => {
* context.log.info('Code was pushed to the repo, what should we do with it?');
* });
* };
* ```
*
* @property {octokit} octokit - An Octokit instance
* @property {payload} payload - The webhook event payload
* @property {logger} log - A logger
*/
export class Context<E extends WebhookPayloadWithRepository = any>
implements WebhookEvent<E> {
public name: WebhookEvents;
public id: string;
public payload: E;
public octokit: InstanceType<typeof ProbotOctokit>;
public log: DeprecatedLogger;
constructor(
event: WebhookEvent<E>,
octokit: InstanceType<typeof ProbotOctokit>,
log: Logger
) {
this.name = event.name;
this.id = event.id;
this.payload = event.payload;
this.octokit = octokit;
this.log = aliasLog(log);
}
/**
* Return the `owner` and `repo` params for making API requests against a
* repository.
*
* ```js
* const params = context.repo({path: '.github/config.yml'})
* // Returns: {owner: 'username', repo: 'reponame', path: '.github/config.yml'}
* ```
*
* @param object - Params to be merged with the repo params.
*
*/
public repo<T>(object?: T) {
const repo = this.payload.repository;
if (!repo) {
throw new Error(
"context.repo() is not supported for this webhook event."
);
}
return Object.assign(
{
owner: repo.owner.login || repo.owner.name!,
repo: repo.name,
},
object
);
}
/**
* Return the `owner`, `repo`, and `issue_number` params for making API requests
* against an issue. The object passed in will be merged with the repo params.
*
*
* ```js
* const params = context.issue({body: 'Hello World!'})
* // Returns: {owner: 'username', repo: 'reponame', issue_number: 123, body: 'Hello World!'}
* ```
*
* @param object - Params to be merged with the issue params.
*/
public issue<T>(object?: T) {
const payload = this.payload;
return Object.assign(
{
issue_number: (payload.issue || payload.pull_request || payload).number,
},
this.repo(object)
);
}
/**
* Return the `owner`, `repo`, and `issue_number` params for making API requests
* against an issue. The object passed in will be merged with the repo params.
*
*
* ```js
* const params = context.pullRequest({body: 'Hello World!'})
* // Returns: {owner: 'username', repo: 'reponame', pull_number: 123, body: 'Hello World!'}
* ```
*
* @param object - Params to be merged with the pull request params.
*/
public pullRequest<T>(object?: T) {
const payload = this.payload;
return Object.assign(
{
pull_number: (payload.issue || payload.pull_request || payload).number,
},
this.repo(object)
);
}
/**
* Returns a boolean if the actor on the event was a bot.
* @type {boolean}
*/
get isBot() {
return this.payload.sender!.type === "Bot";
}
/**
* Reads the app configuration from the given YAML file in the `.github`
* directory of the repository.
*
* For example, given a file named `.github/config.yml`:
*
* ```yml
* close: true
* comment: Check the specs on the rotary girder.
* ```
*
* Your app can read that file from the target repository:
*
* ```js
* // Load config from .github/config.yml in the repository
* const config = await context.config('config.yml')
*
* if (config.close) {
* context.octokit.issues.comment(context.issue({body: config.comment}))
* context.octokit.issues.edit(context.issue({state: 'closed'}))
* }
* ```
*
* You can also use a `defaultConfig` object:
*
* ```js
* // Load config from .github/config.yml in the repository and combine with default config
* const config = await context.config('config.yml', {comment: 'Make sure to check all the specs.'})
*
* if (config.close) {
* context.octokit.issues.comment(context.issue({body: config.comment}));
* context.octokit.issues.edit(context.issue({state: 'closed'}))
* }
* ```
*
* Config files can also specify a base that they extend. `deepMergeOptions` can be used
* to configure how the target config, extended base, and default configs are merged.
*
* For security reasons, configuration is only loaded from the repository's default branch,
* changes made in pull requests from different branches or forks are ignored.
*
* If you need more lower-level control over reading and merging configuration files,
* you can `context.octokit.config.get(options)`, see https://github.com/probot/octokit-plugin-config.
*
* @param fileName - Name of the YAML file in the `.github` directory
* @param defaultConfig - An object of default config options
* @param deepMergeOptions - Controls merging configs (from the [deepmerge](https://github.com/TehShrike/deepmerge) module)
* @return Configuration object read from the file
*/
public async config<T>(
fileName: string,
defaultConfig?: T,
deepMergeOptions?: MergeOptions
): Promise<T | null> {
const params = this.repo({
path: path.posix.join(".github", fileName),
defaults(configs: object[]) {
const result = merge.all(
[defaultConfig || {}, ...configs],
deepMergeOptions
);
return result;
},
});
// @ts-ignore
const { config, files } = await this.octokit.config.get(params);
// if no default config is set, and no config files are found, return null
if (!defaultConfig && !files.find((file: any) => file.config !== null)) {
return null;
}
return config as T;
}
/**
* @deprecated `context.event` is deprecated, use `context.name` instead.
*/
public get event() {
this.log.warn(
`[probot] "context.event" is deprecated. Use "context.name" instead.`
);
return this.name;
}
/**
* @deprecated `context.github` is deprecated. Use `context.octokit` instead.
*/
public get github() {
this.log.warn(
`[probot] "context.github" is deprecated. Use "context.octokit" instead.`
);
return this.octokit;
}
}