-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
103 lines (91 loc) · 2.65 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
core = require('@actions/core');
const github = require('@actions/github');
var request = require('request');
try {
const title = core.getInput('title');
const body = core.getInput('body');
const submitter = core.getInput('submitter');
const submitter_id = `github.com:${submitter}`
const submitter_url = `https://github.com/${submitter}`
const tracker_owner = core.getInput('tracker-owner');
const tracker_name = core.getInput('tracker-name');
const oauth_token = core.getInput('oauth-token');
var repository = core.getInput('label');
var repo_name = repository.split("/");
// If the repo name was provided from the ENV variable,
// it will be in the format OWNER/REPO
if (repo_name.length > 1) {
repo_name = repo_name[1];
} else {
repo_name = repo_name[0];
}
// If the repository was not provided, error
if (repo_name == "") {
throw "Repository not passed in as label: \"${repository}\""
}
var uri = `https://todo.sr.ht/api/user/${tracker_owner}/trackers/${tracker_name}/tickets`;
var description = `Issue mirrored from [github](https://github.com/${repository}).\n\nOpened by [${submitter}](${submitter_url}).\n\n${body}`;
create_issue(uri, oauth_token, title, description, submitter_id, submitter_url, repo_name)
} catch (error) {
core.setFailed(error.message);
}
function create_issue(uri, oauth_token, title, description, submitter_id, submitter_url, repo) {
return request(
{
method: 'POST',
url: uri,
headers: {
'Authorization': `token ${oauth_token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
'title': title,
'description': description,
'external_id': submitter_id,
'external_url': submitter_url
})
},
(error, res, body) => {
if (error) {
console.error(error)
return
}
console.log(JSON.parse(res.body).id)
ticket_id = JSON.parse(res.body).id
if (res.statusCode != 201) {
core.setFailed(`Failed to post: ${res.body}`);
return
}
console.log("Successfully opened issue")
annotate_ticket(uri, ticket_id, oauth_token, repo)
}
)
}
function annotate_ticket(uri, id, oauth_token, repo) {
console.log(`Adding label ${repo} to ${uri}/${id}`)
request(
{
method: 'PUT',
url: `${uri}/${id}`,
headers: {
'Authorization': `token ${oauth_token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
'labels': [repo]
}),
},
(error, res, body) => {
if (error) {
console.error(error)
return
}
console.log(`Status Code: ${res.statusCode}`)
if (res.statusCode != 200) {
core.setFailed(`Failed to update issue: ${body}`);
return
}
console.log("Successfully labeled issue")
}
)
}