aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubAuthenticationTab.tsx
blob: 456d1c3f7e6d7067be575e5b1309b7cabe017884 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * 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 { Spinner } from '@sonarsource/echoes-react';
import { ButtonSecondary, FlagMessage, Highlight, Note } from 'design-system/lib';
import { isEmpty, omitBy } from 'lodash';
import React, { FormEvent, useContext, useState } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import GitHubSynchronisationWarning from '../../../../app/components/GitHubSynchronisationWarning';
import { AvailableFeaturesContext } from '../../../../app/components/available-features/AvailableFeaturesContext';
import DocumentationLink from '../../../../components/common/DocumentationLink';
import { DocLink } from '../../../../helpers/doc-links';
import { translate } from '../../../../helpers/l10n';
import {
  useDeleteGitHubConfigurationMutation,
  useSearchGitHubConfigurationsQuery,
  useUpdateGitHubConfigurationMutation,
} from '../../../../queries/dop-translation';
import { useIdentityProviderQuery } from '../../../../queries/identity-provider/common';
import {
  useGithubRolesMappingMutation,
  useSyncWithGitHubNow,
} from '../../../../queries/identity-provider/github';
import { GitHubConfigurationPayload } from '../../../../types/dop-translation';
import { Feature } from '../../../../types/features';
import { DevopsRolesMapping, ProvisioningType } from '../../../../types/provisioning';
import { Provider } from '../../../../types/types';
import AuthenticationFormField from './AuthenticationFormField';
import AutoProvisioningConsent from './AutoProvisionningConsent';
import ConfigurationDetails from './ConfigurationDetails';
import ConfirmProvisioningModal from './ConfirmProvisioningModal';
import GitHubConfigurationForm from './GitHubConfigurationForm';
import GitHubConfigurationValidity from './GitHubConfigurationValidity';
import GitHubMappingModal from './GitHubMappingModal';
import ProvisioningSection from './ProvisioningSection';
import TabHeader from './TabHeader';
import { GITHUB_PROVISIONING_FIELDS_DEFINITIONS } from './utils';

type ChangesForm = Partial<
  Pick<GitHubConfigurationPayload, 'allowUsersToSignUp' | 'projectVisibility' | 'provisioningType'>
>;

