Created
August 9, 2019 00:16
-
-
Save matt-ball/54fd9892b6956896fb07000d4c3cb2b8 to your computer and use it in GitHub Desktop.
Postman x git push hook
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 | |
const fs = require('fs') | |
const path = require('path') | |
const https = require('https') | |
const childProcess = require('child_process') | |
const cwd = process.cwd() | |
const cfg = require(path.resolve(cwd, 'hooks/config.js')) | |
const options = { | |
hostname: 'api.getpostman.com', | |
port: 443, | |
path: `/collections/${cfg.uid}`, | |
method: 'GET', | |
headers: { | |
'X-Api-Key': cfg.postmanApiKey | |
} | |
} | |
const req = https.request(options, (res) => { | |
let d = '' | |
res.on('data', (chunk) => { | |
d += chunk | |
}) | |
res.on('end', () => { | |
const { collectionFile } = cfg | |
const existingCollection = JSON.stringify(require(path.resolve(cwd, collectionFile))) | |
const collection = JSON.stringify(JSON.parse(d).collection) | |
if (existingCollection !== collection) { | |
fs.writeFileSync(collectionFile, collection) | |
childProcess.execSync(`git add ${collectionFile}`) | |
childProcess.execSync(`git commit -m 'Updated Postman collection'`) | |
console.log('Postman collection was updated. Commited to branch, please push again!') | |
process.exit(1) | |
// delete local copy? | |
} | |
}); | |
}) | |
req.on('error', (error) => { | |
console.error(error) | |
}) | |
req.end() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment