Skip to content

Commit

Permalink
Avoid rate limiting by reducing GitHub API calls
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <sora@morimoto.io>
  • Loading branch information
smorimoto committed Oct 1, 2021
1 parent 219e2a8 commit 5e29c34
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 51 deletions.
44 changes: 27 additions & 17 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 27 additions & 17 deletions dist/post/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 29 additions & 17 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as github from "@actions/github";
import * as path from "path";
import * as semver from "semver";

import { GITHUB_TOKEN } from "./constants";
import { GITHUB_TOKEN, Platform } from "./constants";
import { getPlatform } from "./system";

export function isSemverStyle(semverVersion: string): boolean {
const result = semver.validRange(semverVersion, { loose: true });
Expand All @@ -22,24 +24,34 @@ function unique(array: string[]) {

async function getAllCompilerVersions(): Promise<string[]> {
const octokit = github.getOctokit(GITHUB_TOKEN);
const releases = [];
const state = { continue: true, count: 0 };
while (state.continue) {
const response = await octokit.rest.repos.listReleases({
owner: "ocaml",
repo: "ocaml",
per_page: 100,
page: state.count,
});
if (response.data.length > 0) {
releases.push(...response.data);
state.count++;
} else {
state.continue = false;
const platform = getPlatform();
const owner = platform === Platform.Win32 ? "fdopen" : "ocaml";
const repo =
platform === Platform.Win32 ? "opam-repository-mingw" : "opam-repository";
const prefix =
platform === Platform.Win32 ? "ocaml-variants" : "ocaml-base-compiler";
const { data: packages } = await octokit.rest.repos.getContent({
owner,
repo,
path: `packages/${prefix}`,
});
if (Array.isArray(packages)) {
const versions = [];
for (const { path: p } of packages) {
const basename = path.basename(p);
const version = basename.replace(`${prefix}.`, "");
const parsed = semver.parse(version, { loose: true });
if (parsed !== null) {
const { major, minor: _minor, patch } = parsed;
const minor = _minor.toString().length > 1 ? _minor : `0${_minor}`;
const version = `${major}.${minor}.${patch}`;
versions.push(version);
}
}
return unique(versions);
} else {
throw new Error("Failed to get compiler list from opam-repository.");
}
const versions = unique(releases.map(({ tag_name }) => tag_name));
return versions;
}

export async function resolveVersion(semverVersion: string): Promise<string> {
Expand Down

0 comments on commit 5e29c34

Please sign in to comment.