Skip to content

Commit

Permalink
Merge pull request #117 from hypermodules/fix-timeouts
Browse files Browse the repository at this point in the history
Use octokit instead of simple-get
  • Loading branch information
bcomnes authored Jan 16, 2021
2 parents d4d36cb + 69545d1 commit 315c638
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 75 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ Project versioning adheres to [Semantic Versioning](http://semver.org/).
Commit convention is based on [Conventional Commits](http://conventionalcommits.org).
Change log format is based on [Keep a Changelog](http://keepachangelog.com/).

## [4.0.5-beta.0](https://github.com/hypermodules/gh-release/compare/v4.0.4...v4.0.5-beta.0) - 2020-12-04

- Replace `simple-get` with `@octokit/rest`. This should fix some spurious timeout bugs.
- Republish on correct npm channel.

## [4.0.4](https://github.com/hypermodules/gh-release/compare/v4.0.3...v4.0.4) - 2020-12-04

- Republish of 4.0.3 due to a bad beta version publish on the latest npm tag.

## [4.0.4-beta.0](https://github.com/hypermodules/gh-release/compare/v4.0.3...v4.0.4-beta.0) - 2020-12-04

- Replace `simple-get` with `@octokit/rest`. This should fix some spurious timeout bugs.

## [4.0.3](https://github.com/hypermodules/gh-release/compare/v4.0.2...v4.0.3) - 2020-09-30

- update-notifier version 5.0.0 (drops old node, and bugfixes)
Expand Down
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function performRelease (options) {
}

function handleError (err) {
const msg = err.message || JSON.stringify(err)
const msg = err.message || JSON.stringify(err, null, 2)
console.log(chalk.red(msg))
process.exit(1)
}
121 changes: 50 additions & 71 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const extend = require('deep-extend')
const get = require('simple-get')
const format = require('util').format
const path = require('path')
const ghReleaseAssets = require('gh-release-assets')
const getDefaults = require('./bin/lib/get-defaults')
const Emitter = require('events').EventEmitter
const { Octokit } = require('@octokit/rest')

const clientId = '04dac3c40b7e49b11f38'

Expand Down Expand Up @@ -81,65 +81,30 @@ function _Release (options, emitter, callback) {
}

// err if auth info not provided (token or user/pass)
if (!getAuth(options)) return callback(new Error('missing auth info'))
if (!getToken(options)) return callback(new Error('missing auth info'))

const commitOptsPath = format('/repos/%s/%s/commits/%s', options.owner, options.repo, options.target_commitish)
const commitOptsUrl = options.endpoint.replace(/\/+$/, '') + commitOptsPath

// check if commit exists on remote
const commitOpts = extend(getAuth(options), {
method: 'GET',
url: commitOptsUrl
const octokit = new Octokit({
auth: getToken(options),
baseUrl: options.endpoint
})

get.concat(commitOpts, function (err, res, body) {
if (err || res.statusCode === 404) {
const errorMessage = format('Target commitish %s not found in %s/%s', options.target_commitish, options.owner, options.repo)
return callback(new Error(errorMessage))
}

const releaseOptsPath = format('/repos/%s/%s/releases', options.owner, options.repo)
const releaseOptsUrl = options.endpoint.replace(/\/+$/, '') + releaseOptsPath

const releaseOpts = extend(getAuth(options), {
url: releaseOptsUrl,
body: {
tag_name: options.tag_name,
target_commitish: options.target_commitish,
name: options.name,
body: options.body,
draft: options.draft,
prerelease: options.prerelease,
repo: options.repo,
owner: options.owner
}
})

octokit.repos.getCommit({
owner: options.owner,
repo: options.repo,
ref: options.target_commitish
}).then(results => {
if (options.dryRun) return callback(null, options)

get.concat(releaseOpts, function (err, res, body) {
if (err) {
return callback(err)
}

if (res.statusCode === 404) {
const authErrorMessage = format('404 Not Found. Review gh-release oAuth Organization access: https://github.com/settings/connections/applications/%s', clientId)
return callback(new Error(authErrorMessage))
}

if (body.errors) {
if (body.errors[0].code !== 'already_exists') {
return callback(body.errors)
}

const errorMessage = format('Release already exists for tag %s in %s/%s', options.tag_name, options.owner, options.repo)
return callback(new Error(errorMessage))
}

if (body.message === 'Bad credentials') {
return callback(new Error('GitHub says password is no bueno. please clear your cache manually. https://github.com/hypermodules/gh-release#config-location'))
}

octokit.repos.createRelease({
tag_name: options.tag_name,
target_commitish: options.target_commitish,
name: options.name,
body: options.body,
draft: options.draft,
prerelease: options.prerelease,
repo: options.repo,
owner: options.owner
}).then(results => {
if (options.assets) {
const assets = options.assets.map(function (asset) {
if (typeof asset === 'object') {
Expand All @@ -153,7 +118,7 @@ function _Release (options, emitter, callback) {
})

const assetOptions = {
url: body.upload_url,
url: results.data.upload_url,
assets: assets
}

Expand All @@ -165,7 +130,7 @@ function _Release (options, emitter, callback) {

const assetUpload = ghReleaseAssets(assetOptions, function (err) {
if (err) return callback(err)
return callback(null, body)
return callback(null, results.data)
})
assetUpload.on('upload-asset', function (name) {
emitter.emit('upload-asset', name)
Expand All @@ -177,9 +142,33 @@ function _Release (options, emitter, callback) {
emitter.emit('uploaded-asset', name)
})
} else {
callback(null, body)
callback(null, results.data)
}
}).catch(err => {
// Create Release error handling
if (err.status === 404) {
const notFoundError = new Error(format('404 Not Found. Review gh-release oAuth Organization access: https://github.com/settings/connections/applications/%s', clientId))
notFoundError.wrapped = err
return callback(notFoundError)
}
if (err.errors) {
if (err.errors[0].code !== 'already_exists') {
return callback(err)
}

const errorMessage = format('Release already exists for tag %s in %s/%s', options.tag_name, options.owner, options.repo)
return callback(new Error(errorMessage))
}
return callback(err)
})
}).catch(err => {
// Check target error handling
if (err.status === 404) {
const errorMessage = format('Target commitish %s not found in %s/%s', options.target_commitish, options.owner, options.repo)
return callback(new Error(errorMessage))
} else {
return callback(err)
}
})
}

Expand All @@ -206,24 +195,14 @@ function validate (options) {
}
}

function getAuth (options) {
const defaultRequest = {
method: 'POST',
json: true,
headers: {
'User-Agent': 'gh-release'
}
}

function getToken (options) {
if (options.auth.token) {
defaultRequest.headers.Authorization = 'token ' + options.auth.token
return options.auth.token
} else if (options.auth.username && options.auth.password) {
defaultRequest.auth = options.auth
return options.auth
} else {
return false
}

return defaultRequest
}

Release.OPTIONS = OPTIONS
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gh-release",
"description": "Create a release for a node package on github.",
"version": "4.0.4",
"version": "4.0.5-beta.0",
"author": "Nate Goldman <[email protected]> (http://ungoldman.com/)",
"bin": {
"gh-release": "./bin/cli.js"
Expand All @@ -21,16 +21,16 @@
"Ted Janeczko <[email protected]>"
],
"dependencies": {
"ghauth": "^5.0.0",
"@octokit/rest": "^18.0.9",
"chalk": "^4.1.0",
"changelog-parser": "^2.0.0",
"deep-extend": "^0.6.0",
"gauge": "^v2.7.4",
"gh-release-assets": "^2.0.0",
"ghauth": "^5.0.0",
"github-url-to-object": "^4.0.4",
"inquirer": "^7.3.3",
"shelljs": "^0.8.4",
"simple-get": "^4.0.0",
"update-notifier": "^5.0.0",
"yargs": "^16.0.3"
},
Expand Down

0 comments on commit 315c638

Please sign in to comment.