aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/projectDump/ProjectDumpApp.tsx
blob: 0e9dd698d057cd576c684fcf7e759a280761229c (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * SonarQube
 * Copyright (C) 2009-2022 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 * as React from 'react';
import { getActivity } from '../../api/ce';
import { getStatus } from '../../api/project-dump';
import withAvailableFeatures, {
  WithAvailableFeaturesProps
} from '../../app/components/available-features/withAvailableFeatures';
import withComponentContext from '../../app/components/componentContext/withComponentContext';
import { throwGlobalError } from '../../helpers/error';
import { translate } from '../../helpers/l10n';
import { Feature } from '../../types/features';
import { DumpStatus, DumpTask } from '../../types/project-dump';
import { ActivityRequestParameters, TaskStatuses, TaskTypes } from '../../types/tasks';
import { Component } from '../../types/types';
import Export from './components/Export';
import Import from './components/Import';
import './styles.css';

const POLL_INTERNAL = 5000;

interface Props extends WithAvailableFeaturesProps {
  component: Component;
}

interface State {
  lastAnalysisTask?: DumpTask;
  lastExportTask?: DumpTask;
  lastImportTask?: DumpTask;
  status?: DumpStatus;
}

export class ProjectDumpApp extends React.Component<Props, State> {
  mounted = false;
  state: State = {};

  componentDidMount() {
    this.mounted = true;
    this.loadStatus();
  }

  componentDidUpdate(prevProps: Props) {
    if (prevProps.component.key !== this.props.component.key) {
      this.loadStatus();
    }
  }

  componentWillUnmount() {
    this.mounted = false;
  }

  getLastTask(component: string, type: TaskTypes) {
    const data: ActivityRequestParameters = {
      type,
      component,
      onlyCurrents: true,
      status: [
        TaskStatuses.Pending,
        TaskStatuses.InProgress,
        TaskStatuses.Success,
        TaskStatuses.Failed,
        TaskStatuses.Canceled
      ].join(',')
    };
    return getActivity(data)
      .then(({ tasks }) => (tasks.length > 0 ? tasks[0] : undefined), throwGlobalError)
      .catch(() => undefined);
  }

  getLastTaskOfEachType(componentKey: string) {
    const projectImportFeatureEnabled = this.props.hasFeature(Feature.ProjectImport);
    const all = projectImportFeatureEnabled
      ? [
          this.getLastTask(componentKey, TaskTypes.ProjectExport),
          this.getLastTask(componentKey, TaskTypes.ProjectImport),
          this.getLastTask(componentKey, TaskTypes.Report)
        ]
      : [
          this.getLastTask(componentKey, TaskTypes.ProjectExport),
          Promise.resolve(),
          this.getLastTask(componentKey, TaskTypes.Report)
        ];
    return Promise.all(all).then(([lastExportTask, lastImportTask, lastAnalysisTask]) => ({
      lastExportTask,
      lastImportTask,
      lastAnalysisTask
    }));
  }

  loadStatus = () => {
    const { component } = this.props;
    return Promise.all([getStatus(component.key), this.getLastTaskOfEachType(component.key)]).then(
      ([status, { lastExportTask, lastImportTask, lastAnalysisTask }]) => {
        if (this.mounted) {
          this.setState({
            status,
            lastExportTask,
            lastImportTask,
            lastAnalysisTask
          });
        }
        return {
          status,
          lastExportTask,
          lastImportTask,
          lastAnalysisTask
        };
      }
    );
  };

  poll = () => {
    this.loadStatus().then(
      ({ lastExportTask, lastImportTask }) => {
        if (this.mounted) {
          const progressStatus = [TaskStatuses.Pending, TaskStatuses.InProgress];
          const exportNotFinished =
            lastExportTask === undefined || progressStatus.includes(lastExportTask.status);
          const importNotFinished =
            lastImportTask === undefined || progressStatus.includes(lastImportTask.status);
          if (exportNotFinished || importNotFinished) {
            setTimeout(this.poll, POLL_INTERNAL);
          } else {
            // Since we fetch status separate from task we could not get an up to date status.
            // even if we detect that export / import is finish.
            // Doing a last call will make sur we get the latest status.
            this.loadStatus();
          }
        }
      },
      () => {
        /* no catch needed */
      }
    );
  };

  render() {
    const { component } = this.props;
    const projectImportFeatureEnabled = this.props.hasFeature(Feature.ProjectImport);
    const { lastAnalysisTask, lastExportTask, lastImportTask, status } = this.state;

    return (
      <div className="page page-limited" id="project-dump">
        <header className="page-header">
          <h1 className="page-title">{translate('project_dump.page')}</h1>
          <div className="page-description">
            {projectImportFeatureEnabled
              ? translate('project_dump.page.description')
              : translate('project_dump.page.description_without_import')}
          </div>
        </header>

        {status === undefined ? (
          <i className="spinner" />
        ) : (
          <div className="columns">
            <div className="column-half">
              <Export
                componentKey={component.key}
                loadStatus={this.poll}
                status={status}
                task={lastExportTask}
              />
            </div>
            <div className="column-half">
              <Import
                importEnabled={!!projectImportFeatureEnabled}
                analysis={lastAnalysisTask}
                componentKey={component.key}
                loadStatus={this.poll}
                status={status}
                task={lastImportTask}
              />
            </div>
          </div>
        )}
      </div>
    );
  }
}

export default withComponentContext(withAvailableFeatures(ProjectDumpApp));