From: Jeremy Davis Date: Mon, 21 Oct 2019 15:36:17 +0000 (+0200) Subject: SONAR-12515 UI for project-level form - Azure X-Git-Tag: 8.1.0.31237~133 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=efe02985a999dcc36c966ade6153e3af4829bdab;p=sonarqube.git SONAR-12515 UI for project-level form - Azure --- diff --git a/server/sonar-web/src/main/js/api/almSettings.ts b/server/sonar-web/src/main/js/api/almSettings.ts index 52cf1b61366..e7aaee50827 100644 --- a/server/sonar-web/src/main/js/api/almSettings.ts +++ b/server/sonar-web/src/main/js/api/almSettings.ts @@ -57,13 +57,17 @@ export function countBindedProjects(almSetting: string) { } export function getProjectAlmBinding(project: string): Promise { - return getJSON('/api/alm_settings/get_github_binding', { project }); + return getJSON('/api/alm_settings/get_binding', { project }); } export function deleteProjectAlmBinding(project: string): Promise { return post('/api/alm_settings/delete_binding', { project }).catch(throwGlobalError); } -export function setProjectAlmBinding(data: T.GithubProjectAlmBinding) { +export function setProjectAzureBinding(data: T.AzureProjectAlmBinding) { + return post('/api/alm_settings/set_azure_binding', data).catch(throwGlobalError); +} + +export function setProjectGithubBinding(data: T.GithubProjectAlmBinding) { return post('/api/alm_settings/set_github_binding', data).catch(throwGlobalError); } diff --git a/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBinding.tsx b/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBinding.tsx index 82482cbcbfc..6c31725727a 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBinding.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBinding.tsx @@ -22,9 +22,11 @@ import { deleteProjectAlmBinding, getAlmSettings, getProjectAlmBinding, - setProjectAlmBinding + setProjectAzureBinding, + setProjectGithubBinding } from '../../../../api/almSettings'; import throwGlobalError from '../../../../app/utils/throwGlobalError'; +import { ALM_KEYS } from '../../utils'; import PRDecorationBindingRenderer from './PRDecorationBindingRenderer'; interface Props { @@ -32,7 +34,7 @@ interface Props { } interface State { - formData: T.GithubBinding; + formData: T.ProjectAlmBinding; hasBinding: boolean; instances: T.AlmSettingsInstance[]; isValid: boolean; @@ -41,6 +43,11 @@ interface State { success: boolean; } +const FIELDS_BY_ALM: { [almKey: string]: Array<'repository'> } = { + [ALM_KEYS.AZURE]: [], + [ALM_KEYS.GITHUB]: ['repository'] +}; + export default class PRDecorationBinding extends React.PureComponent { mounted = false; state: State = { @@ -70,13 +77,16 @@ export default class PRDecorationBinding extends React.PureComponent { if (this.mounted) { - this.setState(({ formData }) => ({ - formData: data || formData, - hasBinding: Boolean(data), - instances, - isValid: this.validateForm(), - loading: false - })); + this.setState(({ formData }) => { + const newFormData = data || formData; + return { + formData: newFormData, + hasBinding: Boolean(data), + instances, + isValid: this.validateForm(newFormData), + loading: false + }; + }); if (!data && instances.length === 1) { this.handleFieldChange('key', instances[0].key); @@ -125,18 +135,50 @@ export default class PRDecorationBinding extends React.PureComponent { + const almSetting = key; + const project = this.props.component.key; + + switch (alm) { + case ALM_KEYS.AZURE: + return setProjectAzureBinding({ + almSetting, + project + }); + case ALM_KEYS.GITHUB: { + const repository = almSpecificFields && almSpecificFields.repository; + if (!repository) { + return Promise.reject(); + } + return setProjectGithubBinding({ + almSetting, + project, + repository + }); + } + default: + return Promise.reject(); + } + } + handleSubmit = () => { this.setState({ saving: true }); const { - formData: { key, repository } + formData: { key, ...additionalFields }, + instances } = this.state; - if (key && repository) { - setProjectAlmBinding({ - almSetting: key, - project: this.props.component.key, - repository - }) + const selected = instances.find(i => i.key === key); + if (!key || !selected) { + return; + } + + if (key) { + this.submitProjectAlmBinding(selected.alm as ALM_KEYS, key, additionalFields) .then(() => { if (this.mounted) { this.setState({ @@ -150,21 +192,28 @@ export default class PRDecorationBinding extends React.PureComponent { - this.setState(({ formData: formdata }) => ({ - formData: { - ...formdata, + handleFieldChange = (id: keyof T.ProjectAlmBinding, value: string) => { + this.setState(({ formData }) => { + const newFormData = { + ...formData, [id]: value - }, - isValid: this.validateForm(), - success: false - })); + }; + return { + formData: newFormData, + isValid: this.validateForm(newFormData), + success: false + }; + }); }; - validateForm = () => { - const { formData } = this.state; - return Object.values(formData).reduce( - (result: boolean, value) => result && Boolean(value), + validateForm = ({ key, ...additionalFields }: State['formData']) => { + const { instances } = this.state; + const selected = instances.find(i => i.key === key); + if (!key || !selected) { + return false; + } + return FIELDS_BY_ALM[selected.alm as ALM_KEYS].reduce( + (result: boolean, field) => result && Boolean(additionalFields[field]), true ); }; diff --git a/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBindingRenderer.tsx b/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBindingRenderer.tsx index a36aca0c226..3fa7892e320 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBindingRenderer.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/pullRequestDecorationBinding/PRDecorationBindingRenderer.tsx @@ -26,14 +26,15 @@ import AlertSuccessIcon from 'sonar-ui-common/components/icons/AlertSuccessIcon' import { Alert } from 'sonar-ui-common/components/ui/Alert'; import DeferredSpinner from 'sonar-ui-common/components/ui/DeferredSpinner'; import { translate } from 'sonar-ui-common/helpers/l10n'; +import { ALM_KEYS } from '../../utils'; export interface PRDecorationBindingRendererProps { - formData: T.GithubBinding; + formData: T.ProjectAlmBinding; hasBinding: boolean; instances: T.AlmSettingsInstance[]; isValid: boolean; loading: boolean; - onFieldChange: (id: keyof T.GithubBinding, value: string) => void; + onFieldChange: (id: keyof T.ProjectAlmBinding, value: string) => void; onReset: () => void; onSubmit: () => void; saving: boolean; @@ -79,6 +80,9 @@ export default function PRDecorationBindingRenderer(props: PRDecorationBindingRe ); } + const selected = key && instances.find(i => i.key === key); + const alm = selected && (selected.alm as ALM_KEYS); + return (
@@ -110,7 +114,7 @@ export default function PRDecorationBindingRenderer(props: PRDecorationBindingRe />
- {key && ( + {alm === ALM_KEYS.GITHUB && (