Skip to content

Commit bfd61cb

Browse files
committed
Use workflow secret to provide API token. Improve feedback to the user.
1 parent 5777c01 commit bfd61cb

File tree

5 files changed

+27
-37
lines changed

5 files changed

+27
-37
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ To request development snapshots, use one of these options:
6666
swift-version: "main-snapshot-2024-08-01"
6767
```
6868

69-
Or you can omit date to lookup for latest available snapshot (note that you may run into GitHub API limits,
70-
which can be avoided if you specify API Token in your project settings)
69+
Or you can omit date to lookup for latest available snapshot
7170

7271
```yaml
7372
swift-version: "main-snapshot"
@@ -85,6 +84,13 @@ If date is ommited, it will lookup for the latest snapshot
8584
swift-version: "5.7-snapshot"
8685
```
8786

87+
Note that you may run into GitHub API limits, which can be avoided if you pass API token via environment variable
88+
89+
```yaml
90+
env:
91+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
92+
```
93+
8894
## Note about versions
8995

9096
This project uses strict semantic versioning to determine what version of Swift to configure. This differs slightly from the official convention used by Swift.

__tests__/snapshot-resolver.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { SnapshotResolver } from "../src/snapshot-resolver";
33
import { readFileSync } from "fs";
44

55
class MockGitHubClient implements GitHubClient {
6+
retryTimeout: number = 0;
7+
68
hasApiToken(): boolean {
79
return true;
810
}

dist/index.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,21 @@ exports.versionFromString = versionFromString;
5151
Object.defineProperty(exports, "__esModule", ({ value: true }));
5252
exports.DefaultGitHubClient = void 0;
5353
class DefaultGitHubClient {
54+
retryTimeout = 5000;
5455
githubToken;
5556
constructor(githubToken = null) {
56-
this.githubToken =
57-
githubToken || process.env.API_GITHUB_ACCESS_TOKEN || null;
57+
this.githubToken = githubToken || process.env.GH_TOKEN || null;
5858
}
5959
hasApiToken() {
6060
return this.githubToken != null && this.githubToken != "";
6161
}
6262
async getTags(page, limit) {
63-
const url = `https://api.github.com/repos/apple/swift/tags?per_page=${limit}&page=${page}`;
63+
const url = `https://api.github.com/repos/swiftlang/swift/tags?per_page=${limit}&page=${page}`;
6464
return await this.get(url);
6565
}
6666
async get(url) {
6767
let headers = {};
68-
if (this.githubToken) {
68+
if (this.hasApiToken()) {
6969
headers = {
7070
Authorization: `Bearer ${this.githubToken}`,
7171
};
@@ -471,6 +471,7 @@ exports.getSystem = getSystem;
471471

472472
Object.defineProperty(exports, "__esModule", ({ value: true }));
473473
exports.SnapshotResolver = void 0;
474+
const core_1 = __nccwpck_require__(2186);
474475
const github_client_1 = __nccwpck_require__(4072);
475476
class SnapshotResolver {
476477
githubClient;
@@ -529,19 +530,8 @@ class SnapshotResolver {
529530
}
530531
async getTags(page) {
531532
let json = await this.githubClient.getTags(page, this.limit);
532-
if (!Array.isArray(json)) {
533-
// try second time after 5s if response not an array of tags
534-
await new Promise((r) => setTimeout(r, 5000));
535-
json = await this.githubClient.getTags(page, this.limit);
536-
}
537-
if (!Array.isArray(json)) {
538-
// fail if couldn't get from second try
539-
let errorMessage = "Failed to retrive snapshot tags. Please, try again later.";
540-
if (!this.githubClient.hasApiToken()) {
541-
errorMessage +=
542-
" To avoid limits specify `API_GITHUB_ACCESS_TOKEN` in your project settings.";
543-
}
544-
throw new Error(errorMessage);
533+
if (!this.githubClient.hasApiToken()) {
534+
(0, core_1.warning)("To avoid hitting limits specify env variable `GH_TOKEN` in your workflow.");
545535
}
546536
const tags = json.map((e) => {
547537
return { name: e.name };

src/github-client.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
export interface GitHubClient {
2+
retryTimeout: number;
23
hasApiToken(): boolean;
34
getTags(page: number, limit: number): Promise<any>;
45
}
56

67
export class DefaultGitHubClient implements GitHubClient {
8+
retryTimeout: number = 5000;
79
private githubToken: string | null;
810

911
constructor(githubToken: string | null = null) {
10-
this.githubToken =
11-
githubToken || process.env.API_GITHUB_ACCESS_TOKEN || null;
12+
this.githubToken = githubToken || process.env.GH_TOKEN || null;
1213
}
1314

1415
hasApiToken(): boolean {
1516
return this.githubToken != null && this.githubToken != "";
1617
}
1718

1819
async getTags(page: number, limit: number): Promise<any> {
19-
const url = `https://api.github.com/repos/apple/swift/tags?per_page=${limit}&page=${page}`;
20+
const url = `https://api.github.com/repos/swiftlang/swift/tags?per_page=${limit}&page=${page}`;
2021
return await this.get(url);
2122
}
2223

2324
private async get(url: string): Promise<any> {
2425
let headers = {};
25-
if (this.githubToken) {
26+
if (this.hasApiToken()) {
2627
headers = {
2728
Authorization: `Bearer ${this.githubToken}`,
2829
};

src/snapshot-resolver.ts

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { warning } from "@actions/core";
12
import { DefaultGitHubClient, GitHubClient } from "./github-client";
23
import { System } from "./os";
34

@@ -77,20 +78,10 @@ export class SnapshotResolver {
7778

7879
private async getTags(page: number): Promise<Tag[]> {
7980
let json = await this.githubClient.getTags(page, this.limit);
80-
if (!Array.isArray(json)) {
81-
// try second time after 5s if response not an array of tags
82-
await new Promise((r) => setTimeout(r, 5000));
83-
json = await this.githubClient.getTags(page, this.limit);
84-
}
85-
if (!Array.isArray(json)) {
86-
// fail if couldn't get from second try
87-
let errorMessage =
88-
"Failed to retrive snapshot tags. Please, try again later.";
89-
if (!this.githubClient.hasApiToken()) {
90-
errorMessage +=
91-
" To avoid limits specify `API_GITHUB_ACCESS_TOKEN` in your project settings.";
92-
}
93-
throw new Error(errorMessage);
81+
if (!this.githubClient.hasApiToken()) {
82+
warning(
83+
"To avoid hitting limits specify env variable `GH_TOKEN` in your workflow."
84+
);
9485
}
9586
const tags: Tag[] = json.map((e: any) => {
9687
return { name: e.name };

0 commit comments

Comments
 (0)