Last active
November 27, 2017 21:41
-
-
Save thesephist/e89bbfd80bb8f84bf86640a5d94f3265 to your computer and use it in GitHub Desktop.
A simple script that I use to pipe BitBucket push message into it and auto-open a browser page for the PR.
This file contains hidden or 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 | |
var exec = require('child_process').exec | |
// accept input from pipe in | |
var data = '' | |
var IN = process.stdin | |
IN.resume() | |
IN.setEncoding('utf8') | |
IN.on('data', function(chunk) { | |
data += chunk; | |
}); | |
IN.on('end', function() { | |
parseDataIntoURL(data); | |
}); | |
setTimeout(function() { | |
if (data === '') { | |
IN.pause(); | |
parseDataIntoURL(data); | |
} | |
}, 100) // 100 milisecond timeout to read stdin | |
function parseDataIntoURL(data) { | |
// first, join all lines | |
var dataOneLine = data.replace('\n', ' '); | |
// regex match for the url | |
var pullRequestUrlMatches = dataOneLine.match(/https:\/\/bitbucket\.org\/.*\/pull-requests\/new.*&t=1/); | |
if (pullRequestUrlMatches) { | |
var pullRequestUrl = pullRequestUrlMatches[0]; | |
console.log('Opening BitBucket PR link in your browser...\nThe link is also copied into your clipboard. :)\nLink: ' + pullRequestUrl); | |
exec('open "' + pullRequestUrl + '"'); | |
exec('echo "' + pullRequestUrl + '" | pbcopy'); | |
} else { | |
console.log('This push apparently didn\'t contain any new commits. No link given by bitbucket.\nStill attempting to open this branch...\n'); | |
// assume remote branch name is also origin branch name | |
exec('git rev-parse --abbrev-ref HEAD', function(err, stdout) { | |
var branchName = stdout.trim(); | |
exec('git config --get remote.origin.url', function(err, stdout) { | |
var repoNameMatch = stdout.match(/bitbucket.org:.*\/(.*)\.git/) | |
if (repoNameMatch) { | |
var repoName = repoNameMatch[1]; | |
var pullRequestUrl = 'https://bitbucket.org/spensatech/' + repoName + '/pull-requests/new?source=' + branchName + '&t=1'; | |
exec('open "' + pullRequestUrl + '"'); | |
exec('echo "' + pullRequestUrl + '" | pbcopy'); | |
console.log('Opening BitBucket PR link in your browser...\nThe link is also copied into your clipboard. :)\nLink: ' + pullRequestUrl); | |
} else { | |
console.log('Could not find repo name or branch name. Failing out.') | |
} | |
}) | |
}); | |
} | |
} | |
This file contains hidden or 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
#!/bin/sh | |
echo 'Installing bb-parser (bbpr) into /usr/local/bin as bbpr.' | |
echo "If you've got another thing called bbpr installed, you'll want to stop this installer here." | |
sleep .5 | |
echo 'Downloading source...' | |
sleep .5 | |
curl "https://gist.githubusercontent.com/thesephist/e89bbfd80bb8f84bf86640a5d94f3265/raw/aeb74b46e577aa5f4b78d1de24fae6cb3ef23c85/bbpr.js" > /usr/local/bin/bbpr | |
echo 'Setting execute permisson...' | |
chmod u+rwx /usr/local/bin/bbpr | |
echo "Installation done! Try $ bbpr in your repo" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment