DEV Community

Memphis.dev team for Memphis.dev

Posted on • Edited on

2 1

Get GitHub Release Information with Node.js

GitHub latest release

I recently worked on an open-source Node.js project and encountered a small challenge while checking if the version that the user runs locally is up to date with the latest release.
A few npm packages are trying to do just that, but they did not work as expected and did not always give the correct answer.

In short, my main goal is to notify the user when his local project is out of date and should be updated.

After a lot of searching, I found the simple answer- A simple HTTP request to the GitHub API using the ‘axios’ npm package.

An example from my code:

const response = await axios({ 'GET', 'https://api.github.com/repos/Memphis-OS/memphis-cli/releases'});

Then I isolate the element in the first place (response[0]) get the name and remove the ‘v’ so I can compare it to the version that can be found in the local package.json.

const latestVersion = response[0].name.replace('v', '');
const localVersion = require('../package.json').version;
if (latestVersion !== localVersion) {
console.log(
Memphis CLI is out of date, we strongly recommend upgrading it\nThe version installed is ${localVersion} but current version is ${latestVersion}
);}

The response I get is a list of all releases and it looks like this:

[
{
url: 'https://api.github.com/repos/Memphis-OS/memphis-cli/releases/67245347',
assets_url: 'https://api.github.com/repos/Memphis-OS/memphis-cli/releases/67245347/assets',
upload_url: 'https://uploads.github.com/repos/Memphis-OS/memphis-cli/releases/67245347/assets{?name,label}',
html_url: 'https://github.com/Memphis-OS/memphis-cli/releases/tag/v0.2.8',
id: 67245347,
author: {
login: 'idanasulinmemphis',
id: 74712806,
node_id: 'MDQ6VXNlcjc0NzEyODA2',
avatar_url: 'https://avatars.githubusercontent.com/u/74712806?v=4',
gravatar_id: '',
url: 'https://api.github.com/users/idanasulinmemphis',
html_url: 'https://github.com/idanasulinmemphis',
followers_url: 'https://api.github.com/users/idanasulinmemphis/followers',
following_url: 'https://api.github.com/users/idanasulinmemphis/following{/other_user}',
gists_url: 'https://api.github.com/users/idanasulinmemphis/gists{/gist_id}',
starred_url: 'https://api.github.com/users/idanasulinmemphis/starred{/owner}{/repo}',
subscriptions_url: 'https://api.github.com/users/idanasulinmemphis/subscriptions',
organizations_url: 'https://api.github.com/users/idanasulinmemphis/orgs',
repos_url: 'https://api.github.com/users/idanasulinmemphis/repos',
events_url: 'https://api.github.com/users/idanasulinmemphis/events{/privacy}',
received_events_url: 'https://api.github.com/users/idanasulinmemphis/received_events',
type: 'User',
site_admin: false
},
node_id: 'RE_kwDOGxUwZc4EAhUj',
tag_name: 'v0.2.8',
target_commitish: 'master',
name: 'v0.2.8',
draft: false,
prerelease: false,
created_at: '2022-05-19T06:23:21Z',
published_at: '2022-05-19T06:45:02Z',
assets: [ [Object] ],
tarball_url: 'https://api.github.com/repos/Memphis-OS/memphis-cli/tarball/v0.2.8',
zipball_url: 'https://api.github.com/repos/Memphis-OS/memphis-cli/zipball/v0.2.8',
body: ''
},
.......]

Hope this helps someone 😃

Thanks to Shay Bratslavsky from Memphis for sharing

Image of Bright Data

High-Quality Data for AI – Access diverse datasets ready for your ML models.

Browse our extensive library of pre-collected datasets tailored for various AI and ML projects.

Explore Datasets

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay