Created
May 17, 2018 17:24
-
-
Save sampaiodiego/3febc65883bd1bcacc7d66399c323d45 to your computer and use it in GitHub Desktop.
pr.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// USAGE | |
// pr.js <pr_number> | |
const https = require('https'); | |
const _execSync = require('child_process').execSync; | |
function execSync(command) { | |
console.log(command); | |
return _execSync(command).toString(); | |
} | |
const prNumber = process.argv[2]; | |
if (!prNumber || isNaN(parseInt(prNumber))) { | |
console.error('PR number is required'); | |
process.exit(1); | |
} | |
var options = { | |
hostname: 'api.github.com', | |
port: 443, | |
path: '/repos/RocketChat/Rocket.Chat/pulls/'+prNumber, | |
method: 'GET', | |
headers: { | |
'User-Agent': 'Node app' | |
} | |
}; | |
function configBranch(ssh_url, ref, login) { | |
const remotes = execSync('git remote').split('\n'); | |
if (remotes.indexOf(login) === -1) { | |
console.log(execSync(`git remote add ${login} ${ssh_url}`)); | |
} | |
console.log(execSync(`git fetch ${login}`)); | |
const branchName = `pr/${prNumber}-${login}-${ref}`; | |
if (execSync(`git branch --list ${branchName}`).trim() === '') { | |
console.log(execSync(`git checkout -b pr/${prNumber}-${login}-${ref} --track ${login}/${ref}`)); | |
} else { | |
console.log(execSync(`git checkout pr/${prNumber}-${login}-${ref}`)); | |
console.log(execSync('git pull')); | |
} | |
} | |
var req = https.request(options, (res) => { | |
const buffer = []; | |
res.on('data', (d) => { | |
buffer.push(d); | |
}); | |
res.on('end', () => { | |
const data = JSON.parse(Buffer.concat(buffer).toString()); | |
if (data.message) { | |
console.error(data.message); | |
process.exit(1); | |
} | |
configBranch(data.head.repo.ssh_url, data.head.ref, data.user.login); | |
}); | |
}); | |
req.end(); | |
req.on('error', (e) => { | |
console.error(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you <3