Skip to content

Commit

Permalink
Use GitHub token to avoid hitting rate limiting (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Massimiliano Pippi authored Oct 22, 2019
1 parent c579dc0 commit 7562b44
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 40 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
pull_request:
pull_request:

jobs:
test:
Expand All @@ -21,12 +21,14 @@ jobs:
- name: Set Node.js 10.x
uses: actions/setup-node@master
with:
version: 10.x
node-version: 10.x

- name: npm install
run: npm install

- name: npm lint
# check style only once
if: matrix.operating-system == 'ubuntu-latest'
run: npm run format-check

- name: npm test
Expand Down
8 changes: 4 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("installer tests", () => {
});

it("Downloads version of protoc if no matching version is installed", async () => {
await installer.getProtoc("3.9.0", true);
await installer.getProtoc("3.9.0", true, "");
const protocDir = path.join(toolDir, "protoc", "3.9.0", os.arch());

expect(fs.existsSync(`${protocDir}.complete`)).toBe(true);
Expand All @@ -58,7 +58,7 @@ describe("installer tests", () => {
});

it("Gets the latest 3.7.x version of protoc using 3.7 and no matching version is installed", async () => {
await installer.getProtoc("3.7", true);
await installer.getProtoc("3.7", true, "");
const protocDir = path.join(toolDir, "protoc", "3.7.1", os.arch());

expect(fs.existsSync(`${protocDir}.complete`)).toBe(true);
Expand All @@ -72,7 +72,7 @@ describe("installer tests", () => {
}, 100000);

it("Gets latest version of protoc using 3.x and no matching version is installed", async () => {
await installer.getProtoc("3.x", true);
await installer.getProtoc("3.x", true, "");
const protocDir = path.join(toolDir, "protoc", "3.9.1", os.arch());

expect(fs.existsSync(`${protocDir}.complete`)).toBe(true);
Expand All @@ -99,7 +99,7 @@ describe("installer tests", () => {
});

it("Gets latest version of protoc using 3.x with a broken rc tag, filtering pre-releases", async () => {
await installer.getProtoc("3.x", false);
await installer.getProtoc("3.x", false, "");
const protocDir = path.join(toolDir, "protoc", "3.9.1", os.arch());

expect(fs.existsSync(`${protocDir}.complete`)).toBe(true);
Expand Down
37 changes: 22 additions & 15 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ const exc = __importStar(require("@actions/exec"));
const io = __importStar(require("@actions/io"));
let osPlat = os.platform();
let osArch = os.arch();
function getProtoc(version, includePreReleases) {
function getProtoc(version, includePreReleases, repoToken) {
return __awaiter(this, void 0, void 0, function* () {
// resolve the version number
const targetVersion = yield computeVersion(version, includePreReleases);
const targetVersion = yield computeVersion(version, includePreReleases, repoToken);
if (targetVersion) {
version = targetVersion;
}
process.stdout.write("Getting protoc version: " + version + os.EOL);
// look if the binary is cached
let toolPath;
toolPath = tc.find("protoc", version);
// if not: download, extract and cache
if (!toolPath) {
toolPath = yield downloadRelease(version);
core.debug("Protoc cached under " + toolPath);
process.stdout.write("Protoc cached under " + toolPath + os.EOL);
}
// add the bin folder to the PATH
toolPath = path.join(toolPath, "bin");
Expand Down Expand Up @@ -89,6 +90,7 @@ function downloadRelease(version) {
// Download
let fileName = getFileName(version);
let downloadUrl = util.format("https://github.com/protocolbuffers/protobuf/releases/download/%s/%s", version, fileName);
process.stdout.write("Downloading archive: " + downloadUrl + os.EOL);
let downloadPath = null;
try {
downloadPath = yield tc.downloadTool(downloadUrl);
Expand All @@ -114,13 +116,23 @@ function getFileName(version) {
return util.format("protoc-%s-win%s.zip", version, arch);
}
const arch = osArch == "x64" ? "x86_64" : "x86_32";
const filename = util.format("protoc-%s-linux-%s.zip", version, arch);
return filename;
if (osPlat == "darwin") {
return util.format("protoc-%s-osx-%s.zip", version, arch);
}
return util.format("protoc-%s-linux-%s.zip", version, arch);
}
// Retrieve a list of versions scraping tags from the Github API
function fetchVersions(includePreReleases) {
function fetchVersions(includePreReleases, repoToken) {
return __awaiter(this, void 0, void 0, function* () {
let rest = new restm.RestClient("setup-protoc");
let rest;
if (repoToken != "") {
rest = new restm.RestClient("setup-protoc", "", [], {
headers: { Authorization: "Bearer " + repoToken }
});
}
else {
rest = new restm.RestClient("setup-protoc");
}
let tags = (yield rest.get("https://api.github.com/repos/protocolbuffers/protobuf/releases")).result || [];
return tags
.filter(tag => tag.tag_name.match(/v\d+\.[\w\.]+/g))
Expand All @@ -129,7 +141,7 @@ function fetchVersions(includePreReleases) {
});
}
// Compute an actual version starting from the `version` configuration param.
function computeVersion(version, includePreReleases) {
function computeVersion(version, includePreReleases, repoToken) {
return __awaiter(this, void 0, void 0, function* () {
// strip leading `v` char (will be re-added later)
if (version.startsWith("v")) {
Expand All @@ -139,7 +151,7 @@ function computeVersion(version, includePreReleases) {
if (version.endsWith(".x")) {
version = version.slice(0, version.length - 2);
}
const allVersions = yield fetchVersions(includePreReleases);
const allVersions = yield fetchVersions(includePreReleases, repoToken);
const validVersions = allVersions.filter(v => semver.valid(v));
const possibleVersions = validVersions.filter(v => v.startsWith(version));
const versionMap = new Map();
Expand Down Expand Up @@ -195,10 +207,5 @@ function normalizeVersion(version) {
return version;
}
function includePrerelease(isPrerelease, includePrereleases) {
if (!includePrereleases) {
if (isPrerelease) {
return false;
}
}
return true;
return includePrereleases || !isPrerelease;
}
3 changes: 2 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function run() {
try {
let version = core.getInput("version");
let includePreReleases = convertToBoolean(core.getInput("include-pre-releases"));
yield installer.getProtoc(version, includePreReleases);
let repoToken = core.getInput("repo-token");
yield installer.getProtoc(version, includePreReleases, repoToken);
}
catch (error) {
core.setFailed(error.message);
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

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

37 changes: 30 additions & 7 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ interface IProtocRelease {
prerelease: boolean;
}

export async function getProtoc(version: string, includePreReleases: boolean) {
export async function getProtoc(
version: string,
includePreReleases: boolean,
repoToken: string
) {
// resolve the version number
const targetVersion = await computeVersion(version, includePreReleases);
const targetVersion = await computeVersion(
version,
includePreReleases,
repoToken
);
if (targetVersion) {
version = targetVersion;
}
process.stdout.write("Getting protoc version: " + version + os.EOL);

// look if the binary is cached
let toolPath: string;
Expand All @@ -49,7 +58,7 @@ export async function getProtoc(version: string, includePreReleases: boolean) {
// if not: download, extract and cache
if (!toolPath) {
toolPath = await downloadRelease(version);
core.debug("Protoc cached under " + toolPath);
process.stdout.write("Protoc cached under " + toolPath + os.EOL);
}

// add the bin folder to the PATH
Expand Down Expand Up @@ -88,6 +97,8 @@ async function downloadRelease(version: string): Promise<string> {
version,
fileName
);
process.stdout.write("Downloading archive: " + downloadUrl + os.EOL);

let downloadPath: string | null = null;
try {
downloadPath = await tc.downloadTool(downloadUrl);
Expand Down Expand Up @@ -125,8 +136,19 @@ function getFileName(version: string): string {
}

// Retrieve a list of versions scraping tags from the Github API
async function fetchVersions(includePreReleases: boolean): Promise<string[]> {
let rest: restm.RestClient = new restm.RestClient("setup-protoc");
async function fetchVersions(
includePreReleases: boolean,
repoToken: string
): Promise<string[]> {
let rest: restm.RestClient;
if (repoToken != "") {
rest = new restm.RestClient("setup-protoc", "", [], {
headers: { Authorization: "Bearer " + repoToken }
});
} else {
rest = new restm.RestClient("setup-protoc");
}

let tags: IProtocRelease[] =
(await rest.get<IProtocRelease[]>(
"https://api.github.com/repos/protocolbuffers/protobuf/releases"
Expand All @@ -141,7 +163,8 @@ async function fetchVersions(includePreReleases: boolean): Promise<string[]> {
// Compute an actual version starting from the `version` configuration param.
async function computeVersion(
version: string,
includePreReleases: boolean
includePreReleases: boolean,
repoToken: string
): Promise<string> {
// strip leading `v` char (will be re-added later)
if (version.startsWith("v")) {
Expand All @@ -153,7 +176,7 @@ async function computeVersion(
version = version.slice(0, version.length - 2);
}

const allVersions = await fetchVersions(includePreReleases);
const allVersions = await fetchVersions(includePreReleases, repoToken);
const validVersions = allVersions.filter(v => semver.valid(v));
const possibleVersions = validVersions.filter(v => v.startsWith(version));

Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ async function run() {
let includePreReleases = convertToBoolean(
core.getInput("include-pre-releases")
);
await installer.getProtoc(version, includePreReleases);
let repoToken = core.getInput("repo-token");
await installer.getProtoc(version, includePreReleases, repoToken);
} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit 7562b44

Please sign in to comment.