<?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: Caterina</title>
    <description>The latest articles on Forem by Caterina (@ccurti).</description>
    <link>https://forem.com/ccurti</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%2F1159863%2F4d068e42-171d-44e4-a51e-eb86ccd4d13a.png</url>
      <title>Forem: Caterina</title>
      <link>https://forem.com/ccurti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ccurti"/>
    <language>en</language>
    <item>
      <title>Assign PRs randomly to a specific list of users in Bitbucket Cloud</title>
      <dc:creator>Caterina</dc:creator>
      <pubDate>Wed, 04 Oct 2023 05:06:26 +0000</pubDate>
      <link>https://forem.com/atlassian/assign-prs-randomly-to-a-specific-list-of-users-in-bitbucket-cloud-2l5m</link>
      <guid>https://forem.com/atlassian/assign-prs-randomly-to-a-specific-list-of-users-in-bitbucket-cloud-2l5m</guid>
      <description>&lt;p&gt;Every pull request needs a reviewer but selecting the right person is something many teams want to automate. After chatting with a few developers and Atlassian teams, I realized that different teams have different ideas about the logic of assigning a reviewer to a PR.&lt;/p&gt;

&lt;p&gt;This blog shows how I implemented one of the most popular options so that you can either use it for your team or take it as a starting point for building your team’s preferred logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Forge - Atlassian serverless app development platform
&lt;/h2&gt;

&lt;p&gt;The magic for assigning the reviewers to a PR is implemented using &lt;a href="https://developer.atlassian.com/platform/forge/"&gt;Forge&lt;/a&gt;, Atlassian serverless app development platform. Everybody with an Atlassian account can use Forge and you install the app right away on a workspace you administer.&lt;/p&gt;

&lt;p&gt;You can always &lt;a href="https://support.atlassian.com/bitbucket-cloud/docs/create-your-workspace/"&gt;create a new workspace&lt;/a&gt; for testing the app and then make it available on your team’s workspace at a later stage.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the app do?
&lt;/h2&gt;

&lt;p&gt;With this app installed, every time a pull request is created a user from a predefined list is randomly selected and added as the PR reviewer. A comment is also added mentioning so that it’s clear that the assignment has been performed by the app.&lt;/p&gt;

&lt;p&gt;Let's see it in action:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_-vtDNKr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://community.atlassian.com/t5/image/serverpage/image-id/283124iAEBC36E5748F1A9A/image-size/large%3Fv%3Dv2%26px%3D999" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_-vtDNKr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://community.atlassian.com/t5/image/serverpage/image-id/283124iAEBC36E5748F1A9A/image-size/large%3Fv%3Dv2%26px%3D999" alt="A PR being automatically assigned" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s break down the app into its main components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;fetchRepository&lt;/code&gt; function is used to load the current repository. This is required to get the name of the default branch.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;.reviewers&lt;/code&gt; file is loaded by the &lt;code&gt;fetchFile&lt;/code&gt; function and the list of possible &lt;code&gt;reviewers&lt;/code&gt; is saved in the reviewers variable. The version of the file is the one at the head of the default branch, whose name has been retrieved in the first step.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;getPRAuthor&lt;/code&gt; function retrieves the author of the PR&lt;/li&gt;
&lt;li&gt;the author of the PR is removed from the list of possible reviewers. This is because the author cannot be set as a reviewer.&lt;/li&gt;
&lt;li&gt;a random number is generated and used to select one of the possible reviewers&lt;/li&gt;
&lt;li&gt;a reviewer is assigned to the PR via the &lt;code&gt;assignPR&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;commentOnPR&lt;/code&gt; function adds a comment to the PR and the reviewer is mentioned&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The app is triggered by a PR creation event (&lt;code&gt;avi:bitbucket:created:pullrequest&lt;/code&gt;).&lt;/p&gt;
&lt;h2&gt;
  
  
  Let’s have a look at the source code
&lt;/h2&gt;

&lt;p&gt;The repository for this app with the instructions on how to install it is available here: &lt;a href="https://bitbucket.org/atlassian/forge-bitbucket-assign-pr-reviewer"&gt;https://bitbucket.org/atlassian/forge-bitbucket-assign-pr-reviewer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;index.js file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The app logic is defined in a single file called &lt;code&gt;index.js&lt;/code&gt; which is available in the &lt;code&gt;src&lt;/code&gt; folder, here you’ll find all the functions mentioned above.&lt;/p&gt;

