Using bash variable in Github Action expression

What I’m trying to do is actually see if an array of label names is one of the labels in a PR. I know that I can get an array of the labels in the PR using github.event.pull_request.labels.*.name. In one of my matrix, I have an array of required labels. For example:

matrix:
  required-labels: [ 'label1', 'label2' ]

Some approaches, using bash I thought of were iterating both arrays or trying to use contains.

I’m having problem converting github.event.pull_request.labels.*.name to bash array. The problem here seems to be it converts to something that looks like

[
  "some-label"
]

Which causes problems like some-label being trying to be executed as a command.

I thought I could use contains, but it doesn’t seem like I can use replacement in bash.

For example something like the following

contains(matrix.required-labels, $label)

This $label doesn’t work. Is there any mechanism to do this at all?

Or any suggestions on how I can convert github.event.pull_request.labels.*.name to a bash array?

Thanks!

There is no built-in. You could use the toJSON() function to get a JSON (not shell!) array, or read the JSON event data from github.event, and then convert the data to a shell structure. Note that when converting from JSON to shell you need to make sure to handle characters that have special meanings to the shell (e.g. quotes and semicolons).

Personally I’d use a language that can directly load JSON data from the github.event file into native data structures (like Python), which avoids the shell limitations. :slightly_smiling_face:

Great thanks for the info. I eventually just used this action.