| 1 | "use server"; |
| 2 | |
| 3 | import { revalidatePath } from |
| 4 | import { cookies } |
| 5 | import { redirect } |
| 6 | import { createSafeActionClient } |
| 7 | import { z } |
| 8 | |
| 9 | import { fetchClient } |
| 10 | |
| 11 | import { createPullRequestSchema, mergeMethods |
| 12 | |
| 13 | const actionClient = createSafeActionClient |
| 14 | handleServerError: (error) |
| 15 | }); |
| 16 | |
| 17 | const target = z |
| 18 | username: z.string |
| 19 | repo: z.string |
| 20 | number: z.number |
| 21 | }); |
| 22 | |
| 23 | export const createPullRequest = |
| 24 | .inputSchema( |
| 25 | createPullRequestSchema.extend({ |
| 26 | username: z.string |
| 27 | repo: z.string |
| 28 | draft: z.boolean |
| 29 | }), |
| 30 | ) |
| 31 | .action( |
| 32 | async ({ |
| 33 | parsedInput: { username, |
| 34 | }) => { |
| 35 | const { data, |
| 36 | "/api/repositories/{username}/{repo}/pulls", |
| 37 | { |
| 38 | params: { path: { |
| 39 | body: { title, |
| 40 | headers: { cookie: ( |
| 41 | }, |
| 42 | ); |
| 43 | |
| 44 | if (error) throw |
| 45 | |
| 46 | redirect(`/${username |
| 47 | }, |
| 48 | ); |
| 49 | |
| 50 | export const mergePullRequest = |
| 51 | .inputSchema( |
| 52 | target.extend({ |
| 53 | title: z.string |
| 54 | message: z.string |
| 55 | method: z.enum |
| 56 | }), |
| 57 | ) |
| 58 | .action( |
| 59 | async ({ |
| 60 | parsedInput: { username, |
| 61 | }) => { |
| 62 | const { data, |
| 63 | "/api/repositories/{username}/{repo}/pulls/{number}/merge", |
| 64 | { |
| 65 | params: { path: { |
| 66 | body: { title, |
| 67 | headers: { cookie: ( |
| 68 | }, |
| 69 | ); |
| 70 | |
| 71 | if (error) throw |
| 72 | |
| 73 | revalidatePath(`/${username |
| 74 | return data; |
| 75 | }, |
| 76 | ); |
| 77 | |
| 78 | export const closePullRequest = |
| 79 | .inputSchema(target) |
| 80 | .action(async ( |
| 81 | const { error |
| 82 | "/api/repositories/{username}/{repo}/pulls/{number}/close", |
| 83 | { |
| 84 | params: { path: { |
| 85 | headers: { cookie: ( |
| 86 | }, |
| 87 | ); |
| 88 | |
| 89 | if (error) throw |
| 90 | |
| 91 | revalidatePath(`/${username |
| 92 | }); |
| 93 | |
| 94 | /** Marks a draft ready for review, or turns a request back into a draft. */ |
| 95 | export const setDraft = |
| 96 | .inputSchema(target.extend |
| 97 | .action(async ( |
| 98 | const { error |
| 99 | draft |
| 100 | ? "/api/repositories/{username}/{repo}/pulls/{number}/draft" |
| 101 | : "/api/repositories/{username}/{repo}/pulls/{number}/ready", |
| 102 | { |
| 103 | params: { path: { |
| 104 | headers: { cookie: ( |
| 105 | }, |
| 106 | ); |
| 107 | |
| 108 | if (error) throw |
| 109 | |
| 110 | revalidatePath(`/${username |
| 111 | }); |
| 112 | |
| 113 | const lineComment = z |
| 114 | path: z.string |
| 115 | side: z.enum |
| 116 | line: z.number |
| 117 | startSide: z.enum |
| 118 | startLine: z.number |
| 119 | body: z |
| 120 | .string() |
| 121 | .trim() |
| 122 | .min(1, " |
| 123 | .max(20000, " |
| 124 | }); |
| 125 | |
| 126 | const commentBody = z |
| 127 | .string() |
| 128 | .trim() |
| 129 | .min(1, " |
| 130 | .max(20000, " |
| 131 | |
| 132 | /** Submits the viewer's pending review, if any, with the comments given here. */ |
| 133 | export const submitReview = |
| 134 | .inputSchema( |
| 135 | target.extend({ |
| 136 | state: z.enum |
| 137 | body: z |
| 138 | .string() |
| 139 | .max(20000, " |
| 140 | .optional(), |
| 141 | comments: z.array |
| 142 | }), |
| 143 | ) |
| 144 | .action( |
| 145 | async ({ |
| 146 | parsedInput: { username, |
| 147 | }) => { |
| 148 | const { error |
| 149 | "/api/repositories/{username}/{repo}/pulls/{number}/reviews", |
| 150 | { |
| 151 | params: { path: { |
| 152 | body: { state, |
| 153 | headers: { cookie: ( |
| 154 | }, |
| 155 | ); |
| 156 | |
| 157 | if (error) throw |
| 158 | |
| 159 | revalidatePath(`/${username |
| 160 | }, |
| 161 | ); |
| 162 | |
| 163 | export const addPendingComment = |
| 164 | .inputSchema(target.extend |
| 165 | .action(async ( |
| 166 | const { error |
| 167 | "/api/repositories/{username}/{repo}/pulls/{number}/reviews/pending/comments", |
| 168 | { |
| 169 | params: { path: { |
| 170 | body: comment, |
| 171 | headers: { cookie: ( |
| 172 | }, |
| 173 | ); |
| 174 | |
| 175 | if (error) throw |
| 176 | |
| 177 | revalidatePath(`/${username |
| 178 | }); |
| 179 | |
| 180 | export const discardPendingReview = |
| 181 | .inputSchema(target) |
| 182 | .action(async ( |
| 183 | const { error |
| 184 | "/api/repositories/{username}/{repo}/pulls/{number}/reviews/pending", |
| 185 | { |
| 186 | params: { path: { |
| 187 | headers: { cookie: ( |
| 188 | }, |
| 189 | ); |
| 190 | |
| 191 | if (error) throw |
| 192 | |
| 193 | revalidatePath(`/${username |
| 194 | }); |
| 195 | |
| 196 | export const dismissReview = |
| 197 | .inputSchema( |
| 198 | target.extend({ |
| 199 | reviewId: z.string |
| 200 | message: z |
| 201 | .string() |
| 202 | .trim() |
| 203 | .min(1, " |
| 204 | .max(1000, " |
| 205 | }), |
| 206 | ) |
| 207 | .action( |
| 208 | async ({ parsedInput |
| 209 | const { error |
| 210 | "/api/repositories/{username}/{repo}/pulls/{number}/reviews/{reviewId}/dismiss", |
| 211 | { |
| 212 | params: { path: { |
| 213 | body: { message |
| 214 | headers: { cookie: ( |
| 215 | }, |
| 216 | ); |
| 217 | |
| 218 | if (error) throw |
| 219 | |
| 220 | revalidatePath(`/${username |
| 221 | }, |
| 222 | ); |
| 223 | |
| 224 | export const applySuggestion = |
| 225 | .inputSchema(target.extend |
| 226 | .action(async ( |
| 227 | const { error |
| 228 | "/api/repositories/{username}/{repo}/pulls/{number}/comments/{commentId}/apply", |
| 229 | { |
| 230 | params: { path: { |
| 231 | headers: { cookie: ( |
| 232 | }, |
| 233 | ); |
| 234 | |
| 235 | if (error) throw |
| 236 | |
| 237 | revalidatePath(`/${username |
| 238 | }); |
| 239 | |
| 240 | export const replyToReviewComment = |
| 241 | .inputSchema(target.extend |
| 242 | .action( |
| 243 | async ({ parsedInput |
| 244 | const { error |
| 245 | "/api/repositories/{username}/{repo}/pulls/{number}/comments/{commentId}/replies", |
| 246 | { |
| 247 | params: { path: { |
| 248 | body: { body |
| 249 | headers: { cookie: ( |
| 250 | }, |
| 251 | ); |
| 252 | |
| 253 | if (error) throw |
| 254 | |
| 255 | revalidatePath(`/${username |
| 256 | }, |
| 257 | ); |
| 258 | |
| 259 | export const updateReviewComment = |
| 260 | .inputSchema(target.extend |
| 261 | .action( |
| 262 | async ({ parsedInput |
| 263 | const { error |
| 264 | "/api/repositories/{username}/{repo}/pulls/{number}/comments/{commentId}", |
| 265 | { |
| 266 | params: { path: { |
| 267 | body: { body |
| 268 | headers: { cookie: ( |
| 269 | }, |
| 270 | ); |
| 271 | |
| 272 | if (error) throw |
| 273 | |
| 274 | revalidatePath(`/${username |
| 275 | }, |
| 276 | ); |
| 277 | |
| 278 | export const deleteReviewComment = |
| 279 | .inputSchema(target.extend |
| 280 | .action(async ( |
| 281 | const { error |
| 282 | "/api/repositories/{username}/{repo}/pulls/{number}/comments/{commentId}", |
| 283 | { |
| 284 | params: { path: { |
| 285 | headers: { cookie: ( |
| 286 | }, |
| 287 | ); |
| 288 | |
| 289 | if (error) throw |
| 290 | |
| 291 | revalidatePath(`/${username |
| 292 | }); |
| 293 | |
| 294 | /** A review's summary is edited like a comment, so its id travels as `commentId`. */ |
| 295 | export const updateReviewSummary = |
| 296 | .inputSchema( |
| 297 | target.extend({ |
| 298 | commentId: z.string |
| 299 | body: z.string |
| 300 | }), |
| 301 | ) |
| 302 | .action( |
| 303 | async ({ parsedInput |
| 304 | const { error |
| 305 | "/api/repositories/{username}/{repo}/pulls/{number}/reviews/{reviewId}", |
| 306 | { |
| 307 | params: { path: { |
| 308 | body: { body: body |
| 309 | headers: { cookie: ( |
| 310 | }, |
| 311 | ); |
| 312 | |
| 313 | if (error) throw |
| 314 | |
| 315 | revalidatePath(`/${username |
| 316 | }, |
| 317 | ); |
| 318 | |
| 319 | export const updatePullRequest = |
| 320 | .inputSchema( |
| 321 | target.extend({ |
| 322 | title: z |
| 323 | .string() |
| 324 | .trim() |
| 325 | .min(1, " |
| 326 | .max(200, " |
| 327 | .optional(), |
| 328 | body: z |
| 329 | .string() |
| 330 | .max(20000, " |
| 331 | .nullable() |
| 332 | .optional(), |
| 333 | }), |
| 334 | ) |
| 335 | .action(async ( |
| 336 | const { error |
| 337 | "/api/repositories/{username}/{repo}/pulls/{number}", |
| 338 | { |
| 339 | params: { path: { |
| 340 | body: { title, |
| 341 | headers: { cookie: ( |
| 342 | }, |
| 343 | ); |
| 344 | |
| 345 | if (error) throw |
| 346 | |
| 347 | revalidatePath(`/${username |
| 348 | }); |