-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment-utils.ts
More file actions
200 lines (183 loc) · 3.92 KB
/
deployment-utils.ts
File metadata and controls
200 lines (183 loc) · 3.92 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import type { GitHubContext, RepoInfo } from './types.js';
/**
* Deployment structure from GitHub API
*/
export type Deployment = {
id: number;
sha: string;
ref: string;
task: string;
payload: Record<string, unknown>;
environment: string;
description: string | null;
creator: {
login: string;
id: number;
} | null;
created_at: string;
updated_at: string;
statuses_url: string;
repository_url: string;
};
/**
* Deployment status structure
*/
export type DeploymentStatus = {
id: number;
state: 'error' | 'failure' | 'inactive' | 'pending' | 'success' | 'queued' | 'in_progress';
creator: {
login: string;
id: number;
} | null;
description: string | null;
environment: string;
target_url: string | null;
created_at: string;
updated_at: string;
deployment_url: string;
repository_url: string;
};
/**
* Lists deployments for a repository or specific ref
*/
export async function listDeployments({
ctx,
repo,
ref,
environment,
limit = 100,
}: {
ctx: GitHubContext;
repo: RepoInfo;
ref?: string;
environment?: string;
limit?: number;
}): Promise<Deployment[]> {
const deployments: Deployment[] = [];
let page = 1;
const perPage = Math.min(limit, 100);
while (deployments.length < limit) {
const { data } = await ctx.github.rest.repos.listDeployments({
...repo,
...(ref && { ref }),
...(environment && { environment }),
per_page: perPage,
page,
});
if (data.length === 0) {
break;
}
deployments.push(...(data as Deployment[]));
if (data.length < perPage) {
break;
}
page++;
}
return deployments.slice(0, limit);
}
/**
* Gets deployment statuses for a specific deployment
*/
export async function getDeploymentStatuses({
ctx,
repo,
deploymentId,
}: {
ctx: GitHubContext;
repo: RepoInfo;
deploymentId: number;
}): Promise<DeploymentStatus[]> {
const { data } = await ctx.github.rest.repos.listDeploymentStatuses({
...repo,
deployment_id: deploymentId,
});
return data as DeploymentStatus[];
}
/**
* Creates a deployment status
*/
export async function setDeploymentStatus({
ctx,
repo,
deploymentId,
state,
description,
targetUrl,
environment,
}: {
ctx: GitHubContext;
repo: RepoInfo;
deploymentId: number;
state: 'error' | 'failure' | 'inactive' | 'pending' | 'success' | 'queued' | 'in_progress';
description?: string;
targetUrl?: string;
environment?: string;
}): Promise<DeploymentStatus> {
const { data } = await ctx.github.rest.repos.createDeploymentStatus({
...repo,
deployment_id: deploymentId,
state,
...(description && { description }),
...(targetUrl && { target_url: targetUrl }),
...(environment && { environment }),
});
return data as DeploymentStatus;
}
/**
* Deletes a deployment
*/
export async function deleteDeployment({
ctx,
repo,
deploymentId,
}: {
ctx: GitHubContext;
repo: RepoInfo;
deploymentId: number;
}): Promise<void> {
// First set deployment to inactive
await setDeploymentStatus({
ctx,
repo,
deploymentId,
state: 'inactive',
});
// Then delete the deployment
await ctx.github.rest.repos.deleteDeployment({
...repo,
deployment_id: deploymentId,
});
}
/**
* Creates a new deployment
*/
export async function createDeployment({
ctx,
repo,
ref,
environment,
description,
payload,
autoMerge = true,
requiredContexts,
}: {
ctx: GitHubContext;
repo: RepoInfo;
ref: string;
environment: string;
description?: string;
payload?: Record<string, unknown>;
autoMerge?: boolean;
requiredContexts?: string[];
}): Promise<Deployment> {
const { data } = await ctx.github.rest.repos.createDeployment({
...repo,
ref,
environment,
auto_merge: autoMerge,
...(description && { description }),
...(payload && { payload }),
...(requiredContexts && { required_contexts: requiredContexts }),
});
return data as Deployment;
}