forked from atom/mistaken-pull-closer
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (50 loc) · 2.25 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
const commentBody = `
Thanks for your submission.
It appears that you've created a pull request using one of our repository's branches. Since this is
almost always a mistake, we're going to go ahead and close this. If it was intentional, please
let us know what you were intending and we can see about reopening it.
Thanks again!
`
async function addInvalidLabel (context, issue) {
const params = Object.assign({}, issue, {labels: ['invalid']})
await ensureLabelExists(context, {name: 'invalid', color: 'e6e6e6'})
await context.github.issues.addLabels(params)
}
async function close (context, params) {
const closeParams = Object.assign({}, params, {state: 'closed'})
return context.github.issues.edit(closeParams)
}
async function comment (context, params) {
return context.github.issues.createComment(params)
}
async function ensureLabelExists (context, {name, color}) {
return context.github.issues.getLabel(context.repo({name})).catch(() => {
return context.github.issues.createLabel(context.repo({name, color}))
})
}
async function hasPushAccess (context, params) {
const permissionResponse = await context.github.repos.reviewUserPermissionLevel(params)
const level = permissionResponse.data.permission
return level === 'admin' || level === 'write'
}
module.exports = (robot) => {
robot.on('pull_request.opened', async context => {
const {owner} = context.repo()
const branchLabel = context.payload.pull_request.head.label
// If the branch label starts with the owner name, then it is a PR from a branch in the local
// repo
if (branchLabel.startsWith(owner)) {
robot.log.debug(`PR created from branch in the local repo`)
const username = context.payload.pull_request.user.login
const canPush = await hasPushAccess(context, context.repo({username}))
// If the user creating the PR from a local branch doesn't have push access, then they
// can't push to their own PR and it isn't going to be useful
if (!canPush) {
robot.log.debug(`PR created from repo branch and user cannot push - closing PR`)
await comment(context, context.issue({body: commentBody}))
await addInvalidLabel(context, context.issue())
return close(context, context.issue())
}
}
})
}