-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (81 loc) · 2.68 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
// modules
const express = require('express')
const axios = require('axios')
const dotenv = require('dotenv').config()
// express setup
const app = express()
const port = 3000
const bodyParser = require('body-parser')
app.use(bodyParser.json())
// api elements
const accessToken = process.env.GH_API_KEY
const username = process.env.GH_USERNAME
const apiURL = "https://api.github.com"
// root route
app.get('/', (req, res) => {
res.send('GitHub API Interaction 👋')
})
// route for receiving the webhook
app.post('/payload', async (req, res) => {
// org and repo name provided in the body
const repoFullName = req.body.repository.full_name
// check for "created" action
if (req.body.action !== "created") {
res.send()
return
}
try {
// create a temporary file to create the master branch
await axios.request({
url: `${apiURL}/repos/${repoFullName}/contents/README.md`,
method: 'PUT',
headers: {
Accept: "application/json",
Authorization: "token " + accessToken
},
data: {
"branch": "master",
"message": "hello world",
"content": "aGVsbG8gd29ybGQ="
}
})
// protect the branch
await axios.request({
url: `${apiURL}/repos/${repoFullName}/branches/master/protection`,
method: 'PUT',
headers: {
Accept: "application/vnd.github.luke-cage-preview+json",
Authorization: "token " + accessToken
},
data: {
"allow_deletions": false,
"enforce_admins": null,
"required_pull_request_reviews": {
"required_approving_review_count": 2
},
"restrictions": null,
"required_status_checks": null
}
})
// create the issue
await axios.request({
url: `${apiURL}/repos/${repoFullName}/issues`,
method: 'POST',
headers: {
Accept: "application/json",
Authorization: "token " + accessToken
},
data: {
"title": "Branch protection now enabled on master",
"body": `@${username} please note that 2 PR reviews are required before merging into \`master\`.`,
"assignee": `${username}`
}
})
} catch (error) {
// log any errors (pending something a little more robust)
console.log(error)
}
res.send()
})
// start the express server
app.listen(port, () => console.log(`Listening on port ${port}!`))