Skip to content
This repository was archived by the owner on Dec 19, 2025. It is now read-only.

Commit 539232c

Browse files
committed
Add comparator
1 parent 644ba01 commit 539232c

4 files changed

Lines changed: 97 additions & 33 deletions

File tree

packages/core/src/query/rest.ts

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
11
import { Octokit } from '@octokit/core'
2-
import semver from 'semver'
32
import { CheckOptions, ReleaseDescriptor, TagDescriptor } from '../types'
3+
import { compareReleases, compareTags } from '../util/comparator'
44
import { release, tag } from '../util/scheme-mapper'
55

6-
/**
7-
* @param options The options for the version check.
8-
* @param json The json response from the api.
9-
*/
10-
function compare(options: CheckOptions, json: any) {
11-
let found = false
12-
let version = null
13-
for (let i = 0; i < json.length; i++) {
14-
version = json[i]
15-
16-
if (options.excludePrereleases) {
17-
if (version['prerelease'] !== undefined && version['prerelease']) {
18-
continue
19-
}
20-
}
21-
22-
if (semver.gt(version[options.fetchTags ? 'name' : 'tag_name'], options.currentVersion)) {
23-
found = true
24-
break
25-
}
26-
}
27-
28-
return found ? version : null
29-
}
30-
316
/**
327
* Executes an API call on the Github Rest API (v3) that should return the latest version.
338
* The returned version is compared to the given version.
@@ -71,12 +46,10 @@ export default async function rest(options: CheckOptions): Promise<ReleaseDescri
7146
throw new Error(response.data.message)
7247
}
7348

74-
// Compare versions
75-
const version = compare(options, response.data)
76-
if (version) {
77-
const mapper = options.fetchTags ? tag : release
78-
return mapper(version)
79-
} else {
80-
return undefined
49+
if (options.fetchTags) {
50+
return tag(compareTags(options, response.data))
51+
}
52+
else {
53+
return release(compareReleases(options, response.data))
8154
}
8255
}

packages/core/src/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,15 @@ export interface ReleaseDescriptor {
9292
export type CallbackFunction = (error?: Error|string, update?: ReleaseDescriptor | TagDescriptor) => void
9393

9494
export type RestHandlerFunction = (res: IncomingMessage) => void
95+
96+
export interface RestResponseTag {
97+
name: string
98+
}
99+
100+
export interface RestResponseRelease {
101+
name: string
102+
tag_name: string
103+
prerelease: boolean
104+
published_at: string
105+
html_url: string
106+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { gt } from 'semver'
2+
import { CheckOptions, RestResponseRelease, RestResponseTag } from '../types'
3+
4+
export function compareTags(options: CheckOptions, data: RestResponseTag[]): RestResponseTag|undefined {
5+
for (const item of data) {
6+
if (gt(item.name, options.currentVersion)) {
7+
return item
8+
}
9+
}
10+
11+
return undefined
12+
}
13+
14+
export function compareReleases(options: CheckOptions, data: RestResponseRelease[]): RestResponseRelease|undefined {
15+
for (const item of data) {
16+
if (options.excludePrereleases && item.prerelease) {
17+
continue
18+
}
19+
20+
if (gt(item.tag_name, options.currentVersion)) {
21+
return item
22+
}
23+
}
24+
25+
return undefined
26+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import test from 'ava'
2+
import { CheckOptions, RestResponseRelease, RestResponseTag } from '../src/types'
3+
import { compareReleases, compareTags } from '../src/util/comparator'
4+
5+
const options: CheckOptions = {
6+
owner: 'axelrindle',
7+
repo: 'github-version-checker',
8+
currentVersion: '1.0.0'
9+
}
10+
11+
test('comparator returns newer release', t => {
12+
const v1337: RestResponseRelease = {
13+
name: 'A newer release',
14+
html_url: 'https://example.org',
15+
prerelease: false,
16+
published_at: 'some day',
17+
tag_name: 'v1.3.37'
18+
}
19+
const result = compareReleases(options, [v1337])
20+
t.is(result, v1337)
21+
})
22+
23+
test('comparator returns undefined release', t => {
24+
const v1337: RestResponseRelease = {
25+
name: 'A newer release',
26+
html_url: 'https://example.org',
27+
prerelease: false,
28+
published_at: 'some day',
29+
tag_name: 'v1.3.37'
30+
}
31+
32+
const myOptions = Object.assign({}, options, { currentVersion: 'v2.0.0' })
33+
const result = compareReleases(myOptions, [v1337])
34+
t.is(result, undefined)
35+
})
36+
37+
test('comparator returns newer tag', t => {
38+
const v1337: RestResponseTag = {
39+
name: 'v1.3.37'
40+
}
41+
const result = compareTags(options, [v1337])
42+
t.is(result, v1337)
43+
})
44+
45+
test('comparator returns undefined tag', t => {
46+
const v1337: RestResponseTag = {
47+
name: 'v1.3.37'
48+
}
49+
50+
const myOptions = Object.assign({}, options, { currentVersion: 'v2.0.0' })
51+
const result = compareTags(myOptions, [v1337])
52+
t.is(result, undefined)
53+
})

0 commit comments

Comments
 (0)