Last active
March 10, 2022 08:24
-
-
Save devpilot/3c20e9c46c6a8f3d4e06deb0be02f3a3 to your computer and use it in GitHub Desktop.
project standard format for Pivotal
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 | |
/** | |
* Format commit message as required by Pivotal hook | |
* export PIVTOKEN="<your token>" | |
* write you commit message with story id anywhere, preferred at the beginning or end of message | |
* commit message will formats into | |
* [Delivers #<story ID>] <Story title>\n <commit message> | |
*/ | |
const path = require('path') | |
const fs = require('fs') | |
const https = require('https') | |
// MUST BE KEPT SECRET | |
const PIVTOKEN = process.env.PIVTOKEN | |
const baseUrl = 'https://www.pivotaltracker.com/services/v5/stories/' | |
const filePath = process.argv[2] | |
const getStory = (url) => { | |
const options = { | |
method: 'GET', | |
headers: { | |
'X-TrackerToken': PIVTOKEN, | |
}, | |
timeout: 1000, // in ms | |
} | |
return new Promise((resolve, reject) => { | |
const req = https.request(url, options, (res) => { | |
if (res.statusCode < 200 || res.statusCode > 299) { | |
return reject(new Error(`HTTP status code ${res.statusCode}`)) | |
} | |
const body = [] | |
res.on('data', (chunk) => body.push(chunk)) | |
res.on('end', () => { | |
const resString = Buffer.concat(body).toString() | |
resolve(resString) | |
}) | |
}) | |
req.on('error', (err) => { | |
reject(err) | |
}) | |
req.on('timeout', () => { | |
req.destroy() | |
reject(new Error('Request time out')) | |
}) | |
req.end() | |
}) | |
} | |
const writeMsg = (filePath, message, res) => { | |
const prepMsg = `[Delivers #${res.id}] ${res.name}\n${message}` | |
fs.writeFile(filePath, prepMsg, function (err) { | |
if (err) { | |
console.log(err); | |
process.exit(1) | |
} | |
}); | |
} | |
try { | |
const message = fs.readFileSync(filePath, 'utf8') | |
const isFormatted = /^\[Delivers/.test(message) | |
if (!isFormatted) { | |
// get story id from message | |
const storyId = /#(\d+)/.exec(message)?.pop() | |
if (storyId) { | |
// remove id from message | |
const onlyMsg = message.replace(/#(\d+)/, '') | |
// fetch story name | |
getStory(baseUrl + storyId) | |
.then((res) => { | |
res = JSON.parse(res) | |
// write to file | |
writeMsg(filePath, onlyMsg, res) | |
}) | |
.catch((err) => { | |
console.error(err) | |
process.exit(1) | |
}) | |
} | |
} | |
} catch (err) { | |
console.error(err) | |
process.exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment