Abstract: Using technology to solve the TEDIOUS TASK of PM approval PR, this article will explain how to automate the retrieval of pull requests for each repO to merge under the GitHub Organization and inform the relevant personnel instead of daily manual operations.

In your daily work, do you encounter the following scenarios:

  • Github has multiple REPOs, and daily work requires you to manually filter a large number of pull requests to merge
  • To find multiple REPOsready to reviewPull requests, manually filtered and pasted over and over again to submit dev for review # boring
  • GitHub Webhooks do not have this Event when they want to automatically push GitHub PRS to merge

Using technology to solve the TEDIOUS APPROVAL PR work of PM, this article will explain how to automate the retrieval of pull requests to be merged for each repO in the GitHub Organization and inform the relevant personnel instead of daily manual operations. This article mainly provides the idea to solve the automatic sending of APPROVAL PRS, and gives the implementation method of Python with the example of pin group and Slack. If you use other communication tools, the implementation principle is the same.

Configuring message reception

Configure the nail group robot

  1. The robot management page is displayed. Take the PC as an example, open the PIN on the PC and click “Group Settings” => “Intelligent Group Assistant” => “Add Robot”.

  1. Click “Add Robot” and select “Custom”

In this example, “Security Settings” uses a custom keyword, which must be included in the message sent to the robot.

  1. Click Finish to get the Webhook

Detailed configuration documentation of the pin Bot can be found in the official documentation: ding-doc.dingtalk.com/doc#/server…

Configuration Slack bot

  • Create an app (link: api.slack.com/apps), set the app Name, and select the target Slack Workspace
  • Select “Basic Information” in the left column => “Add Features and Functionality” choose “Bots”

  • Select “OAuth & Permissions” in the left column, and in “Scopes” click “Add an OAuth Scope” to Addchat:write.public
  • Click “Install App to Workspace”
  • Obtain the OAuth Access Token

For detailed Slack Bot configuration steps, see the official English document: slack.com/intl/en-cn/…

Configure Github to get Personal Access Tokens

Generates a Token and grants corresponding permissions. In this case, all Public and Private Repos under Organization are read, and repo needs to be checked.

Making detailed Token configuration steps to see the official document: help.github.com/en/github/a…

Code instructions

Obtain Github to merge PR

PyGithub provides access to the Github V3 API, which allows you to implement Github operations with code. PIP Install PyGithub is available for installation.

FILTER_TEMPLATE = "repo:{org}/{repo} is:pr is:open review:approved"

class GithubPrList:

    @property
    def gh(self):
        return self._gh

    @property
    def org(self):
        return self._org

    FILTER_TEMPLATE = "repo:{org}/{repo} is:pr is:open review:approved"

    def __init__(self,
                 org,
                 repo,
                 login_or_token,
                 password=None,
                 timeout=DEFAULT_CONNECT_TIMEOUT,
                 retry=None,
                 ):
        """:param org: string :param repo: string :param login_or_token: string, token or username :param password: string :param timeout: integer :param retry: int or urllib3.util.retry.Retry object """
        Instantiate access to Github API V3
        self._gh = Github(login_or_token=login_or_token,
                          password=password,
                          timeout=timeout,
                          retry=retry)
        self._org = org
        self._repo = repo

		def getIssues(self,
                   		 filter=None,
                       sort=DEFAULT_PR_SORT,
                       order=DEFAULT_ORDER,
                       ):
        """ :param filter: string :param order: string ('asc', 'desc') :param sort: string('comments', 'created', 'updated') :rtype :class:`List` of :class:`PrList2.PrElement` """
        if not filter:
        		/repo > pr > pr > pr > pr
            filter = self.FILTER_TEMPLATE.format(org=self._org,
                                                 repo=self._repo)
        # query
        issues = self._gh.search_issues(filter, sort, order)
        prList = []

        for issue in issues:
            prList.append(PrElement(issue.number, issue.title, issue.html_url))

        return prList
Copy the code

Function description:

  • __init__Support to instantiate to access the GitHub API V3 using username/ password or token.
  • On Github, pull Requests are also issues,getIssues()Function allows the user to use default conditions (repo:{org}/{repo} is:pr is:open review:approvedFind pull requests for the Approved org/repo status, PRS to merge. Among them:
Qualifier instructions
repo:org_/_repo Finds projects under the specified organization’s REPO
is:pr Find a pull requests
is:open Find open issues
review:approved If the review status is approved, review status may be a valuenone,required,approved,changes requested

You can also specify filter criteria for Github issues. An example is as follows:

filter = "repo:myOrg/myRepo is:pr is:open review:approved"
GithubPrList(self.org, 
							self.repo, 
              self.token).getIssues(filter)
Copy the code

More filtering condition, please see the official document: help.github.com/en/github/s…

Send a message

Send a pin message

DingtalkChatbot encapsulates the pin message type. This article uses this tool to send the PR to be merged to the pin group. DingtalkChatbot can be installed by PIP Install DingtalkChatbot.

from dingtalkchatbot.chatbot import DingtalkChatbot

webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxx"
atPerson = ["123xxx456"."123xxx678"] xiaoding = DingtalkChatbot(webhook) xiaoding. SendMsg ({custom keyword} +"Above PR list", atPerson)
Copy the code

Send the message to the spike group, which requires the Webhook and custom keywords of the spike group robot mentioned above.

Sending Slack Messages

Slackclient is an official API library developed by Slack, which can fetch and send messages from Slack channels. It supports Python 3.6 and later. You can install pip3 Install slackClient.

from slack import WebClient
from slack.errors import SlackApiError

client = WebClient(token={your_token})

try:
    response = client.chat_postMessage(
        channel='#{channel_name}',
        text="Hello world!")
    assert response["message"] ["text"] == {pr_list}
except SlackApiError as e:
    # You will get a SlackApiError if "ok" is False
    assert e.response["ok"] is False
    assert e.response["error"]  # str like 'invalid_auth', 'channel_not_found'
    print(f"Got an error: {e.response['error']}")
Copy the code

Replace {your_token} with the token configured above, replace {channel_name}, and send pr_list to the destination channel.

At this point, you’re done! Let’s see what happens

If there are any errors or omissions in this article, please go to GitHub: github.com/vesoft-inc/… Submit an issue to us in the Issue section or submit a suggestion under the suggestion feedback section of the official forum: discuss.nebula-graph.com.cn/ 👏; NebulaGraphbot to join the NebulaGraph Networking group, contact NebulaGraphbot, the NebulaGraph’s official assistant account

Hi, I’m Jude, THE Nebula Graph’s PM, and I welcome your requests. They may not always come to light, but we’ll be reviewing them carefully