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