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