-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmark-pr-head-as-trusted.cjs
43 lines (41 loc) · 1.33 KB
/
mark-pr-head-as-trusted.cjs
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
const Git = require('nodegit')
const assert = require('assert')
const meow = require('meow');
/**
* For reference, the unix shell equivalent of this script is kind of:
*
* ```sh
* # `ORIGIN` is your remote name for the Snabbdom repository.
* # `PR` is the pull request number
* REF=refs/remotes/$ORIGIN/pull/$PR/head
* git fetch $ORIGIN +refs/pull/$PR/head:$REF
* BRANCH=allow-ci_$PR
* git push $ORIGIN +${REF}:refs/heads/$BRANCH
* ```
*/
(async function () {
const [pr] = meow({ hardRejection: false }).input
assert.notStrictEqual(pr, undefined)
const repo = await Git.Repository.open(__dirname)
const remote = (await repo.getRemotes())
.find(r => r.url() === '[email protected]:snabbdom/snabbdom.git')
assert.notStrictEqual(remote, undefined)
const ref = `refs/remotes/${remote.name()}/pull/${pr}/head`
const credentials = (url, userName) => Git.Cred
.sshKeyFromAgent(userName)
await remote
.fetch(
[`+refs/pull/${pr}/head:${ref}`],
{ callbacks: { credentials } }
)
const branchName = `allow-ci_${pr}`
await remote
.push(
[`+${ref}:refs/heads/${branchName}`],
{ callbacks: { credentials } }
)
console.log(`\
Marked head of pull request #${pr} as trusted by pushing it to branch \`${branchName}\`.
As soon as the pull request is closed/merged, this branch can be deleted.\
`)
})()