Customizing slack notification for Github pull requests
Asked Answered
V

3

19

I have a use case wherein every time a pull request is raised against the develop branch, a notification needs to be sent to slack channel with PR link and possibly info on which user opened it.

I'm confused as to which path to take

  1. Github actions which posts to slack
  2. Github app for slack(https://github.com/integrations/slack#configuration)

After going through https://github.com/integrations/slack#configuration, I see the option to subscribe solely to pull requests with /github subscribe org/repo pulls, but this does not allow customizations(e.g. only notify for PRs to develop, not for every PR)

Github actions has ways to specify to only run when pull request is raised for develop, but since it is repository specific, I'll have to push the workflow file to 100+ repositories

Is there a better solution/automation to post to slack when a PR is raised(not merged) to a specific branch and customizable(like adding user info who opened the PR)?

Valora answered 5/4, 2021 at 6:9 Comment(2)
The github actions approach sounds good to me. You can share a workflow template within your organization. This way, you don't need to add the workflow in all your repositories. docs.github.com/en/actions/learn-github-actions/…Southdown
I’d do that with GitHub Actions myself. Had good results with this one, that supports some customization: github.com/act10ns/slackDelois
E
2

Since you need the notification content to be customizable, I would go with notification action + slack workflow (if your workspace has access to those).

Create a notification workflow

  1. Use the Workflow Builder to create a new workflow
  2. Choose From a webhook as the workflow trigger and define data variables. These are the expected fields that your GitHub action will post to the webhook URL, e.g. title, author, pullRequestLink etc. See Opened PR event payload for a list of fields available during this workflow event.
  3. Choose Send a message as the workflow action and define the message template. The interface lets you use the variables defined in the previous step as placeholders.

Set up a GitHub workflow to call the webhook

Use a GitHub action to post a structured payload to the workflow URL. The workflow can look something like this:

name: PR creation event notification

on:
  pull_request:
    - opened
    - reopened

jobs:
  notify:
    name: PR notification
    runs-on: [ubuntu-latest]
    steps:
      - name: Post message
        uses: slackapi/[email protected]
        env:
          SLACK_WEBHOOK_URL: the webhook URL, preferably a GitHub secret
        with:
          payload: |
            {
              "title": ${{ toJson(github.event.pull_request.title) }},
              "author": ${{ toJson(github.event.pull_request.user.login) }},
              "link": ${{ toJson(github.event.pull_request.html_url) }}
            }

Using toJson() ensures that special characters are escaped properly (the payload argument must be a valid JSON).

Reusing the workflow

This GHA feature wasn't available at the time the original question was asked.

If you expect to use the workflow in a large number of repositories, it's possible to make it reusable (to simplify maintenance). In that case, the generic workflow would have a workflow_call trigger:

name: Reusable Slack notification workflow

on:
  workflow_call:
    inputs:
      workflow-event:
        required: true
        type: string
    secrets:
      slackWebhookUrl:
        required: true

jobs:
  notify:
    name: PR notification
    runs-on: [ubuntu-latest]
    steps:
      - name: Post message
        uses: slackapi/[email protected]
        env:
          SLACK_WEBHOOK_URL: the webhook URL, preferably a GitHub secret
        with:
          payload: |
            {
              "title": ${{ toJson(fromJson(inputs.workflow-event).title) }},
              "author": ${{ toJson(fromJson(inputs.workflow-event).user.login) }},
              "link": ${{ toJson(fromJson(inputs.workflow-event).html_url) }}
            }

Let's assume this file is located at /.github/workflows/.generic-slack-notification.yml in your-reusable-workflow-repo repository within your-org organization.

Notice that instead of referencing github.event.pull_request (which is not available, since the reusable workflow is triggered by a different event), it now uses fromJson(inputs.workflow-event) to parse the data from its input. The input is provided by the individual "caller workflows" from your repositories like this:

name: PR creation event notification

on:
  pull_request:
    - opened
    - reopened

jobs:
  notify:
    name: PR notification
    runs-on: [ubuntu-latest]
    steps:
      - name: Post message
        uses: your-org/your-reusable-workflow-repo/.github/workflows/.generic-slack-notification.yml@main
    with:
      workflow-event: ${{ toJson(github.event.pull_request) }}
    secrets:
      slackWebhookUrl: ${{ secrets.SLACK_DEPLOYMENT_WEBHOOK_URL }}
Endogen answered 29/1 at 10:30 Comment(0)
F
0

You can create a webhook in GitHub and point it to Microsoft's Power Automate or IFTTT or a similar tool. Then you transform and forward the payload to slack's API endpoint. I've forwarded to an Azure Function in the past to allow me to use a programming language of choice (node, C#, PowerShell, Python, you name it) to forward the event.

This gives you total control over the message contents and will allow you to do any filtering on repository, branch or otherwise when forwarding the message.

You will however have to do some configuration yourself in the tool(s) you choose to use as a forwarder.

There are also a few commercial solutions that will offer this type of integration. Axolo provides such functionality.

With some limitations you may be able to use Required Workflows to register a workflow with all your repositories at once. This will only work for PR triggers (which seems to fit your bill) and only on the default branch (which might be enough for you).

With callable workflows and composite actions you'd be able to minimize the amount of workflow you'd have to replicate to all repositories, but would retain control over the exact triggers you'd want in each individual repositories.

Favela answered 28/2, 2023 at 14:25 Comment(5)
Notice that required workflows are now part of repository rules (Require workflows to pass before merging), and only available for GitHub Enterprise.Winthrop
And public repositories.Favela
I don't think they're available for anything other than Enterprise: i.imgur.com/lH3v2y5.pngWinthrop
I yeah, I meant Rulesets. Not the specific Required Workflows.Favela
Ah, yes, repo-level rulesets are available in public repos, but required workflows exist only as org-level rules, and the other rules don't help with the issue at hand.Winthrop
G
0

i have one more idea, but you also need to touch the settings in all of your 100+ repos.

idea: setup a webhook in the github repo settings to only trigger the webhook on pr‘s and set target address to a endpoint on the slack api. and on the slack side filter the pr‘s to target branch develop only and modify the mesage string to your needs.

Gluteal answered 23/11, 2023 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.