-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
non-TTY required variables checks #852
Conversation
🦋 Changeset detectedLatest commit: 40a5f50 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
06f5e6d
to
607b433
Compare
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/2271600192/npm-package-wrangler-852 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/852/npm-package-wrangler-852 Or you can use npx https://prerelease-registry.developers.workers.dev/runs/2271600192/npm-package-wrangler-852 dev path/to/script.js |
927fa20
to
f3f6e6c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a better way to do this, which is to only throw the error when request. (So, inside getAccountId and getAPIToken. I'm on phone so I can't remember function names exactly). This would also remove any checks for test environments and such.
1ab1306
to
afa1b39
Compare
afa1b39
to
27c024a
Compare
@@ -21,10 +22,13 @@ describe("Error Reporting", () => { | |||
jest.mock("@sentry/node"); | |||
jest.spyOn(Sentry, "captureException"); | |||
process.stdout.isTTY = true; | |||
jest.mock("ci-info"); | |||
(ci.isCI as jest.Mocked<boolean>) = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ciCheck
function doesn't make sense to modify to handle this use case, directly mocking ci-info
Alternatively we can make a simple wrapper function that is just returns a boolean from, ci.isCI
|
||
expect(error_tracking_opt).toBe(false); | ||
expect(error_tracking_opt_date).toBeTruthy(); | ||
it("should not prompt in CI/CD environment", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added additional test that handles the change in reporting.ts
that checks if its TTY or CI and does nothing now.
Cc: @petebacondarwin
expect( | ||
fs.existsSync(path.join(os.homedir(), reportingTOMLPath, "utf-8")) | ||
).toBe(false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to reporting does nothing if in a TTY or CI environment, test reflects that by confirming no Reporting TOML is created.
27c024a
to
1adc237
Compare
1adc237
to
2413b3c
Compare
87cb0d2
to
7e63bd5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's looking good, marking as blocking so I can test it myself before we land it. thank you @JacobMGEvans!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this new ciCheck()
we are kind of reproducing work already done in user.tsx.
getAPIToken()
and getAccountId()
can already handle non-interactive cases, so I think we should just update those to throw errors rather than returning undefined
if in non-TTY mode.
For example:
export function getAPIToken(): string | undefined {
if (LocalState.apiToken) {
logger.warn(
"It looks like you have used Wrangler 1's `config` command to login with an API token.\n" +
"This is no longer supported in the current version of Wrangler.\n" +
"If you wish to authenticate via an API token then please set the `CLOUDFLARE_API_TOKEN` environment variable."
);
}
if (process.stdin.isTTY) {
return LocalState.accessToken?.value;
} else {
throw new Error(...);
}
}
and
export async function getAccountId(
isInteractive = true
): Promise<string | undefined> {
const apiToken = getAPIToken();
if (!apiToken) return;
const accountIdFromEnv = getCloudflareAccountIdFromEnv();
if (accountIdFromEnv) {
return accountIdFromEnv;
} else if (!isInteractive) {
throw new Error(...);
}
...
I don't see the reproducing of work, I can see the value of co-locating the checks, control logic, within the |
I'm confused -- the issue we're trying to close with this PR is that we should enforce the use of an api token via environment variable when in CI, right? So then why are we checking |
a3c07af
to
a3757e9
Compare
a3757e9
to
45b4735
Compare
45b4735
to
e50fa70
Compare
e50fa70
to
cd17369
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is missing checks for CLOUDFLARE_ACCOUNT_ID the way it's doing it for API_TOKEN. Did you plan on doing that in a later PR?
.toMatchInlineSnapshot(` | ||
[Error: More than one account available but unable to select one in non-interactive mode. | ||
Please set the appropriate \`account_id\` in your \`wrangler.toml\` file. | ||
Available accounts are ("<name>" - "<id>"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the message shouldn't be to set the account_id in the toml file, but to prefer setting the CLOUDFLARE_ACCOUNT_ID env var. I also think you can suggest running wrangler whoami
to get the account id value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps suggest both options?
6c3a0e4
to
ce9db80
Compare
ce9db80
to
f1c3343
Compare
Added a check in non-TTY environments for `account_id`, `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml` then `CLOUDFLARE_ACCOUNT_ID` is not needed in non-TTY scope. The `CLOUDFLARE_API_TOKEN` is necessary in non-TTY scope and will always error if missing. resolves #827
16b92d5
to
dca0292
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's getting close! one last note regarding a user without any accounts
@@ -169,10 +171,14 @@ describe("wrangler secret", () => { | |||
mockAccountId({ accountId: null }); | |||
|
|||
it("should error if a user has no account", async () => { | |||
mockGetMemberships({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will ever happen. If a user has no account, then the api will throw a 10000 error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can reproduce this locally with a garbage invalid token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a preexisting test, I just updated to using a new mock for consistency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exisitng test had the right error message tho, this isn't the right mock. It will never return a list with zero memberships.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh hmm I see. Aight never mind then.
await expect( | ||
runWrangler("secret put the-key --name script-name") | ||
).rejects.toThrowErrorMatchingInlineSnapshot( | ||
`"No account id found, quitting..."` | ||
`"Failed to automatically retrieve account IDs for the logged in user. In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file."` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which will make this error be "No account id found, quitting..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is occuring in a "non-TTY" block
.toMatchInlineSnapshot(` | ||
[Error: More than one account available but unable to select one in non-interactive mode. | ||
Please set the appropriate \`account_id\` in your \`wrangler.toml\` file. | ||
Available accounts are ("<name>" - "<id>"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps suggest both options?
Added a check in non-TTY environments for `account_id`, `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml` then `CLOUDFLARE_ACCOUNT_ID` is not needed in non-TTY scope. The `CLOUDFLARE_API_TOKEN` is necessary in non-TTY scope and will always error if missing. resolves #827
dca0292
to
40a5f50
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's land it and try this out.
Added a check in non-TTY environments for
account_id
,CLOUDFLARE_ACCOUNT_ID
andCLOUDFLARE_API_TOKEN
. Ifaccount_id
exists inwrangler.toml
then
CLOUDFLARE_ACCOUNT_ID
is not needed in non-TTY scope. TheCLOUDFLARE_API_TOKEN
is necessary in non-TTY scope and will always error if missing.resolves #827