From 80e01984fcffe2c76f61d76c34620edfb73f68be Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Fri, 26 May 2023 16:58:05 +0200 Subject: [PATCH] SONAR-19347 Add synchronize now button for Github --- .../sonar-web/src/main/js/api/provisioning.ts | 4 +++ .../GitHubSynchronisationWarning.tsx | 10 ++---- .../GithubAuthenticationTab.tsx | 11 ++++++ .../queries/IdentityProvider.ts | 6 +--- .../src/main/js/queries/github-sync.ts | 34 ++++++++++++++++--- .../resources/org/sonar/l10n/core.properties | 1 + 6 files changed, 48 insertions(+), 18 deletions(-) diff --git a/server/sonar-web/src/main/js/api/provisioning.ts b/server/sonar-web/src/main/js/api/provisioning.ts index 10220bb92c2..fbadc9dee85 100644 --- a/server/sonar-web/src/main/js/api/provisioning.ts +++ b/server/sonar-web/src/main/js/api/provisioning.ts @@ -46,3 +46,7 @@ export function activateGithubProvisioning(): Promise { export function deactivateGithubProvisioning(): Promise { return post('/api/github_provisioning/disable').catch(throwGlobalError); } + +export function syncNowGithubProvisioning(): Promise { + return post('/api/github_provisioning/sync').catch(throwGlobalError); +} diff --git a/server/sonar-web/src/main/js/app/components/GitHubSynchronisationWarning.tsx b/server/sonar-web/src/main/js/app/components/GitHubSynchronisationWarning.tsx index e011d74d7bb..5d7cb2544c7 100644 --- a/server/sonar-web/src/main/js/app/components/GitHubSynchronisationWarning.tsx +++ b/server/sonar-web/src/main/js/app/components/GitHubSynchronisationWarning.tsx @@ -19,18 +19,15 @@ */ import { formatDistance } from 'date-fns'; import * as React from 'react'; -import { useContext } from 'react'; import { FormattedMessage } from 'react-intl'; import Link from '../../components/common/Link'; import CheckIcon from '../../components/icons/CheckIcon'; import { Alert } from '../../components/ui/Alert'; import { translate, translateWithParameters } from '../../helpers/l10n'; import { useSyncStatusQuery } from '../../queries/github-sync'; -import { Feature } from '../../types/features'; import { GithubStatusEnabled } from '../../types/provisioning'; import { TaskStatuses } from '../../types/tasks'; import './SystemAnnouncement.css'; -import { AvailableFeaturesContext } from './available-features/AvailableFeaturesContext'; interface LastSyncProps { short?: boolean; @@ -100,10 +97,7 @@ function LastSyncAlert({ info, short }: LastSyncProps) { } function GitHubSynchronisationWarning({ short }: GitHubSynchronisationWarningProps) { - const hasGithubProvisioning = useContext(AvailableFeaturesContext).includes( - Feature.GithubProvisioning - ); - const { data } = useSyncStatusQuery({ enabled: hasGithubProvisioning }); + const { data } = useSyncStatusQuery(); if (!data) { return null; @@ -113,7 +107,7 @@ function GitHubSynchronisationWarning({ short }: GitHubSynchronisationWarningPro <> {!short && data?.nextSync && ( <> - + {translate( data.nextSync.status === TaskStatuses.Pending ? 'settings.authentication.github.synchronization_pending' diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx index 5df3bcdc37d..1f2348a1f62 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/GithubAuthenticationTab.tsx @@ -30,6 +30,7 @@ import DeleteIcon from '../../../../components/icons/DeleteIcon'; import EditIcon from '../../../../components/icons/EditIcon'; import { Alert } from '../../../../components/ui/Alert'; import { translate, translateWithParameters } from '../../../../helpers/l10n'; +import { useSyncNow } from '../../../../queries/github-sync'; import { AlmKeys } from '../../../../types/alm-settings'; import { ExtendedSettingDefinition } from '../../../../types/settings'; import { DOCUMENTATION_LINK_SUFFIXES } from './Authentication'; @@ -78,6 +79,7 @@ export default function GithubAuthenticationTab(props: GithubAuthenticationProps } = useGithubConfiguration(definitions); const hasDifferentProvider = data?.provider !== undefined && data.provider !== Provider.Github; + const { canSyncNow, synchronizeNow } = useSyncNow(); const handleCreateConfiguration = () => { setShowEditModal(true); @@ -220,6 +222,15 @@ export default function GithubAuthenticationTab(props: GithubAuthenticationProps />

{githubProvisioningStatus && } +
+ +
) : (

diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/queries/IdentityProvider.ts b/server/sonar-web/src/main/js/apps/settings/components/authentication/queries/IdentityProvider.ts index d5b5858339a..1396ec4dc1f 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/queries/IdentityProvider.ts +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/queries/IdentityProvider.ts @@ -52,11 +52,7 @@ export function useScimStatusQuery() { } export function useGithubStatusQuery() { - const hasGithubProvisioning = useContext(AvailableFeaturesContext).includes( - Feature.GithubProvisioning - ); - - const res = useSyncStatusQuery({ enabled: hasGithubProvisioning }); + const res = useSyncStatusQuery(); return { ...res, data: res.data?.enabled }; } diff --git a/server/sonar-web/src/main/js/queries/github-sync.ts b/server/sonar-web/src/main/js/queries/github-sync.ts index d2be1351260..67f7e3c8edf 100644 --- a/server/sonar-web/src/main/js/queries/github-sync.ts +++ b/server/sonar-web/src/main/js/queries/github-sync.ts @@ -17,9 +17,33 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { useQuery } from '@tanstack/react-query'; -import { fetchGithubProvisioningStatus } from '../api/provisioning'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { useContext } from 'react'; +import { fetchGithubProvisioningStatus, syncNowGithubProvisioning } from '../api/provisioning'; +import { AvailableFeaturesContext } from '../app/components/available-features/AvailableFeaturesContext'; +import { Feature } from '../types/features'; -export const useSyncStatusQuery = ({ enabled }: { enabled?: boolean }) => { - return useQuery(['github_sync', 'status'], fetchGithubProvisioningStatus, { enabled }); -}; +export function useSyncStatusQuery() { + const hasGithubProvisioning = useContext(AvailableFeaturesContext).includes( + Feature.GithubProvisioning + ); + return useQuery(['github_sync', 'status'], fetchGithubProvisioningStatus, { + enabled: hasGithubProvisioning, + refetchInterval: 10_000, + }); +} + +export function useSyncNow() { + const queryClient = useQueryClient(); + const { data } = useSyncStatusQuery(); + const mutation = useMutation(syncNowGithubProvisioning, { + onSuccess: () => { + queryClient.invalidateQueries(['github_sync']); + }, + }); + + return { + synchronizeNow: mutation.mutate, + canSyncNow: data && data.enabled && !data.nextSync && !mutation.isLoading, + }; +} diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index 291c3fedba3..246c5c5c0d5 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -1386,6 +1386,7 @@ settings.authentication.github.form.provisioning_with_github=Automatic user and settings.authentication.github.form.provisioning_with_github.description=Users and groups are automatically provisioned from your GitHub organizations. Once activated, managed users and groups can only be modified from your GitHub organizations/teams. Existing local users and groups will be kept. settings.authentication.github.form.provisioning_with_github.description.doc=For more details, see {documentation}. settings.authentication.github.form.provisioning.disabled=Your current edition does not support provisioning with GitHub. See the {documentation} for more information. +settings.authentication.github.synchronize_now=Synchronize now settings.authentication.github.synchronization_in_progress=Synchronization is in progress. settings.authentication.github.synchronization_pending=Synchronization is scheduled. settings.authentication.github.synchronization_successful=Last synchronization was done {0} ago. -- 2.39.5