Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Last active January 4, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apaleslimghost/aab5e9b796ec9accba5cc4a9c2b7b45b to your computer and use it in GitHub Desktop.
Save apaleslimghost/aab5e9b796ec9accba5cc4a9c2b7b45b to your computer and use it in GitHub Desktop.
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