Skip to content

build: add otp prompt to release tool #17035

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

Merged
merged 1 commit into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions tools/release/npm/npm-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export function npmLoginInteractive(): boolean {
}

/** Runs NPM publish within a specified directory */
export function npmPublish(packagePath: string, distTag: string): string | null {
const result = spawnSync('npm', ['publish', '--access', 'public', '--tag', distTag], {
cwd: packagePath,
shell: true,
env: npmClientEnvironment,
});
export function npmPublish(packagePath: string, distTag: string, otp: string): string | null {
const result =
spawnSync('npm', ['publish', '--access', 'public', '--tag', distTag, '--otp', otp], {
cwd: packagePath,
shell: true,
env: npmClientEnvironment,
});

// We only want to return an error if the exit code is not zero. NPM by default prints the
// logging messages to "stdout" and therefore just checking for "stdout" is not reliable.
Expand Down
14 changes: 14 additions & 0 deletions tools/release/prompt/npm-dist-tag-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ export async function promptForNpmDistTag(version: Version): Promise<string> {
return distTag;
}

/**
* Prompts the current user-input interface for a npm one-time password (OTP). This is required
* to publish packages in the @angular namespace.
*/
export async function promptForNpmOtp(): Promise<string> {
const {otp} = await prompt<{otp: string}>({
type: 'text',
name: 'otp',
message: 'Enter the one-time password (OTP) for the angular npm account:',
});

return otp;
}

/**
* Determines all allowed npm dist-tag choices for a specified version. For example,
* a pre-release version should be never published to the "latest" tag.
Expand Down
12 changes: 8 additions & 4 deletions tools/release/publish-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
npmLoginInteractive,
npmPublish,
} from './npm/npm-client';
import {promptForNpmDistTag} from './prompt/npm-dist-tag-prompt';
import {promptForNpmDistTag, promptForNpmOtp} from './prompt/npm-dist-tag-prompt';
import {promptForUpstreamRemote} from './prompt/upstream-remote-prompt';
import {releasePackages} from './release-output/release-packages';
import {CHANGELOG_FILE_NAME} from './stage-release';
Expand Down Expand Up @@ -123,8 +123,12 @@ class PublishReleaseTask extends BaseReleaseTask {
// the user to interactively confirm that the script should continue.
await this._promptConfirmReleasePublish();

// Prompt for the OTP right before publishing so that it doesn't expire while building the
// packages.
const npmOtp = await promptForNpmOtp();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: just something I noticed after approving. Maybe we should have a comment here saying that the OTP token is needed as NPM requires two-factor auth for each write to the registry.


for (let packageName of releasePackages) {
this._publishPackageToNpm(packageName, npmDistTag);
this._publishPackageToNpm(packageName, npmDistTag, npmOtp);
}

const newReleaseUrl = getGithubNewReleaseUrl({
Expand Down Expand Up @@ -237,10 +241,10 @@ class PublishReleaseTask extends BaseReleaseTask {
}

/** Publishes the specified package within the given NPM dist tag. */
private _publishPackageToNpm(packageName: string, npmDistTag: string) {
private _publishPackageToNpm(packageName: string, npmDistTag: string, npmOtp: string) {
console.info(green(` ⭮ Publishing "${packageName}"..`));

const errorOutput = npmPublish(join(this.releaseOutputPath, packageName), npmDistTag);
const errorOutput = npmPublish(join(this.releaseOutputPath, packageName), npmDistTag, npmOtp);

if (errorOutput) {
console.error(red(` ✘ An error occurred while publishing "${packageName}".`));
Expand Down