-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
96 lines (82 loc) · 1.79 KB
/
Copy pathmain.js
File metadata and controls
96 lines (82 loc) · 1.79 KB
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
const BOTS = [
"dependabot-preview[bot]",
"dependabot-preview",
"dependabot",
"dependabot[bot]",
];
module.exports = async ({ github, context, core }) => {
const {
ORGANISATION: org,
TEAM_SLUG: team_slug,
PR_NUMBER: prNumber,
REPOSITORY_NAME: repositoryName,
COMMENT: comment,
USERNAME: username,
} = process.env;
if (!prNumber) {
console.log("no PR found.");
return;
}
if (BOTS.includes(username)) {
console.log("skipping because PR was made by a bot.");
return;
}
const query = `
query GetTeam($org: String!, $team_slug: String!, $username: String!) {
organization(login: $org) {
team(slug: $team_slug) {
members(first: 1, query: $username) {
totalCount
}
}
}
}
`;
const variables = {
org,
team_slug,
username,
};
const result = await github.graphql(query, variables);
const {
organization: {
team: {
members: { totalCount },
},
},
} = result;
if (totalCount > 0) {
console.log("user is a member of the team");
return;
}
const invitations = await github.paginate(
"GET /orgs/{org}/teams/{team_slug}/invitations",
{
org,
team_slug,
},
);
const invitationsForUser = invitations.filter(
(invitation) => invitation.login === username,
);
if (invitationsForUser.length > 0) {
console.log("user is already invited to the team");
return;
}
await github.request(
"PUT /orgs/{org}/teams/{team_slug}/memberships/{username}",
{
org,
team_slug,
username,
role: "member",
},
);
const commentResult = await github.issues.createComment({
owner: org,
repo: repositoryName,
issue_number: prNumber,
body: comment,
});
console.log(commentResult);
};