Create pull request and add labels in a single request

Hi there,

I’m trying to create a pull request with labels in a single request via Github’s REST (v3) API.
From the docs it seems like it is not possible to add labels while creating a pull request.
This surprises me, since it’s possible to create an issue and add labels with a single request.

To be sure I did try creating a PR with labels in the body but it did not work (PR created but no label added):

curl --request POST \
  --url https://api.github.com/repos/sqren/backport-demo/pulls \
  --data '{
	"title": "My PR title",
	"body": "Some description...",
	"head": "sqren:7.x",
	"base": "master",
	"labels": ["my-label"]
}'

Is this possible with the current REST (v3) api or via the graphql (v4) api?

Thanks!

You are correct that there isn’t a way to do what you’re describing using the REST API.

Additionally, there isn’t a way to add labels via the GraphQL API createPullRequest mutation. You can submit multiple mutations in one request via GraphQL, but I have not been able to discover a way to use the output of one mutation as an input of a successive mutation in the same request via GraphQL. Because of this, it would appear that you would have to use two separate requests even via the GraphQL API.

Perhaps someone more experienced with GraphQL could comment on a way to make it happen.

Hi @sqren Thanks for being here, while I have no input as to how to add this programmatically at creation via the API (I’m not sure this is possible. When using the API, adding labels takes two steps: PR saved first then labels are saved to the database vs in the UI is done in one step)
In anycase, I’d like to share that the GitHub CLI has included the ability to add metadata such as labels at PR creation in one of its latest releases maybe check it out.

I hope that helps!

Thanks both. I’ll have to make due with multiple requests (In addition to adding labels I also need to add assignees to the PR).

Would be great if this could be done at creation time like it’s possible with issues.

1 Like