aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/app/components/SonarLintConnection.tsx
blob: bc0e19f2d978aa8229dbb89eff2366848c8f7af2 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * 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 { FormattedMessage } from 'react-intl';
import { useSearchParams } from 'react-router-dom';
import { generateToken, getTokens } from '../../api/user-tokens';
import Link from '../../components/common/Link';
import { Button } from '../../components/controls/buttons';
import { ClipboardButton } from '../../components/controls/clipboard';
import { whenLoggedIn } from '../../components/hoc/whenLoggedIn';
import CheckIcon from '../../components/icons/CheckIcon';
import { translate, translateWithParameters } from '../../helpers/l10n';
import { portIsValid, sendUserToken } from '../../helpers/sonarlint';
import {
  computeTokenExpirationDate,
  getAvailableExpirationOptions,
  getNextTokenName,
} from '../../helpers/tokens';
import { NewUserToken, TokenExpiration } from '../../types/token';
import { LoggedInUser } from '../../types/users';
import './SonarLintConnection.css';

enum Status {
  request,
  tokenError,
  tokenCreated,
  tokenSent,
}

interface Props {
  currentUser: LoggedInUser;
}

const TOKEN_PREFIX = 'SonarLint';

const getNextAvailableTokenName = async (login: string, tokenNameBase: string) => {
  const tokens = await getTokens(login);

  return getNextTokenName(tokenNameBase, tokens);
};

async function computeExpirationDate() {
  const options = await getAvailableExpirationOptions();
  const maxOption = options[options.length - 1];

  return computeTokenExpirationDate(maxOption.value || TokenExpiration.OneYear);
}

export function SonarLintConnection({ currentUser }: Props) {
  const [searchParams] = useSearchParams();
  const [status, setStatus] = React.useState(Status.request);
  const [newToken, setNewToken] = React.useState<NewUserToken | undefined>(undefined);

  const port = parseInt(searchParams.get('port') ?? '0', 10);
  const ideName = searchParams.get('ideName') ?? translate('sonarlint-connection.unspecified-ide');

  const { login } = currentUser;

  const authorize = React.useCallback(async () => {
    const newTokenName = await getNextAvailableTokenName(login, `${TOKEN_PREFIX}-${ideName}`);
    const expirationDate = await computeExpirationDate();
    const token = await generateToken({ name: newTokenName, login, expirationDate }).catch(
      () => undefined
    );

    if (!token) {
      setStatus(Status.tokenError);
      return;
    }

    setNewToken(token);

    if (!portIsValid(port)) {
      setStatus(Status.tokenCreated);
      return;
    }

    try {
      await sendUserToken(port, token);
      setStatus(Status.tokenSent);
    } catch (_) {
      setStatus(Status.tokenCreated);
    }
  }, [port, ideName, login]);

  return (
    <div className="sonarlint-connection-page">
      <div className="sonarlint-connection-content boxed-group">
        <div className="boxed-group-inner text-center">
          {status === Status.request && (
            <>
              <h1 className="big-spacer-top big-spacer-bottom">
                {translate('sonarlint-connection.request.title')}
              </h1>
              <img
                alt=""
                aria-hidden={true}
                className="big-spacer-top big-spacer-bottom"
                src="/images/SonarLint-connection-request.png"
              />
              <p className="big big-spacer-top big-spacer-bottom">
                {translateWithParameters('sonarlint-connection.request.description', ideName)}
              </p>
              <p className="big huge-spacer-bottom">
                {translate('sonarlint-connection.request.description2')}
              </p>

              <Button className="big-spacer-bottom" onClick={authorize}>
                <CheckIcon className="spacer-right" />
                {translate('sonarlint-connection.request.action')}
              </Button>
            </>
          )}

          {status === Status.tokenError && (
            <>
              <img
                alt=""
                aria-hidden={true}
                className="big-spacer-top big-spacer-bottom padded-top"
                src="/images/cross.svg"
              />
              <h1 className="big-spacer-bottom">
                {translate('sonarlint-connection.token-error.title')}
              </h1>
              <p className="big big-spacer-top big-spacer-bottom">
                {translate('sonarlint-connection.token-error.description')}
              </p>
              <p className="big huge-spacer-bottom">
                <FormattedMessage
                  id="sonarlint-connection.token-error.description2"
                  defaultMessage={translate('sonarlint-connection.token-error.description2')}
                  values={{
                    link: (
                      <Link to="/account/security">
                        {translate('sonarlint-connection.token-error.description2.link')}
                      </Link>
                    ),
                  }}
                />
              </p>
            </>
          )}

          {status === Status.tokenCreated && newToken && (
            <>
              <img
                alt=""
                aria-hidden={true}
                className="big-spacer-top big-spacer-bottom padded-top"
                src="/images/check.svg"
              />
              <h1 className="big-spacer-bottom">
                {translate('sonarlint-connection.connection-error.title')}
              </h1>
              <p className="big big-spacer-top big-spacer-bottom">
                {translate('sonarlint-connection.connection-error.description')}
              </p>
              <div className="display-flex-center">
                <span className="field-label">
                  {translate('sonarlint-connection.connection-error.token-name')}
                </span>
                {newToken.name}
              </div>
              <hr />
              <div className="display-flex-center">
                <span className="field-label">
                  {translate('sonarlint-connection.connection-error.token-value')}
                </span>
                <span className="sonarlint-token-value">{newToken.token}</span>
                <ClipboardButton className="big-spacer-left" copyValue={newToken.token} />
              </div>
              <div className="big huge-spacer-top">
                <strong>{translate('sonarlint-connection.connection-error.next-steps')}</strong>
              </div>
              <ol className="big big-spacer-top big-spacer-bottom">
                <li>{translate('sonarlint-connection.connection-error.step1')}</li>
                <li>{translate('sonarlint-connection.connection-error.step2')}</li>
              </ol>
            </>
          )}

          {status === Status.tokenSent && newToken && (
            <>
              <h1 className="big-spacer-top big-spacer-bottom">
                {translate('sonarlint-connection.success.title')}
              </h1>
              <img
                alt=""
                aria-hidden={true}
                className="big-spacer-bottom"
                src="/images/SonarLint-connection-ok.png"
              />
              <p className="big big-spacer-top big-spacer-bottom">
                {translateWithParameters('sonarlint-connection.success.description', newToken.name)}
              </p>
              <div className="big huge-spacer-top">
                <strong>{translate('sonarlint-connection.success.last-step')}</strong>
              </div>
              <div className="big big-spacer-top big-spacer-bottom">
                {translate('sonarlint-connection.success.step')}
              </div>
            </>
          )}
        </div>
      </div>
    </div>
  );
}

export default whenLoggedIn(SonarLintConnection);