-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
78 lines (64 loc) · 2.2 KB
/
index.js
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
const core = require('@actions/core');
const github = require('@actions/github');
const main = async () => {
const repoToken = core.getInput('repo-token', { required: true });
const client = github.getOctokit(repoToken);
const { payload } = github.context;
const existingMemberMessage = core.getInput('existingMemberMessage')
try {
const { INVITE_TOKEN } = process.env;
if (!INVITE_TOKEN) {
return core.setFailed('ENV required and not supplied: INVITE_TOKEN');
}
const octokit = github.getOctokit(INVITE_TOKEN);
const inviteeId = payload.issue.user.id;
const currentLabel = payload.label.name;
const org = core.getInput('organization', { required: true });
const label = core.getInput('label', { required: true });
const comment = core.getInput('comment');
if (currentLabel === label) {
try {
await octokit.orgs.checkMembership({
org,
username: payload.issue.user.login,
});
} catch (error) {
await octokit.orgs.createInvitation({
org,
invitee_id: inviteeId,
});
core.info('Invitation sent successfully 🎉🎉');
await client.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
body: comment,
});
await client.issues.update({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
state: 'closed',
});
}
}
} catch (error) {
if (error.message.toString().includes('already a part')) {
await client.issues.createComment({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
body: existingMemberMessage,
});
await client.issues.update({
owner: payload.repository.owner.login,
repo: payload.repository.name,
issue_number: payload.issue.number,
state: 'closed',
});
}
return core.setFailed(error.message);
}
return core.setOutput('Invitation sent successfully 🎉🎉');
};
main();