forked from silverwind/updates
refactor api fetches
This commit is contained in:
parent
c33e97fce0
commit
350ed7f535
40
updates.js
40
updates.js
@ -516,6 +516,27 @@ function findNewVersion(data, {mode, range, useGreatest, useRel, usePre, semvers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fetchGitHub(url) {
|
||||||
|
const opts = {maxSockets};
|
||||||
|
const token = env.UPDATES_GITHUB_API_TOKEN || env.GITHUB_API_TOKEN || env.GH_TOKEN || env.HOMEBREW_GITHUB_API_TOKEN;
|
||||||
|
if (token) {
|
||||||
|
opts.headers = {Authorization: `Bearer ${token}`};
|
||||||
|
}
|
||||||
|
return fetch(url, opts);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getLastestCommit(user, repo) {
|
||||||
|
const url = `${githubApiUrl}/repos/${user}/${repo}/commits`;
|
||||||
|
if (args.verbose) console.error(`${magenta("fetch")} ${url}`);
|
||||||
|
const res = await fetchGitHub(url);
|
||||||
|
if (args.verbose && res?.ok) console.error(`${green("done")} ${url}`);
|
||||||
|
|
||||||
|
if (!res || !res.ok) return;
|
||||||
|
const data = await res.json();
|
||||||
|
const {sha: hash, commit} = data[0];
|
||||||
|
return {hash, commit};
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: refactor this mess
|
// TODO: refactor this mess
|
||||||
async function checkUrlDep([key, dep], {useGreatest} = {}) {
|
async function checkUrlDep([key, dep], {useGreatest} = {}) {
|
||||||
const stripped = dep.old.replace(stripRe, "");
|
const stripped = dep.old.replace(stripRe, "");
|
||||||
@ -523,24 +544,11 @@ async function checkUrlDep([key, dep], {useGreatest} = {}) {
|
|||||||
if (!user || !repo || !oldRef) return;
|
if (!user || !repo || !oldRef) return;
|
||||||
|
|
||||||
if (hashRe.test(oldRef)) {
|
if (hashRe.test(oldRef)) {
|
||||||
const opts = {maxSockets};
|
const {hash, commit} = await getLastestCommit(user, repo);
|
||||||
const token = env.UPDATES_GITHUB_API_TOKEN || env.GITHUB_API_TOKEN || env.GH_TOKEN || env.HOMEBREW_GITHUB_API_TOKEN;
|
if (!hash?.length) return;
|
||||||
if (token) {
|
|
||||||
opts.headers = {Authorization: `Bearer ${token}`};
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = `${githubApiUrl}/repos/${user}/${repo}/commits`;
|
|
||||||
if (args.verbose) console.error(`${magenta("fetch")} ${url}`);
|
|
||||||
const res = await fetch(url, opts);
|
|
||||||
if (args.verbose && res?.ok) console.error(`${green("done")} ${url}`);
|
|
||||||
|
|
||||||
if (!res || !res.ok) return;
|
|
||||||
const data = await res.json();
|
|
||||||
let {sha: newRef, commit} = data[0];
|
|
||||||
if (!newRef || !newRef.length) return;
|
|
||||||
|
|
||||||
const newDate = commit?.committer?.date ?? commit?.author?.date;
|
const newDate = commit?.committer?.date ?? commit?.author?.date;
|
||||||
newRef = newRef.substring(0, oldRef.length);
|
const newRef = hash.substring(0, oldRef.length);
|
||||||
if (oldRef !== newRef) {
|
if (oldRef !== newRef) {
|
||||||
const newRange = dep.old.replace(oldRef, newRef);
|
const newRange = dep.old.replace(oldRef, newRef);
|
||||||
return {key, newRange, user, repo, oldRef, newRef, newDate};
|
return {key, newRange, user, repo, oldRef, newRef, newDate};
|
||||||
|
Loading…
Reference in New Issue
Block a user