Skip to content
Draft
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Prompt for login after interactive install ([#3406](https://github.com/getsentry/sentry-cli/pull/3406))

## 3.7.0

### Features
Expand Down
26 changes: 26 additions & 0 deletions scripts/__tests__/prompt-login.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { promptLogin } = require('../prompt-login');

describe('promptLogin', () => {
test('runs login for an interactive install', () => {
const spawnSync = jest.fn();

promptLogin('/path/to/sentry-cli', { isTTY: true }, { isTTY: true }, spawnSync);

expect(spawnSync).toHaveBeenCalledWith(
'/path/to/sentry-cli',
['login', '--global', '--if-needed'],
{ stdio: 'inherit' }
);
});

test.each([
[{ isTTY: false }, { isTTY: true }],
[{ isTTY: true }, { isTTY: false }],
])('skips login for a non-interactive install', (stdin, stdout) => {
const spawnSync = jest.fn();

promptLogin('/path/to/sentry-cli', stdin, stdout, spawnSync);

expect(spawnSync).not.toHaveBeenCalled();
});
});
3 changes: 3 additions & 0 deletions scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const which = require('which');
const helper = require('../js/helper');
const pkgInfo = require('../package.json');
const { Logger } = require('../js/logger');
const { promptLogin } = require('./prompt-login');

const logger = new Logger(getLogStream('stderr'));

Expand Down Expand Up @@ -317,6 +318,7 @@ if (distributionPackageName === undefined) {
try {
require.resolve(`${distributionPackageName}/${distributionSubpath}`);
// If the `resolve` call succeeds it means a binary was installed successfully via optional dependencies so we can skip the manual postinstall download.
promptLogin(helper.getPath());
process.exit(0);
} catch (e) {
// Optional dependencies likely didn't get installed - proceed with fallback downloading manually
Expand All @@ -329,6 +331,7 @@ This can happen if you use an option to disable optional dependencies during ins

downloadBinary()
.then(() => checkVersion())
.then(() => promptLogin(helper.getPath()))
.then(() => {
process.exit(0);
})
Expand Down
20 changes: 20 additions & 0 deletions scripts/prompt-login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const childProcess = require('child_process');

function promptLogin(
binaryPath,
stdin = process.stdin,
stdout = process.stdout,
spawnSync = childProcess.spawnSync
) {
if (!stdin.isTTY || !stdout.isTTY) {
return;
}

spawnSync(binaryPath, ['login', '--global', '--if-needed'], {
stdio: 'inherit',
});
}

module.exports = { promptLogin };
27 changes: 20 additions & 7 deletions src/commands/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ use crate::utils::auth_token::AuthToken;
use crate::utils::ui::{prompt, prompt_to_continue};

pub fn make_command(command: Command) -> Command {
command.about("Authenticate with the Sentry server.").arg(
Arg::new("global")
.short('g')
.long("global")
.action(ArgAction::SetTrue)
.help("Store authentication token globally rather than locally."),
)
command
.about("Authenticate with the Sentry server.")
.arg(
Arg::new("global")
.short('g')
.long("global")
.action(ArgAction::SetTrue)
.help("Store authentication token globally rather than locally."),
)
.arg(
Arg::new("if_needed")
.long("if-needed")
.action(ArgAction::SetTrue)
.hide(true),
)
}

fn update_config(config: &Config, token: AuthToken, url: &str) -> Result<()> {
Expand All @@ -28,6 +36,11 @@ fn update_config(config: &Config, token: AuthToken, url: &str) -> Result<()> {

pub fn execute(matches: &ArgMatches) -> Result<()> {
let config = Config::current();

if matches.get_flag("if_needed") && config.get_auth().is_some() {
return Ok(());
}

let token_url = format!(
"{}/orgredirect/organizations/:orgslug/settings/auth-tokens/",
config.get_base_url()?
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/_cases/login/login-if-needed.trycmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```
$ sentry-cli login --if-needed --auth-token 0000000000000000000000000000000000000000000000000000000000000000
? success
```
Loading