Skip to content

Commit 7720fcc

Browse files
authored
feat: Add support for with.token (#106)
1 parent 0fbf6d3 commit 7720fcc

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ steps:
6262
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6363
```
6464

65+
or named `token` using [`with:`](https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepswith)
66+
67+
```yml
68+
steps:
69+
- name: My action
70+
with:
71+
token: ${{ secrets.GITHUB_TOKEN }}
72+
```
73+
6574
`GITHUB_TOKEN` can be set to any of the repository's secret, e.g. if you want to use a personal access token.
6675

6776
```yml
@@ -73,7 +82,7 @@ steps:
7382

7483
`createActionAuth()` is also checking for the `GITHUB_ACTION` variable to be present to make sure that it runs within a GitHub Action.
7584

76-
If `GITHUB_ACTION` or either `GITHUB_TOKEN` or `INPUT_GITHUB_TOKEN` is not set an error is thrown.
85+
If `GITHUB_ACTION` or neither `GITHUB_TOKEN`, `INPUT_GITHUB_TOKEN` or `INPUT_TOKEN` are set an error is thrown.
7786

7887
## `auth()`
7988

src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ export const createActionAuth: StrategyInterface<
1919
);
2020
}
2121

22-
if (!process.env.GITHUB_TOKEN && !process.env.INPUT_GITHUB_TOKEN) {
22+
const definitions = [
23+
process.env.GITHUB_TOKEN,
24+
process.env.INPUT_GITHUB_TOKEN,
25+
process.env.INPUT_TOKEN,
26+
].filter(Boolean);
27+
28+
if (definitions.length === 0) {
2329
throw new Error(
2430
"[@octokit/auth-action] `GITHUB_TOKEN` variable is not set. It must be set on either `env:` or `with:`. See https://github.com/octokit/auth-action.js#createactionauth"
2531
);
2632
}
2733

28-
if (process.env.GITHUB_TOKEN && process.env.INPUT_GITHUB_TOKEN) {
34+
if (definitions.length > 1) {
2935
throw new Error(
30-
"[@octokit/auth-action] `GITHUB_TOKEN` variable is set on both `env:` and `with:`. Use either the one or the other. See https://github.com/octokit/auth-action.js#createactionauth"
36+
"[@octokit/auth-action] The token variable is specified more than once. Use either `with.token`, `with.GITHUB_TOKEN`, or `env.GITHUB_TOKEN`. See https://github.com/octokit/auth-action.js#createactionauth"
3137
);
3238
}
3339

34-
const token = process.env.GITHUB_TOKEN || process.env.INPUT_GITHUB_TOKEN;
40+
const token = definitions.pop();
3541
return createTokenAuth(token as string);
3642
};

test/index.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ afterEach(() => {
77
delete process.env.GITHUB_ACTION;
88
delete process.env.GITHUB_TOKEN;
99
delete process.env.INPUT_GITHUB_TOKEN;
10+
delete process.env.INPUT_TOKEN;
1011
});
1112

1213
test("README example", async () => {
@@ -38,6 +39,20 @@ test("README example using with:", async () => {
3839
});
3940
});
4041

42+
test("README example using with.token", async () => {
43+
process.env.GITHUB_ACTION = "my-action";
44+
process.env.INPUT_TOKEN = "v1.1234567890abcdef1234567890abcdef12345678";
45+
46+
const auth = createActionAuth();
47+
const authentication = await auth();
48+
49+
expect(authentication).toEqual({
50+
type: "token",
51+
token: "v1.1234567890abcdef1234567890abcdef12345678",
52+
tokenType: "installation",
53+
});
54+
});
55+
4156
test("GITHUB_ACTION not set", async () => {
4257
try {
4358
const auth = createActionAuth();
@@ -60,7 +75,22 @@ test("both GITHUB_TOKEN and INPUT_GITHUB_TOKEN set", async () => {
6075
throw new Error("Should not resolve");
6176
} catch (error) {
6277
expect(error.message).toMatch(
63-
/\[@octokit\/auth-action\] `GITHUB_TOKEN` variable is set on both `env:` and `with:`/i
78+
/\[@octokit\/auth-action\] The token variable is specified more than once/i
79+
);
80+
}
81+
});
82+
83+
test("both GITHUB_TOKEN and INPUT_TOKEN set", async () => {
84+
process.env.GITHUB_ACTION = "my-action";
85+
process.env.GITHUB_TOKEN = "v1.1234567890abcdef1234567890abcdef12345678";
86+
process.env.INPUT_TOKEN = "v1.1234567890abcdef1234567890abcdef12345678";
87+
88+
try {
89+
const auth = createActionAuth();
90+
throw new Error("Should not resolve");
91+
} catch (error) {
92+
expect(error.message).toMatch(
93+
/\[@octokit\/auth-action\] The token variable is specified more than once/i
6494
);
6595
}
6696
});

0 commit comments

Comments
 (0)