<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Brian Canzanella</title>
    <description>The latest articles on Forem by Brian Canzanella (@bc).</description>
    <link>https://forem.com/bc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F131578%2Fd7eb4d57-c1d3-4c7d-aecf-57e420f051f0.jpeg</url>
      <title>Forem: Brian Canzanella</title>
      <link>https://forem.com/bc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bc"/>
    <language>en</language>
    <item>
      <title>How we extended GitLens’ Pull Request functionality in Visual Studio Code</title>
      <dc:creator>Brian Canzanella</dc:creator>
      <pubDate>Fri, 15 Jan 2021 15:23:53 +0000</pubDate>
      <link>https://forem.com/codestream/how-we-extended-gitlens-pull-request-functionality-in-visual-studio-code-42g8</link>
      <guid>https://forem.com/codestream/how-we-extended-gitlens-pull-request-functionality-in-visual-studio-code-42g8</guid>
      <description>&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens" rel="noopener noreferrer"&gt;GitLens&lt;/a&gt;, the incredible git-toolbelt extension for Visual Studio Code, recently added the ability for other VS Code extensions to tie into its user interface.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F799%2F1%2A4gmSNSe3dgKh7DVFWu9c3A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F799%2F1%2A4gmSNSe3dgKh7DVFWu9c3A.png" alt="We're looking at the GitLens UI. Click on the pull request icon to open this GitHub pull request with CodeStream"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Specifically, it added additional touchpoints around the ability to create and view pull requests. As a developer of an open source VS Code extension (&lt;a href="https://marketplace.visualstudio.com/items?itemName=CodeStream.codestream" rel="noopener noreferrer"&gt;CodeStream&lt;/a&gt;), this seemed like a great way to give out users additional choices with regard to how they create and view their pull requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F797%2F1%2ACK4QOS5617Vn8CBPvhbayw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F797%2F1%2ACK4QOS5617Vn8CBPvhbayw.png" alt="Another view in GitLens. Click the pull request icon to create a new GitHub pull request with CodeStream"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here comes the technical parts!&lt;/p&gt;

&lt;p&gt;At CodeStream, we use TypeScript, so you'll need a definition file for the GitLens api. I have this saved in @types/gitlens.d.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Disposable } from "vscode";

export { Disposable } from "vscode";

export interface CreatePullRequestActionContext {
    readonly type: "createPullRequest";
    readonly branch: {
        readonly name: string;
        readonly remote?: {
            readonly name: string;
            readonly provider?: string;
            readonly url?: string;
        };
        readonly repoPath: string;
    };
}

export interface OpenPullRequestActionContext {
    readonly type: "openPullRequest";
    readonly pullRequest: {
        readonly id: string;
        readonly provider: string;
        readonly repoPath: string;
        readonly url: string;
    };
}

export type ActionContext = CreatePullRequestActionContext | OpenPullRequestActionContext;
export type Action&amp;lt;T extends ActionContext&amp;gt; = T["type"];

export interface ActionRunner {
    readonly label: string;

    run(context: ActionContext): void | Promise&amp;lt;void&amp;gt;;
}

export interface GitLensApi {
    registerActionRunner&amp;lt;T extends ActionContext&amp;gt;(
        action: Action&amp;lt;T&amp;gt;,
        runner: ActionRunner
    ): Disposable;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside your extension's &lt;code&gt;activatefunction&lt;/code&gt; is where you can create this integration. Below, I've added a simplified version of what we finally went with. The main idea is to find either the stable version of GitLens or the insiders (aka the nightly version) and listen for "openPullRequest" and "createPullRequest" attaching using the GitLens api &lt;code&gt;registerActionProvider&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const gitLens = extensions.getExtension&amp;lt;Promise&amp;lt;GitLensApi&amp;gt;&amp;gt;("eamodio.gitlens") ||
extensions.getExtension&amp;lt;Promise&amp;lt;GitLensApi&amp;gt;&amp;gt;("eamodio.gitlens-insiders");
if (gitlens &amp;amp;&amp;amp; gitlens.isActive) {
 const api: GitLensApi = await gitlens.exports;
 api.registerActionRunner("openPullRequest", {
  label: "CodeStream",
  run: function(context: OpenPullRequestActionContext) {
   console.log(context, "it worked!")
  }
 });
 api.registerActionRunner("createPullRequest", {
  label: "CodeStream",
  run: function(context: CreatePullRequestActionContext) {
   console.log(context, "it worked")
  }
 }); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our implementation was a little more involved since we wanted to ensure that GitLens was actually active before attempting to register. There isn't a great way to truly know when another extension is active, so we used a polling mechanism. As noted in a comment in the code, there is an open issue with the VS Code team to address this.&lt;/p&gt;

&lt;p&gt;Our complete solution can be found in the &lt;a href="https://github.com/TeamCodeStream/codestream/commit/236b4843a0d48a96ae0ecfebbb524e127a68ac7d" rel="noopener noreferrer"&gt;CodeStream repo&lt;/a&gt; on GitHub or alternatively on &lt;a href="https://api.codestream.com/p/W75JY6zGmBhgt48D/HP9pe9TTRnOdG_7XJhusYA" rel="noopener noreferrer"&gt;CodeStream&lt;/a&gt; itself.&lt;/p&gt;

&lt;p&gt;Look out for this feature in the next version of &lt;a href="https://www.codestream.com/" rel="noopener noreferrer"&gt;CodeStream&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>vscode</category>
      <category>gitlens</category>
      <category>codestream</category>
      <category>git</category>
    </item>
  </channel>
</rss>
