11// Require modules
2- const https = require ( 'https' )
32const semver = require ( 'semver' )
43const schemeMapper = require ( '../util/scheme-mapper' )
54const 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*/
8642module . 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