aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/api/projectActivity.ts
blob: f9c1a1f7d63b6885ff7881e4223e44f733a393a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { throwGlobalError } from '../helpers/error';
import { getJSON, post, postJSON } from '../helpers/request';
import { BranchParameters } from '../types/branch-like';
import {
  Analysis,
  ApplicationAnalysisEventCategory,
  ProjectAnalysisEventCategory,
} from '../types/project-activity';
import { Paging } from '../types/types';

export enum ProjectActivityStatuses {
  STATUS_PROCESSED = 'P',
  STATUS_UNPROCESSED = 'U',
  STATUS_LIVE_MEASURE_COMPUTE = 'L',
}

export type ProjectActivityParams = {
  project?: string;
  statuses?: string;
  category?: string;
  from?: string;
  p?: number;
  ps?: number;
} & BranchParameters;

export interface ProjectActivityResponse {
  analyses: Analysis[];
  paging: Paging;
}

export function getProjectActivity(data: ProjectActivityParams): Promise<ProjectActivityResponse> {
  return getJSON('/api/project_analyses/search', data).catch(throwGlobalError);
}

const PROJECT_ACTIVITY_PAGE_SIZE = 500;

export function getAllTimeProjectActivity(
  data: ProjectActivityParams,
  prev?: ProjectActivityResponse,
): Promise<ProjectActivityResponse> {
  return getProjectActivity({ ...data, ps: data.ps ?? PROJECT_ACTIVITY_PAGE_SIZE }).then((r) => {
    const result = prev
      ? {
          analyses: prev.analyses.concat(r.analyses),
          paging: r.paging,
        }
      : r;

    if (result.paging.pageIndex * result.paging.pageSize >= result.paging.total) {
      return result;
    }

    return getAllTimeProjectActivity(
      { ...data, ps: data.ps ?? PROJECT_ACTIVITY_PAGE_SIZE, p: result.paging.pageIndex + 1 },
      result,
    );
  });
}

export interface CreateEventResponse {
  analysis: string;
  key: string;
  name: string;
  category: ProjectAnalysisEventCategory | ApplicationAnalysisEventCategory;
  description?: string;
}

export function createEvent(data: {
  analysis: string;
  name: string;
  category?: string;
  description?: string;
}): Promise<CreateEventResponse> {
  return postJSON('/api/project_analyses/create_event', data).then(
    (r) => r.event,
    throwGlobalError,
  );
}

export function deleteEvent(event: string): Promise<void | Response> {
  return post('/api/project_analyses/delete_event', { event }).catch(throwGlobalError);
}

export function changeEvent(data: {
  event: string;
  name?: string;
  description?: string;
}): Promise<CreateEventResponse> {
  return postJSON('/api/project_analyses/update_event', data).then(
    (r) => r.event,
    throwGlobalError,
  );
}

export function deleteAnalysis(analysis: string): Promise<void | Response> {
  return post('/api/project_analyses/delete', { analysis }).catch(throwGlobalError);
}