export default function GitHubAuthenticationTab() {
  const [isFormOpen, setIsFormOpen] = React.useState(false);
  const [changes, setChanges] = React.useState<ChangesForm | undefined>(undefined);
  const [tokenKey, setTokenKey] = React.useState<number>(0);
  const [isConfirmProvisioningModalOpen, setIsConfirmProvisioningModalOpen] = React.useState(false);
  const [isMappingModalOpen, setIsMappingModalOpen] = useState(false);
  const [rolesMapping, setRolesMapping] = useState<DevopsRolesMapping[] | null>(null);

  const hasGithubProvisioning = useContext(AvailableFeaturesContext).includes(
    Feature.GithubProvisioning,
  );

  const { data: identityProvider } = useIdentityProviderQuery();
  const { data: list, isLoading: isLoadingList } = useSearchGitHubConfigurationsQuery();
  const gitHubConfiguration = list?.githubConfigurations[0];

  const { canSyncNow, synchronizeNow } = useSyncWithGitHubNow();

  const { mutate: updateConfig, isPending: isUpdating } = useUpdateGitHubConfigurationMutation();
  const { mutate: deleteConfig, isPending: isDeleting } = useDeleteGitHubConfigurationMutation();
  const { mutateAsync: updateMapping } = useGithubRolesMappingMutation();

  const { formatMessage } = useIntl();

  const hasConfiguration = gitHubConfiguration !== undefined;
  const hasLegacyConfiguration = gitHubConfiguration?.applicationId === '';
  const isConfigurationEnabled = gitHubConfiguration?.enabled === true;

  const hasDifferentProvider =
    identityProvider?.provider !== undefined && identityProvider.provider !== Provider.Github;
  const allowUsersToSignUpDefinition = GITHUB_PROVISIONING_FIELDS_DEFINITIONS.allowUsersToSignUp;
  const projectVisibilityDefinition = GITHUB_PROVISIONING_FIELDS_DEFINITIONS.projectVisibility;

  const provisioningType = changes?.provisioningType ?? gitHubConfiguration?.provisioningType;
  const allowUsersToSignUp = changes?.allowUsersToSignUp ?? gitHubConfiguration?.allowUsersToSignUp;
  const projectVisibility = changes?.projectVisibility ?? gitHubConfiguration?.projectVisibility;

  const onChangeProvisioningType = (val?: ProvisioningType) => {
    setRolesMapping(null);

    if (val === ProvisioningType.auto) {
      setAutoProvisioning();
    } else if (val === ProvisioningType.jit) {
      setJITProvisioning();
    } else {
      setChanges({
        allowUsersToSignUp: gitHubConfiguration?.allowUsersToSignUp,
        projectVisibility: gitHubConfiguration?.projectVisibility,
        provisioningType: gitHubConfiguration?.provisioningType,
      });
    }
  };

  const onDeleteConfiguration = () => {
    if (!gitHubConfiguration) {
      return;
    }
    deleteConfig(gitHubConfiguration.id);
  };

  const onSaveProvisioning = (e: FormEvent) => {
    e.preventDefault();
    if (
      changes?.provisioningType !== undefined ||
      (isEmpty(gitHubConfiguration?.allowedOrganizations) &&
        (allowUsersToSignUp || provisioningType === ProvisioningType.auto))
    ) {
      setIsConfirmProvisioningModalOpen(true);
    } else {
      onUpdateProvisioning();
    }
  };

  const onToggleConfiguration = () => {
    if (!gitHubConfiguration) {
      return;
    }
    updateConfig({
      id: gitHubConfiguration.id,
      gitHubConfiguration: { enabled: !isConfigurationEnabled },
    });
  };

  const onUpdateProvisioning = () => {
    if ((!changes && !rolesMapping) || !gitHubConfiguration) {
      return;
    }

    updateConfig(
      {
        id: gitHubConfiguration.id,
        gitHubConfiguration: omitBy(changes, (value) => value === undefined),
      },
      {
        onSuccess: () => {
          setChanges(undefined);
          setTokenKey(tokenKey + 1);
        },
      },
    );
    if (provisioningType === ProvisioningType.auto && rolesMapping) {
      updateMapping(rolesMapping)
        .then(() => {
          setRolesMapping(null);
        })
        .catch(() => {});
    }
  };

  const setAutoProvisioning = () => {
    setChanges((prevChanges) => ({
      ...prevChanges,
      provisioningType: ProvisioningType.auto,
    }));
  };

  const setJITProvisioning = () => {
    setChanges((prevChanges) => ({
      ...prevChanges,
      provisioningType: ProvisioningType.jit,
    }));
  };

  return (
    <Spinner isLoading={isLoadingList}>
      <div>
        <TabHeader
          title={translate('settings.authentication.github.configuration')}
          showCreate={!hasConfiguration || hasLegacyConfiguration}
          onCreate={() => {
            setIsFormOpen(true);
          }}
          configurationValidity={
            <>
              {!isLoadingList && isConfigurationEnabled && !hasLegacyConfiguration && (
                <GitHubConfigurationValidity
                  isAutoProvisioning={provisioningType === ProvisioningType.auto}
                  selectedOrganizations={gitHubConfiguration.allowedOrganizations}
                />
              )}
            </>
          }
        />
        {!hasConfiguration && (
          <div>{translate('settings.authentication.github.form.not_configured')}</div>
        )}
        {hasLegacyConfiguration && (
          <FlagMessage variant="warning">
            <div>
              <FormattedMessage
                id="settings.authentication.github.form.legacy_configured"
                defaultMessage={translate('settings.authentication.github.form.legacy_configured')}
                values={{
                  documentation: (
                    <DocumentationLink to={DocLink.AlmGitHubAuth}>
                      {translate('settings.authentication.github.form.legacy_configured.link')}
                    </DocumentationLink>
                  ),
                }}
              />
            </div>
          </FlagMessage>
        )}
        {!hasLegacyConfiguration && hasConfiguration && (
          <>
            <ConfigurationDetails
              title={formatMessage(
                { id: 'settings.authentication.github.appid_x' },
                { applicationId: gitHubConfiguration.applicationId },
              )}
              url={gitHubConfiguration.apiUrl}
              canDisable={
                !isUpdating && gitHubConfiguration.provisioningType !== ProvisioningType.auto
              }
              enabled={isConfigurationEnabled}
              isDeleting={isDeleting}
              onEdit={() => {
                setIsFormOpen(true);
              }}
              onDelete={onDeleteConfiguration}
              onToggle={onToggleConfiguration}
            />

            <ProvisioningSection
              autoDescription={
                <FormattedMessage
                  id="settings.authentication.github.form.provisioning_with_github.description"
                  values={{
                    documentation: (
                      <DocumentationLink to={DocLink.AlmGitHubAuth}>
                        {translate('learn_more')}
                      </DocumentationLink>
                    ),
                  }}
                />
              }
              autoFeatureDisabledText={
                <FormattedMessage
                  id="settings.authentication.github.form.provisioning.disabled"
                  defaultMessage={translate(
                    'settings.authentication.github.form.provisioning.disabled',
                  )}
                  values={{
                    documentation: (
                      <DocumentationLink to={DocLink.AlmGitHubAuth}>
                        {translate('documentation')}
                      </DocumentationLink>
                    ),
                  }}
                />
              }
              autoSettings={
                <>
                  <AuthenticationFormField
                    definition={projectVisibilityDefinition}
                    isNotSet={gitHubConfiguration.provisioningType !== ProvisioningType.auto}
                    mandatory
                    onFieldChange={(_, value) =>
                      setChanges((prevChanges) => ({
                        ...prevChanges,
                        projectVisibility: value as boolean,
                      }))
                    }
                    settingValue={projectVisibility}
                  />
                  <div className="sw-mt-6">
                    <div className="sw-flex">
                      <Highlight className="sw-mb-4 sw-mr-4 sw-flex sw-items-center sw-gap-2">
                        <FormattedMessage id="settings.authentication.configuration.roles_mapping.title" />
                      </Highlight>
                      <ButtonSecondary
                        className="sw--mt-2"
                        onClick={() => setIsMappingModalOpen(true)}
                      >
                        <FormattedMessage id="settings.authentication.configuration.roles_mapping.button_label" />
                      </ButtonSecondary>
                    </div>
                    <Note className="sw-mt-2">
                      <FormattedMessage id="settings.authentication.github.configuration.roles_mapping.description" />
                    </Note>
                  </div>
                </>
              }
              autoTitle={translate('settings.authentication.github.form.provisioning_with_github')}
              canSync={canSyncNow}
              disabledConfigText={translate('settings.authentication.github.enable_first')}
              enabled={isConfigurationEnabled}
              hasDifferentProvider={hasDifferentProvider}
              hasFeatureEnabled={hasGithubProvisioning}
              hasUnsavedChanges={changes !== undefined || rolesMapping !== null}
              isLoading={isUpdating}
              jitDescription={
                <FormattedMessage
                  id="settings.authentication.github.form.provisioning_at_login.description"
                  values={{
                    documentation: (
                      <DocumentationLink to={DocLink.AlmGitHubAuth}>
                        {translate('learn_more')}
                      </DocumentationLink>
                    ),
                  }}
                />
              }
              jitSettings={
                <AuthenticationFormField
                  definition={allowUsersToSignUpDefinition}
                  isNotSet={gitHubConfiguration.provisioningType !== ProvisioningType.auto}
                  mandatory
                  onFieldChange={(_, value) =>
                    setChanges((prevChanges) => ({
                      ...prevChanges,
                      allowUsersToSignUp: value as boolean,
                    }))
                  }
                  settingValue={allowUsersToSignUp}
                />
              }
              jitTitle={translate('settings.authentication.form.provisioning_at_login')}
              onCancel={() => {
                setChanges(undefined);
                onChangeProvisioningType();
              }}
              onChangeProvisioningType={onChangeProvisioningType}
              onSave={onSaveProvisioning}
              onSyncNow={synchronizeNow}
              provisioningType={provisioningType ?? ProvisioningType.jit}
              synchronizationDetails={<GitHubSynchronisationWarning />}
            />

            {provisioningType && (
              <ConfirmProvisioningModal
                allowUsersToSignUp={allowUsersToSignUp}
                hasProvisioningTypeChange={changes?.provisioningType !== undefined}
                isAllowListEmpty={isEmpty(gitHubConfiguration.allowedOrganizations)}
                isOpen={isConfirmProvisioningModalOpen}
                onClose={() => setIsConfirmProvisioningModalOpen(false)}
                onConfirm={onUpdateProvisioning}
                provider={Provider.Github}
                provisioningStatus={provisioningType}
              />
            )}

            {isMappingModalOpen && (
              <GitHubMappingModal
                mapping={rolesMapping}
                setMapping={setRolesMapping}
                onClose={() => setIsMappingModalOpen(false)}
              />
            )}
          </>
        )}

        {isFormOpen && (
          <GitHubConfigurationForm
            gitHubConfiguration={gitHubConfiguration}
            isStandardModeConfiguration={hasLegacyConfiguration}
            onClose={() => {
              setIsFormOpen(false);
            }}
          />
        )}

        <AutoProvisioningConsent githubConfiguration={gitHubConfiguration} />
      </div>
    </Spinner>
  );
}