-
Notifications
You must be signed in to change notification settings - Fork 0
/
aeryn.ts
63 lines (56 loc) · 2.46 KB
/
aeryn.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
// For the inspiration for this file, see: https://github.com/danger/peril-settings/blob/fc0015452438c8a1624c0951a600f723f2e60fea/org/aeryn.ts#L18-L39
import { schedule, danger, markdown } from "danger";
// Hey there!
//
// When a PR is closed, this file gets run. The purpose of this file is to
// replicate Aeryn (https://github.com/Moya/Aeryn), which invites new
// contributors to join the organization after their first PR gets merged.
//
// Ignore the next four const lines.
// The inspiration for this is https://github.com/artsy/artsy-danger/blob/f019ee1a3abffabad65014afabe07cb9a12274e7/org/all-prs.ts
const isJest = typeof jest !== "undefined";
// Returns the promise itself, for testing.
const _test = (reason: string, promise: Promise<any>) => promise;
// Schedules the promise for execution via Danger.
const _run = (reason: string, promise: Promise<any>) => schedule(promise);
const wrap: any = isJest ? _test : _run;
export const aeryn = wrap(
"When a PR is merged, check if the author is in the org",
async () => {
const pr = danger.github.pr;
const username = pr.user.login;
const api = danger.github.api;
if (!pr.merged) {
// Only proceed if the PR was merged (as opposed to closed without merging).
return;
}
if (pr.user.type !== "User") {
// Ignore PRs from bots.
return
}
const org = "Moya";
const inviteMarkdown = `
@${username} Thanks a lot for contributing to Moya! We've invited you to join
the Moya GitHub organization – no pressure to accept! If you'd like more
information on what that means, check out our [contributor guidelines][c] and
feel free to reach out to @Moya/core-team with any questions.
[c]: https://github.com/Moya/Moya/blob/master/Contributing.md
`;
try {
// This throws if the user isn't a member of the org yet. If it doesn't
// throw, then it means the user was already invited or has already
// accepted the invitation (we ignore the return value here).
await api.orgs.checkMembership({ org, username });
} catch (error) {
// Search to check if user has had a PR merged before
const query = `org:moya+author:${username}+type:pr+is:merged`;
await api.search.issues({ q: query }).then(async res => {
const mergedPrCount = res.data.total_count;
if (mergedPrCount === 1) {
markdown(inviteMarkdown);
await api.orgs.addOrUpdateMembership({ org, username, role: "member" });
}
});
}
}
);