You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 line
1.4 KiB

  1. 'use strict';
  2. import { env, Uri } from 'vscode';
  3. import { Command, command, CommandContext, Commands } from './common';
  4. import { Container } from '../container';
  5. import { PullRequestNode } from '../views/nodes';
  6. export interface OpenPullRequestOnRemoteCommandArgs {
  7. clipboard?: boolean;
  8. ref?: string;
  9. repoPath?: string;
  10. pr?: { url: string };
  11. }
  12. @command()
  13. export class OpenPullRequestOnRemoteCommand extends Command {
  14. constructor() {
  15. super([Commands.OpenPullRequestOnRemote, Commands.CopyRemotePullRequestUrl]);
  16. }
  17. protected preExecute(context: CommandContext, args?: OpenPullRequestOnRemoteCommandArgs) {
  18. if (context.type === 'viewItem' && context.node instanceof PullRequestNode) {
  19. args = {
  20. ...args,
  21. pr: { url: context.node.pullRequest.url },
  22. clipboard: context.command === Commands.CopyRemotePullRequestUrl,
  23. };
  24. }
  25. return this.execute(args);
  26. }
  27. async execute(args?: OpenPullRequestOnRemoteCommandArgs) {
  28. if (args?.pr == null) {
  29. if (args?.repoPath == null || args?.ref == null) return;
  30. const remote = await Container.git.getRichRemoteProvider(args.repoPath);
  31. if (remote?.provider == null) return;
  32. const pr = await Container.git.getPullRequestForCommit(args.ref, remote.provider);
  33. if (pr == null) return;
  34. args = { ...args };
  35. args.pr = pr;
  36. }
  37. if (args.clipboard) {
  38. void (await env.clipboard.writeText(args.pr.url));
  39. } else {
  40. void env.openExternal(Uri.parse(args.pr.url));
  41. }
  42. }
  43. }