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', ''});

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) {
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: '',
assets_url: '',
upload_url: '{?name,label}',
html_url: '',
id: 67245347,
author: {
login: 'idanasulinmemphis',
id: 74712806,
node_id: 'MDQ6VXNlcjc0NzEyODA2',
avatar_url: '',
gravatar_id: '',
url: '',
html_url: '',
followers_url: '',
following_url: '{/other_user}',
gists_url: '{/gist_id}',
starred_url: '{/owner}{/repo}',
subscriptions_url: '',
organizations_url: '',
repos_url: '',
events_url: '{/privacy}',
received_events_url: '',
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: '',
zipball_url: '',
body: ''

Hope this helps someone 😃

Thanks to Shay Bratslavsky from Memphis for sharing

Top comments (0)