File size: 4,643 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
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
import { __, sprintf } from '@wordpress/i18n';
import { useMemo, useReducer } from 'react';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { errorNotice, successNotice } from 'calypso/state/notices/actions';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import { useDispatch, useSelector } from '../../../state';
import { GitHubConnectionForm } from '../components/github-connection-form';
import { GitHubInstallationData } from '../use-github-installations-query';
import { GitHubRepositoryData } from '../use-github-repositories-query';
import { RepositorySelectionDialog } from './repository-selection-dialog';
import { useCreateCodeDeployment } from './use-create-code-deployment';

const noticeOptions = {
	duration: 3000,
};

interface GitHubDeploymentCreationFormProps {
	onConnected(): void;
}

interface ConnectionFormReducerData {
	isRepositoryPickerOpen: boolean;
	installation?: GitHubInstallationData;
	repository?: GitHubRepositoryData;
}

const INITIAL_VALUES: ConnectionFormReducerData = {
	isRepositoryPickerOpen: false,
	installation: undefined,
	repository: undefined,
};

type ConnectionFormReducerActions =
	| { type: 'open-repository-picker' }
	| {
			type: 'select-repository';
			installation: GitHubInstallationData;
			repository: GitHubRepositoryData;
	  }
	| { type: 'close-repository-picker' };

const connectionFormReducer = ( data = INITIAL_VALUES, action: ConnectionFormReducerActions ) => {
	if ( action.type === 'open-repository-picker' ) {
		return {
			...data,
			isRepositoryPickerOpen: true,
		};
	}

	if ( action.type === 'select-repository' ) {
		return {
			isRepositoryPickerOpen: false,
			installation: action.installation,
			repository: action.repository,
		};
	}

	if ( action.type === 'close-repository-picker' ) {
		return {
			...data,
			isRepositoryPickerOpen: false,
		};
	}

	return data;
};

export const GitHubDeploymentCreationForm = ( {
	onConnected,
}: GitHubDeploymentCreationFormProps ) => {
	const [ { isRepositoryPickerOpen, installation, repository }, dispatch ] = useReducer(
		connectionFormReducer,
		INITIAL_VALUES
	);

	const initialValues = useMemo( () => {
		return {
			branch: repository?.default_branch ?? 'main',
			destPath: '/',
			isAutomated: false,
			workflowPath: undefined,
		};
	}, [ repository ] );

	const siteId = useSelector( getSelectedSiteId );
	const reduxDispatch = useDispatch();
	const { createDeployment } = useCreateCodeDeployment( siteId, {
		onSuccess: ( data ) => {
			reduxDispatch( successNotice( __( 'Deployment created.' ), noticeOptions ) );
			reduxDispatch(
				recordTracksEvent( 'calypso_hosting_github_create_deployment_success', {
					deployment_type: data ? getDeploymentTypeFromPath( data.target_dir ) : null,
					is_automated: data?.is_automated,
					workflow_path: data?.workflow_path,
				} )
			);
			onConnected();
		},
		onError: ( error ) => {
			reduxDispatch(
				recordTracksEvent( 'calypso_hosting_github_create_deployment_failure', {
					reason: error.code,
				} )
			);
			reduxDispatch(
				errorNotice(
					// translators: "reason" is why connecting the branch failed.
					sprintf( __( 'Failed to create deployment: %(reason)s' ), { reason: error.message } ),
					{
						...noticeOptions,
					}
				)
			);
		},
	} );

	return (
		<>
			<GitHubConnectionForm
				installationId={ installation?.external_id }
				key={ repository?.id ?? 'none' }
				repository={ repository }
				initialValues={ initialValues }
				changeRepository={ () => {
					reduxDispatch( recordTracksEvent( 'calypso_hosting_github_repository_picker_click' ) );
					dispatch( { type: 'open-repository-picker' } );
				} }
				onSubmit={ ( {
					externalRepositoryId,
					branchName,
					targetDir,
					installationId,
					isAutomated,
					workflowPath,
				} ) =>
					createDeployment( {
						externalRepositoryId,
						branchName,
						targetDir,
						installationId,
						isAutomated,
						workflowPath,
					} )
				}
			/>
			<RepositorySelectionDialog
				isVisible={ isRepositoryPickerOpen }
				onChange={ ( installation, repository ) => {
					reduxDispatch( recordTracksEvent( 'calypso_hosting_github_select_repository_click' ) );
					dispatch( { type: 'select-repository', installation, repository } );
				} }
				onClose={ () => dispatch( { type: 'close-repository-picker' } ) }
			/>
		</>
	);
};

export function getDeploymentTypeFromPath( path: string ) {
	if ( path === '/' ) {
		return 'root';
	} else if ( path === '/wp-content' ) {
		return 'wp-content';
	} else if ( path.includes( 'wp-content/plugins' ) ) {
		return 'plugin';
	}
	return 'theme';
}