Skip to content

Commit

Permalink
separate org and project params
Browse files Browse the repository at this point in the history
  • Loading branch information
obostjancic committed Sep 18, 2024
1 parent 28e5495 commit ae1fd86
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Args = {

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

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

switch (integration) {
Expand Down
55 changes: 44 additions & 11 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, options.project),
askForProjectSelection(projects, options.orgSlug, options.projectSlug),
);

const { token } = apiKeys ?? {};
Expand Down Expand Up @@ -1087,25 +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));
});

if (projectSlug) {
const selectedProject = sortedProjects.find(
(p) => label(p) === projectSlug,
);
if (selectedProject) {
return selectedProject;
}
}

const selection: SentryProjectData | symbol = await abortIfCancelled(
clack.select({
maxItems: 12,
Expand All @@ -1125,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
11 changes: 9 additions & 2 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ export type WizardOptions = {
url?: string;

/**
* Org slug and project slug to pre-select in the wizard.
* 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-org/my-project`
* Example: `--project my-project`
*/
projectSlug?: string;

Expand Down

0 comments on commit ae1fd86

Please sign in to comment.