This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_github.js
87 lines (71 loc) · 2.64 KB
/
update_github.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
const https = require('https');
/*
* Node.js does not have a simple built-in to use API for sending HTTPS POST requests
* Code adapted from https://stackoverflow.com/a/67094088/3105260
*/
async function post(url, data, headers) {
console.log("Sending a post request with data: ", JSON.stringify(data, null, 2))
const dataString = JSON.stringify(data);
headers['Content-Length'] = dataString.length;
const options = {
method: 'POST',
headers: headers,
timeout: 10000, // in ms
};
console.log("options for posst request: ", JSON.stringify(options, null, 2));
return new Promise((resolve, reject) => {
const req = https.request(url, options, (res) => {
const body = [];
res.on('data', (chunk) => {
body.push(chunk);
});
res.on('end', () => {
const resString = Buffer.concat(body).toString();
if (res.statusCode < 200 || res.statusCode > 299) {
reject(new Error(`POST code=${res.statusCode} body: ${resString}`));
} else {
resolve(resString);
}
});
});
req.on('error', (err) => {
reject(err);
});
req.on('timeout', () => {
req.destroy();
reject(new Error('Request time out'));
});
req.write(dataString);
req.end();
});
}
exports.handler = async (event, context) => {
console.log('Received event:', JSON.stringify(event, null, 2));
if(event.version && event.version != '2.0') {
console.log(`[!!] WARNING: event.version ('${event.version}') different `
+ `from expected '2.0' - if something breaks it's propably because of this`);
}
const httpMethod = event.httpMethod || event.requestContext.http.method;
if(httpMethod == 'POST') {
const postResponse = await post(
`https://api.github.com/repos/${process.env.GITHUB_REPO}/actions/workflows/pr-to-development.yml/dispatches`,
{
ref: "development"
},
{
'Content-Type': 'application/vnd.github.v3+json',
'Authorization': `token ${process.env.GITHUB_TOKEN}`,
'User-Agent': 'curl/7.79.1'
}
);
console.log(`Response from post: ${postResponse}`);
return {
statusCode: 303,
headers: {
Location: `https://github.com/${process.env.GITHUB_REPO}/pulls`
}
};
} else {
throw new Error(`Unsupported method "${event.httpMethod}"`);
}
}