Skip to content

Commit

Permalink
Updated mechanism to get Github API token.
Browse files Browse the repository at this point in the history
Replaced explicit action input with environment variable. This is
the recommended method for retrieving an API token instead of an
input to avoid accidental leakage.

- Replaced `getInput` call to get API token with call to `process.env`.
- Updated unit tests to reflect the prior change.
  - Removed mocks for `getInput` and no-longer-used import.
  - Added automatic assignment for `process.env.GITHUB_TOKEN` to jest
    config.
- Updated test runner to pass token via environment variable.
- Updated README to include env customizations.
  • Loading branch information
MrFlynn committed Jul 7, 2024
1 parent 6c2ade4 commit 16ad0d6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ jobs:
id: run-action
with:
script: examples/script.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: 'echo "${{ steps.run-action.outputs.output }}" | jq'
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Actions.

# Path to script executed by Flyscrape. This input is required.
script: ''
env:
# Required to access Github API to find download URL for specific Flyscrape
# version.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
## Getting Script Output
Expand Down
13 changes: 0 additions & 13 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { DeepPick } from "ts-deep-pick";

import { getInput } from "@actions/core";
import { getOctokit } from "@actions/github";
import { components } from "@octokit/openapi-types";

Expand All @@ -19,10 +18,6 @@ type OnlyAssetURLForRelease = DeepPick<
>;

// Setup mocks
jest.mock("@actions/core", () => ({
getInput: jest.fn(),
}));

jest.mock("@actions/github", () => ({
getOctokit: jest.fn(),
}));
Expand All @@ -33,8 +28,6 @@ describe("getAssetURL", () => {
});

it("return url for v0.8.0 for linux/amd64", async () => {
(getInput as jest.Mock).mockReturnValueOnce("gh-token");

const expectedURL =
"https://github.com/philippta/flyscrape/releases/download/v0.8.0/flyscrape_linux_amd64.tar.gz";
const mockOctokit = {
Expand All @@ -59,8 +52,6 @@ describe("getAssetURL", () => {
});

it("return v0.9.0 url for latest for linux/amd64", async () => {
(getInput as jest.Mock).mockReturnValueOnce("gh-token");

const mockOctokit = {
rest: {
repos: {
Expand Down Expand Up @@ -102,8 +93,6 @@ describe("getAssetURL", () => {
});

it("throws error for invalid latest version", async () => {
(getInput as jest.Mock).mockReturnValueOnce("gh-token");

const mockOctokit = {
rest: {
repos: {
Expand All @@ -126,8 +115,6 @@ describe("getAssetURL", () => {
});

it("throws error if unable to locate specified version", async () => {
(getInput as jest.Mock).mockReturnValueOnce("gh-token");

const expectedURL =
"https://github.com/philippta/flyscrape/releases/download/v0.8.0/flyscrape_linux_amd64.tar.gz";
const mockOctokit = {
Expand Down
4 changes: 4 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ module.exports = {
testEnvironment: "node",
verbose: true,
};

process.env = Object.assign(process.env, {
GITHUB_TOKEN: "gh-token",
});
2 changes: 1 addition & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function getTagFromVersion(version: string): Promise<string> {
return version;
}

const octokit = github.getOctokit(core.getInput("github-token"));
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);

var tags: string[] = [];
const { data } = await octokit.rest.repos.listTags({
Expand Down

0 comments on commit 16ad0d6

Please sign in to comment.