Get Pull Request number when building its merge commit

The Pull Request number environment variable is “false” when the build is merging a Pull Request. Is there any way to retrieve it or it is only available when the current job is a pull request ?

Googling “github api merge commit get pull request number” shows that it’s possible to retrieve this information with Github v4 API:

In “More options->Requests” in Travis UI (link for your test project), you can see what kinds of requests Github sends to Travis. Then, Event Types & Payloads | GitHub Developer Guide documents what information is available in the payload of each event’s type.
This is the entire information that Travis can potentially expose to a build.

The solution I found:

  1. Retrieve all Pull Requests via the GitHub API.
  2. Filter the needed Pull Request using the environment variable TRAVIS_COMMIT and the ‘merge_commit_sha’ property of the Pull Request. (Those two should be equal for the correct Pull Request).

Note that this will become increasingly taxing and eventually run afoul of Github API limits as the number of pull requests grows.