| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | |
| 3 | import { GithubNotFoundError } |
| 4 | import { labelNamesOf, pageOf |
| 5 | import type { LabelDTO |
| 6 | import { IssuesService } |
| 7 | import type { GithubRepositoryTarget |
| 8 | import type { RepositoryNode |
| 9 | import type { GithubPageQueryDTO |
| 10 | import type { |
| 11 | GithubCreateLabelDTO, |
| 12 | GithubUpdateLabelDTO, |
| 13 | } from './dto/label.dto.js'; |
| 14 | import { GithubIssuesService } |
| 15 | |
| 16 | /** GitHub's REST labels, of a repository and of an issue, over IssuesService. */ |
| 17 | @Injectable() |
| 18 | export class GithubLabelsService |
| 19 | constructor( |
| 20 | private readonly github |
| 21 | private readonly issues |
| 22 | ) {} |
| 23 | |
| 24 | async list( |
| 25 | { owner, repo |
| 26 | query: GithubPageQueryDTO |
| 27 | ) { |
| 28 | const repository = |
| 29 | const { labels |
| 30 | this.github.ref |
| 31 | ); |
| 32 | return this.page |
| 33 | } |
| 34 | |
| 35 | async create( |
| 36 | { owner, repo |
| 37 | body: GithubCreateLabelDTO |
| 38 | ) { |
| 39 | const { repository, |
| 40 | owner, |
| 41 | repo, |
| 42 | viewer, |
| 43 | 'POST /repos/:owner/:repo/labels', |
| 44 | ); |
| 45 | const created = |
| 46 | ...this.github. |
| 47 | requesterId: signedIn.userId |
| 48 | body: { |
| 49 | name: body.name |
| 50 | color: body.color |
| 51 | description: body.description |
| 52 | }, |
| 53 | }); |
| 54 | const [label] |
| 55 | return label; |
| 56 | } |
| 57 | |
| 58 | async get({ |
| 59 | const repository = |
| 60 | const label = |
| 61 | const [rest] |
| 62 | return rest; |
| 63 | } |
| 64 | |
| 65 | async update( |
| 66 | { owner, repo |
| 67 | name: string, |
| 68 | body: GithubUpdateLabelDTO |
| 69 | ) { |
| 70 | const { repository, |
| 71 | owner, |
| 72 | repo, |
| 73 | viewer, |
| 74 | 'PATCH /repos/:owner/:repo/labels/:name', |
| 75 | ); |
| 76 | const label = |
| 77 | const updated = |
| 78 | ...this.github. |
| 79 | requesterId: signedIn.userId |
| 80 | labelId: label.id |
| 81 | body: { |
| 82 | name: body.new_name |
| 83 | color: body.color |
| 84 | description: body.description |
| 85 | }, |
| 86 | }); |
| 87 | const [rest] |
| 88 | return rest; |
| 89 | } |
| 90 | |
| 91 | async delete({ |
| 92 | const { repository, |
| 93 | owner, |
| 94 | repo, |
| 95 | viewer, |
| 96 | 'DELETE /repos/:owner/:repo/labels/:name', |
| 97 | ); |
| 98 | const label = |
| 99 | await this.issues |
| 100 | ...this.github. |
| 101 | requesterId: signedIn.userId |
| 102 | labelId: label.id |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | async listOnIssue( |
| 107 | { owner, repo |
| 108 | number: number, |
| 109 | query: GithubPageQueryDTO |
| 110 | ) { |
| 111 | const repository = |
| 112 | const issue = |
| 113 | ...this.github. |
| 114 | number, |
| 115 | }); |
| 116 | return this.page |
| 117 | } |
| 118 | |
| 119 | /** `body` is any shape GitHub accepts for a list of labels; answers all of the issue's labels. */ |
| 120 | addToIssue(target: |
| 121 | const names = |
| 122 | return this.change |
| 123 | target, |
| 124 | number, |
| 125 | 'POST /repos/:owner/:repo/issues/:number/labels', |
| 126 | names, |
| 127 | (current) => |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | setOnIssue(target: |
| 132 | const names = |
| 133 | return this.change |
| 134 | target, |
| 135 | number, |
| 136 | 'PUT /repos/:owner/:repo/issues/:number/labels', |
| 137 | names, |
| 138 | () => names, |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | async clearOnIssue(target |
| 143 | await this.change |
| 144 | target, |
| 145 | number, |
| 146 | 'DELETE /repos/:owner/:repo/issues/:number/labels', |
| 147 | [], |
| 148 | () => [], |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | /** Answers the labels left; 404 when the issue does not have the label. */ |
| 153 | removeFromIssue( |
| 154 | target: GithubRepositoryTarget |
| 155 | number: number, |
| 156 | name: string, |
| 157 | ) { |
| 158 | return this.change |
| 159 | target, |
| 160 | number, |
| 161 | 'DELETE /repos/:owner/:repo/issues/:number/labels/:name', |
| 162 | [], |
| 163 | (current) => |
| 164 | if (!current.includes |
| 165 | return current.filter |
| 166 | }, |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** Every label write is a read, then a full replacement through IssuesService, creating the labels it names that do not exist yet. */ |
| 171 | private async change |
| 172 | { owner, repo |
| 173 | number: number, |
| 174 | route: string, |
| 175 | added: string[] |
| 176 | next: (current |
| 177 | ) { |
| 178 | const { repository, |
| 179 | owner, |
| 180 | repo, |
| 181 | viewer, |
| 182 | route, |
| 183 | ); |
| 184 | const ref = |
| 185 | ...this.github. |
| 186 | number, |
| 187 | requesterId: signedIn.userId |
| 188 | }; |
| 189 | const issue = |
| 190 | if (!this.github |
| 191 | return this.github |
| 192 | await this.github |
| 193 | // ponytail: read then replace; two concurrent adds can lose one. Add IssuesService.addIssueLabels if that bites. |
| 194 | const { labels |
| 195 | ...ref, |
| 196 | names: next(issue |
| 197 | }); |
| 198 | return this.github |
| 199 | } |
| 200 | |
| 201 | // ponytail: labels are listed whole by IssuesService and sliced here; a repository with thousands of labels would want paging in the query. |
| 202 | private async page |
| 203 | repository: RepositoryNode |
| 204 | labels: LabelDTO |
| 205 | query: GithubPageQueryDTO |
| 206 | ) { |
| 207 | const page = |
| 208 | return new RestPage |
| 209 | await this.github |
| 210 | repository, |
| 211 | labels.slice(page |
| 212 | ), |
| 213 | page, |
| 214 | labels.length, |
| 215 | ); |
| 216 | } |
| 217 | } |