Skip to content

Commit

Permalink
Add github_token input (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsok authored Oct 26, 2021
1 parent de9c423 commit b36ffc1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ steps:
- run: buf --version
```

Optionally, you can supply a `github_token` input so that any GitHub API
requests are authenticated, avoiding any potential rate limit issues when
running on GitHub hosted runners:

```yaml
steps:
- uses: bufbuild/buf-setup-action@v0.5.0
with:
github_token: ${{ github.token }}
version: 'latest'
```

The `buf-setup` action is commonly used by the other `buf` actions,
such as [buf-breaking][1], [buf-lint][2], and [buf-push][3].

Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ inputs:
version:
description: 'The version of buf to setup.'
default: '1.0.0-rc6'
github_token:
description: 'GitHub token to use when making API requests'
runs:
using: 'node12'
main: './dist/main.js'
6 changes: 3 additions & 3 deletions src/buf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Error, isError } from './error';
// optionally be specified in the action's version parameter.
const versionPrefix = "v";

export async function getBuf(version: string): Promise<string|Error> {
export async function getBuf(version: string, token: string): Promise<string|Error> {
const binaryPath = tc.find('buf', version, os.arch());
if (binaryPath !== '') {
core.info(`Found in cache @ ${binaryPath}`);
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function getBuf(version: string): Promise<string|Error> {

// getDownloadURL resolves Buf's Github download URL for the
// current architecture and platform.
async function getDownloadURL(version: string): Promise<string|Error> {
async function getDownloadURL(version: string, token: string): Promise<string|Error> {
let architecture = '';
switch (os.arch()) {
// The available architectures can be found at:
Expand Down Expand Up @@ -111,7 +111,7 @@ async function getDownloadURL(version: string): Promise<string|Error> {
} else {
assetName = `buf-${platform}-${architecture}.tar.gz`
}
const octokit = new Octokit();
const octokit = new Octokit({ auth: token });
const {data: releases} = await octokit.request(
'GET /repos/{owner}/{repo}/releases',
{
Expand Down
8 changes: 7 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ async function runSetup(): Promise<null|Error> {
};
}

const githubToken = core.getInput('github_token');
if (githubToken === '') {
core.warning('No github_token supplied, API requests will be subject to stricter rate limiting');
}


core.info(`Setting up buf version "${version}"`);
const installDir = await getBuf(version);
const installDir = await getBuf(version, githubToken);
if (isError(installDir)) {
return installDir
}
Expand Down

0 comments on commit b36ffc1

Please sign in to comment.