GitHub Workflows for Newbies: Add Labels and Comments to Pull Requests
Tags: todayilearned, github
Published: 2020-08-23 22:12:22

I manage a CSS community website called [Style Stage](https://stylestage.dev) 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](https://github.com/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](https://github.com/GeorgianaElena/.github/blob/dc3f3b0d8dc46afc6b5f93b3e4b41d576e40d458/.github/workflows/request_info.yml) which was pretty dang close to what I wanted to do!

Here is a shortened and more generic version:

```yml
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']
                })
              }
            }
```
> **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](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target) 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](https://stylestage.dev) 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](https://css-tricks.com/building-a-community-driven-site-with-eleventy-preparing-for-contributions/).