The Ops Community ⚙️

Saar Ryan Cohen
Saar Ryan Cohen

Posted on

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

Top comments (0)