Pull Requests
Learn and practice the functionalities of pull requests in the GitHub API.
Overview
Being a distributed version control manager, GitHub allows the feature of creating pull requests through which we can share our changes committed to a branch with the contributors. These contributors can then review and add comments for any feedback or changes. The changes can be addressed by adding follow-up commits to that branch. We can also add descriptions of the proposed changes, add labels, add reviewers and assignees, and mention people. Additionally, we can review comments or commits made in that branch in chronological order. Once finalized, the reviewers then approve these changes, and then that branch is merged into the base branch.
Create a pull request
Creating a pull request requires granting write
access to the head branch. There should be commits between the head and base branches to create a pull request.
Note: The following code extracts the pull number of the pull request to be used later in the lesson.
The table below shows the parameters in the body of creating a pull request.
Request Parameters
Parameters | Type | Description |
| String | Username of the repository owner |
| String | The new pull request title |
| String | This is the name of the branch in which changes are made. If the changes are made in the user’s cross-repository in the same network, the branch is referred to as |
| String | This is the name of the branch in which the changes are to be merged. This branch should be present in the current repository. |
| String | Contents of the pull request |
| Boolean | This is |
| Boolean | This is |
| Integer | Number of the pull requests |
const endpointUrl = 'https://api.github.com/repos/{{USERNAME}}/{{REPOSITORY}}/pulls';const headers = {Authorization: 'token {{ACCESS_TOKEN}}',Accept: 'application/vnd.github.v3+json',};const body = JSON.stringify({title: '<title>',head: '<head-name>',base: '<base-name>',});const options = {method: 'POST',headers,body,};async function CreatePullRequest() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printErrors(error);}};CreatePullRequest();
In case of successful execution of the above code, it will return the response code 201
...