Skip to content

Commit

Permalink
fix: fetch changelog files via github api
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed May 3, 2019
1 parent 0ce5b91 commit f8113be
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 11 deletions.
7 changes: 5 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"extends": [
"@jedwards1211/eslint-config","@jedwards1211/eslint-config-flow", "eslint-config-prettier"
"@jedwards1211/eslint-config",
"@jedwards1211/eslint-config-flow",
"eslint-config-prettier"
],
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"es6": true
"es6": true,
"node": true
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"@babel/runtime": "^7.1.5",
"@octokit/rest": "^16.25.1",
"chalk": "^2.4.2",
"node-fetch": "^2.5.0",
"js-base64": "^2.5.1",
"npm-registry-fetch": "^3.9.0",
"semver": "^6.0.0"
},
Expand Down
23 changes: 23 additions & 0 deletions src/getNpmToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @flow

import os from 'os'
import fs from 'fs-extra'
import once from './util/once'

const getNpmToken = once(
async (env: { [name: string]: ?string } = process.env): Promise<?string> => {
const { NPM_TOKEN } = env
if (NPM_TOKEN) return NPM_TOKEN
try {
const homedir = os.homedir()
const npmrc = await fs.readFile(`${homedir}/.npmrc`)
const match = /:_authToken=([a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12})/.exec(
npmrc
)
if (match) return match[1]
} catch (error) {
return null
}
}
)
export default getNpmToken
26 changes: 19 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

import chalk from 'chalk'
import npmRegistryFetch from 'npm-registry-fetch'
import fetch from 'node-fetch'
import { Base64 } from 'js-base64'
import parseChangelog, { type Release } from './changelog-parser'
import semver from 'semver'
import Octokit from '@octokit/rest'
import getNpmToken from './getNpmToken'

const { GH_TOKEN } = process.env

const octokitOptions = {}
if (process.env.GH_TOKEN) octokitOptions.auth = `token ${process.env.GH_TOKEN}`
if (GH_TOKEN) octokitOptions.auth = `token ${GH_TOKEN}`
const octokit = new Octokit(octokitOptions)

export async function getChangelog(
Expand All @@ -18,11 +21,18 @@ export async function getChangelog(
): Promise<Array<Release>> {
let changelog
for (const file of ['CHANGELOG.md', 'changelog.md']) {
const changelogUrl = `https://raw.githubusercontent.com/${owner}/${repo}/master/${file}`
const res = await fetch(changelogUrl)
if (res.ok) {
changelog = await res.text()
try {
const {
data: { content },
} = await octokit.repos.getContents({
owner,
repo,
path: file,
})
changelog = Base64.decode(content)
break
} catch (error) {
continue
}
}
if (!changelog) throw new Error('failed to get changelog')
Expand All @@ -39,7 +49,9 @@ export async function whatBroke(
toVersion?: ?string,
} = {}
): Promise<Object> {
const npmInfo = await npmRegistryFetch.json(pkg)
const npmInfo = await npmRegistryFetch.json(pkg, {
token: await getNpmToken(),
})
const { repository: { url } = {} } = npmInfo
if (!url) throw new Error('failed to get repository.url')
const match = /github\.com\/([^\\]+)\/([^.\\]+)/i.exec(url)
Expand Down
14 changes: 14 additions & 0 deletions src/util/once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @flow
* @prettier
*/

export default function once<F: (...any[]) => any>(fn: F): F {
let result: any
let called = false
return ((...args: Array<any>): any => {
if (called) return result
called = true
return (result = fn(...args))
}: any)
}
20 changes: 20 additions & 0 deletions util/once.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = once;

/**
*
* @prettier
*/
function once(fn) {
var result;
var called = false;
return function () {
if (called) return result;
called = true;
return result = fn.apply(void 0, arguments);
};
}
14 changes: 14 additions & 0 deletions util/once.js.flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @flow
* @prettier
*/

export default function once<F: (...any[]) => any>(fn: F): F {
let result: any
let called = false
return ((...args: Array<any>): any => {
if (called) return result
called = true
return (result = fn(...args))
}: any)
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4489,6 +4489,11 @@ jest-validate@^23.5.0:
leven "^2.1.0"
pretty-format "^23.6.0"

js-base64@^2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.5.1.tgz#1efa39ef2c5f7980bb1784ade4a8af2de3291121"
integrity sha512-M7kLczedRMYX4L8Mdh4MzyAMM9O5osx+4FcOQuTvr3A9F2D9S5JXheN0ewNbrvK2UatkTRhL5ejGmGSjNMiZuw==

js-levenshtein@^1.1.3:
version "1.1.4"
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.4.tgz#3a56e3cbf589ca0081eb22cd9ba0b1290a16d26e"
Expand Down Expand Up @@ -5532,7 +5537,7 @@ node-fetch@^2.1.1:
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==

node-fetch@^2.3.0, node-fetch@^2.5.0:
node-fetch@^2.3.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.5.0.tgz#8028c49fc1191bba56a07adc6e2a954644a48501"
integrity sha512-YuZKluhWGJwCcUu4RlZstdAxr8bFfOVHakc1mplwHkk8J+tqM1Y5yraYvIUpeX8aY7+crCwiELJq7Vl0o0LWXw==
Expand Down

0 comments on commit f8113be

Please sign in to comment.