Skip to content

Commit 7562b44

Browse files
author
Massimiliano Pippi
authored
Use GitHub token to avoid hitting rate limiting (#1)
1 parent c579dc0 commit 7562b44

File tree

7 files changed

+74
-40
lines changed

7 files changed

+74
-40
lines changed

.github/workflows/test.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
push:
55
branches:
66
- master
7-
pull_request:
7+
pull_request:
88

99
jobs:
1010
test:
@@ -21,12 +21,14 @@ jobs:
2121
- name: Set Node.js 10.x
2222
uses: actions/setup-node@master
2323
with:
24-
version: 10.x
24+
node-version: 10.x
2525

2626
- name: npm install
2727
run: npm install
2828

2929
- name: npm lint
30+
# check style only once
31+
if: matrix.operating-system == 'ubuntu-latest'
3032
run: npm run format-check
3133

3234
- name: npm test

__tests__/main.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe("installer tests", () => {
3131
});
3232

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

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

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

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

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

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

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

105105
expect(fs.existsSync(`${protocDir}.complete`)).toBe(true);

lib/installer.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,21 @@ const exc = __importStar(require("@actions/exec"));
4444
const io = __importStar(require("@actions/io"));
4545
let osPlat = os.platform();
4646
let osArch = os.arch();
47-
function getProtoc(version, includePreReleases) {
47+
function getProtoc(version, includePreReleases, repoToken) {
4848
return __awaiter(this, void 0, void 0, function* () {
4949
// resolve the version number
50-
const targetVersion = yield computeVersion(version, includePreReleases);
50+
const targetVersion = yield computeVersion(version, includePreReleases, repoToken);
5151
if (targetVersion) {
5252
version = targetVersion;
5353
}
54+
process.stdout.write("Getting protoc version: " + version + os.EOL);
5455
// look if the binary is cached
5556
let toolPath;
5657
toolPath = tc.find("protoc", version);
5758
// if not: download, extract and cache
5859
if (!toolPath) {
5960
toolPath = yield downloadRelease(version);
60-
core.debug("Protoc cached under " + toolPath);
61+
process.stdout.write("Protoc cached under " + toolPath + os.EOL);
6162
}
6263
// add the bin folder to the PATH
6364
toolPath = path.join(toolPath, "bin");
@@ -89,6 +90,7 @@ function downloadRelease(version) {
8990
// Download
9091
let fileName = getFileName(version);
9192
let downloadUrl = util.format("https://github.com/protocolbuffers/protobuf/releases/download/%s/%s", version, fileName);
93+
process.stdout.write("Downloading archive: " + downloadUrl + os.EOL);
9294
let downloadPath = null;
9395
try {
9496
downloadPath = yield tc.downloadTool(downloadUrl);
@@ -114,13 +116,23 @@ function getFileName(version) {
114116
return util.format("protoc-%s-win%s.zip", version, arch);
115117
}
116118
const arch = osArch == "x64" ? "x86_64" : "x86_32";
117-
const filename = util.format("protoc-%s-linux-%s.zip", version, arch);
118-
return filename;
119+
if (osPlat == "darwin") {
120+
return util.format("protoc-%s-osx-%s.zip", version, arch);
121+
}
122+
return util.format("protoc-%s-linux-%s.zip", version, arch);
119123
}
120124
// Retrieve a list of versions scraping tags from the Github API
121-
function fetchVersions(includePreReleases) {
125+
function fetchVersions(includePreReleases, repoToken) {
122126
return __awaiter(this, void 0, void 0, function* () {
123-
let rest = new restm.RestClient("setup-protoc");
127+
let rest;
128+
if (repoToken != "") {
129+
rest = new restm.RestClient("setup-protoc", "", [], {
130+
headers: { Authorization: "Bearer " + repoToken }
131+
});
132+
}
133+
else {
134+
rest = new restm.RestClient("setup-protoc");
135+
}
124136
let tags = (yield rest.get("https://api.github.com/repos/protocolbuffers/protobuf/releases")).result || [];
125137
return tags
126138
.filter(tag => tag.tag_name.match(/v\d+\.[\w\.]+/g))
@@ -129,7 +141,7 @@ function fetchVersions(includePreReleases) {
129141
});
130142
}
131143
// Compute an actual version starting from the `version` configuration param.
132-
function computeVersion(version, includePreReleases) {
144+
function computeVersion(version, includePreReleases, repoToken) {
133145
return __awaiter(this, void 0, void 0, function* () {
134146
// strip leading `v` char (will be re-added later)
135147
if (version.startsWith("v")) {
@@ -139,7 +151,7 @@ function computeVersion(version, includePreReleases) {
139151
if (version.endsWith(".x")) {
140152
version = version.slice(0, version.length - 2);
141153
}
142-
const allVersions = yield fetchVersions(includePreReleases);
154+
const allVersions = yield fetchVersions(includePreReleases, repoToken);
143155
const validVersions = allVersions.filter(v => semver.valid(v));
144156
const possibleVersions = validVersions.filter(v => v.startsWith(version));
145157
const versionMap = new Map();
@@ -195,10 +207,5 @@ function normalizeVersion(version) {
195207
return version;
196208
}
197209
function includePrerelease(isPrerelease, includePrereleases) {
198-
if (!includePrereleases) {
199-
if (isPrerelease) {
200-
return false;
201-
}
202-
}
203-
return true;
210+
return includePrereleases || !isPrerelease;
204211
}

lib/main.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function run() {
2222
try {
2323
let version = core.getInput("version");
2424
let includePreReleases = convertToBoolean(core.getInput("include-pre-releases"));
25-
yield installer.getProtoc(version, includePreReleases);
25+
let repoToken = core.getInput("repo-token");
26+
yield installer.getProtoc(version, includePreReleases, repoToken);
2627
}
2728
catch (error) {
2829
core.setFailed(error.message);

package-lock.json

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/installer.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@ interface IProtocRelease {
3535
prerelease: boolean;
3636
}
3737

38-
export async function getProtoc(version: string, includePreReleases: boolean) {
38+
export async function getProtoc(
39+
version: string,
40+
includePreReleases: boolean,
41+
repoToken: string
42+
) {
3943
// resolve the version number
40-
const targetVersion = await computeVersion(version, includePreReleases);
44+
const targetVersion = await computeVersion(
45+
version,
46+
includePreReleases,
47+
repoToken
48+
);
4149
if (targetVersion) {
4250
version = targetVersion;
4351
}
52+
process.stdout.write("Getting protoc version: " + version + os.EOL);
4453

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

5564
// add the bin folder to the PATH
@@ -88,6 +97,8 @@ async function downloadRelease(version: string): Promise<string> {
8897
version,
8998
fileName
9099
);
100+
process.stdout.write("Downloading archive: " + downloadUrl + os.EOL);
101+
91102
let downloadPath: string | null = null;
92103
try {
93104
downloadPath = await tc.downloadTool(downloadUrl);
@@ -125,8 +136,19 @@ function getFileName(version: string): string {
125136
}
126137

127138
// Retrieve a list of versions scraping tags from the Github API
128-
async function fetchVersions(includePreReleases: boolean): Promise<string[]> {
129-
let rest: restm.RestClient = new restm.RestClient("setup-protoc");
139+
async function fetchVersions(
140+
includePreReleases: boolean,
141+
repoToken: string
142+
): Promise<string[]> {
143+
let rest: restm.RestClient;
144+
if (repoToken != "") {
145+
rest = new restm.RestClient("setup-protoc", "", [], {
146+
headers: { Authorization: "Bearer " + repoToken }
147+
});
148+
} else {
149+
rest = new restm.RestClient("setup-protoc");
150+
}
151+
130152
let tags: IProtocRelease[] =
131153
(await rest.get<IProtocRelease[]>(
132154
"https://api.github.com/repos/protocolbuffers/protobuf/releases"
@@ -141,7 +163,8 @@ async function fetchVersions(includePreReleases: boolean): Promise<string[]> {
141163
// Compute an actual version starting from the `version` configuration param.
142164
async function computeVersion(
143165
version: string,
144-
includePreReleases: boolean
166+
includePreReleases: boolean,
167+
repoToken: string
145168
): Promise<string> {
146169
// strip leading `v` char (will be re-added later)
147170
if (version.startsWith("v")) {
@@ -153,7 +176,7 @@ async function computeVersion(
153176
version = version.slice(0, version.length - 2);
154177
}
155178

156-
const allVersions = await fetchVersions(includePreReleases);
179+
const allVersions = await fetchVersions(includePreReleases, repoToken);
157180
const validVersions = allVersions.filter(v => semver.valid(v));
158181
const possibleVersions = validVersions.filter(v => v.startsWith(version));
159182

src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ async function run() {
77
let includePreReleases = convertToBoolean(
88
core.getInput("include-pre-releases")
99
);
10-
await installer.getProtoc(version, includePreReleases);
10+
let repoToken = core.getInput("repo-token");
11+
await installer.getProtoc(version, includePreReleases, repoToken);
1112
} catch (error) {
1213
core.setFailed(error.message);
1314
}

0 commit comments

Comments
 (0)