From 9c780580d3e6cd763948b063848fce993ea04faf Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 30 Oct 2020 03:11:43 -0400 Subject: [PATCH] Adds copy PR url command --- package.json | 17 ++++++++++- src/commands/common.ts | 1 + src/commands/openPullRequestOnRemote.ts | 50 +++++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index c9691de..7a5f4a3 100644 --- a/package.json +++ b/package.json @@ -154,6 +154,7 @@ "onCommand:gitlens.openFileRevision", "onCommand:gitlens.openFileRevisionFrom", "onCommand:gitlens.openPullRequestOnRemote", + "onCommand:gitlens.copyRemotePullRequestUrl", "onCommand:gitlens.openAssociatedPullRequestOnRemote", "onCommand:gitlens.openRepoOnRemote", "onCommand:gitlens.copyRemoteRepositoryUrl", @@ -3142,6 +3143,15 @@ "icon": "$(globe)" }, { + "command": "gitlens.copyRemotePullRequestUrl", + "title": "Copy Pull Request Url", + "category": "GitLens", + "icon": { + "dark": "images/dark/icon-copy-link.svg", + "light": "images/light/icon-copy-link.svg" + } + }, + { "command": "gitlens.openAssociatedPullRequestOnRemote", "title": "Open Associated Pull Request", "category": "GitLens", @@ -4712,6 +4722,10 @@ "when": "false" }, { + "command": "gitlens.copyRemotePullRequestUrl", + "when": "false" + }, + { "command": "gitlens.openAssociatedPullRequestOnRemote", "when": "gitlens:activeFileStatus =~ /blameable/ && gitlens:activeFileStatus =~ /remotes/" }, @@ -6842,7 +6856,8 @@ { "command": "gitlens.openPullRequestOnRemote", "when": "viewItem =~ /gitlens:pullrequest\\b/", - "group": "inline@99" + "group": "inline@99", + "alt": "gitlens.copyRemotePullRequestUrl" }, { "command": "gitlens.views.addRemote", diff --git a/src/commands/common.ts b/src/commands/common.ts index 835ba03..678b95f 100644 --- a/src/commands/common.ts +++ b/src/commands/common.ts @@ -39,6 +39,7 @@ export enum Commands { CopyRemoteBranchUrl = 'gitlens.copyRemoteBranchUrl', CopyRemoteCommitUrl = 'gitlens.copyRemoteCommitUrl', CopyRemoteFileUrl = 'gitlens.copyRemoteFileUrlToClipboard', + CopyRemotePullRequestUrl = 'gitlens.copyRemotePullRequestUrl', CopyRemoteRepositoryUrl = 'gitlens.copyRemoteRepositoryUrl', CopyShaToClipboard = 'gitlens.copyShaToClipboard', DiffDirectory = 'gitlens.diffDirectory', diff --git a/src/commands/openPullRequestOnRemote.ts b/src/commands/openPullRequestOnRemote.ts index 05f81ef..fa99a3f 100644 --- a/src/commands/openPullRequestOnRemote.ts +++ b/src/commands/openPullRequestOnRemote.ts @@ -1,5 +1,5 @@ 'use strict'; -import { env, Uri } from 'vscode'; +import { env, Uri, window } from 'vscode'; import { Command, command, @@ -9,9 +9,12 @@ import { isCommandContextViewNodeHasFileCommit, } from './common'; import { Container } from '../container'; -import { PullRequestNode } from '../views/nodes/pullRequestNode'; +import { PullRequestNode } from '../views/nodes'; +import { Logger } from '../logger'; +import { Messages } from '../messages'; export interface OpenPullRequestOnRemoteCommandArgs { + clipboard?: boolean; ref?: string; repoPath?: string; pr?: { url: string }; @@ -20,19 +23,24 @@ export interface OpenPullRequestOnRemoteCommandArgs { @command() export class OpenPullRequestOnRemoteCommand extends Command { constructor() { - super([Commands.OpenPullRequestOnRemote, Commands.OpenAssociatedPullRequestOnRemote]); + super([ + Commands.OpenPullRequestOnRemote, + Commands.CopyRemotePullRequestUrl, + Commands.OpenAssociatedPullRequestOnRemote, + ]); } protected preExecute(context: CommandContext, args?: OpenPullRequestOnRemoteCommandArgs) { - if (context.command === Commands.OpenPullRequestOnRemote) { - if (context.type === 'viewItem' && context.node instanceof PullRequestNode) { - args = { - ...args, - pr: { url: context.node.pullRequest.url }, - }; + if (context.command === Commands.OpenAssociatedPullRequestOnRemote) { + if (isCommandContextViewNodeHasCommit(context) || isCommandContextViewNodeHasFileCommit(context)) { + args = { ...args, ref: context.node.commit.sha, repoPath: context.node.commit.repoPath }; } - } else if (isCommandContextViewNodeHasCommit(context) || isCommandContextViewNodeHasFileCommit(context)) { - args = { ...args, ref: context.node.commit.sha, repoPath: context.node.commit.repoPath }; + } else if (context.type === 'viewItem' && context.node instanceof PullRequestNode) { + args = { + ...args, + pr: { url: context.node.pullRequest.url }, + clipboard: context.command === Commands.CopyRemotePullRequestUrl, + }; } return this.execute(args); @@ -52,6 +60,24 @@ export class OpenPullRequestOnRemoteCommand extends Command { args.pr = pr; } - void env.openExternal(Uri.parse(args.pr.url)); + if (args.clipboard) { + try { + void (await env.clipboard.writeText(args.pr.url)); + } catch (ex) { + const msg: string = ex?.toString() ?? ''; + if (msg.includes("Couldn't find the required `xsel` binary")) { + void window.showErrorMessage( + 'Unable to copy remote url, xsel is not installed. Please install it via your package manager, e.g. `sudo apt install xsel`', + ); + + return; + } + + Logger.error(ex, 'CopyRemotePullRequestCommand'); + void Messages.showGenericErrorMessage('Unable to copy pull request url'); + } + } else { + void env.openExternal(Uri.parse(args.pr.url)); + } } }