Alert This post is over a year old, some of this information may be out of date.

Adding or deleting GitHub project (classic) labels on issues

important

This approach makes use of GitHub classic projects and are being discontinued. The approach will not work for new Projects as they require you to use the webhook functionality in order to create Project Item triggers.

Since projects got added to GitHub, I started using it more and more. For Front Matter, I am using projects to manage its releases. Before projects, I used milestones, but with projects, it is easier to follow up on what things I still need to do before the release.

Show image GitHub Project example from Front Matter
GitHub Project example from Front Matter

One thing I was missing was a sort of label in the issue list to easily spot which issues were already added to a project release.

To enhance my experience on GitHub, I went on the journey to find out if it was possible with GitHub Actions.

Getting the event type and project data

To label the issue, what I needed to know were two things:

  1. The event type. Was the issue added to or deleted from the project;
  2. The name of the project.

The event type can be retrieved from the github.event.action environment variable. In the case for my actions, I configured it to trigger on project_card with the [created, moved, deleted] types.

The project name is a bit trickier as it is not passed as an environment variable. You will have to do an API call to get the project name. The API is provided by the github.event.project_card.project_url environment variable. In my case, the variable its value is: https://api.github.com/projects/14468471.

Via a CURL command execution, you can receive this information.

1
2
3
4
5
- name: Fetch project data
  run: |
    echo 'PROJECT_DATA<<EOF' >> $GITHUB_ENV
    curl --request GET --url '${{ github.event.project_card.project_url }}' --header 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' >> $GITHUB_ENV
    echo 'EOF' >> $GITHUB_ENV    
Info

The notation you can see here is to store the CURL response in an environment variable called PROJECT_DATA. You can find more information about setting environment variables in the GitHub documentation.

Once you have the name, all you need is to use the Simple Issue Labeler action to add or remove your project labels.

Here you can see the whole GitHub workflow:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
name: Project labelling

on:
  project_card:
    types: [created, moved, deleted]

jobs:
  automate-issues-labels:
    runs-on: ubuntu-latest
    steps:
      - name: Fetch project data
        run: |
          echo 'PROJECT_DATA<<EOF' >> $GITHUB_ENV
          curl --request GET --url '${{ github.event.project_card.project_url }}' --header 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' >> $GITHUB_ENV
          echo 'EOF' >> $GITHUB_ENV          

      - name: Add the project label
        uses: andymckay/labeler@master
        if: ${{ contains(github.event.action, 'created') || contains(github.event.action, 'moved') }}
        with:
          add-labels: "Project: ${{ fromJSON(env.PROJECT_DATA).name }}"

      - name: Remove the project label
        uses: andymckay/labeler@master
        if: ${{ contains(github.event.action, 'deleted') }}
        with:
          remove-labels: "Project: ${{ fromJSON(env.PROJECT_DATA).name }}"

When you add or remove your issues to a project, the labels will now be automatically added or removed.

Show image Project labelling for issues
Project labelling for issues

Comments

Back to top