Skip to content

Commit

Permalink
Fix tools.getLatestVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Jan 27, 2022
1 parent 0a601eb commit 1db2ec9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
7 changes: 5 additions & 2 deletions __tests__/tools.test.ts
Expand Up @@ -42,12 +42,14 @@ jest
.spyOn(utils, 'fetch')
.mockImplementation(
async (url: string, token?: string): Promise<Record<string, string>> => {
if (url.includes('atom') && !url.includes('no-release')) {
if (url.includes('atom') && !url.includes('no-')) {
return {
data: '"releases/tag/1.2.3", "releases/tag/3.2.1", "releases/tag/2.3.1"'
};
} else if (url.includes('no-data')) {
return {};
} else if (url.includes('no-release')) {
return {data: ''};
return {data: 'no-release'};
} else if (!token || token === 'valid_token') {
return {data: `[{"ref": "refs/tags/1.2.3", "url": "${url}"}]`};
} else if (token === 'beta_token') {
Expand Down Expand Up @@ -77,6 +79,7 @@ describe('Tools tests', () => {
it.each`
tool | fetch_latest | version
${'tool'} | ${'true'} | ${'3.2.1'}
${'tool-no-data'} | ${'true'} | ${'latest'}
${'tool-no-release'} | ${'true'} | ${'latest'}
${'tool'} | ${'false'} | ${'latest'}
`(
Expand Down
15 changes: 9 additions & 6 deletions dist/index.js
Expand Up @@ -548,12 +548,15 @@ async function getLatestVersion(data) {
return 'latest';
}
const resp = await utils.fetch(`${data['github']}/${data['repository']}/releases.atom`);
const releases = [
...resp['data'].matchAll(/releases\/tag\/([a-zA-Z]*)?(\d+.\d+.\d+)"/g)
].map(match => match[2]);
return (releases
.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))
.pop() || 'latest');
if (resp['data']) {
const releases = [
...resp['data'].matchAll(/releases\/tag\/([a-zA-Z]*)?(\d+.\d+.\d+)"/g)
].map(match => match[2]);
return (releases
.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))
.pop() || 'latest');
}
return 'latest';
}
exports.getLatestVersion = getLatestVersion;
async function getVersion(version, data) {
Expand Down
23 changes: 13 additions & 10 deletions src/tools.ts
Expand Up @@ -43,17 +43,20 @@ export async function getLatestVersion(data: RS): Promise<string> {
const resp: Record<string, string> = await utils.fetch(
`${data['github']}/${data['repository']}/releases.atom`
);
const releases: string[] = [
...resp['data'].matchAll(/releases\/tag\/([a-zA-Z]*)?(\d+.\d+.\d+)"/g)
].map(match => match[2]);
if (resp['data']) {
const releases: string[] = [
...resp['data'].matchAll(/releases\/tag\/([a-zA-Z]*)?(\d+.\d+.\d+)"/g)
].map(match => match[2]);

return (
releases
.sort((a: string, b: string) =>
a.localeCompare(b, undefined, {numeric: true})
)
.pop() || 'latest'
);
return (
releases
.sort((a: string, b: string) =>
a.localeCompare(b, undefined, {numeric: true})
)
.pop() || 'latest'
);
}
return 'latest';
}

/**
Expand Down

0 comments on commit 1db2ec9

Please sign in to comment.