# Get Pull Request number when building its merge commit

**URL:** <https://travis-ci.community/t/get-pull-request-number-when-building-its-merge-commit/4440>\
**Category:** Travis CI Discussions & Feedback\
**Tags:** travis-build, env-vars\
**Created:** [July 29, 2019, 7:30am UTC](https://travis-ci.community/t/get-pull-request-number-when-building-its-merge-commit/4440 "2019-07-29T07:30:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimeonGerginov](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/simeongerginov/32/6141_2.png) [@SimeonGerginov](https://travis-ci.community/u/SimeonGerginov)\
**Post date:** [July 29, 2019, 7:30am UTC](https://travis-ci.community/t/get-pull-request-number-when-building-its-merge-commit/4440/1 "2019-07-29T07:30:33Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 29, 2019, 7:56am UTC](https://travis-ci.community/t/get-pull-request-number-when-building-its-merge-commit/4440/2 "2019-07-29T07:56:26Z")

</div>

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

> **[Get pull request associated with a commit](https://github.community/t5/GitHub-API-Development-and/Get-pull-request-associated-with-a-commit/m-p/20305/highlight/true#M1100)**
>
> I was wondering if it’s possible to get a pull request associated with a commit. With a merge-commit or squash-merging I can infer the associated pull request from the commit message, but in the case of rebase-merging the commits are...

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 29, 2019, 8:17am UTC](https://travis-ci.community/t/get-pull-request-number-when-building-its-merge-commit/4440/3 "2019-07-29T08:17:43Z")

</div>

In “More options-\>Requests” in Travis UI ([link for your test project](https://travis-ci.org/SimeonGerginov/TestGitHubAPI/requests)), you can see what kinds of requests Github sends to Travis. Then, [Event Types & Payloads | GitHub Developer Guide](https://developer.github.com/v3/activity/events/types/) 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.

---

<div class="post-metadata">

**Author:** ![SimeonGerginov](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/simeongerginov/32/6141_2.png) [@SimeonGerginov](https://travis-ci.community/u/SimeonGerginov)\
**Post date:** [July 30, 2019, 9:39am UTC](https://travis-ci.community/t/get-pull-request-number-when-building-its-merge-commit/4440/4 "2019-07-30T09:39:37Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [July 30, 2019, 10:31am UTC](https://travis-ci.community/t/get-pull-request-number-when-building-its-merge-commit/4440/5 "2019-07-30T10:31:28Z")

</div>

> [@SimeonGerginov](#):
>
> Retrieve all Pull Requests via the GitHub API.

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