Table of Contents
Changes Made
Related Issue
Screencast/Screenshot
Home Web Front-end JS Tutorial Building jargons.dev [# The Fork Script

Building jargons.dev [# The Fork Script

Sep 10, 2024 am 11:11 AM

This is the first of 4 scripts I set out to write as stated in the system architecture. Felt pumped! it was a step in the direction of creating the "wiki" experience that gets a contribution to Open source without interfacing with the GitHub UI ?.

What are these scripts?

These are js files that holds some related reusable functions particularly meant to be used to interact with the GitHub APIs; they are either consumed within the same script or exported to be used to perform their base functionality elsewhere within the project. They accept an authenticated Octokit instance of a user as params out of others, this instance is used to perform actions/functions through the GitHub APIs on behalf of the authenticated user.

The need to create a flow of contributing to Open source without interfacing with the GitHub UI meant that we had to automate some process - simulating every steps a user will take if they were to contribute via the GitHub UI, the steps are as follows..

  1. Fork Project Repo
  2. Create a Branch
  3. Commit Changes to the Branch (add new mdx file in src/pages/word/ directory for new word or edit existing ones, in our case)
  4. Create a Pull Request (Submit the word changes, in our case)

A Truth worth stating

I started writing this script right after the initial commit, this was infact the PR #2, but it took a hit during the long month break ? i took from the project before getting back to work on the base dictionary feature.

The Script

The task here was to create "The Fork Script" — whose end goal is to create/get a fork of the jargons.dev repo on/from a user's account. It should house every function that'll do the following.

  • Check whether a Fork of jargons.dev already exists on a user's account
    • If Fork Exists
      • Check if the Fork is in-Sync with upstream (i.e. up-to-date with jargons.dev repo main branch); IF NOT — Update the fork
    • If NO Fork is found
      • Create the Fork

Understanding the assignment, I "delved" straight into working on the script.

I already I'm very used to the GitHub APIs due to my frequent consumption in my everyday work on Hearts ❤️... So I had the GitHub's Fork Documentation looking like a broski to me ?...

The Steps

  • I created a main forkRepository function which was the main entry point to executing the fork functionality - it leads everywhere else
  • I added the following functions, which mostly served as helper to the obvious main forkRepository function
    • isRepositoryForked - this function checks whether the jargons.dev repository is already forked to the current authencated user's account
    • isRepositoryForkUpdated - to check whether the fork (if found) is (in Sync with head repo) up-to-date with main jargons.dev repo
    • updateRepositoryFork - used to update (Sync) repository to state of main (head) jargons.dev repository
    • getBranch - is a base utility (required at the point of writing this script) used to fetch Branch/Ref details for the jargons.dev repo and user's fork to use in the comparison that is done in the isRepositoryForkUpdated helper to perform its primary function; it uses the the GitHub References endpoint.

My weird assumption

Running through my mind ? as I wrote this script was a thought that I held onto after reading the below quoted paragraph on the GitHub Fork Documentation

Note: Forking a Repository happens asynchronously. You may have to wait a short period of time before you can access the git objects. If this takes longer than 5 minutes, be sure to contact GitHub Support.

I misunderstood this and assumed that we were only going to be able to initiate a fork process, move on and surely not gonna be able to wait for a response object that returns the details of the new fork because we don't know when the fork process completes.

This assumption forced me to not return any data from the main forkRepositoryfunction and I was already starting to think at this point - how am I gonna get the fork details to process to the next phase of the contribution process!? Hmm, maybe I'll use webhooks ?!?

It turned out I was overthinking it ?, I realised later that I will infact get a response details for the fork and this led me to do a follow up PR to address returning the data required from the fork response object for consumption in the contribution process.

The PR

Main:

Building jargons.dev [# The Fork Script feat: implement `fork` repository script #3

Building jargons.dev [# The Fork Script
babblebey posted on

This Pull Request implements the fork script; this script is intended to be used to programmatically fork the main project repo to a user account; It houses a main function and other helper functions it uses to perform some necessary actions in order to ensure an efficient repo fork operation.

Changes Made

  • Implemented the main forkRepository function within script; this function is the main exported function that performs the main fork operation; it accepts a userOctokit (a user authenticated object with permission to act on behalf of the user) instance and the project's repository details i.e. repoDetails object and it does the following...
    • It checks whether the project repository has already been forked to the user's account using the isRepositoryForked helper function; this returns the fork of null
      • If the repo has already been forked, then we perform a check whether the fork is up-to-date/in sync with main project repo using the isRepositoryForkUpdated helper function; this returns the updatedSHA and a boolean isUpdated property that confirm whether fork is up-to-date
        • If fork is not up-to-date; then we perform the update by bringing it in sync with the main project repo using the updateRepositoryFork helper function
      • If repo is up-to-date/in sync with main project repo; we cancel out of the operation at this point with an early return;
    • If the project repository is not forked onto the user's account; then we proceed to initiating a fork process by calling the "POST /repos/{owner}/{repo}/forks" endpoint using the userOctokit instance. (This starts the fork process, we do not know exactly when the process completes ?)
  • Implement the following helper functions consumed within the main forkRepository function and within other helper functions too
    • updateRepositoryFork - used to update (Sync) repository to state of main (head) repository
    • isRepositoryForkUpdated - used to check whether a fork is (in Sync with head repo) up-to-date with main repo
    • getBranch - used to fetch a Branch/Ref details
    • isRepositoryForked - used to check for the presence of a specific repo in a user's fork repo list
  • Added getRepoParts to /lib/utils; its a utility function that is used to resolve repoOwner and repoName from a repository fullname.

Resolves #2

Screencast/Screenshot

https://github.com/babblebey/jargons.dev/assets/25631971/16221b7e-3c28-4c6c-a1f3-24d583ce7e3a

?

View on GitHub

Follow-up:

Building jargons.dev [# The Fork Script feat: return repo `fullname` in fork script #29

Building jargons.dev [# The Fork Script
babblebey posted on

This PR is a follow-up to a missing step in the fork script initial implementation at #3; the fork script failed to return a repo which can be used to in the next step of computation. This was because of a weird assumption I had during the initial implementation. ?See my assumption below...

I assume that the call to the "POST /repos/{owner}/{repo}/forks" endpoint only assures of initiating a fork process without assuring us of a response at all. Meaning we might not exactly get a response.data following the call

...but that wasn't true, I found out that a response.data actually comes, but it might just take some time and only in cases where the repo being forked is huge.... and at the moment forking the project repo happens in less than 5secs.

Changes Made

  • Returned fork repo - this is a repo fullname value returned from the isRepositoryForked helper function; I hereby return it as main returned value from the forkRepository function execution in the condition where the repo is already forked on a executing user's account
  • Returned response.data.full_name - this is a newly created fork repo fullname; Its a value from the response to the "POST /repos/{owner}/{repo}/forks" endpoint call; I hereby return it as main retuned value from the forkRepository function execution in cases where there was no fork already already found on the executing user's account
  • Cherry picked some changes from #25 to use on here
    • f12f25f548a5c5836e9be7d601ed226c5269f5ee
    • 436ceea649b67812c0ec1164fde95d443ce556e0

?

View on GitHub

The above is the detailed content of Building jargons.dev [# The Fork Script. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Zustand asynchronous operation: How to ensure the latest state obtained by useStore? Zustand asynchronous operation: How to ensure the latest state obtained by useStore? Apr 04, 2025 pm 02:09 PM

Data update problems in zustand asynchronous operations. When using the zustand state management library, you often encounter the problem of data updates that cause asynchronous operations to be untimely. �...

See all articles