File size: 1,216 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
import { UseQueryOptions, useQuery } from '@tanstack/react-query';
import { addQueryArgs } from '@wordpress/url';
import wp from 'calypso/lib/wp';
import { GITHUB_DEPLOYMENTS_QUERY_KEY } from '../../constants';

export const CODE_DEPLOYMENTS_QUERY_KEY = 'code-deployments';

type GetWorkflowContentsParams = {
	repositoryOwner: string;
	repositoryName: string;
	branchName: string;
	workflowFilename?: string;
};

export const useGetWorkflowContents = (
	{ repositoryOwner, repositoryName, branchName, workflowFilename }: GetWorkflowContentsParams,
	options?: Partial< UseQueryOptions< { content: string } > >
) => {
	return useQuery< { content: string } >( {
		queryKey: [
			GITHUB_DEPLOYMENTS_QUERY_KEY,
			CODE_DEPLOYMENTS_QUERY_KEY,
			repositoryOwner,
			repositoryName,
			branchName,
			workflowFilename,
		],
		queryFn: (): { content: string } => {
			const path = addQueryArgs( '/hosting/github/workflows/content', {
				repository_owner: repositoryOwner,
				repository_name: repositoryName,
				branch_name: branchName,
				workflow_filename: workflowFilename,
			} );

			return wp.req.get( {
				path,
				apiNamespace: 'wpcom/v2',
			} );
		},
		meta: {
			persist: false,
		},
		...options,
	} );
};