0

I want to streamline the following GitHub workflow by removing the if statements and removing the redundant code.

As you can see, it's largely the same, I need to create a environment variable which is set to either issue or pull_request based on the value of ${{ github.event_name }}

name: my-workflow
run-name: my-workflow
on:
  issues:
    types: [opened, edited, closed]
  pull_request:
    types: [opened, edited, closed]
jobs:
  issue-push:
    if: github.event_name == 'issues'
    runs-on: "ubuntu-latest"
    steps:
      - run: |
          # Do something here with ${{ github.event.issue.html_url }}

  pull-request-push:
    if: github.event_name == 'pull_request'
    runs-on: "ubuntu-latest"
    steps:
      -  run: |
          # Do something here with ${{ github.event.pull_request.html_url }}

In other words, I'm trying to achieve this:

name: my-workflow
run-name: my-workflow
on:
  issues:
    types: [opened, edited, closed]
  pull_request:
    types: [opened, edited, closed]
jobs:
  issue-push:
    runs-on: "ubuntu-latest"
    steps:
      - env:
          # Pseudocode
          TRIGGER_TYPE: "(if github.event_name == "issues" then 'issue') else 'pull_request'"
      - run: |
          # Do something here with ${{ github.event.$TRIGGER_TYPE.html_url }}
A. Gardner
  • 571
  • 2
  • 7
  • 17
  • See https://stackoverflow.com/a/75904266/7670262. – Azeem Apr 28 '23 at 07:14
  • `TRIGGER_TYPE: ${{ (github.event_name == 'issues') && 'issue' || 'pull_request' }}` – Azeem Apr 28 '23 at 07:15
  • Thanks, now trying to use it but my workflow fails: run: echo "Event: ${{ github.event.$TRIGGER_TYPE.number }}" – A. Gardner Apr 28 '23 at 07:46
  • Try: `run: echo "$TRIGGER_TYPE"` – Azeem Apr 28 '23 at 07:50
  • Yes, I get that and it works but I'm trying to dynamically build a string which should result in either: `${{ github.event.issue.html_url }}` or `${{ github.event.pull_request.html_url }}` those are valid expressions which are then evaluated. GitHub doesn't seem to like nesting: `${{ github.event.$TRIGGER_TYPE.html_url }}` – A. Gardner Apr 28 '23 at 07:52
  • This `run: echo "Event: ${{ github.event.$TRIGGER_TYPE.number }}"` won't work. It has a YAML syntax error. And, it should fail with that `$TRIGGER_TYPE` in there as it won't be evaluated until shell is invoked. – Azeem Apr 28 '23 at 07:55
  • BTW, why do you need it? What is your complete use case? – Azeem Apr 28 '23 at 07:59
  • Try [this](https://rhysd.github.io/actionlint/#eJx1j8GqwjAQRff5iosIUbDlrQPu/AT3pZXRVNKkdma6afvvr/U9iyCuwuSeOZeJZUMOZ2IxJkWHVtkbc08VOwPI/L+8QKeRswXQSqNoFkp57iwZC7X8hwEZ4lN5SuDUkPg63v4joL463GrxWuXUU5RiYXE8wtbMShbj+CVvNYSio4fOrXb1UezdOgBemlBoFxy2w4Br6ppSdvZdmA8/U/7C7OGzbI9pWo3z0Q7jWwFdfMJm+xJszC8HqmEh). See if it works. Not tested. – Azeem Apr 28 '23 at 08:09
  • 1
    The linked code is closed (thanks!) but it just literally echoes out `github.event.issues.html_url`. Usecase: Whether I open an issue or a PR I want to grab the URL and curl -X POST it. Having two jobs (one for issues and one for PRs) works, but it feels messy so I'm trying to streamline into a single job (hence dynamically changing `issue` to `pull_request`. It's a shame that GitHub mixes singular and plural. For `pull_request` this isn't an issue as I can use `github.event_name` but for `issues`, the property is `issue` (singular) – A. Gardner Apr 29 '23 at 10:43
  • Right. Looks like there's no operator/function available to do that kind of manipulation. However, here's an example with `jq` that you can use, see it [here](https://rhysd.github.io/actionlint/#eJxNjk+PgjAQxe/9FO9gHE0WsucGj8a4B03YvRMw3RVTWmmnXoDvvhTQeJnMmz/v/ayRAqi9D8rH7h60LpxqR8lSiJutpvGljhVwwfjEGolQBcMh0SWPh2LaeVZ3P58BCUzZKIkrN7oITi9jQJmHfAngJz8eDvtcYtV12PzVfA1Vqh7KcBENsNuBZjraYr1eBKHvQe+shGF42Y6UEv1byq1F4kAx49e6puQNzSFp9zmkT0b6iHTpgrQdHQlZlk1vbL++z6cFcFqJf6eJXEs=). – Azeem Apr 29 '23 at 11:47

1 Answers1

0

Here's a possible solution with format and toJSON functions along with jq:

- name: Get html_url
  env:
    TRIGGER: ${{ (github.event_name == 'issues') && 'issue' || 'pull_request' }}
  run: |
    jq -r '${{ format('.event.{0}.html_url', env.TRIGGER) }}' <<< '${{ toJSON(github) }}'
Azeem
  • 11,148
  • 4
  • 27
  • 40