Spaces:
Sleeping

File size: 5,297 Bytes
4e83772
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
169
170
171
172
173
174
175
176
import { ENVS } from "@/config/env.config.js";
import AppError, { WebhookError } from "./AppError.js";
import { STATUS_CODES } from "./statusCodes.js";
import { generateTokenContainerAccessToken } from "./generateToken.js";
import {
	GITHUB_ACCEPT_HEADER,
	installationIdVerifyUrl as installationIdVerifyUrl,
	makeRepoBranchUr,
	makeRepoUr,
	newAccessTokensUrl,
	reposUrl,
} from "@/constants/gh.js";
import { WEBHOOK_ERRORS } from "@/constants/errors.js";
import { GithubRepoResponse, GithubRepositoryBranch, GithubRepositoryOwner } from "@/constants/types/github.js";

class DispatchRequests {
	async dispatchBuild(deploymentId: string, projectId: string, instalatnAcsTokn?: string): Promise<void> {
		const buildDispatchUrl = ENVS.BUILD_DISPATCH_URL || "";
		const buildDispatchEventType = "run-build";

		const token = generateTokenContainerAccessToken(projectId, deploymentId);
		const payload = {
			event_type: buildDispatchEventType,
			client_payload: {
				projectId: projectId,
				deploymentId: deploymentId,
				serviceToken: token,
				installationAccessToken: instalatnAcsTokn,
			},
		};
		const result = await fetch(buildDispatchUrl, {
			method: "POST",
			body: JSON.stringify(payload),
			headers: {
				Accept: GITHUB_ACCEPT_HEADER,
				"Content-Type": "application/json",
				Authorization: "Bearer " + ENVS.BUILD_DISPATCH_PAT_TOKEN,
			},
		});
		if (!result.ok) {
			const text = await result.text();
			throw new AppError(`Build dispatch failed: ${result.status} ${text}`, STATUS_CODES.INTERNAL_SERVER_ERROR);
		}
	}

	async githubInsatallationVerify(installationId: number, token: string): Promise<Record<string, any>> {
		const url = installationIdVerifyUrl + installationId;
		const data = await fetch(url, {
			method: "GET",
			headers: {
				Accept: GITHUB_ACCEPT_HEADER,
				Authorization: `Bearer ${token}`,
			},
		});

		if (!data.ok) {
			const result = (await data.json()) as unknown as {
				message: string;
				status: number;
			};
			const message = result.message;
			console.log(message);
			throw new WebhookError(message, STATUS_CODES.INTERNAL_SERVER_ERROR);
		}
		return await data.json();
	}

	async githubInstallationDelete(installationId: number, token: string): Promise<boolean> {
		const url = installationIdVerifyUrl + installationId;
		const data = await fetch(url, {
			method: "DELETE",
			headers: {
				Accept: GITHUB_ACCEPT_HEADER,
				Authorization: `Bearer ${token}`,
			},
		});
		if (!data.ok) {
			const result = (await data.json()) as unknown as {
				message: string;
				status: number;
			};
			const message = result.message;
			console.log(message);
			throw new WebhookError(message, STATUS_CODES.INTERNAL_SERVER_ERROR);
		}
		return true;
	}

	async getInstallationAccessToken(installationId: number, token: string): Promise<string> {
		const requestUrl = newAccessTokensUrl(installationId);

		const data = await fetch(requestUrl, {
			method: "POST",
			headers: {
				Accept: GITHUB_ACCEPT_HEADER,
				Authorization: `Bearer ${token}`,
			},
		});
		const result = (await data.json()) as unknown as {
			token: string;
			expires_at: string;
			permissions: {
				contents: string;
				metadata: string;
			};
			repository_selection: string;
		};
		if (!data.ok) {
			throw new WebhookError(WEBHOOK_ERRORS.ACCESS_TOKEN_CREATE_ERROR, STATUS_CODES.INTERNAL_SERVER_ERROR);
		}
		const accessToken = result.token;
		return accessToken;
	}

	async getUserRepos(token: string): Promise<GithubRepoResponse[]> {
		const requestUrl = reposUrl;

		const data = await fetch(requestUrl, {
			method: "GET",
			headers: {
				Accept: GITHUB_ACCEPT_HEADER,
				Authorization: `Bearer ${token}`,
			},
		});
		const result = (await data.json()) as unknown as {
			total_count: number;
			repositories: GithubRepoResponse[];
		};

		if (!data.ok) {
			throw new WebhookError(WEBHOOK_ERRORS.REPO_FETCH, STATUS_CODES.INTERNAL_SERVER_ERROR);
		}
		return result.repositories;
	}
	async getUserRepoBranches(token: string, owner: string, repo: string): Promise<GithubRepositoryBranch[]> {
		const requestUrl = makeRepoBranchUr(owner, repo);

		const data = await fetch(requestUrl, {
			method: "GET",
			headers: {
				Accept: GITHUB_ACCEPT_HEADER,
				Authorization: `Bearer ${token}`,
			},
		});
		const result = (await data.json()) as unknown as GithubRepositoryBranch[];

		if (!data.ok) {
			throw new WebhookError(WEBHOOK_ERRORS.BRANCH_FETCH, STATUS_CODES.INTERNAL_SERVER_ERROR);
		}
		return result;
	}
	async getUserRepo(token: string, owner: string, repo: string): Promise<GithubRepoResponse> {
		const requestUrl = makeRepoUr(owner, repo);

		const data = await fetch(requestUrl, {
			method: "GET",
			headers: {
				Accept: GITHUB_ACCEPT_HEADER,
				Authorization: `Bearer ${token}`,
			},
		});
		const result = (await data.json()) as unknown as GithubRepoResponse;

		if (!data.ok) {
			throw new WebhookError(WEBHOOK_ERRORS.BRANCH_FETCH, STATUS_CODES.INTERNAL_SERVER_ERROR);
		}
		return result;
	}
	async getUserAccountConnected(token: string, installationId: number): Promise<GithubRepositoryOwner> {
		const data = await this.githubInsatallationVerify(installationId, token);
		return data.account as GithubRepositoryOwner;
	}
}
const dispatchRequestService = new DispatchRequests();
export default dispatchRequestService;