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

USERNAME

String

Username of the repository owner

title

String

The new pull request title

head

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 username:branch.

base

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.

body

String

Contents of the pull request

maintainer_can_modify

Boolean

This is true if the maintainer can modify the pull request and false otherwise

draft

Boolean

This is true if the pull request is a draft and false otherwise

issues

Integer

Number of the pull requests

Press + to interact
Please provide values for the following:
ACCESS_TOKEN
Not Specified...
USERNAME
Not Specified...
REPOSITORY
Not Specified...
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 ...