|
1 | 1 | import { Octokit } from "@octokit/core"; |
| 2 | +import { |
| 3 | + CreateOrUpdateUniqueIssueOptionsT, |
| 4 | + CreateOrUpdateUniqueIssueResponseT, |
| 5 | +} from "./types"; |
2 | 6 | import { VERSION } from "./version"; |
3 | | -import { Endpoints } from "@octokit/types"; |
| 7 | +import { composeUniqueIssue } from "./compose-unique-issue"; |
4 | 8 |
|
5 | | -const OCTOKIT_UNIQUE_ISSUE_ID_PREFIX = "octokit-unique-issue id="; |
6 | | - |
7 | | -type CreateOrUpdateUniqueIssueOptionsT = |
8 | | - Endpoints["POST /repos/{owner}/{repo}/issues"]["parameters"] & { |
9 | | - identifier: string; |
10 | | - close_previous?: boolean; |
11 | | - }; |
12 | | - |
13 | | -type CreatedIssueResponseT = |
14 | | - Endpoints["POST /repos/{owner}/{repo}/issues"]["response"] & { |
15 | | - updated: false; |
16 | | - closed_issues: Endpoints["GET /search/issues"]["response"]["data"]["items"]; |
17 | | - }; |
18 | | -type UpdatedIssueResponseT = |
19 | | - Endpoints["PATCH /repos/{owner}/{repo}/issues/{issue_number}"]["response"] & { |
20 | | - updated: true; |
21 | | - closed_issues: [...[]]; |
22 | | - }; |
23 | | -type CreateOrUpdateUniqueIssueResponseT = |
24 | | - | CreatedIssueResponseT |
25 | | - | UpdatedIssueResponseT; |
26 | 9 |
|
27 | 10 | export function uniqueIssue(octokit: Octokit) { |
28 | 11 | return { |
29 | 12 | async createOrUpdateUniqueIssue( |
30 | 13 | options: CreateOrUpdateUniqueIssueOptionsT, |
31 | 14 | ): Promise<CreateOrUpdateUniqueIssueResponseT> { |
32 | | - const { |
33 | | - owner, |
34 | | - repo, |
35 | | - identifier, |
36 | | - close_previous = false, |
37 | | - body, |
38 | | - ...rest |
39 | | - } = options; |
40 | | - |
41 | | - if (!identifier) { |
42 | | - throw Object.assign(new Error("identifier is required."), { |
43 | | - name: "MissingIdentifierError", |
44 | | - }); |
45 | | - } |
46 | | - |
47 | | - const term = `${OCTOKIT_UNIQUE_ISSUE_ID_PREFIX}"${identifier}"`; |
48 | | - |
49 | | - // MDAST compatible comment marker; see https://github.com/syntax-tree/mdast-comment-marker |
50 | | - const commentMarker = `<!-- ${term} -->`; |
51 | | - |
52 | | - const { |
53 | | - data: { items, total_count }, |
54 | | - } = await octokit.request("GET /search/issues", { |
55 | | - q: `"${term}" is:issue is:open repo:${owner}/${repo}`, |
56 | | - }); |
57 | | - |
58 | | - if (total_count === 1 && !close_previous) { |
59 | | - const response = await octokit.request( |
60 | | - "PATCH /repos/{owner}/{repo}/issues/{issue_number}", |
61 | | - { |
62 | | - owner, |
63 | | - repo, |
64 | | - issue_number: items[0].number, |
65 | | - ...(body !== undefined |
66 | | - ? { body: body + `\n\n${commentMarker}` } |
67 | | - : {}), |
68 | | - ...rest, |
69 | | - }, |
70 | | - ); |
71 | | - |
72 | | - return { |
73 | | - ...response, |
74 | | - updated: true, |
75 | | - closed_issues: [], |
76 | | - }; |
77 | | - } |
78 | | - |
79 | | - if (total_count > 1 && !close_previous) { |
80 | | - throw Object.assign( |
81 | | - new Error("More than 1 issue was found with identifier."), |
82 | | - { |
83 | | - identifier, |
84 | | - issue_numbers: items.map((item) => item.number).join(), |
85 | | - name: "DuplicateIssueError", |
86 | | - }, |
87 | | - ); |
88 | | - } |
89 | | - |
90 | | - for (const item of items) { |
91 | | - await octokit.request( |
92 | | - "PATCH /repos/{owner}/{repo}/issues/{issue_number}", |
93 | | - { |
94 | | - owner, |
95 | | - repo, |
96 | | - issue_number: item.number, |
97 | | - state: "closed", |
98 | | - }, |
99 | | - ); |
100 | | - } |
101 | | - |
102 | | - const response = await octokit.request( |
103 | | - "POST /repos/{owner}/{repo}/issues", |
104 | | - { |
105 | | - owner, |
106 | | - repo, |
107 | | - body: |
108 | | - body !== undefined ? body + `\n\n${commentMarker}` : commentMarker, |
109 | | - ...rest, |
110 | | - }, |
111 | | - ); |
112 | | - |
113 | | - return { |
114 | | - ...response, |
115 | | - updated: false, |
116 | | - closed_issues: items.map((item) => item), |
117 | | - }; |
| 15 | + return composeUniqueIssue(octokit, options); |
118 | 16 | }, |
119 | 17 | }; |
120 | 18 | } |
|
0 commit comments