-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import https from 'https' | ||
|
||
/** | ||
* get latest release info from GitHub API | ||
* @param {String} repoOwner repo owner | ||
* @param {String} repoName repo name | ||
* @example getLatestRelease('hugo-fixit', 'FixIt') | ||
* @returns | ||
*/ | ||
function getLatestRelease(repoOwner, repoName) { | ||
return new Promise((resolve, reject) => { | ||
const options = { | ||
hostname: 'api.github.com', | ||
path: `/repos/${repoOwner}/${repoName}/releases/latest`, | ||
headers: { 'User-Agent': 'mozilla/5.0' }, | ||
} | ||
// set Authorization header set to avoid GitHub API rate limit | ||
if (process.env.GITHUB_TOKEN) { | ||
options.headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}` | ||
} | ||
|
||
const req = https.get(options, (res) => { | ||
let data = '' | ||
|
||
res.on('data', (chunk) => { | ||
data += chunk | ||
}) | ||
|
||
res.on('end', () => { | ||
if (res.statusCode === 200) { | ||
const releaseInfo = JSON.parse(data) | ||
const version = releaseInfo.tag_name | ||
const changelog = releaseInfo.body | ||
const homeUrl = releaseInfo.html_url | ||
resolve({ version, changelog, homeUrl }) | ||
} else { | ||
reject(`Failed to get latest release (${res.statusCode})`) | ||
} | ||
}) | ||
}) | ||
|
||
req.on('error', (err) => { | ||
reject(err.message) | ||
}) | ||
|
||
req.end() | ||
}) | ||
} | ||
|
||
export { | ||
getLatestRelease, | ||
} |