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

5t3ph profile image Stephanie Eckles Updated on ใƒป3 min read

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
    types: [opened]

    runs-on: ubuntu-latest
      - uses: actions/github-script@v3
          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
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: msg

              # Add 'new submission' label
                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) {
                  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 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.

Posted on by:

5t3ph profile

Stephanie Eckles


(she/her) โœ๏ธ ModernCSS.dev, ๐Ÿ‘ฉ๐Ÿผโ€๐ŸŽจ StyleStage.dev, ๐Ÿ‘ฉโ€๐Ÿ’ป Lead design system dev, ๐Ÿ‘ฉโ€๐Ÿซ @eggheadio instructor, ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ง mom


markdown guide

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