Skip to content

Commit

Permalink
feat: allow passing org and project slug parameters (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
obostjancic authored Sep 19, 2024
1 parent 863ed49 commit b7d9335
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- feat: Allow passing org and project slug parameters (#671)

## 3.29.0

- ref(nextjs): Adjust dev server command in verification message (#665)
Expand Down
4 changes: 4 additions & 0 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type Args = {

url?: string;
platform?: Platform[];
org?: string;
project?: string;
};

export async function run(argv: Args) {
Expand Down Expand Up @@ -80,6 +82,8 @@ export async function run(argv: Args) {
telemetryEnabled: !argv.disableTelemetry,
promoCode: argv.promoCode,
url: argv.url,
orgSlug: argv.org,
projectSlug: argv.project,
};

switch (integration) {
Expand Down
48 changes: 46 additions & 2 deletions src/utils/clack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ export async function getOrAskForProjectData(
}

const selectedProject = await traceStep('select-project', () =>
askForProjectSelection(projects),
askForProjectSelection(projects, options.orgSlug, options.projectSlug),
);

const { token } = apiKeys ?? {};
Expand Down Expand Up @@ -1087,14 +1087,38 @@ async function askForWizardLogin(options: {

async function askForProjectSelection(
projects: SentryProjectData[],
orgSlug?: string,
projectSlug?: string,
): Promise<SentryProjectData> {
const label = (project: SentryProjectData): string => {
return `${project.organization.slug}/${project.slug}`;
};
const sortedProjects = [...projects];

const filteredProjects = filterProjectsBySlugs(
projects,
orgSlug,
projectSlug,
);

if (filteredProjects.length === 1) {
const selection = filteredProjects[0];

Sentry.setTag('project', selection.slug);
Sentry.setUser({ id: selection.organization.slug });
clack.log.step(`Selected project ${label(selection)}`);

return selection;
}

if (filteredProjects.length === 0) {
clack.log.warn('Could not find a project with the provided slugs.');
}

const sortedProjects = filteredProjects.length ? filteredProjects : projects;
sortedProjects.sort((a: SentryProjectData, b: SentryProjectData) => {
return label(a).localeCompare(label(b));
});

const selection: SentryProjectData | symbol = await abortIfCancelled(
clack.select({
maxItems: 12,
Expand All @@ -1114,6 +1138,26 @@ async function askForProjectSelection(
return selection;
}

function filterProjectsBySlugs(
projects: SentryProjectData[],
orgSlug?: string,
projectSlug?: string,
): SentryProjectData[] {
if (!orgSlug && !projectSlug) {
return projects;
}
if (orgSlug && !projectSlug) {
return projects.filter((p) => p.organization.slug === orgSlug);
}
if (!orgSlug && projectSlug) {
return projects.filter((p) => p.slug === projectSlug);
}

return projects.filter(
(p) => p.organization.slug === orgSlug && p.slug === projectSlug,
);
}

/**
* Asks users if they have a config file for @param tool (e.g. Vite).
* If yes, asks users to specify the path to their config file.
Expand Down
14 changes: 14 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export type WizardOptions = {
*/
url?: string;

/**
* The org to pre-select in the wizard.
* This can be passed via the `--org` arg.
* Example: `--org my-org`
*/
orgSlug?: string;

/**
* Project slug to pre-select in the wizard.
* This can be passed via the `--project` arg.
* Example: `--project my-project`
*/
projectSlug?: string;

/**
* If this is set, the wizard will skip the login and project selection step.
* (This can not yet be set externally but for example when redirecting from
Expand Down

0 comments on commit b7d9335

Please sign in to comment.