-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathfetchGithubUpstreamVersion.ts
More file actions
48 lines (40 loc) · 1.41 KB
/
fetchGithubUpstreamVersion.ts
File metadata and controls
48 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { Github } from "../../../../providers/github/Github.js";
import { isValidRelease } from "./isValidRelease.js";
import { components } from '@octokit/openapi-types';
type Release = components["schemas"]["release"];
export async function fetchGithubUpstreamVersion(
repo: string
): Promise<string | null> {
try {
const newVersion = await fetchGithubLatestTag(repo);
if (!isValidRelease(newVersion)) {
console.log(
`This is not a valid release (probably a release candidate) - ${repo}: ${newVersion.tag_name}`
);
return null;
}
console.log(`Fetch latest version(s) - ${repo}: ${newVersion.tag_name}`);
return newVersion.tag_name;
} catch (e) {
console.error("Error fetching upstream repo versions:", e);
throw e;
}
}
async function fetchGithubLatestTag(repo: string): Promise<Release> {
const [owner, repoName] = repo.split("/");
const githubRepo = new Github({ owner, repo: repoName });
const releases = await githubRepo.listReleases();
// Check if is empty
if (!releases || releases.length === 0) {
throw Error(`No releases found for ${repo}`);
}
// Filter out draft and prerelease
const validReleases = releases.filter(
release => !release.draft && !release.prerelease
);
if (validReleases.length === 0) {
throw Error(`No valid releases found for ${repo}`);
}
const latestRelease = validReleases[0];
return latestRelease;
}