-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (127 loc) · 4.99 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import http from "http";
import express from "express";
import axios from "axios";
import bodyParser from 'body-parser'
const app = express()
const port = process.env.PORT || 8080
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }));
http.createServer(app).listen(port, function () {
console.log("\nApp running at http://localhost:" + port);
console.log("________________________________________________________________");
});
const API_URL = 'https://api.github.com';
const GITHUB_APIKEY = process.env.GITHUB_APIKEY
/* ZenHub Data Example:
{
"type": "issue_transfer",
"github_url": "https://github.com/ZenHubIO/support/issues/618",
"organization": "ZenHubHQ",
"repo": "support",
"user_name": "ZenHubIO",
"issue_number": "618",
"issue_title": "Zenhub Change Log",
"to_pipeline_name": "New Issues",
"workspace_id": "603fc3e575de63001cc163f9",
"workspace_name": "My Workspace",
"from_pipeline_name": "Discussion"
}
*/
app.post('/', function (_req, _res) {
console.log("Received a request: ", _req.body)
if (_req.body.type !== "issue_transfer") _res.status(200).send("Received an event of type '" + _req.body.type + "', not an 'issue_transfer' event. Ignoring...")
else {
const urlSplit = _req.body.github_url.split("/")
let owner = urlSplit[3]
let repo = urlSplit[4]
let issueNumber = urlSplit[6]
getGithubProjectsV2OptionsId(owner, repo, issueNumber).then(res => {
const projectsV2 = res.data?.data?.repository?.issue?.projectsV2?.nodes
if (!projectsV2) _res.status(500).send(res.data)
else if (projectsV2.length === 0) _res.status(500).send(`The issue with ID ${issueNumber} is not linked to any project.`)
else {
for (let project of projectsV2) {
const pipelineField = project.fields.nodes[2]
const projectId = project?.id
const fieldId = pipelineField?.id
const optionId = pipelineField.options.find(option => option.name === _req.body.to_pipeline_name)?.id
const itemId = project.items.nodes.find(item => item.fieldValues.nodes.find(fieldValue => fieldValue.text === _req.body.issue_title))?.id
updateGithubProjectsV2(projectId, itemId, fieldId, optionId).then(res => {
if (res.data?.errors?.find(error => error?.type === "NOT_FOUND")) _res.status(404).send(res.data)
else if (res.data?.errors) _res.status(500).send(res.data)
else _res.status(200).send(res.data)
}).catch(err => {
console.log("Error: ", err)
_res.status(500).send(err)
})
}
}
}).catch(err => {
console.log("Error: ", err)
_res.status(500).send(err)
})
}
})
function getGithubProjectsV2OptionsId(owner, repo, issueNumber) {
return axios.post(API_URL + '/graphql', {
query: `{
repository(owner: "${owner}", name: "${repo}") {
issue(number: ${issueNumber}) {
number
projectsV2(first: 5) {
nodes {
id
fields(first: 100) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
items(first: 100) {
nodes {
id
fieldValues(first: 100) {
nodes {
... on ProjectV2ItemFieldTextValue {
text
}
... on ProjectV2ItemFieldSingleSelectValue {
name
optionId
}
}
}
}
}
}
}
}
}
}`
}, { headers: { Authorization: 'Bearer ' + GITHUB_APIKEY, Accept: 'application/vnd.github.starfox-preview+json' } })
}
function updateGithubProjectsV2(projectId, itemId, fieldId, optionId) {
return axios.post(API_URL + '/graphql', {
query: `mutation {
updateProjectV2ItemFieldValue(
input: {projectId: "${projectId}", itemId: "${itemId}", fieldId: "${fieldId}", value: {singleSelectOptionId: "${optionId}"}}
) {
projectV2Item {
fieldValues(first: 100) {
nodes {
... on ProjectV2ItemFieldSingleSelectValue {
name
}
}
}
}
}
}`
}, { headers: { Authorization: 'Bearer ' + GITHUB_APIKEY, Accept: 'application/vnd.github.starfox-preview+json' } })
}