-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
112 lines (104 loc) · 4.27 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
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
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
const clientId = 'lrmknreis8iakk53pwt87469523kr6';
const NO_TWITCH_URL_FOUND = "We couldn't find a twitch URL in your application"
const NO_TWITCH_USER_FOUND = "We couldn't retrieve your twitch information, make sure your URL is correct."
const NOT_AFFILIATE_OR_PARTNER = "It looks like you're not Twitch affiliate or partner yet. Please reapply when you've met all the requirements. We'd be happy to evaluate your application then."
async function addLabel(client, labelName) {
console.log(`Start addLabel: ${labelName}`);
try {
await client.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.issue.number,
labels: [ labelName ]
});
} catch (error) {
core.setFailed(error.message);
}
console.log(`End addLabel: ${labelName}`);
}
async function addComment(client, comment) {
console.log(`Start addComment ${comment}`);
try {
await client.issues.createComment({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.issue.number,
body: comment
});
} catch (error) {
core.setFailed(error.message);
}
console.log(`End addComment: ${comment}`);
}
async function getAppAccessToken() {
let result = await axios.post(`https://id.twitch.tv/oauth2/token?client_id=${clientId}&client_secret=${core.getInput('twitch-api-secret', {required: true})}&grant_type=client_credentials`);
return result.data.access_token
}
async function closeIssue(client) {
console.log(`Start closeIssue`);
try {
await client.issues.update({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: github.context.payload.issue.number,
state: "closed"
});
} catch (error) {
core.setFailed(error.message);
}
console.log(`End closeIssue`);
}
async function run() {
console.log('Start run');
const token = core.getInput('repo-token', {required: true});
const client = new github.GitHub(token);
try {
if (github.context.payload.issue) {
console.log('Found issue payload');
if (github.context.payload.issue.labels.filter(label => label.name === "application").length > 0) {
console.log('Has application label');
let urls = github.context.payload.issue.body.match(/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/gm);
if(urls == null || urls.length == 0) {
await addComment(client, NO_TWITCH_URL_FOUND);
} else {
console.log(`Has ${urls.length} urls in the body`);
let found = false;
for (let index in urls) {
console.log(urls[index]);
let url = new URL(urls[index]);
if (url.host.includes('twitch.tv') && !url.host.includes('clips.twitch.tv') && (url.pathname.match(/\//g) || []).length == 1) {
console.log('Twitch Url Found');
found = true;
let login = url.pathname.replace('/', '').toLowerCase();
//get user from twitch
var appaccessToken = await getAppAccessToken();
let result = await axios.get(`https://api.twitch.tv/helix/users?login=${login}`, { headers: { 'Client-Id': clientId, 'Authorization': `Bearer ${appaccessToken}` } });
if (result.data.data.length === 1) {
let user = result.data.data[0];
if (user.broadcaster_type === 'affiliate' || user.broadcaster_type === 'partner') {
await addLabel(client, user.broadcaster_type);
} else {
console.log("User is not affiliate or partner yet. Commenting and closing issue.");
await addComment(client, NOT_AFFILIATE_OR_PARTNER);
await closeIssue(client);
}
} else {
await addComment(client, NO_TWITCH_USER_FOUND);
}
}
}
if (!found) {
await addComment(client, NO_TWITCH_URL_FOUND);
}
}
}
}
} catch (error) {
core.setFailed(error.message);
}
console.log('End run');
}
run();