DEV Community

Cover image for Building jargons.dev [#6]: The Branch Script
Olabode Lawal-Shittabey
Olabode Lawal-Shittabey

Posted on

Building jargons.dev [#6]: The Branch Script

This should be really short haha πŸ˜ƒ, considering the branch script is the smallest of them (The Scripts) all.

The Branch Script's primary assignment is to hold all helper functions that can be used to perform operation related to References (aka Branch) on the GitHub APIs.

The Script

Noting the primary responsibility of the branch script which is
to create a reference/branch of repo (in this case would be the jargons.dev repo fork) on an authenticated user's account. I got to work quickly by doing the following...

  • Implemented the main createBranch function in the branch script which accepts necessary param to perform 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 (a function that was created in the fork script); this return the SHA of the branch
    • Then makes a POST request to the endpoint "/repos/{owner}/{repo}/git/refs" to create the new branch; passing in the full name of the user's fork, the head branch SHA and the name of the new branch we wish to create.
  • I also took the initiative of moving the getBranch helper function from the fork script to a the branch script β€” feels more like home πŸ˜‰.

The PR

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

Top comments (0)