Skip to content

Commit 07f6f9d

Browse files
authored
feat: implement branch creation script (#17)
This Pull request implements the branch creation script whose primary function is to create a new branch from the forked repository for an authenticated user. ### Changes Made - Implemented the main `createBranch` function in the `branch` script at `src/lib/branch.js`; this function takes in 3 params; - `userOctokit` - a user authenticated instance of octokit that can be used to perform action on user's behalf - `repoDetails` - the user's forked repo details; hold the `repoFullname` and `repoMainBranchRef` as properties in the object type param - `newBranchName` - name of the branch that will be created for the user The `createBranch` function performs the following operation in the stated order... - It initially fetches the forked repository main branch where we wished to create the new branch off of; it does this using the `getBranch` helper function; this return the `SHA` of the branch - Then we make a request to the endpoint `"POST /repos/{owner}/{repo}/git/refs"` to create the new branch; passing in the `repoDetails.repoFullname` properties, the head branch `SHA` and the `newBranchName`. - Moved the `getBranch` function from the `fork` script to the `branch` script ### Screencast/Screenshot [screencast-bpconcjcammlapcogcnnelfmaeghhagj-2024.04.01-13_54_14.webm](https://github.com/babblebey/jargons.dev/assets/25631971/62a2f9b1-7643-42e3-b91f-1427fad1c170)
1 parent 700fd08 commit 07f6f9d

2 files changed

Lines changed: 51 additions & 19 deletions

File tree

src/lib/branch.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { getRepoParts } from "./utils/index.js";
2+
3+
/**
4+
* Create a new branch on a given repository
5+
* @param {import("octokit").Octokit} userOctokit
6+
* @param {{ repoFullname: string, repoMainBranchRef: string }} repoDetails
7+
* @param {string} newBranchName
8+
* @returns New Branch/Ref details
9+
*
10+
* @todo handle error for `'Reference already exists'`
11+
*/
12+
export async function createBranch(userOctokit, repoDetails, newBranchName) {
13+
const { repoFullname, repoMainBranchRef } = repoDetails;
14+
const { repoName, repoOwner } = getRepoParts(repoFullname);
15+
const { object: { sha } } = await getBranch(userOctokit, repoFullname, repoMainBranchRef);
16+
17+
try {
18+
const response = await userOctokit.request("POST /repos/{owner}/{repo}/git/refs", {
19+
owner: repoOwner,
20+
repo: repoName,
21+
ref: `refs/heads/${newBranchName}`,
22+
sha,
23+
});
24+
25+
return response.data
26+
} catch (error) {
27+
throw new Error("error creating a new branch", {
28+
cause: error.message
29+
})
30+
}
31+
}
32+
33+
/**
34+
* Get a Branch/Ref details
35+
* @param {import("octokit").Octokit} userOctokit
36+
* @param {string} repo
37+
* @param {string} ref
38+
* @returns Branch/Ref details
39+
*/
40+
export async function getBranch(userOctokit, repo, ref) {
41+
const { repoOwner, repoName } = getRepoParts(repo);
42+
43+
const response = await userOctokit.request("GET /repos/{owner}/{repo}/git/ref/{ref}", {
44+
owner: repoOwner,
45+
repo: repoName,
46+
ref: ref,
47+
});
48+
49+
return response.data;
50+
}

src/lib/fork.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getBranch } from "./branch.js";
12
import { getRepoParts } from "./utils/index.js";
23

34
/**
@@ -90,25 +91,6 @@ async function isRepositoryForkUpdated(userOctokit, repoDetails, fork) {
9091
};
9192
}
9293

93-
/**
94-
* Get a Branch/Ref details
95-
* @param {import("octokit").Octokit} userOctokit
96-
* @param {string} repo
97-
* @param {string} ref
98-
* @returns Branch/Ref details
99-
*/
100-
async function getBranch(userOctokit, repo, ref) {
101-
const { repoOwner, repoName } = getRepoParts(repo);
102-
103-
const response = await userOctokit.request("GET /repos/{owner}/{repo}/git/ref/{ref}", {
104-
owner: repoOwner,
105-
repo: repoName,
106-
ref: ref,
107-
});
108-
109-
return response.data;
110-
}
111-
11294
/**
11395
* Check for the presence of a specific repo in a user's fork repo list
11496
* @param {import("octokit").Octokit} userOctokit

0 commit comments

Comments
 (0)