From 48k lines of code to 10—the story of GitHub’s JavaScript SDK

Learn about the legacy, architecture, and methods used to reduce 48k lines of code to 10 as we take a deep dive into GitHub’s Javascript SDK.

Gregor Martynus
8 min readintermediate
--
View Original

Overview

The article discusses the transformation of GitHub's JavaScript SDK, @octokit/rest, from an unwieldy 48,000 lines of code to a streamlined 10 lines. It highlights the importance of maintainability, automation, and a modular architecture in software development.

What You'll Learn

1

How to automate API endpoint management using scripts

2

Why modular architecture improves maintainability in SDKs

3

How to implement plugin architecture in JavaScript libraries

Prerequisites & Requirements

  • Understanding of JavaScript and REST APIs
  • Familiarity with GitHub and npm(optional)

Key Questions Answered

What were the main challenges faced during the transformation of @octokit/rest?
The main challenges included managing a large codebase with nearly 48,000 lines of code, ensuring maintainability, and transitioning to a modular architecture that supports various JavaScript environments. The project aimed to automate the management of API endpoints and improve the overall developer experience.
How did GitHub automate the maintenance of the routes.json file?
GitHub automated the maintenance of the routes.json file by creating a script that scrapes GitHub's REST API documentation nightly. This script generates a machine-readable JSON representation, ensuring that the routes.json file is always up-to-date with the latest API changes.
What is the significance of the Octokit plugin architecture?
The Octokit plugin architecture allows developers to use lower-level libraries tailored to their needs, improving flexibility and reducing bundle size. This modular approach enables users to choose only the features they require, enhancing performance across different JavaScript environments.
What testing strategies were implemented for the new version of @octokit/rest?
Extensive testing strategies were implemented, including turning usage examples into integration tests and ensuring 100% test coverage for the new version. This approach aimed to prevent bugs and maintain the reliability of the SDK after significant code changes.

Key Statistics & Figures

Lines of code in the original @octokit/rest
48,000
The original version of the SDK was unwieldy, prompting the need for a significant refactor.
Lines of code after refactoring
10
The refactored version of the SDK dramatically reduced complexity while maintaining functionality.
Lines of code in routes.json
10,275
The routes.json file was updated to ensure comprehensive coverage of GitHub's REST API endpoints.
Lines of TypeScript definitions
26,700
The TypeScript definitions grew significantly as part of the refactoring process.

Technologies & Tools

Backend
@octokit/REST
Official JavaScript SDK for GitHub's REST APIs.
Backend
@octokit/Core
New core library that serves as a foundation for @octokit/rest and other Octokit libraries.

Key Actionable Insights

1
Automate API endpoint management to reduce manual errors and improve efficiency.
By implementing scripts that automatically update API endpoint definitions, developers can ensure their SDKs remain current without the risk of human error, leading to a more reliable product.
2
Adopt a modular architecture to enhance maintainability and performance.
Modular architectures allow developers to create smaller, focused libraries that can be combined as needed, which is particularly beneficial for projects that require flexibility across different environments.
3
Implement comprehensive testing strategies to ensure code reliability.
By establishing rigorous testing protocols, including integration tests and maintaining high coverage, developers can confidently make changes to their codebase without introducing new bugs.

Common Pitfalls

1
Failing to automate API endpoint updates can lead to outdated or incorrect SDK functionality.
Without automation, developers may overlook changes in the API, resulting in discrepancies between the SDK and the actual API, which can frustrate users and lead to bugs.
2
Neglecting comprehensive testing after significant code changes can introduce new bugs.
When refactoring a large codebase, it's crucial to implement thorough testing strategies to catch any issues early, ensuring the reliability of the software.

Related Concepts

Modular Architecture
API Management
Software Testing Strategies