Skip to content

Commit 935fe87

Browse files
authored
Automatically use the GitHub-provided token to allow most users to avoid explicit GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} configuration (#564)
1 parent 6af4a7e commit 935fe87

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

.changeset/quiet-vans-fail.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@changesets/action": minor
3+
---
4+
5+
Automatically use the GitHub-provided token to allow most users to avoid explicit `GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}` configuration.

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ jobs:
5454

5555
- name: Create Release Pull Request
5656
uses: changesets/action@v1
57-
env:
58-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5957
```
6058
6159
#### With Publishing
@@ -95,7 +93,6 @@ jobs:
9593
# This expects you to have a script called release which does a build for your packages and calls changeset publish
9694
publish: yarn release
9795
env:
98-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9996
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
10097
10198
- name: Send a Slack notification if a publish happens
@@ -156,8 +153,6 @@ jobs:
156153
- name: Create Release Pull Request or Publish to npm
157154
id: changesets
158155
uses: changesets/action@v1
159-
env:
160-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
161156
162157
- name: Publish
163158
if: steps.changesets.outputs.hasChangesets == 'false'
@@ -202,8 +197,6 @@ jobs:
202197
with:
203198
# this expects you to have a npm script called version that runs some logic and then calls `changeset version`.
204199
version: yarn version
205-
env:
206-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
207200
```
208201
209202
#### With Yarn 2 / Plug'n'Play

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ runs:
44
using: "node24"
55
main: "dist/index.js"
66
inputs:
7+
github-token:
8+
description: "The GitHub token to use for authentication. Defaults to the GitHub-provided token."
9+
required: false
10+
default: ${{ github.token }}
711
publish:
812
description: "The command to use to build and publish packages"
913
required: false

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import { fileExists } from "./utils.ts";
1010
const getOptionalInput = (name: string) => core.getInput(name) || undefined;
1111

1212
(async () => {
13-
let githubToken = process.env.GITHUB_TOKEN;
13+
// to maintain compatibility with workflows created before github-token input was introduced
14+
// it's important to prefer the explicitly set GITHUB_TOKEN over the default token coming from github.token
15+
let githubToken = process.env.GITHUB_TOKEN || core.getInput("github-token");
1416

1517
if (!githubToken) {
1618
core.setFailed("Please add the GITHUB_TOKEN to the changesets action");
@@ -114,6 +116,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
114116

115117
const result = await runPublish({
116118
script: publishScript,
119+
githubToken,
117120
git,
118121
octokit,
119122
createGithubReleases: core.getBooleanInput("createGithubReleases"),
@@ -136,6 +139,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
136139
const octokit = setupOctokit(githubToken);
137140
const { pullRequestNumber } = await runVersion({
138141
script: getOptionalInput("version"),
142+
githubToken,
139143
git,
140144
octokit,
141145
prTitle: getOptionalInput("title"),

src/run.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe("version", () => {
8282

8383
await runVersion({
8484
octokit: setupOctokit("@@GITHUB_TOKEN"),
85+
githubToken: "@@GITHUB_TOKEN",
8586
git: new Git({ cwd }),
8687
cwd,
8788
});
@@ -116,6 +117,7 @@ describe("version", () => {
116117

117118
await runVersion({
118119
octokit: setupOctokit("@@GITHUB_TOKEN"),
120+
githubToken: "@@GITHUB_TOKEN",
119121
git: new Git({ cwd }),
120122
cwd,
121123
});
@@ -150,6 +152,7 @@ describe("version", () => {
150152

151153
await runVersion({
152154
octokit: setupOctokit("@@GITHUB_TOKEN"),
155+
githubToken: "@@GITHUB_TOKEN",
153156
git: new Git({ cwd }),
154157
cwd,
155158
});
@@ -204,6 +207,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis.
204207

205208
await runVersion({
206209
octokit: setupOctokit("@@GITHUB_TOKEN"),
210+
githubToken: "@@GITHUB_TOKEN",
207211
git: new Git({ cwd }),
208212
cwd,
209213
prBodyMaxCharacters: 1000,
@@ -262,6 +266,7 @@ fluminis divesque vulnere aquis parce lapsis rabie si visa fulmineis.
262266

263267
await runVersion({
264268
octokit: setupOctokit("@@GITHUB_TOKEN"),
269+
githubToken: "@@GITHUB_TOKEN",
265270
git: new Git({ cwd }),
266271
cwd,
267272
prBodyMaxCharacters: 500,

src/run.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const createRelease = async (
6060

6161
type PublishOptions = {
6262
script: string;
63+
githubToken: string;
6364
octokit: Octokit;
6465
createGithubReleases: boolean;
6566
git: Git;
@@ -79,6 +80,7 @@ type PublishResult =
7980

8081
export async function runPublish({
8182
script,
83+
githubToken,
8284
git,
8385
octokit,
8486
createGithubReleases,
@@ -89,7 +91,7 @@ export async function runPublish({
8991
let changesetPublishOutput = await getExecOutput(
9092
publishCommand,
9193
publishArgs,
92-
{ cwd }
94+
{ cwd, env: { ...process.env, GITHUB_TOKEN: githubToken } }
9395
);
9496

9597
let { packages, tool } = await getPackages(cwd);
@@ -249,6 +251,7 @@ export async function getVersionPrBody({
249251

250252
type VersionOptions = {
251253
script?: string;
254+
githubToken: string;
252255
git: Git;
253256
octokit: Octokit;
254257
cwd?: string;
@@ -265,6 +268,7 @@ type RunVersionResult = {
265268

266269
export async function runVersion({
267270
script,
271+
githubToken,
268272
git,
269273
octokit,
270274
cwd = process.cwd(),
@@ -282,9 +286,11 @@ export async function runVersion({
282286

283287
let versionsByDirectory = await getVersionsByDirectory(cwd);
284288

289+
const env = { ...process.env, GITHUB_TOKEN: githubToken };
290+
285291
if (script) {
286292
let [versionCommand, ...versionArgs] = script.split(/\s+/);
287-
await exec(versionCommand, versionArgs, { cwd });
293+
await exec(versionCommand, versionArgs, { cwd, env });
288294
} else {
289295
let changesetsCliPkgJson = requireChangesetsCliPkgJson(cwd);
290296
let cmd = semverLt(changesetsCliPkgJson.version, "2.0.0")
@@ -300,6 +306,7 @@ export async function runVersion({
300306
],
301307
{
302308
cwd,
309+
env,
303310
}
304311
);
305312
}
@@ -330,7 +337,7 @@ export async function runVersion({
330337
/**
331338
* Fetch any existing pull requests that are open against the branch,
332339
* before we push any changes that may inadvertently close the existing PRs.
333-
*
340+
*
334341
* (`@changesets/ghcommit` has to reset the branch to the same commit as the base,
335342
* which GitHub will then react to by closing the PRs)
336343
*/
@@ -340,7 +347,13 @@ export async function runVersion({
340347
head: `${github.context.repo.owner}:${versionBranch}`,
341348
base: branch,
342349
});
343-
core.info(`Existing pull requests: ${JSON.stringify(existingPullRequests.data, null, 2)}`);
350+
core.info(
351+
`Existing pull requests: ${JSON.stringify(
352+
existingPullRequests.data,
353+
null,
354+
2
355+
)}`
356+
);
344357

345358
await git.pushChanges({ branch: versionBranch, message: finalCommitMessage });
346359

0 commit comments

Comments
 (0)