Checking for labels in Github Actions conditions

Is there a way to test the presence of a label in a GitHub Action?

I have a GitHub Action setup like this:

name: My Action

on:
  pull_request:
    types:
      - closed

jobs:
  myjob:
    runs-on: ubuntu-latest
    name: Test
    if: github.event.pull_request.merged && XXX

From the docs, contains( github.event.pull_request.labels, 'my_label')doesn’t seem to work since github.event.pull_request.labels is a dictionary. Is there any way to accomplish what I’m trying to do?

Yes, you can use the contains function along with the map function to check for the presence of a label in a GitHub Action. Here’s what your if statement would look like:

if: github.event.pull_request.merged && contains(map(github.event.pull_request.labels, 'name'), 'my_label')

This maps the name attribute of each label in github.event.pull_request.labels to a new array, and then checks if that new array contains the label name 'my_label'.