DEV Community

Stephanie Eckles
Stephanie Eckles

Posted on • Edited on

GitHub Workflows for Newbies: Add Labels and Comments to Pull Requests

I manage a CSS community website called Style Stage which accepts PRs from contributors to provide new stylesheets for the site showcase.

Today I learned how to create a simple GitHub workflow to do the following:

  • add a comment thanking contributors for participating and providing some follow-up details about the process
  • label it with "stylesheet submission"
  • label it with "invalid" if it appears they did not check off all list items provided in the PR template

At first, it seemed this was going to be kind of complicated and involve creating multiple files, until I stumbled upon actions/github-script:

This action makes it easy to quickly write a script in your workflow that uses the GitHub API and the workflow run context.

The README lists several practical examples, including how to add a comment. But it was missing how to check the pull request description.

A handy trick to find out other ways to use something you're exploring is to pick a "key phrase" and search for that across GitHub.

Searching for github.issues.createComment led me to this example by GeorgianaElena which was pretty dang close to what I wanted to do!

Here is a shortened and more generic version:

name: Pull Request Messenger
on:
  pull_request_target:
    types: [opened]

jobs:
  comment:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            # Create comment body as Markdown

            var msg = `πŸ‘‹ Thanks for participating! I will review as soon as possible, usually within a few hours.

            **Watch for notifications** as I may request some small changes to make sure this meets the guidelines.`

            # Get pull request description
            var body = context.payload.pull_request.body

            # If `body` exists, check for conditions
            # related to being from a contributor
            if(body) {
              var isContribution = body.contains('New Submission')
              var incompleteGuidelines = body.contains('[]')
            }

            if(isContribution) {
              # Create comment
              github.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: msg
              })

              # Add 'new submission' label
              github.issues.addLabels({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                labels: ['new submission']
              })

              # If missed checking guidelines,
              # add 'invalid' label
              if(incompleteGuidelines) {
                github.issues.addLabels({
                  issue_number: context.issue.number,
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  labels: ['invalid']
                })
              }
            }
Enter fullscreen mode Exit fullscreen mode

Updated when I learned that setting it just "on" pull_request didn't work for forks by non-contributors. Updating this to pull_request_target allows the workflow to run on forks. Read more about it in the docs which specifically mentions this change is needed for adding comments or labels.

This should be placed in your project under .github/workflows/chose-a-name.yml

I'm sure there's a slicker way to test, but I simply did a follow-up PR that met the conditions to verify that it worked.

So - if you happen to contribute to Style Stage you will now be greeted by this new workflow bot 😊

PS - if you are interested in creating a community-driven site, you may enjoy my recent series on CSS-Tricks: Building a Community Driven Site with Eleventy.

Top comments (2)

Collapse
 
dailydevtips1 profile image
Chris Bongers

Hi Stephanie, Wow that's amaizng, these workflows are so cool!
Want to see more of this.

Collapse
 
ben profile image
Ben Halpern

Great post Stephanie