Slack Notification for Bitbucket Forgotten Pull Requests

Onur Taşhan
Trendyol Tech
Published in
4 min readJun 16, 2020
https://3kllhk1ibq34qk6sp3bhtox1-wpengine.netdna-ssl.com/wp-content/uploads/bitbucket411-blog-1200x-branches2.png

In this article, I will talk about the python code we wrote for Pull Requests that remained open and forgotten.

In the development teams, as the business and the team grow, the developed projects and their Code Repos are starting to increase. Especially if you use version control tools such as git; In these repos, branches are created for each code developed. Then, pull requests are opened and merged to Main Branch.

Our Repo’s Criteria

In fact, the definition of Forgotten Pull Request also appears here. I will explain the issue on my own team (Trendyol Data Warehouse).

First of all, we use Bitbucket as a git tool and we have a project called DWH. Under this project, a git repo is opened for each small executable project and the following features are defined by default in these repo;

  • A notification message is sent to the Slack channel for each PR created.
  • Each PR should get at least one reviewer approval.
  • Merge cannot be made to Main Branch without PR.
Forgotten Pull Request (PR)

So how do the forgotten PRs exist in these environment?

  1. Some PRs are being created in such repos that may contain changes that come up once a year, even monthly. Review or merge can be skipped.
  2. PR changes frequently or opens in the main code repo and needs to be taken urgently. Review or merge can be skipped.
  3. There is no need to merge the opened PR at that very moment, but it should definitely be merged before a new change comes in the future. Review or merge can be skipped.

In the three cases above, because of the busyness of the team or unintentionally, PR review or merge cannot be made.

This leads us to opinion of; what if there is a Slack Bot and checks all our repos with the information of how long their PRs are open, who is the reviewer, and throws us a notification with these packed. In this way, on dozens of repos; we decider that there is no need a manuel work to see if there are open PRs left. On top of that, we wrote the python code below.

:)

Let me explain the code step by step;

Step #1;

  • Let’s go to https://api.slack.com/messaging/webhooks and follow the steps to create a Slack APP and get our Webhook URL. Let’s put this URL in the section that says {YOUR_WEBHOOK_URL}.
  • When we call the api_call function, our Slack APP will send the msg variable it received as input to our slack channel using the Webhook URL above.

Step #2;

  1. Bitbucket Domain URL should be written in {YOUR_BITBUCKET_DOMAIN}.
  2. The name of the project in Bitbucket should be written in {PROJECT_NAME}.
  3. The access token you get from this link should be written in {YOUR_ACCESS_TOKEN}.
  • In this section, using the Bitbucket Rest API, it throws the repo in the given project into the repo_list list.

Step #3;

  • In this section, we reuse Bitbucket’s Rest API and we receive pull-requests from each repo according to the repo_list list we have. If there is a pull-request; for each pull-request;

    # Reviewer
    # Day and hour after PR occurred

    We take the information and append it to the msg variable in the format in the code.

Step #4;

  • We write the message on the console screen.

Step #5;

If the code runs between 9 and 18 hours, we call the api_call function to send the msg variable we created to the Slack channel.

And after the code runs, its output is similar to the below. :)

PR URLs are censored :)

You can reach the project in Github from this link.

See you. :)

--

--