diff --git a/.vscode/launch.json b/.vscode/launch.json index c7ec671b..69faf925 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -19,7 +19,7 @@ "request": "launch", "env": { "NODE_ENV": "testing", "GH_USER": "protoEvangelion", "GH_TOKEN": "0001" }, "runtimeArgs": ["-r", "ts-node/register"], - "args": ["${workspaceFolder}/src/debug.ts", "pr", "--prettyPrint"], + "args": ["${workspaceFolder}/src/debug.ts", "pr", "659", "--browser"], "console": "integratedTerminal" } ] diff --git a/__tests__/__snapshots__/gists.test.ts.snap b/__tests__/__snapshots__/gists.test.ts.snap index bccf1672..58b1dbf6 100644 --- a/__tests__/__snapshots__/gists.test.ts.snap +++ b/__tests__/__snapshots__/gists.test.ts.snap @@ -39,3 +39,8 @@ https://gist.github.com/dfc000000001b8c49c8a97dbc98cf7193 " `; + +exports[`E2E: Gist Module Test Open gist in browser \`gh gi --browser --id 5444883\` 1`] = ` +"https://gist.github.com/5444883 +" +`; diff --git a/__tests__/__snapshots__/issue.test.ts.snap b/__tests__/__snapshots__/issue.test.ts.snap index 0deaee7f..a6f30f54 100644 --- a/__tests__/__snapshots__/issue.test.ts.snap +++ b/__tests__/__snapshots__/issue.test.ts.snap @@ -51,6 +51,11 @@ https://github.com/protoEvangelion/gh/issues/10 " `; +exports[`E2E: Issues Module Test Open issue in browser \`gh is 55 --browser\` 1`] = ` +"https://github.com/protoEvangelion/gh/issues/55 +" +`; + exports[`E2E: Issues Module Test Search Issues \`gh is -s hi\` 1`] = ` "Searching for hi in issues for protoEvangelion/gh diff --git a/__tests__/__snapshots__/pull-request.test.ts.snap b/__tests__/__snapshots__/pull-request.test.ts.snap index 5720a1a3..b9626930 100644 --- a/__tests__/__snapshots__/pull-request.test.ts.snap +++ b/__tests__/__snapshots__/pull-request.test.ts.snap @@ -30,7 +30,7 @@ exports[`E2E: Pull Request Module Test Fwd PR to another user \`gh pr 97 --fwd n "Forwarding pull request #97 to @node-gh-bot https://github.com/node-gh-bot/gh/pull/3 [hook] gh pr 3 --user node-gh-bot --comment '/cc @protoEvangelion' -[hook] gh pr 97 --user protoEvangelion --repo gh --comment 'Pull request forwarded to https://api.github.com/node-gh-bot/gh/pull/3.… +[hook] gh pr 97 --user protoEvangelion --repo gh --comment 'Pull request forwarded to https://github.com/node-gh-bot/gh/pull/3.… [hook] gh pr 97 --close " `; @@ -130,6 +130,11 @@ https://github.com/protoEvangelion/gh/pull/40 " `; +exports[`E2E: Pull Request Module Test Open pr in browser \`gh pr 55 --browser\` 1`] = ` +"https://github.com/protoEvangelion/gh/pull/55 +" +`; + exports[`E2E: Pull Request Module Test Submit PR \`gh pr -s protoEvangelion -b master -t "pr title" -D "pr description"\` 1`] = ` "Submitting pull request to @protoEvangelion https://github.com/protoEvangelion/gh/pull/97 diff --git a/__tests__/__snapshots__/repo.test.ts.snap b/__tests__/__snapshots__/repo.test.ts.snap index e275c96a..c971352a 100644 --- a/__tests__/__snapshots__/repo.test.ts.snap +++ b/__tests__/__snapshots__/repo.test.ts.snap @@ -20,6 +20,11 @@ Successfully forked: https://github.com/protoEvangelion/prettier " `; +exports[`E2E: Repo Module Test List Repos \`gh re --browser\` 1`] = ` +"https://github.com/protoEvangelion/gh +" +`; + exports[`E2E: Repo Module Test List Repos \`gh re --list\` 1`] = ` "protoEvangelion/100-days-of-code protoEvangelion/adopt-a-family diff --git a/__tests__/gists.test.ts b/__tests__/gists.test.ts index e015ed68..b35c378b 100644 --- a/__tests__/gists.test.ts +++ b/__tests__/gists.test.ts @@ -13,6 +13,11 @@ describe('E2E: Gist Module Test', () => { done() }) + it('Open gist in browser `gh gi --browser --id 5444883`', done => { + expect(runCmd('gh gi --browser --id 5444883')).toMatchSnapshot() + done() + }) + it('Create new gist `gh gi --new hello --content "Hello World!"`', done => { expect(runCmd('gh gi --new hello --content "Hello World!"')).toMatchSnapshot() done() diff --git a/__tests__/issue.test.ts b/__tests__/issue.test.ts index e0053e9b..55d9a22a 100644 --- a/__tests__/issue.test.ts +++ b/__tests__/issue.test.ts @@ -17,6 +17,11 @@ describe('E2E: Issues Module Test', () => { done() }) + it('Open issue in browser `gh is 55 --browser`', done => { + expect(runCmd('gh is 55 --browser')).toMatchSnapshot() + done() + }) + it('Create a new issue `gh is -N -t "Node GH rocks!" -L bug,question,test`', done => { expect(runCmd(`gh is -N -t "Node GH rocks!" -L bug,question,test`)).toMatchSnapshot() done() diff --git a/__tests__/pull-request.test.ts b/__tests__/pull-request.test.ts index 134d3468..6a64c413 100644 --- a/__tests__/pull-request.test.ts +++ b/__tests__/pull-request.test.ts @@ -26,6 +26,11 @@ describe('E2E: Pull Request Module Test', () => { done() }) + it('Open pr in browser `gh pr 55 --browser`', done => { + expect(runCmd('gh pr 55 --browser')).toMatchSnapshot() + done() + }) + /* These have been passing locally but failing in Travis, need to find out work around */ xit('List PRs `gh pr --list --prettyPrint`', done => { // strip ansi characters so it doesn't fail on Travis diff --git a/__tests__/repo.test.ts b/__tests__/repo.test.ts index 6fa83886..53a2cc97 100644 --- a/__tests__/repo.test.ts +++ b/__tests__/repo.test.ts @@ -13,6 +13,11 @@ describe('E2E: Repo Module Test', () => { done() }) + it('List Repos `gh re --browser`', done => { + expect(runCmd('gh re --browser')).toMatchSnapshot() + done() + }) + it('Create new repo `gh re --new foo --init`', done => { expect(runCmd('gh re --new foo --init')).toMatchSnapshot() done() diff --git a/src/cmds/gists.ts b/src/cmds/gists.ts index 23f5300d..2c0af17e 100644 --- a/src/cmds/gists.ts +++ b/src/cmds/gists.ts @@ -7,7 +7,7 @@ // -- Requires ------------------------------------------------------------------------------------- import * as inquirer from 'inquirer' -import * as openUrl from 'opn' +import { openUrl } from '../utils' import * as base from '../base' import { getGitHubInstance } from '../github' import { afterHooks, beforeHooks } from '../hooks' @@ -70,7 +70,7 @@ Gists.prototype.run = async function(done) { } if (options.browser) { - !testing && instance.browser(options.id || options.loggedUser) + instance.browser(options.id || options.loggedUser) } if (options.delete) { @@ -160,7 +160,7 @@ Gists.prototype.run = async function(done) { } Gists.prototype.browser = function(gist) { - openUrl(config.github_gist_host + gist, { wait: false }) + openUrl(config.github_gist_host + gist) } Gists.prototype.delete = function(id) { diff --git a/src/cmds/issue.ts b/src/cmds/issue.ts index 6a394a29..941174f9 100644 --- a/src/cmds/issue.ts +++ b/src/cmds/issue.ts @@ -7,7 +7,7 @@ // -- Requires ------------------------------------------------------------------------------------- import { isArray } from 'lodash' -import * as openUrl from 'opn' +import { openUrl } from '../utils' import * as base from '../base' import { getGitHubInstance } from '../github' import { afterHooks, beforeHooks } from '../hooks' @@ -136,7 +136,7 @@ Issue.prototype.run = async function(done) { } if (options.browser) { - !testing && instance.browser(options.user, options.repo, options.number) + instance.browser(options.user, options.repo, options.number) } if (options.comment) { @@ -244,7 +244,7 @@ Issue.prototype.browser = function(user, repo, number) { number = '' } - openUrl(`${config.github_host}/${user}/${repo}/issues/${number}`, { wait: false }) + openUrl(`${config.github_host}/${user}/${repo}/issues/${number}`) } Issue.prototype.close = async function(number) { diff --git a/src/cmds/pull-request.ts b/src/cmds/pull-request.ts index 550ae5a1..f960ecba 100755 --- a/src/cmds/pull-request.ts +++ b/src/cmds/pull-request.ts @@ -10,7 +10,7 @@ import * as Table from 'cli-table3' import { startsWith } from 'lodash' import * as marked from 'marked' import * as TerminalRenderer from 'marked-terminal' -import * as openUrl from 'opn' +import { openUrl } from '../utils' import * as wrap from 'wordwrap' import * as base from '../base' import * as git from '../git' @@ -173,7 +173,7 @@ PullRequest.prototype.run = async function(done) { logger.error("You've invoked a method that requires an issue number.") } - if (options.browser && !testing) { + if (options.browser) { instance.browser(options.user, options.repo, number) } @@ -258,10 +258,9 @@ PullRequest.prototype.addComplexityParamToPulls_ = async function(pulls) { PullRequest.prototype.browser = function(user, repo, number) { if (number) { - const hey = `${config.github_host}/${user}/${repo}/pull/${number}` - openUrl(`${config.github_host}/${user}/${repo}/pull/${number}`, { wait: false }) + openUrl(`${config.github_host}/${user}/${repo}/pull/${number}`) } else { - openUrl(`${config.github_host}/${user}/${repo}/pulls`, { wait: false }) + openUrl(`${config.github_host}/${user}/${repo}/pulls`) } } diff --git a/src/cmds/repo.ts b/src/cmds/repo.ts index 32ebea9f..0ff702a8 100644 --- a/src/cmds/repo.ts +++ b/src/cmds/repo.ts @@ -9,7 +9,7 @@ import * as Octokit from '@octokit/rest' import * as fs from 'fs' import * as inquirer from 'inquirer' -import * as openUrl from 'opn' +import { openUrl } from '../utils' import * as url from 'url' import * as base from '../base' import * as git from '../git' @@ -356,7 +356,7 @@ Repo.prototype.run = async function(done) { } Repo.prototype.browser = function(user, repo) { - !testing && openUrl(`${config.github_host}/${user}/${repo}`, { wait: false }) + openUrl(`${config.github_host}/${user}/${repo}`) } Repo.prototype.clone_ = function(user, repo, repo_url) { diff --git a/src/configs.ts b/src/configs.ts index 857c02f4..54bacfae 100644 --- a/src/configs.ts +++ b/src/configs.ts @@ -81,7 +81,7 @@ export function getConfig() { const is_enterprise = config.api.host !== 'api.github.com' if (config.github_host === undefined) { - config.github_host = `${protocol}${is_enterprise ? config.api.host : 'api.github.com'}` + config.github_host = `${protocol}${is_enterprise ? config.api.host : 'github.com'}` } if (config.github_gist_host === undefined) { config.github_gist_host = `${protocol}${ diff --git a/src/github.ts b/src/github.ts index b47aa454..ce276395 100644 --- a/src/github.ts +++ b/src/github.ts @@ -18,11 +18,14 @@ const config = getConfig() export async function getGitHubInstance(): Promise { const { - github_host, - api: { pathPrefix }, + api: { protocol, pathPrefix, host }, } = config - const { href, ...rest } = new URL(`${github_host}${pathPrefix || ''}`) + const is_enterprise = config.api.host !== 'api.github.com' + + const apiUrl = `${protocol}://${is_enterprise ? config.api.host : 'api.github.com'}` + + const { href, ...rest } = new URL(`${apiUrl}${pathPrefix || ''}`) // trim trailing slash for Octokit const baseUrl = href.replace(/\/+$/, '') diff --git a/src/utils.ts b/src/utils.ts index 0d8b5211..d59c3980 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,6 +7,16 @@ import { isArray, isObject, isPlainObject, map, mapValues, upperFirst } from 'lodash' import * as nock from 'nock' import * as zlib from 'zlib' +import * as open from 'opn' + +const testing = process.env.NODE_ENV === 'testing' + +/** + * Opens url in browser + */ +export function openUrl(url) { + testing ? console.log(url) : open(url, { wait: false }) +} export function getCurrentFolderName(): string { const cwdArr = process