File size: 1,522 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
import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import wp from 'calypso/lib/wp';
import { GITHUB_DEPLOYMENTS_QUERY_KEY } from '../constants';
import { CodeDeploymentData } from '../deployments/use-code-deployments-query';

export const CODE_DEPLOYMENTS_RUNS_QUERY_KEY = 'code-deployments-runs';

export type DeploymentRunStatus =
	| 'pending'
	| 'queued'
	| 'running'
	| 'success'
	| 'failed'
	| 'warnings'
	| 'building'
	| 'dispatched'
	| 'unknown';

export interface DeploymentRun {
	id: number;
	code_deployment_id: number;
	created_on: string;
	started_on: string;
	completed_on: string;
	status: DeploymentRunStatus;
	failure_code: string | null;
	triggered_by_user_id: number;
	metadata: Metadata;
	code_deployment?: CodeDeploymentData;
}

export interface Metadata {
	commit_message: string;
	commit_sha: string;
	job_id: number;
	author: {
		avatar_url: string;
		id: number;
		name: string;
		profile_url: string;
	};
}

export const useCodeDeploymentsRunsQuery = (
	siteId: number | null,
	deploymentId: number,
	options?: UseQueryOptions< DeploymentRun[] >
) => {
	return useQuery< DeploymentRun[] >( {
		enabled: !! siteId,
		queryKey: [
			GITHUB_DEPLOYMENTS_QUERY_KEY,
			CODE_DEPLOYMENTS_RUNS_QUERY_KEY,
			siteId,
			deploymentId,
		],
		queryFn: (): DeploymentRun[] =>
			wp.req.get( {
				path: `/sites/${ siteId }/hosting/code-deployments/${ deploymentId }/runs`,
				apiNamespace: 'wpcom/v2',
			} ),
		meta: {
			persist: false,
		},
		refetchInterval: 5000,
		...options,
	} );
};