|
1 | 1 | import { Probot } from "probot"; |
2 | 2 | import { EmitterWebhookEvent } from "@octokit/webhooks"; |
3 | 3 | import { GitHubAppWebHookPayload } from "./types"; |
4 | | - |
5 | | -// import * as fs from 'fs'; |
6 | | -// import * as yaml from 'yaml'; |
7 | | - |
8 | | -// // read in filter config, from a YAML file called "filter.yml" in the root of the repo |
9 | | -// // if the file doesn't exist, or is invalid YAML, we handle that gracefully |
10 | | -// let filter_config: any; |
11 | | - |
12 | | -// try { |
13 | | -// yaml.parse(fs.readFileSync('filter.yml', 'utf8')); |
14 | | -// } catch (error) { |
15 | | -// console.log(error); |
16 | | -// } |
| 4 | +import { eventFilter } from "./filter"; |
17 | 5 |
|
18 | 6 | const setupApp = (app: Probot) => { |
19 | 7 | app.onAny(async (event: EmitterWebhookEvent) => { |
20 | 8 | const payload = event.payload as GitHubAppWebHookPayload; |
21 | 9 |
|
22 | 10 | const octokit = await app.auth(payload.installation.id); |
23 | 11 |
|
24 | | - // if (filter(context)) { |
25 | | - await octokit.repos.createDispatchEvent({ |
26 | | - owner: payload.repository.owner.login, |
27 | | - repo: payload.repository.name, |
28 | | - event_type: event.name, |
29 | | - client_payload: payload as unknown as {[key:string]: unknown}, |
30 | | - }); |
31 | | - // } |
| 12 | + if (eventFilter(event)) { |
| 13 | + await octokit.repos.createDispatchEvent({ |
| 14 | + owner: payload.repository.owner.login, |
| 15 | + repo: payload.repository.name, |
| 16 | + event_type: event.name, |
| 17 | + client_payload: payload as unknown as { [key: string]: unknown }, |
| 18 | + }); |
| 19 | + } |
32 | 20 | }); |
33 | 21 | }; |
34 | 22 |
|
35 | | -// const filter = (event: EmitterWebhookEvent): boolean => { |
36 | | -// if (filter_config === undefined) return true; |
37 | | - |
38 | | -// const event_name = event.name; |
39 | | - |
40 | | -// // check if the event type is allowed by the filter config |
41 | | -// // if there is an include list, and this type isn't on it, return false |
42 | | -// // if there is a matching entry in the excludes, and there are no more details under that entry, return false |
43 | | -// if (filter_config.include !== undefined && !(event_name in filter_config.include) |
44 | | -// || filter_config.exclude !== undefined && event_name in filter_config.exclude && Object.keys(filter_config.exclude).length === 0 |
45 | | -// ) return false; |
46 | | - |
47 | | -// // check the event payload against the filter config's include rule, if it exists |
48 | | -// const include_filter = filter_config.include[event_name]; |
49 | | -// const payload = event.payload; |
50 | | - |
51 | | -// if (include_filter !== undefined) { |
52 | | - |
53 | | -// const matches = Object.keys(include_filter).every(key => { |
54 | | -// if (typeof include_filter[key] === typeof (String)) return payload[key] === include_filter[key]; |
55 | | -// if (typeof include_filter[key] === typeof (Array)) return payload[key] in include_filter[key]; |
56 | | -// return false; |
57 | | -// }); |
58 | | - |
59 | | -// if (!matches) return false; |
60 | | -// } |
61 | | - |
62 | | -// // same for the exclude rule |
63 | | -// const exclude_filter = filter_config.exclude[event_name]; |
64 | | - |
65 | | -// if (exclude_filter !== undefined) { |
66 | | -// const matches = Object.keys(exclude_filter).every(key => { |
67 | | -// if (typeof exclude_filter[key] === typeof (String)) return payload[key] === exclude_filter[key]; |
68 | | -// if (typeof exclude_filter[key] === typeof (Array)) return payload[key] in exclude_filter[key]; |
69 | | -// return false; |
70 | | -// }); |
71 | | - |
72 | | -// if (matches) return false; |
73 | | -// } |
74 | | - |
75 | | -// return true; |
76 | | -// } |
77 | | - |
78 | 23 | export default setupApp; |
0 commit comments