Home > front end >  how to set GitHub action to comment on new PR?
how to set GitHub action to comment on new PR?

Time:10-06

I want to integrate github-action-comment-pull-request from the official GitHub marketplace.

This will be my yaml:

on: [pull_request]

jobs:
    build:
        name: Comment a pull_request
        runs-on: ubuntu-latest
        steps:
            - name: Checkout
              uses: actions/checkout@v2

            - name: Comment a pull_request
              uses: mb2dev/[email protected]
              with:
                message: "Hello, Thank you for this PR!"
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

My problem is that I want the message to be shown for all new PRs except PRs raised by specific users: userA, userB. Preferably also to exclude users for a specific GitHub team.

Is this possible?

CodePudding user response:

If you want this workflow to run the job for everyone except for specific users, an option could be to add an if condition to your job to run only if the github.actor from the Github context isn't among a list of users you set.

Example with your workflow:


on: [pull_request]

jobs:
    build:
        name: Comment a pull_request
        runs-on: ubuntu-latest
        if: ${{ github.actor != 'Slava' }}
        steps:
            - name: Checkout
              uses: actions/checkout@v2

            - name: Comment a pull_request
              uses: mb2dev/[email protected]
              with:
                message: "Hello, Thank you for this PR!"
                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

This also work for many users if you add && or || inside the condition.

For example, I use this workflow with some of my repositories to add PR comments:

name: PR Comment

on:
  pull_request_target:
    types: 
      - opened

jobs:
  PR-Comment:
    if: github.actor != 'user1' && github.actor != 'user2' && github.actor != 'user3' && github.actor != 'user4' && github.actor != 'user5'  
    runs-on: ubuntu-latest
    steps:
    - name: PR Comment
      uses: actions/github-script@v2
      with:
        github-token: ${{secrets.GITHUB_TOKEN}}
        script: |
          github.issues.createComment({
            issue_number: ${{ github.event.number }},
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: ':warning: Have you followed the [contributions guidance](<contribution_guidance_url>)? Content PRs should generally be made against the the [source repo](https://github.com/<owner>/<repo>).'
          })

A full workflow example can be found in this repo

I don't think it's possible to use or inform the Github Team directly with a condition yet.

  • Related