123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /*
- * 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 { useEffect, useState } from 'react';
- import {
- checkPersonalAccessTokenIsValid,
- setAlmPersonalAccessToken,
- } from '../../../api/alm-integrations';
- import { translate } from '../../../helpers/l10n';
- import { AlmSettingsInstance } from '../../../types/alm-settings';
- import { tokenExistedBefore } from './utils';
-
- export interface PATType {
- validationFailed: boolean;
- validationErrorMessage?: string;
- touched: boolean;
- password: string;
- username?: string;
- submitting: boolean;
- checkingPat: boolean;
- firstConnection: boolean;
- handleUsernameChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
- handlePasswordChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
- handleSubmit: (e: React.SyntheticEvent<HTMLFormElement>) => Promise<void>;
- }
-
- export const usePersonalAccessToken = (
- almSetting: AlmSettingsInstance,
- resetPat: boolean,
- onPersonalAccessTokenCreated: () => void,
- ): PATType => {
- const [checkingPat, setCheckingPat] = useState(false);
- const [touched, setTouched] = useState(false);
- const [password, setPassword] = useState('');
- const [submitting, setSubmitting] = useState(false);
- const [validationFailed, setValidationFailed] = useState(false);
- const [validationErrorMessage, setValidationErrorMessage] = useState<string | undefined>();
- const [firstConnection, setFirstConnection] = useState(false);
- const [username, setUsername] = useState('');
-
- useEffect(() => {
- const checkPATAndUpdateView = async () => {
- const { key } = almSetting;
-
- // We don't need to check PAT if we want to reset
- if (!resetPat) {
- setCheckingPat(true);
- const { patIsValid, error } = await checkPersonalAccessTokenIsValid(key)
- .then(({ status, error }) => ({ patIsValid: status, error }))
- .catch(() => ({ patIsValid: status, error: translate('default_error_message') }));
- if (patIsValid) {
- onPersonalAccessTokenCreated();
- return;
- }
- // This is the initial message when no token was provided
- if (tokenExistedBefore(error)) {
- setCheckingPat(false);
- setFirstConnection(true);
- } else {
- setCheckingPat(false);
- setValidationFailed(true);
- setValidationErrorMessage(error);
- }
- }
- };
- checkPATAndUpdateView();
- }, [almSetting, resetPat, onPersonalAccessTokenCreated]);
-
- const handleUsernameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
- setTouched(true);
- setUsername(event.target.value);
- };
-
- const handlePasswordChange = (event: React.ChangeEvent<HTMLInputElement>) => {
- setTouched(true);
- setPassword(event.target.value);
- };
-
- const handleSubmit = async (e: React.SyntheticEvent<HTMLFormElement>) => {
- const { key } = almSetting;
-
- e.preventDefault();
- if (password) {
- setSubmitting(true);
-
- await setAlmPersonalAccessToken(key, password, username).catch(() => {
- /* Set will not check pat validity. We need to check again so we will catch issue after */
- });
-
- const { status, error } = await checkPersonalAccessTokenIsValid(key)
- .then(({ status, error }) => ({ status, error }))
- .catch(() => ({ status: false, error: translate('default_error_message') }));
-
- if (status) {
- // Let's reset status,
- setCheckingPat(false);
- setTouched(false);
- setPassword('');
- setSubmitting(false);
- setUsername('');
- setValidationFailed(false);
-
- onPersonalAccessTokenCreated();
- } else {
- setSubmitting(false);
- setTouched(false);
- setValidationFailed(true);
- setValidationErrorMessage(error);
- }
- }
- };
-
- return {
- username,
- password,
- firstConnection,
- validationFailed,
- touched,
- submitting,
- checkingPat,
- validationErrorMessage,
- handleUsernameChange,
- handlePasswordChange,
- handleSubmit,
- };
- };
|