I am trying to create a pull request along with labels included during creation. I am aware this can be done in GitHub UI, but is it possible to do the same using REST API programatically?
The API documentation for creating a pull request doesn't mention specifying labels: https://docs.github.com/en/rest/reference/pulls#create-a-pull-request
I used the following command to create a PR:
execute_command_with_output(
'curl --silent -X POST -H "Authorization: token {}" -H "Accept: application/vnd.github.v3+json" '
'{}/repos/{}/pulls -d \'{{"head":"{}","base":"{}","title":"{} Cherry-Pick from PR {}","labels":"bug"}}\''.format(
token, url, repository, t_branch, b_branch, ticket, pr
),
It's not possible to do it at the Pull Request creation using the Github API (yet).
However, you can call another Github service from the API to add labels to the Pull Request after its creation. The related service is the Add Label To An Issue.
You read it right: TO AN ISSUE!
Actually, issue_number and pr_number use the SAME sequence on a Github user repository and seem to refer to the same object when consumed by the Github API for all those labels services.
(I wasn't aware of this fact before looking for the answer to this question!)
enhancement label)curl --location --request POST 'https://api.github.com/repos/{repo_owner}/{repo_name}/issues/{pr_number}/labels' \
--header 'Accept: application/vnd.github.v3+json' \
--header 'Authorization: Bearer {GITHUB_PAT}' \
--header 'Content-Type: application/json' \
--data-raw '{
"labels": ["enhancement"]
}'
The labels field represents the names of the labels to add to the issue / PR.
Note 1: You can also pass an empty array to remove all labels.
Note 2: You can also pass a single label as a string or an array of labels directly, but GitHub recommends passing an object with the labels key.
I observed this behaviour on the official Labeler action on the Github Marketplace on the addLabels method implementation for PR (which is the same than for ISSUES).
Then, I confirm it though this post on the Github Community asking the same question as yours.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With