Get PR repository name

Here - https://github.com/root-project/cling/actions/runs/341307211/workflow#L113 I use the GITHUB_REPOSITORY environment variable to get the name of the repository that a PR is made to or a commit is pushed. Instead, I want to get the name of the repository that the PR is coming from. If it is a fork, then the name of the fork and if it is from a branch of the main repository, then the name of the main repository.
Is an environment variable available that would give me this information?

1 Like

You can do e.g.

- env:
    REPO: ${{ github.event.pull_request.head.repo.full_name }}
  run: |
    echo "$REPO"
  if: github.event_name == 'pull_request'

REPO will be like other-user/repo-name.

2 Likes

Thanks! I would ideally like to be able to use the same variable for both $GITHUB_REPOSITORY and github.event.pull_request.head.repo.full_name if possible.

Is something like -

- env:
    if [ ${{ github.event_name }} == 'pull_request' ]; then
        REPO: ${{ github.event.pull_request.head.repo.full_name }}
    else
        REPO: $GITHUB_REPOSITORY
   run: |
        xyz
        abcd
   shell: bash

possible?

Almost, you’d have to either write REPO to $GITHUB_ENV in one step and use it in another, or for just one step set REPO right within the run part.

Version with multiple steps:

- run: |
    if [ ${{ github.event_name }} = 'pull_request' ]; then
        echo "REPO=${{ github.event.pull_request.head.repo.full_name }}" >>$GITHUB_ENV
    else
        echo "REPO=$GITHUB_REPOSITORY" >>$GITHUB_ENV
    fi
- run: echo "REPO is ${REPO}"

Version with one step (REPO is available only within that step!):

- run: |
    if [ ${{ github.event_name }} = 'pull_request' ]; then
        REPO="${{ github.event.pull_request.head.repo.full_name }}"
    else
        REPO="$GITHUB_REPOSITORY"
    fi
    echo "REPO is ${REPO}"
1 Like

Seems unnecessary complicated, I would try this:

REPO="${{ github.event.pull_request.head.repo.full_name || github.repository }}"

I have a followup question - how would I do this in Windows cmd?

IF (${{ github.event_name }}=="pull_request") (set REPO="${{ github.event.pull_request.head.repo.full_name }}") else (set REPO="%GITHUB_REPOSITORY%")

doesn’t seem to work.

set REPO=${{ github.event.pull_request.head.repo.full_name || github.repository }}
echo %REPO%

or

IF "${{ github.event_name }}"=="pull_request" (
  set REPO=${{ github.event.pull_request.head.repo.full_name }}
) ELSE (
  set REPO=%GITHUB_REPOSITORY%
)
echo %REPO%

There are apparently a few things to watch out for in batch scripts:

  • Don’t wrap the if condition in parentheses. This seems to make the condition fail!
  • Use double quotes on both sides of a comparison for equality:
    • IF a==a (echo a) ELSE (echo b) → a
    • IF a=="a" (echo a) ELSE (echo b) → b
    • IF "a"=="a" (echo a) ELSE (echo b) → a
  • Don’t use quote marks for set unless you actually want them in the string. There is no need to wrap strings in quotes that have spaces in them in case of set.
  • Place a space between ELSE and ( or it’s a syntax error

The log says:

shell: C:\Program Files\PowerShell\7\pwsh.EXE -command “. ‘{0}’”

Looks like you’re simply using the wrong shell type. The default for Windows runners is pwsh. You should explicitly set shell: cmd if you want to run batch code.

The same in PowerShell would be:

if ('${{ github.event_name }}' -eq 'pull_request') {
  $REPO = '${{ github.event.pull_request.head.repo.full_name }}'
} else {
  $REPO = $Env:GITHUB_REPOSITORY
}
echo $REPO