&lt;p&gt;Because this app is triggered by a PR creation event, the main &lt;code&gt;prtrigger&lt;/code&gt; function has access to the &lt;code&gt;event&lt;/code&gt; and &lt;code&gt;context&lt;/code&gt; parameters which are used throughout the code to access the identifiers for the workspace (&lt;code&gt;event.workspace.uuid&lt;/code&gt;), the repository (&lt;code&gt;event.repository.uuid&lt;/code&gt;) and the pull request (&lt;code&gt;event.pullrequest.id&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;repository.mainbranch.name&lt;/code&gt; retrieved from the &lt;code&gt;fetchRepository&lt;/code&gt; function is used as the &lt;code&gt;commit&lt;/code&gt; path parameter when calling the &lt;code&gt;/2.0/repositories/${workspaceUuid}/${repoUuid}/src/${commit}/${path}&lt;/code&gt;. This is how we retrieve the most recent version of the file on the main branch.&lt;/p&gt;

&lt;p&gt;Fetching the file, the PR author, assigning the PR and adding a comment are operations supported by the &lt;a href="https://developer.atlassian.com/cloud/bitbucket/rest"&gt;Bitbucket REST APIs&lt;/a&gt; called using the &lt;a href="https://developer.atlassian.com/platform/forge/runtime-reference/product-fetch-api/#requestbitbucket"&gt;requestBitbucketForge&lt;/a&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;manifest.yml file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each Forge app comes with a &lt;code&gt;manifest.yml&lt;/code&gt; file that describes it. &lt;/p&gt;

&lt;p&gt;For this app, it contains the logic to trigger the &lt;code&gt;index.prtrigger&lt;/code&gt; (the &lt;code&gt;prtrigger&lt;/code&gt; function in the &lt;code&gt;index.js&lt;/code&gt; file) every time a new PR is created. This is done using the &lt;code&gt;avi:bitbucket:created:pullrequest&lt;/code&gt; event trigger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  trigger:
    - key: pullrequest-created-event
      function: main
      events:
        - avi:bitbucket:created:pullrequest
  function:
    - key: main
      handler: index.prtrigger
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The manifest file also contains all the required &lt;a href="https://developer.atlassian.com/cloud/bitbucket/rest/intro/#forge-app-scopes"&gt;scopes&lt;/a&gt; for the app to run the REST API endpoints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;permissions:
  scopes:
    - read:pullrequest:bitbucket
    - write:pullrequest:bitbucket
    - read:repository:bitbucket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;read:repository:bitbucket&lt;/code&gt; is required for the &lt;a href="https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-get"&gt;GET Get a repository&lt;/a&gt; and the &lt;a href="https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#api-repositories-workspace-repo-slug-src-commit-path-get"&gt;GET Get file or directory&lt;/a&gt; contents endpoint.&lt;/p&gt;

&lt;p&gt;For reading the PR author of a PR via the &lt;a href="https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-pull-request-id-get"&gt;GET Get a pull request&lt;/a&gt; endpoint and adding a comment using the &lt;a href="https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-pull-request-id-comments-post"&gt;POST Create a comment on a pull request&lt;/a&gt; endpoint, the &lt;code&gt;read:pullrequest:bitbucket&lt;/code&gt; is needed. &lt;/p&gt;

&lt;p&gt;While this app doesn’t add any extension points to the UI, Forge for Bitbucket provides a multitude of &lt;a href="https://developer.atlassian.com/platform/forge/manifest-reference/modules/index-bitbucket"&gt;UI modules&lt;/a&gt; that can be used to show your app information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;If you too, like many others, have your own preferred way to select users to review a pull request, you can build your own Forge app to do that. This page walks you through a few implementation details of an app and feel free to tweak it, change it, build on it - whatever you need to make it work for you. &lt;/p&gt;

&lt;p&gt;Check the &lt;a href="https://bitbucket.org/atlassian/forge-bitbucket-assign-pr-reviewer"&gt;repository&lt;/a&gt; for this app and install it on your workspace by following the instructions in the README.&lt;/p&gt;

&lt;p&gt;We can’t wait to see what amazing alternatives you can come up with! Let us know in the comments!&lt;/p&gt;

&lt;p&gt;If you need any help, reach out to our &lt;a href="https://community.atlassian.com/t5/forums/postpage/board-id/bitbucket-questions?add-tags=forge"&gt;community&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>devops</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
