Last active
January 4, 2017 12:31
-
-
Save apaleslimghost/aab5e9b796ec9accba5cc4a9c2b7b45b to your computer and use it in GitHub Desktop.
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
const formatBasicAuthHeader = require('@quarterto/format-basic-auth-header'); | |
const url = require('url'); | |
const fetch = require('node-fetch'); | |
const errorIsAlreadyExists = ({code}) => code === 'already_exists'; | |
module.exports = ({version, repository, commit, user, pass}) => { | |
const githubReleaseCreateEndpoint = url.format({ | |
protocol: 'https', | |
hostname: 'api.github.com', | |
pathname: `/repos/${repository}/releases`, | |
}); | |
return fetch(githubReleaseCreateEndpoint, { | |
method: 'POST', | |
body: JSON.stringify({tag_name: version, commitish: commit}), | |
headers: { | |
'content-type': 'application/json', | |
'authorization': formatBasicAuthHeader(user, pass), | |
} | |
}).then(r => { | |
if(!r.ok) { | |
return r.text().then(text => { | |
try { | |
const data = JSON.parse(text); | |
if(data.errors && data.errors.some(errorIsAlreadyExists)) { | |
return `version ${version} already exists on Github`; | |
} | |
} catch(e) {} | |
throw new Error(`Github returned ${r.status} ${r.statusText}: | |
${text}`); | |
}); | |
} | |
return `created version ${version} on Github`; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment