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

Commit b17360c

Browse files
committed
Migrate to octokit
1 parent ff56414 commit b17360c

4 files changed

Lines changed: 317 additions & 94 deletions

File tree

lib/query/graphql.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Require modules
2-
const github_graphql = require('github-graphql-client')
2+
const { Octokit } = require('@octokit/core')
33
const semver = require('semver')
4+
const pkg = require('../../package.json')
45
const query = require('../util/query')
56

67
/**
@@ -12,18 +13,17 @@ const query = require('../util/query')
1213
* @param {CheckOptions} options The options for the version check.
1314
* @param {CallbackFunction} callback The callback function.
1415
*/
15-
module.exports = (options, callback) => {
16-
// build the query
16+
module.exports = async (options, callback) => {
1717
const theQuery = options.fetchTags ? query.tags(options.repo, options.owner) : query.releases(options.repo, options.owner)
18+
const octokit = new Octokit({
19+
auth: options.token,
20+
headers: {
21+
'user-agent': `${pkg.name} v${pkg.version}`
22+
}
23+
})
1824

19-
// do the api call
20-
github_graphql({
21-
token: options.token,
22-
query: theQuery
23-
}, (err, res) => {
24-
if (err) {
25-
callback(err, null)
26-
} else {
25+
octokit.graphql(theQuery)
26+
.then(res => {
2727
// Retrieve newer version name
2828
const newer = options.fetchTags ? res.data.repository.refs.nodes[0] : res.data.repository.releases.nodes[0]
2929

@@ -33,6 +33,6 @@ module.exports = (options, callback) => {
3333
} else {
3434
callback(null, null)
3535
}
36-
}
37-
})
36+
})
37+
.catch(err => callback(err, null))
3838
}

lib/query/rest.js

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Require modules
2-
const https = require('https')
32
const semver = require('semver')
43
const schemeMapper = require('../util/scheme-mapper')
54
const pkg = require('../../package.json')
5+
const { Octokit } = require('@octokit/core')
66

77
/**
88
* @param {CheckOptions} options The options for the version check.
@@ -30,50 +30,6 @@ function compare(options, json) {
3030
return found ? version : null
3131
}
3232

33-
/**
34-
* @param {CheckOptions} options The options for the version check.
35-
* @param {CallbackFunction} callback The callback function.
36-
* @returns {RestHandlerFunction}
37-
*/
38-
function makeHandler(options, callback) {
39-
return res => {
40-
const chunks = []
41-
res.on('data', chunk => {
42-
return chunks.push(chunk.toString())
43-
})
44-
res.on('end', () => {
45-
// parse response string into an object
46-
const response = chunks.join('')
47-
let json = null
48-
try {
49-
json = JSON.parse(response)
50-
} catch (error) {
51-
return callback(error, null)
52-
}
53-
54-
// 404 error occurs, when no releases are found
55-
if (res.statusCode === 404) {
56-
return callback(null, null)
57-
}
58-
59-
// status codes other than 200 are treated as an error
60-
else if (res.statusCode !== 200) {
61-
return callback(new Error(json.message), null)
62-
}
63-
64-
// Compare versions
65-
const version = compare(options, json)
66-
67-
if (version) {
68-
const mapped = schemeMapper[options.fetchTags ? 'tag' : 'release'](version)
69-
callback(null, mapped)
70-
} else {
71-
callback(null, null)
72-
}
73-
})
74-
}
75-
}
76-
7733
/**
7834
* Executes an API call on the Github Rest API (v3) that should return the latest version.
7935
* The returned version is compared to the given version.
@@ -85,7 +41,7 @@ function makeHandler(options, callback) {
8541
*/
8642
module.exports = (options, callback) => {
8743
// build url
88-
let apiUrl = `/repos/${options.owner}/${options.repo}`
44+
let apiUrl = '/repos/{owner}/{repo}'
8945
if (options.fetchTags) {
9046
apiUrl += '/tags'
9147
} else {
@@ -96,21 +52,35 @@ module.exports = (options, callback) => {
9652
}
9753
}
9854

99-
// define request options
100-
/** @type {https.RequestOptions} */
101-
const opts = {
102-
hostname: 'api.github.com',
103-
path: apiUrl,
55+
const octokit = new Octokit({
56+
auth: options.token
57+
})
58+
59+
octokit.request(apiUrl, {
60+
owner: options.owner,
61+
repo: options.repo,
10462
headers: {
105-
'Accept': 'application/vnd.github.v3+json',
106-
'User-Agent': `${pkg.name} v${pkg.version}`
63+
'user-agent': `${pkg.name} v${pkg.version}`
10764
}
108-
}
109-
110-
// execute request
111-
let req = https.get(opts, makeHandler(options, callback))
112-
req.on('error', err => {
113-
callback(err, null)
11465
})
115-
req.end()
66+
.then(response => {
67+
// 404 error occurs, when no releases are found
68+
if (response.status === 404) {
69+
return callback(null, null)
70+
}
71+
72+
// status codes other than 200 are treated as an error
73+
else if (response.status !== 200) {
74+
return callback(new Error(response.data.message), null)
75+
}
76+
77+
// Compare versions
78+
const version = compare(options, response.data)
79+
if (version) {
80+
const mapped = schemeMapper[options.fetchTags ? 'tag' : 'release'](version)
81+
callback(null, mapped)
82+
} else {
83+
callback(null, null)
84+
}
85+
})
11686
}

0 commit comments

Comments
 (0)