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

Commit 767963a

Browse files
author
Axel Rindle
committed
Migrate to the GitHub GraphQL API (v4)
1 parent 05da1fb commit 767963a

4 files changed

Lines changed: 4780 additions & 488 deletions

File tree

lib/main.coffee

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
# require npm modules
2-
got = require 'got'
1+
# Require modules
2+
graphql = require 'github-graphql-client'
33
chalk = require 'chalk'
44
semver = require 'semver'
5+
query = require './query'
56

6-
baseApiUrl = 'https://api.github.com/repos/REPO/releases'
7+
###
8+
The exported checking function.
79
10+
@param options {object} The options for the version check.
11+
@param callback {function|undefined} An optional callback to pass the result to.
12+
Can be omitted to return a Promise.
13+
@return undefined or a Promise.
14+
###
815
module.exports = (options, callback) ->
916
if callback
1017
check options, callback
@@ -14,42 +21,49 @@ module.exports = (options, callback) ->
1421
reject error if error
1522
if update then resolve update else resolve null
1623

24+
###
25+
The actual checking function. Executes a GraphQL query on the API to fetch
26+
either the latest release or tag. The returned version is compared to the given
27+
version. When to fetched version is greated than the given one, the newer version
28+
is returned.
29+
30+
@param options {object} The options for the version check.
31+
@param callback {function|undefined} An optional callback to pass the result to.
32+
Can be omitted to return a Promise.
33+
###
1734
check = (options, callback) ->
1835
# get options
36+
token = options.token
1937
repo = options.repo
38+
owner = options.owner
2039
currentVersion = options.currentVersion
21-
includePreReleases = options.includePreReleases || false
40+
fetchTags = options.fetchTags || false
2241

2342
# check if required options are defined
43+
callback(null, 'no token specified') if token is undefined
2444
callback(null, 'no repository specified') if repo is undefined
45+
callback(null, 'no owner specified') if owner is undefined
2546
callback(null, 'no current version given') if currentVersion is undefined
2647

48+
# build the query
49+
query = if fetchTags then query.tags repo, owner else query.releases repo, owner
50+
2751
# do the api call
28-
apiUrl = baseApiUrl.replace 'REPO', repo
29-
if not includePreReleases then apiUrl += '/latest'
30-
got(apiUrl).then((response) ->
31-
# parse the response body into an object
32-
releases = JSON.parse response.body
33-
34-
found = false
35-
36-
if includePreReleases
37-
# loop through releases
38-
# if a remote version is higher than the installed one, the callback
39-
# will be called with the release object
40-
for release in releases
41-
tag = release.tag_name
42-
if semver.compare(currentVersion, tag) is -1
43-
found = true
44-
callback release, null
45-
break
52+
graphql (token: token, query: query), (err, res) ->
53+
if err
54+
callback null, err
4655
else
47-
tag = releases.tag_name
48-
if semver.compare(currentVersion, tag) is -1
49-
found = true
50-
callback releases, null
56+
# Retrieve newer version name
57+
newer =
58+
if fetchTags
59+
res.data.repository.refs.nodes[0]
60+
else
61+
res.data.repository.releases.nodes[0]
5162

52-
callback(null, null) if not found
53-
).catch (error) -> callback null, error
63+
# Compare versions
64+
if semver.gt (if fetchTags then newer.name else newer.tag.name), currentVersion
65+
callback newer, null
66+
else
67+
callback null, null
5468

55-
return null
69+
return

lib/query.coffee

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
###
2+
Query for fetching the latest github release.
3+
4+
@param repo {string} The repository name.
5+
@param owner {string} The owner of the repository.
6+
###
7+
module.exports.releases = (repo, owner) ->
8+
"
9+
{
10+
repository(name: \"#{repo}\", owner: \"#{owner}\") {
11+
releases(last: 1) {
12+
nodes {
13+
name
14+
tag {
15+
name
16+
}
17+
isPrerelease
18+
publishedAt
19+
url
20+
}
21+
}
22+
}
23+
}
24+
"
25+
26+
###
27+
Query for fetching the latest git tag.
28+
29+
@param repo {string} The repository name.
30+
@param owner {string} The owner of the repository.
31+
###
32+
module.exports.tags = (repo, owner) ->
33+
"
34+
{
35+
repository(name: \"#{repo}\", owner: \"#{owner}\") {
36+
refs(last: 1, refPrefix: \"refs/tags/\", orderBy: { field: TAG_COMMIT_DATE, direction: ASC}) {
37+
nodes {
38+
name
39+
}
40+
}
41+
}
42+
}
43+
"

0 commit comments

Comments
 (0)