| 1 | "use server"; |
| 2 | |
| 3 | import { revalidatePath } from |
| 4 | import { redirect } |
| 5 | import { actionClient } |
| 6 | import { z } |
| 7 | |
| 8 | import { callApi } |
| 9 | |
| 10 | import { |
| 11 | createIssueSchema, |
| 12 | createLabelSchema, |
| 13 | updateLabelSchema, |
| 14 | } from "./common"; |
| 15 | |
| 16 | const target = z |
| 17 | username: z.string |
| 18 | repo: z.string |
| 19 | number: z.number |
| 20 | }); |
| 21 | |
| 22 | function issuePath(username: |
| 23 | return number === |
| 24 | ? `/${username} |
| 25 | : `/${username} |
| 26 | } |
| 27 | |
| 28 | export const createIssue = |
| 29 | .inputSchema( |
| 30 | createIssueSchema.extend({ |
| 31 | ) |
| 32 | .action( |
| 33 | async ({ |
| 34 | parsedInput: { username, |
| 35 | }) => { |
| 36 | const data = |
| 37 | client.POST("/api/repositories/{username}/{repo}/issues |
| 38 | params: { path: { |
| 39 | body: { title, |
| 40 | }), |
| 41 | ); |
| 42 | |
| 43 | redirect(`/${username |
| 44 | }, |
| 45 | ); |
| 46 | |
| 47 | export const updateIssue = |
| 48 | .inputSchema( |
| 49 | target.extend({ |
| 50 | title: z |
| 51 | .string() |
| 52 | .trim() |
| 53 | .min(1, " |
| 54 | .max(200, " |
| 55 | .optional(), |
| 56 | body: z |
| 57 | .string() |
| 58 | .max(20000, " |
| 59 | .nullable() |
| 60 | .optional(), |
| 61 | }), |
| 62 | ) |
| 63 | .action(async ( |
| 64 | await callApi((client |
| 65 | client.PATCH("/api/repositories/{username}/{repo}/issues/{number} |
| 66 | params: { path: { |
| 67 | body: { title, |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | revalidatePath(issuePath(username |
| 72 | }); |
| 73 | |
| 74 | export const closeIssue = |
| 75 | .inputSchema(target) |
| 76 | .action(async ( |
| 77 | await callApi((client |
| 78 | client.POST("/api/repositories/{username}/{repo}/issues/{number}/close |
| 79 | params: { path: { |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | revalidatePath(issuePath(username |
| 84 | revalidatePath(issuePath(username |
| 85 | }); |
| 86 | |
| 87 | export const reopenIssue = |
| 88 | .inputSchema(target) |
| 89 | .action(async ( |
| 90 | await callApi((client |
| 91 | client.POST( |
| 92 | "/api/repositories/{username}/{repo}/issues/{number}/reopen", |
| 93 | { |
| 94 | params: { path: { |
| 95 | }, |
| 96 | ), |
| 97 | ); |
| 98 | |
| 99 | revalidatePath(issuePath(username |
| 100 | revalidatePath(issuePath(username |
| 101 | }); |
| 102 | |
| 103 | export const commentOnIssue = |
| 104 | .inputSchema( |
| 105 | target.extend({ |
| 106 | body: z |
| 107 | .string() |
| 108 | .trim() |
| 109 | .min(1, " |
| 110 | .max(20000, " |
| 111 | }), |
| 112 | ) |
| 113 | .action(async ( |
| 114 | const data = |
| 115 | client.POST( |
| 116 | "/api/repositories/{username}/{repo}/issues/{number}/comments", |
| 117 | { |
| 118 | params: { path: { |
| 119 | body: { body |
| 120 | }, |
| 121 | ), |
| 122 | ); |
| 123 | |
| 124 | revalidatePath(issuePath(username |
| 125 | return data; |
| 126 | }); |
| 127 | |
| 128 | export const updateIssueComment = |
| 129 | .inputSchema( |
| 130 | target.extend({ |
| 131 | commentId: z.string |
| 132 | body: z |
| 133 | .string() |
| 134 | .trim() |
| 135 | .min(1, " |
| 136 | .max(20000, " |
| 137 | }), |
| 138 | ) |
| 139 | .action( |
| 140 | async ({ parsedInput |
| 141 | await callApi((client |
| 142 | client.PATCH( |
| 143 | "/api/repositories/{username}/{repo}/issues/{number}/comments/{commentId}", |
| 144 | { |
| 145 | params: { path: { |
| 146 | body: { body |
| 147 | }, |
| 148 | ), |
| 149 | ); |
| 150 | |
| 151 | revalidatePath(issuePath(username |
| 152 | }, |
| 153 | ); |
| 154 | |
| 155 | export const deleteIssueComment = |
| 156 | .inputSchema(target.extend |
| 157 | .action(async ( |
| 158 | await callApi((client |
| 159 | client.DELETE( |
| 160 | "/api/repositories/{username}/{repo}/issues/{number}/comments/{commentId}", |
| 161 | { |
| 162 | params: { path: { |
| 163 | }, |
| 164 | ), |
| 165 | ); |
| 166 | |
| 167 | revalidatePath(issuePath(username |
| 168 | }); |
| 169 | |
| 170 | export const setIssueLabels = |
| 171 | .inputSchema(target.extend |
| 172 | .action(async ( |
| 173 | const data = |
| 174 | client.PUT("/api/repositories/{username}/{repo}/issues/{number}/labels |
| 175 | params: { path: { |
| 176 | body: { names |
| 177 | }), |
| 178 | ); |
| 179 | |
| 180 | revalidatePath(issuePath(username |
| 181 | return data; |
| 182 | }); |
| 183 | |
| 184 | export const setIssueAssignees = |
| 185 | .inputSchema(target.extend |
| 186 | .action(async ( |
| 187 | const data = |
| 188 | client.PUT( |
| 189 | "/api/repositories/{username}/{repo}/issues/{number}/assignees", |
| 190 | { |
| 191 | params: { path: { |
| 192 | body: { usernames |
| 193 | }, |
| 194 | ), |
| 195 | ); |
| 196 | |
| 197 | revalidatePath(issuePath(username |
| 198 | return data; |
| 199 | }); |
| 200 | |
| 201 | export const createLabel = |
| 202 | .inputSchema( |
| 203 | createLabelSchema.extend({ |
| 204 | ) |
| 205 | .action( |
| 206 | async ({ parsedInput |
| 207 | const data = |
| 208 | client.POST("/api/repositories/{username}/{repo}/labels |
| 209 | params: { path: { |
| 210 | body: { name, |
| 211 | }), |
| 212 | ); |
| 213 | |
| 214 | revalidatePath(`/${username |
| 215 | // A label change can surface on any issue in the repository. |
| 216 | revalidatePath(issuePath(username |
| 217 | return data; |
| 218 | }, |
| 219 | ); |
| 220 | |
| 221 | export const updateLabel = |
| 222 | .inputSchema( |
| 223 | updateLabelSchema.extend({ |
| 224 | username: z.string |
| 225 | repo: z.string |
| 226 | labelId: z.string |
| 227 | }), |
| 228 | ) |
| 229 | .action( |
| 230 | async ({ |
| 231 | parsedInput: { username, |
| 232 | }) => { |
| 233 | const data = |
| 234 | client.PATCH("/api/repositories/{username}/{repo}/labels/{labelId} |
| 235 | params: { path: { |
| 236 | body: { name, |
| 237 | }), |
| 238 | ); |
| 239 | |
| 240 | revalidatePath(`/${username |
| 241 | // A label change can surface on any issue in the repository. |
| 242 | revalidatePath(issuePath(username |
| 243 | return data; |
| 244 | }, |
| 245 | ); |
| 246 | |
| 247 | export const deleteLabel = |
| 248 | .inputSchema( |
| 249 | z.object({ |
| 250 | ) |
| 251 | .action(async ( |
| 252 | await callApi((client |
| 253 | client.DELETE("/api/repositories/{username}/{repo}/labels/{labelId} |
| 254 | params: { path: { |
| 255 | }), |
| 256 | ); |
| 257 | |
| 258 | revalidatePath(`/${username |
| 259 | // A label change can surface on any issue in the repository. |
| 260 | revalidatePath(issuePath(username |
| 261 | }); |