Skip to content

Commit

Permalink
Fetches the latest Cake version number from GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
ecampidoglio committed May 26, 2023
1 parent 3e49464 commit e3d97f6
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
112 changes: 112 additions & 0 deletions __tests__/cakeRelease.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import * as http from '@actions/http-client';
import * as cakeRelease from '../src/cakeRelease';

describe('When retrieving the latest Cake version', () => {
beforeAll(async () => {
jest
.spyOn(http.HttpClient.prototype, 'getJson')
.mockImplementation(async () => ({
statusCode: 200,
result: {
tag_name: 'v1.0.0'
},
headers: {}
}));
});

test('it should return the latest version number from GitHub', async () => {
expect(await cakeRelease.getLatestVersion()).toBe('1.0.0');
});
});

describe('When retrieving the latest Cake version without the \'v\' prefix', () => {
beforeAll(async () => {
jest
.spyOn(http.HttpClient.prototype, 'getJson')
.mockImplementation(async () => ({
statusCode: 200,
result: {
tag_name: '1.0.0'
},
headers: {}
}));
});

test('it should return the latest version number from GitHub', async () => {
expect(await cakeRelease.getLatestVersion()).toBe('1.0.0');
});
});

describe('When failing to retrieve the latest Cake version due to a GitHub error', () => {
beforeAll(async () => {
jest
.spyOn(http.HttpClient.prototype, 'getJson')
.mockImplementation(async () => ({
statusCode: 500,
result: {},
headers: {}
}));
});

test('it should return null', async () => {
expect(await cakeRelease.getLatestVersion()).toBeNull();
});

test('it should log the fact that the GitHub API returned an error', async () => {
const log = jest.spyOn(console, 'log');
await cakeRelease.getLatestVersion();
expect(log).toHaveBeenCalledWith('Could not determine the latest version of Cake. GitHub returned status code 500');
});
});

describe('When failing to retrieve the latest Cake version due to an empty response from GitHub', () => {
beforeAll(async () => {
jest
.spyOn(http.HttpClient.prototype, 'getJson')
.mockImplementation(async () => ({
statusCode: 200,
result: {},
headers: {}
}));
});

test('it should return null', async () => {
expect(await cakeRelease.getLatestVersion()).toBeNull();
});
});

describe('When failing to retrieve the latest Cake version due to a missing tag name in the GitHub response', () => {
beforeAll(async () => {
jest
.spyOn(http.HttpClient.prototype, 'getJson')
.mockImplementation(async () => ({
statusCode: 200,
result: {
tag_name: null
},
headers: {}
}));
});

test('it should return null', async () => {
expect(await cakeRelease.getLatestVersion()).toBeNull();
});
});

describe('When failing to retrieve the latest Cake version due to an empty tag name in the GitHub response', () => {
beforeAll(async () => {
jest
.spyOn(http.HttpClient.prototype, 'getJson')
.mockImplementation(async () => ({
statusCode: 200,
result: {
tag_name: ''
},
headers: {}
}));
});

test('it should return null', async () => {
expect(await cakeRelease.getLatestVersion()).toBeNull();
});
});
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"dependencies": {
"@actions/core": "^1.9.1",
"@actions/exec": "^1.0.1",
"@actions/http-client": "^2.0.1",
"@actions/io": "^1.0.1"
},
"devDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions src/cakeRelease.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as http from '@actions/http-client';

export async function getLatestVersion(): Promise<string | null> {
const release = await getLatestCakeReleaseFromGitHub();
return extractVersionNumber(release);
}

async function getLatestCakeReleaseFromGitHub(): Promise<GitHubRelease | null> {
const client = new http.HttpClient('cake-build/cake-action');
const response = await client.getJson<GitHubRelease>('https://api.github.com/repos/cake-build/cake/releases/latest');

if (response.statusCode != 200) {
console.log(`Could not determine the latest version of Cake. GitHub returned status code ${response.statusCode}`);
return null;
}

return response.result;
}

function extractVersionNumber(release: GitHubRelease | null): string | null {
return release?.tag_name?.replace(/^v/, '') || null;
}

interface GitHubRelease {
tag_name: string;
}

0 comments on commit e3d97f6

Please sign in to comment.