File size: 3,595 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { FormLabel } from '@automattic/components';
import { Button, ExternalLink } from '@wordpress/components';
import { createInterpolateElement } from '@wordpress/element';
import { useI18n } from '@wordpress/react-i18n';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { GitHubRepositoryData } from '../../use-github-repositories-query';
import { CodeHighlighter } from '../code-highlighter';
import { useDeploymentStyleContext } from './context';
import { Workflow } from './use-deployment-workflows-query';
import { useWorkflowValidations } from './use-workflow-validations';
import { WorkflowValidation } from './workflow-validation';

interface WorkflowValidationWizardProps {
	repository: Pick< GitHubRepositoryData, 'owner' | 'name' >;
	branchName: string;
	workflow: Workflow;
	validYamlFile: string;
}

export const WorkflowValidationWizard = ( {
	repository,
	branchName,
	workflow,
	validYamlFile,
}: WorkflowValidationWizardProps ) => {
	const { __ } = useI18n();
	const dispatch = useDispatch();
	const validations = useWorkflowValidations( { branchName, validYamlFile } );
	const { workflowCheckResult, isCheckingWorkflow, onWorkflowVerify } = useDeploymentStyleContext();

	useEffect( () => {
		if ( ! workflowCheckResult ) {
			return;
		} else if ( workflowCheckResult.conclusion === 'error' ) {
			const checks: Record< string, string > = {};
			workflowCheckResult.checked_items.forEach( ( check ) => {
				checks[ check.validation_name ] = check.status;
			} );
			dispatch( recordTracksEvent( 'calypso_hosting_github_workflow_invalid', checks ) );
		} else {
			dispatch( recordTracksEvent( 'calypso_hosting_github_workflow_valid' ) );
		}
	}, [ workflowCheckResult, dispatch ] );

	const getWorkflowCheckDescription = () => {
		if ( ! workflowCheckResult ) {
			return;
		}

		const workflowPath = `https://github.com/${ repository.owner }/${ repository.name }/blob/${ branchName }/${ workflow.workflow_path }`;
		const description =
			workflowCheckResult.conclusion === 'error'
				? createInterpolateElement( __( 'Your workflow <filename /> is good to go!' ), {
						filename: <ExternalLink href={ workflowPath }>{ workflow.file_name }</ExternalLink>,
				  } )
				: createInterpolateElement(
						__( 'Please edit <filename /> and fix the problems we found.' ),
						{
							filename: <ExternalLink href={ workflowPath }>{ workflow.file_name }</ExternalLink>,
						}
				  );

		return <p css={ { fontSize: '14px' } }>{ description }</p>;
	};

	return (
		<div>
			<FormLabel>{ __( 'Workflow check' ) }</FormLabel>
			{ getWorkflowCheckDescription() }
			{ workflowCheckResult?.checked_items.map( ( workflowCheck ) => {
				const validation = validations[ workflowCheck.validation_name ];

				if ( ! validation ) {
					return null;
				}

				return (
					<WorkflowValidation
						key={ validation.label }
						label={ validation.label }
						status={ isCheckingWorkflow ? 'loading' : workflowCheck.status }
					>
						<p>{ validation.description }</p>
						<CodeHighlighter content={ validation.content } />
					</WorkflowValidation>
				);
			} ) }
			<div css={ { marginTop: '8px' } }>
				<Button
					variant="secondary"
					type="button"
					isBusy={ isCheckingWorkflow }
					onClick={ () => {
						dispatch( recordTracksEvent( 'calypso_hosting_github_validate_workflow_click' ) );
						onWorkflowVerify();
					} }
					disabled={ isCheckingWorkflow }
				>
					{ __( 'Verify workflow' ) }
				</Button>
			</div>
		</div>
	);
};