123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- import {Query} from 'history';
- import chunk from 'lodash/chunk';
- import debounce from 'lodash/debounce';
- import {
- addErrorMessage,
- addLoadingMessage,
- addSuccessMessage,
- } from 'sentry/actionCreators/indicator';
- import ProjectActions from 'sentry/actions/projectActions';
- import {Client} from 'sentry/api';
- import {PlatformKey} from 'sentry/data/platformCategories';
- import {t, tct} from 'sentry/locale';
- import ProjectsStatsStore from 'sentry/stores/projectsStatsStore';
- import {Project, Team} from 'sentry/types';
- type UpdateParams = {
- orgId: string;
- projectId: string;
- data?: {[key: string]: any};
- query?: Query;
- };
- export function update(api: Client, params: UpdateParams) {
- ProjectActions.update(params.projectId, params.data);
- const endpoint = `/projects/${params.orgId}/${params.projectId}/`;
- return api
- .requestPromise(endpoint, {
- method: 'PUT',
- data: params.data,
- })
- .then(
- data => {
- ProjectActions.updateSuccess(data);
- return data;
- },
- err => {
- ProjectActions.updateError(err, params.projectId);
- throw err;
- }
- );
- }
- type StatsParams = Pick<UpdateParams, 'orgId' | 'data' | 'query'>;
- export function loadStats(api: Client, params: StatsParams) {
- ProjectActions.loadStats(params.orgId, params.data);
- const endpoint = `/organizations/${params.orgId}/stats/`;
- api.request(endpoint, {
- query: params.query,
- success: data => {
- ProjectActions.loadStatsSuccess(data);
- },
- error: data => {
- ProjectActions.loadStatsError(data);
- },
- });
- }
- // This is going to queue up a list of project ids we need to fetch stats for
- // Will be cleared when debounced function fires
- const _projectStatsToFetch: Set<string> = new Set();
- // Max projects to query at a time, otherwise if we fetch too many in the same request
- // it can timeout
- const MAX_PROJECTS_TO_FETCH = 10;
- const _queryForStats = (
- api: Client,
- projects: string[],
- orgId: string,
- additionalQuery: Query | undefined
- ) => {
- const idQueryParams = projects.map(project => `id:${project}`).join(' ');
- const endpoint = `/organizations/${orgId}/projects/`;
- const query: Query = {
- statsPeriod: '24h',
- query: idQueryParams,
- ...additionalQuery,
- };
- return api.requestPromise(endpoint, {
- query,
- });
- };
- export const _debouncedLoadStats = debounce(
- (api: Client, projectSet: Set<string>, params: UpdateParams) => {
- const storedProjects: {[key: string]: Project} = ProjectsStatsStore.getAll();
- const existingProjectStats = Object.values(storedProjects).map(({id}) => id);
- const projects = Array.from(projectSet).filter(
- project => !existingProjectStats.includes(project)
- );
- if (!projects.length) {
- _projectStatsToFetch.clear();
- return;
- }
- // Split projects into more manageable chunks to query, otherwise we can
- // potentially face server timeouts
- const queries = chunk(projects, MAX_PROJECTS_TO_FETCH).map(chunkedProjects =>
- _queryForStats(api, chunkedProjects, params.orgId, params.query)
- );
- Promise.all(queries)
- .then(results => {
- ProjectActions.loadStatsForProjectSuccess(
- results.reduce((acc, result) => acc.concat(result), [])
- );
- })
- .catch(() => {
- addErrorMessage(t('Unable to fetch all project stats'));
- });
- // Reset projects list
- _projectStatsToFetch.clear();
- },
- 50
- );
- export function loadStatsForProject(api: Client, project: string, params: UpdateParams) {
- // Queue up a list of projects that we need stats for
- // and call a debounced function to fetch stats for list of projects
- _projectStatsToFetch.add(project);
- _debouncedLoadStats(api, _projectStatsToFetch, params);
- }
- export function setActiveProject(project: Project | null) {
- ProjectActions.setActive(project);
- }
- export function removeProject(api: Client, orgId: string, project: Project) {
- const endpoint = `/projects/${orgId}/${project.slug}/`;
- ProjectActions.removeProject(project);
- return api
- .requestPromise(endpoint, {
- method: 'DELETE',
- })
- .then(
- () => {
- ProjectActions.removeProjectSuccess(project);
- addSuccessMessage(
- tct('[project] was successfully removed', {project: project.slug})
- );
- },
- err => {
- ProjectActions.removeProjectError(project);
- addErrorMessage(tct('Error removing [project]', {project: project.slug}));
- throw err;
- }
- );
- }
- export function transferProject(
- api: Client,
- orgId: string,
- project: Project,
- email: string
- ) {
- const endpoint = `/projects/${orgId}/${project.slug}/transfer/`;
- return api
- .requestPromise(endpoint, {
- method: 'POST',
- data: {
- email,
- },
- })
- .then(
- () => {
- addSuccessMessage(
- tct('A request was sent to move [project] to a different organization', {
- project: project.slug,
- })
- );
- },
- err => {
- let message = '';
- // Handle errors with known failures
- if (err.status >= 400 && err.status < 500 && err.responseJSON) {
- message = err.responseJSON?.detail;
- }
- if (message) {
- addErrorMessage(
- tct('Error transferring [project]. [message]', {
- project: project.slug,
- message,
- })
- );
- } else {
- addErrorMessage(
- tct('Error transferring [project]', {
- project: project.slug,
- })
- );
- }
- throw err;
- }
- );
- }
- /**
- * Associate a team with a project
- */
- /**
- * Adds a team to a project
- *
- * @param api API Client
- * @param orgSlug Organization Slug
- * @param projectSlug Project Slug
- * @param team Team data object
- */
- export function addTeamToProject(
- api: Client,
- orgSlug: string,
- projectSlug: string,
- team: Team
- ) {
- const endpoint = `/projects/${orgSlug}/${projectSlug}/teams/${team.slug}/`;
- addLoadingMessage();
- ProjectActions.addTeam(team);
- return api
- .requestPromise(endpoint, {
- method: 'POST',
- })
- .then(
- project => {
- addSuccessMessage(
- tct('[team] has been added to the [project] project', {
- team: `#${team.slug}`,
- project: projectSlug,
- })
- );
- ProjectActions.addTeamSuccess(team, projectSlug);
- ProjectActions.updateSuccess(project);
- },
- err => {
- addErrorMessage(
- tct('Unable to add [team] to the [project] project', {
- team: `#${team.slug}`,
- project: projectSlug,
- })
- );
- ProjectActions.addTeamError();
- throw err;
- }
- );
- }
- /**
- * Removes a team from a project
- *
- * @param api API Client
- * @param orgSlug Organization Slug
- * @param projectSlug Project Slug
- * @param teamSlug Team Slug
- */
- export function removeTeamFromProject(
- api: Client,
- orgSlug: string,
- projectSlug: string,
- teamSlug: string
- ) {
- const endpoint = `/projects/${orgSlug}/${projectSlug}/teams/${teamSlug}/`;
- addLoadingMessage();
- ProjectActions.removeTeam(teamSlug);
- return api
- .requestPromise(endpoint, {
- method: 'DELETE',
- })
- .then(
- project => {
- addSuccessMessage(
- tct('[team] has been removed from the [project] project', {
- team: `#${teamSlug}`,
- project: projectSlug,
- })
- );
- ProjectActions.removeTeamSuccess(teamSlug, projectSlug);
- ProjectActions.updateSuccess(project);
- },
- err => {
- addErrorMessage(
- tct('Unable to remove [team] from the [project] project', {
- team: `#${teamSlug}`,
- project: projectSlug,
- })
- );
- ProjectActions.removeTeamError(err);
- throw err;
- }
- );
- }
- /**
- * Change a project's slug
- *
- * @param prev Previous slug
- * @param next New slug
- */
- export function changeProjectSlug(prev: string, next: string) {
- ProjectActions.changeSlug(prev, next);
- }
- /**
- * Send a sample event
- *
- * @param api API Client
- * @param orgSlug Organization Slug
- * @param projectSlug Project Slug
- */
- export function sendSampleEvent(api: Client, orgSlug: string, projectSlug: string) {
- const endpoint = `/projects/${orgSlug}/${projectSlug}/create-sample/`;
- return api.requestPromise(endpoint, {
- method: 'POST',
- });
- }
- /**
- * Creates a project
- *
- * @param api API Client
- * @param orgSlug Organization Slug
- * @param team The team slug to assign the project to
- * @param name Name of the project
- * @param platform The platform key of the project
- * @param options Additional options such as creating default alert rules
- */
- export function createProject(
- api: Client,
- orgSlug: string,
- team: string,
- name: string,
- platform: string,
- options: {defaultRules?: boolean} = {}
- ) {
- return api.requestPromise(`/teams/${orgSlug}/${team}/projects/`, {
- method: 'POST',
- data: {name, platform, default_rules: options.defaultRules},
- });
- }
- /**
- * Load platform documentation specific to the project. The DSN and various
- * other project specific secrets will be included in the documentation.
- *
- * @param api API Client
- * @param orgSlug Organization Slug
- * @param projectSlug Project Slug
- * @param platform Project platform.
- */
- export function loadDocs(
- api: Client,
- orgSlug: string,
- projectSlug: string,
- platform: PlatformKey
- ) {
- return api.requestPromise(`/projects/${orgSlug}/${projectSlug}/docs/${platform}/`);
- }
- /**
- * Load the counts of my projects and all projects for the current user
- *
- * @param api API Client
- * @param orgSlug Organization Slug
- */
- export function fetchProjectsCount(api: Client, orgSlug: string) {
- return api.requestPromise(`/organizations/${orgSlug}/projects-count/`);
- }
- /**
- * Check if there are any releases in the last 90 days.
- * Used for checking if project is using releases.
- *
- * @param api API Client
- * @param orgSlug Organization Slug
- * @param projectId Project Id
- */
- export async function fetchAnyReleaseExistence(
- api: Client,
- orgSlug: string,
- projectId: number | string
- ) {
- const data = await api.requestPromise(`/organizations/${orgSlug}/releases/stats/`, {
- method: 'GET',
- query: {
- statsPeriod: '90d',
- project: projectId,
- per_page: 1,
- },
- });
- return data.length > 0;
- }
|