Coverage for src/governed_harness/forges/providers.py: 75%

258 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-12 16:06 +0000

1"""Forge providers behind one interface (#56): GitHub, GitLab, Bitbucket, Azure DevOps, Gitea. 

2 

3Every provider does the same four things through its REST API (a ``Transport``): 

4 

5* ``upsert_comment``: one comment per run on the pull or merge request, marked with 

6 ``<!-- governed-harness:<run id> -->`` so publishing again edits it; 

7* ``upload_reports``: the findings as a quality report where the forge has an API for it 

8 (GitHub code scanning SARIF, Bitbucket Code Insights); GitLab and Azure DevOps read their 

9 reports from CI artifacts, so the provider says so and the CI templates attach the file; 

10* ``create_pull_request``: a pull or merge request from a branch, with base, title, body, 

11 labels and draft; 

12* ``set_status``: a commit status (``success``, ``failure``, ``pending``) named 

13 ``governed-harness``. 

14 

15Nothing here decides: the comment, the status and the reports are information for the people 

16who review the change. Tokens never reach a message, a record or a log.""" 

17 

18from __future__ import annotations 

19 

20import base64 

21import gzip 

22import json 

23from dataclasses import dataclass 

24from typing import Any 

25from urllib.parse import quote 

26 

27from governed_harness.delivery.publisher import MAX_COMMENT_CHARS, PublishError, marker 

28from governed_harness.forges.transport import Transport 

29 

30STATUS_CONTEXT = "governed-harness" 

31StatusState = str # "success" | "failure" | "pending" 

32_MAX_PAGES = 10 

33 

34 

35def _dict(value: Any) -> dict[str, Any]: 

36 return value if isinstance(value, dict) else {} 

37 

38 

39def _cut(body: str, limit: int = MAX_COMMENT_CHARS) -> str: 

40 if len(body) <= limit: 40 ↛ 42line 40 didn't jump to line 42 because the condition on line 40 was always true

41 return body 

42 return body[: limit - 80] + "\n\n(cut: the brief is longer than a comment)\n" 

43 

44 

45@dataclass 

46class BaseForge: 

47 transport: Transport 

48 repository: str 

49 kind: str = "" 

50 

51 # ----- comments --------------------------------------------------------------------------- 

52 def _comments(self, number: int, page: int) -> list[dict[str, Any]]: 

53 raise NotImplementedError 

54 

55 def _comment_body(self, item: dict[str, Any]) -> str: 

56 return str(item.get("body", "")) 

57 

58 def _create_comment(self, number: int, body: str) -> dict[str, Any]: 

59 raise NotImplementedError 

60 

61 def _update_comment(self, number: int, item: dict[str, Any], body: str) -> dict[str, Any]: 

62 raise NotImplementedError 

63 

64 def upsert_comment(self, number: int, run_id: str, body: str) -> dict[str, Any]: 

65 body = _cut(body) 

66 existing: dict[str, Any] | None = None 

67 for page in range(1, _MAX_PAGES + 1): 67 ↛ 76line 67 didn't jump to line 76 because the loop on line 67 didn't complete

68 items = self._comments(number, page) 

69 if not items: 

70 break 

71 existing = next( 

72 (item for item in items if marker(run_id) in self._comment_body(item)), None 

73 ) 

74 if existing is not None: 74 ↛ 67line 74 didn't jump to line 67 because the condition on line 74 was always true

75 break 

76 if existing is not None: 

77 result = self._update_comment(number, existing, body) 

78 action = "updated" 

79 else: 

80 result = self._create_comment(number, body) 

81 action = "created" 

82 return {"action": action, **result} 

83 

84 # ----- reports, pull requests, statuses ---------------------------------------------------- 

85 # The parameters are those of the forge interface; a forge without the API ignores them. 

86 def upload_reports( 

87 self, 

88 number: int, # NOSONAR(S1172) BaseForge interface 

89 *, 

90 sarif: bytes | None, # NOSONAR(S1172) BaseForge interface 

91 code_quality: bytes | None, # NOSONAR(S1172) BaseForge interface 

92 commit_sha: str | None, # NOSONAR(S1172) BaseForge interface 

93 ) -> dict[str, Any]: 

94 return {"status": "SKIPPED", "reason": f"{self.kind} has no report upload API"} 

95 

96 def create_pull_request( 

97 self, 

98 *, 

99 head: str, 

100 base: str, 

101 title: str, 

102 body: str, 

103 labels: tuple[str, ...] = (), 

104 draft: bool = False, 

105 ) -> dict[str, Any]: 

106 raise NotImplementedError 

107 

108 def set_status( 

109 self, 

110 *, 

111 commit_sha: str, 

112 state: StatusState, 

113 description: str, 

114 target_url: str | None = None, 

115 ) -> dict[str, Any]: 

116 raise NotImplementedError 

117 

118 

119# ----- GitHub ---------------------------------------------------------------------------------- 

120@dataclass 

121class GitHubForge(BaseForge): 

122 kind: str = "github" 

123 

124 def _comments(self, number: int, page: int) -> list[dict[str, Any]]: 

125 value = self.transport.request( 

126 "GET", f"repos/{self.repository}/issues/{number}/comments?per_page=100&page={page}" 

127 ) 

128 return [item for item in value if isinstance(item, dict)] if isinstance(value, list) else [] 

129 

130 def _create_comment(self, number: int, body: str) -> dict[str, Any]: 

131 result = _dict( 

132 self.transport.request( 

133 "POST", f"repos/{self.repository}/issues/{number}/comments", {"body": body} 

134 ) 

135 ) 

136 return {"id": result.get("id"), "url": result.get("html_url")} 

137 

138 def _update_comment(self, number: int, item: dict[str, Any], body: str) -> dict[str, Any]: 

139 result = _dict( 

140 self.transport.request( 

141 "PATCH", f"repos/{self.repository}/issues/comments/{item['id']}", {"body": body} 

142 ) 

143 ) 

144 return {"id": result.get("id"), "url": result.get("html_url")} 

145 

146 def upload_reports( 

147 self, 

148 number: int, 

149 *, 

150 sarif: bytes | None, 

151 code_quality: bytes | None, 

152 commit_sha: str | None, 

153 ) -> dict[str, Any]: 

154 if sarif is None: 

155 return {"status": "SKIPPED", "reason": "SARIF upload disabled"} 

156 if commit_sha is None: 

157 pull = _dict(self.transport.request("GET", f"repos/{self.repository}/pulls/{number}")) 

158 commit_sha = _dict(pull.get("head")).get("sha") 

159 if not commit_sha: 

160 return {"status": "SKIPPED", "reason": "the head commit of the pull request is unknown"} 

161 try: 

162 result = _dict( 

163 self.transport.request( 

164 "POST", 

165 f"repos/{self.repository}/code-scanning/sarifs", 

166 { 

167 "commit_sha": commit_sha, 

168 "ref": f"refs/pull/{number}/head", 

169 "sarif": base64.b64encode(gzip.compress(sarif, mtime=0)).decode("ascii"), 

170 "tool_name": "governed-harness", 

171 }, 

172 ) 

173 ) 

174 except PublishError as error: 

175 return {"status": "FAILED", "reason": str(error), "commitSha": commit_sha} 

176 return {"status": "UPLOADED", "id": result.get("id"), "commitSha": commit_sha} 

177 

178 def create_pull_request( 

179 self, 

180 *, 

181 head: str, 

182 base: str, 

183 title: str, 

184 body: str, 

185 labels: tuple[str, ...] = (), 

186 draft: bool = False, 

187 ) -> dict[str, Any]: 

188 result = _dict( 

189 self.transport.request( 

190 "POST", 

191 f"repos/{self.repository}/pulls", 

192 {"title": title, "head": head, "base": base, "body": body, "draft": draft}, 

193 ) 

194 ) 

195 number = result.get("number") 

196 if labels and number is not None: 196 ↛ 200line 196 didn't jump to line 200 because the condition on line 196 was always true

197 self.transport.request( 

198 "POST", f"repos/{self.repository}/issues/{number}/labels", {"labels": list(labels)} 

199 ) 

200 return {"number": number, "url": result.get("html_url")} 

201 

202 def set_status( 

203 self, 

204 *, 

205 commit_sha: str, 

206 state: StatusState, 

207 description: str, 

208 target_url: str | None = None, 

209 ) -> dict[str, Any]: 

210 body: dict[str, Any] = { 

211 "state": state, 

212 "context": STATUS_CONTEXT, 

213 "description": description[:140], 

214 } 

215 if target_url: 

216 body["target_url"] = target_url 

217 result = _dict( 

218 self.transport.request("POST", f"repos/{self.repository}/statuses/{commit_sha}", body) 

219 ) 

220 return {"id": result.get("id"), "state": state} 

221 

222 

223# ----- GitLab ---------------------------------------------------------------------------------- 

224_GITLAB_STATE = {"success": "success", "failure": "failed", "pending": "pending"} 

225 

226 

227@dataclass 

228class GitLabForge(BaseForge): 

229 kind: str = "gitlab" 

230 

231 @property 

232 def _project(self) -> str: 

233 return f"projects/{quote(self.repository, safe='')}" 

234 

235 def _comments(self, number: int, page: int) -> list[dict[str, Any]]: 

236 value = self.transport.request( 

237 "GET", f"{self._project}/merge_requests/{number}/notes?per_page=100&page={page}" 

238 ) 

239 return [item for item in value if isinstance(item, dict)] if isinstance(value, list) else [] 

240 

241 def _create_comment(self, number: int, body: str) -> dict[str, Any]: 

242 result = _dict( 

243 self.transport.request( 

244 "POST", f"{self._project}/merge_requests/{number}/notes", {"body": body} 

245 ) 

246 ) 

247 return {"id": result.get("id")} 

248 

249 def _update_comment(self, number: int, item: dict[str, Any], body: str) -> dict[str, Any]: 

250 result = _dict( 

251 self.transport.request( 

252 "PUT", f"{self._project}/merge_requests/{number}/notes/{item['id']}", {"body": body} 

253 ) 

254 ) 

255 return {"id": result.get("id")} 

256 

257 def upload_reports( 

258 self, 

259 number: int, 

260 *, 

261 sarif: bytes | None, 

262 code_quality: bytes | None, 

263 commit_sha: str | None, 

264 ) -> dict[str, Any]: 

265 return { 

266 "status": "SKIPPED", 

267 "reason": "GitLab reads Code Quality and SAST reports from CI artifacts: attach " 

268 "gl-code-quality-report.json (harness trace --format codequality) as " 

269 "artifacts:reports:codequality", 

270 } 

271 

272 def create_pull_request( 

273 self, 

274 *, 

275 head: str, 

276 base: str, 

277 title: str, 

278 body: str, 

279 labels: tuple[str, ...] = (), 

280 draft: bool = False, 

281 ) -> dict[str, Any]: 

282 payload: dict[str, Any] = { 

283 "source_branch": head, 

284 "target_branch": base, 

285 "title": f"Draft: {title}" if draft else title, 

286 "description": body, 

287 } 

288 if labels: 288 ↛ 290line 288 didn't jump to line 290 because the condition on line 288 was always true

289 payload["labels"] = ",".join(labels) 

290 result = _dict(self.transport.request("POST", f"{self._project}/merge_requests", payload)) 

291 return {"number": result.get("iid"), "url": result.get("web_url")} 

292 

293 def set_status( 

294 self, 

295 *, 

296 commit_sha: str, 

297 state: StatusState, 

298 description: str, 

299 target_url: str | None = None, 

300 ) -> dict[str, Any]: 

301 body: dict[str, Any] = { 

302 "state": _GITLAB_STATE[state], 

303 "name": STATUS_CONTEXT, 

304 "description": description[:255], 

305 } 

306 if target_url: 306 ↛ 307line 306 didn't jump to line 307 because the condition on line 306 was never true

307 body["target_url"] = target_url 

308 result = _dict( 

309 self.transport.request("POST", f"{self._project}/statuses/{commit_sha}", body) 

310 ) 

311 return {"id": result.get("id"), "state": body["state"]} 

312 

313 

314# ----- Bitbucket Cloud ------------------------------------------------------------------------- 

315_BITBUCKET_STATE = {"success": "SUCCESSFUL", "failure": "FAILED", "pending": "INPROGRESS"} 

316_BITBUCKET_SEVERITY = { 

317 "info": "LOW", 

318 "minor": "LOW", 

319 "major": "MEDIUM", 

320 "critical": "HIGH", 

321 "blocker": "CRITICAL", 

322} 

323_REPORT_ID = "governed-harness" 

324 

325 

326@dataclass 

327class BitbucketForge(BaseForge): 

328 kind: str = "bitbucket" 

329 

330 @property 

331 def _repo(self) -> str: 

332 return f"repositories/{self.repository}" 

333 

334 def _comments(self, number: int, page: int) -> list[dict[str, Any]]: 

335 value = _dict( 

336 self.transport.request( 

337 "GET", f"{self._repo}/pullrequests/{number}/comments?pagelen=100&page={page}" 

338 ) 

339 ) 

340 items = value.get("values") 

341 return [item for item in items if isinstance(item, dict)] if isinstance(items, list) else [] 

342 

343 def _comment_body(self, item: dict[str, Any]) -> str: 

344 return str(_dict(item.get("content")).get("raw", "")) 

345 

346 def _create_comment(self, number: int, body: str) -> dict[str, Any]: 

347 result = _dict( 

348 self.transport.request( 

349 "POST", f"{self._repo}/pullrequests/{number}/comments", {"content": {"raw": body}} 

350 ) 

351 ) 

352 return {"id": result.get("id")} 

353 

354 def _update_comment(self, number: int, item: dict[str, Any], body: str) -> dict[str, Any]: 

355 result = _dict( 

356 self.transport.request( 

357 "PUT", 

358 f"{self._repo}/pullrequests/{number}/comments/{item['id']}", 

359 {"content": {"raw": body}}, 

360 ) 

361 ) 

362 return {"id": result.get("id")} 

363 

364 def upload_reports( 

365 self, 

366 number: int, 

367 *, 

368 sarif: bytes | None, 

369 code_quality: bytes | None, 

370 commit_sha: str | None, 

371 ) -> dict[str, Any]: 

372 """A Code Insights report on the commit with one annotation per finding.""" 

373 if code_quality is None: 373 ↛ 374line 373 didn't jump to line 374 because the condition on line 373 was never true

374 return {"status": "SKIPPED", "reason": "the quality report is disabled"} 

375 if commit_sha is None: 375 ↛ 376line 375 didn't jump to line 376 because the condition on line 375 was never true

376 pull = _dict(self.transport.request("GET", f"{self._repo}/pullrequests/{number}")) 

377 commit_sha = _dict(_dict(pull.get("source")).get("commit")).get("hash") 

378 if not commit_sha: 378 ↛ 379line 378 didn't jump to line 379 because the condition on line 378 was never true

379 return {"status": "SKIPPED", "reason": "the head commit of the pull request is unknown"} 

380 issues = json.loads(code_quality.decode("utf-8")) 

381 blocking = any(item.get("severity") in {"critical", "blocker"} for item in issues) 

382 base = f"{self._repo}/commit/{commit_sha}/reports/{_REPORT_ID}" 

383 try: 

384 self.transport.request( 

385 "PUT", 

386 base, 

387 { 

388 "title": "Governed Agent Harness", 

389 "details": f"{len(issues)} finding(s) of the governed run", 

390 "report_type": "BUG", 

391 "reporter": "governed-harness", 

392 "result": "FAILED" if blocking else "PASSED", 

393 }, 

394 ) 

395 annotations = [ 

396 { 

397 "external_id": str(item.get("fingerprint", index))[:64], 

398 "annotation_type": "BUG", 

399 "summary": str(item.get("description", ""))[:450], 

400 "severity": _BITBUCKET_SEVERITY.get(str(item.get("severity")), "LOW"), 

401 "path": _dict(item.get("location")).get("path"), 

402 "line": _dict(_dict(item.get("location")).get("lines")).get("begin"), 

403 } 

404 for index, item in enumerate(issues[:100]) 

405 ] 

406 if annotations: 406 ↛ 410line 406 didn't jump to line 410 because the condition on line 406 was always true

407 self.transport.request("POST", f"{base}/annotations", annotations) 

408 except PublishError as error: 

409 return {"status": "FAILED", "reason": str(error), "commitSha": commit_sha} 

410 return {"status": "UPLOADED", "annotations": min(len(issues), 100), "commitSha": commit_sha} 

411 

412 def create_pull_request( 

413 self, 

414 *, 

415 head: str, 

416 base: str, 

417 title: str, 

418 body: str, 

419 labels: tuple[str, ...] = (), 

420 draft: bool = False, 

421 ) -> dict[str, Any]: 

422 payload: dict[str, Any] = { 

423 "title": title, 

424 "description": body, 

425 "source": {"branch": {"name": head}}, 

426 "destination": {"branch": {"name": base}}, 

427 } 

428 if draft: 428 ↛ 429line 428 didn't jump to line 429 because the condition on line 428 was never true

429 payload["draft"] = True 

430 result = _dict(self.transport.request("POST", f"{self._repo}/pullrequests", payload)) 

431 report: dict[str, Any] = { 

432 "number": result.get("id"), 

433 "url": _dict(_dict(result.get("links")).get("html")).get("href"), 

434 } 

435 if labels: 435 ↛ 437line 435 didn't jump to line 437 because the condition on line 435 was always true

436 report["labelsIgnored"] = list(labels) # Bitbucket pull requests have no labels 

437 return report 

438 

439 def set_status( 

440 self, 

441 *, 

442 commit_sha: str, 

443 state: StatusState, 

444 description: str, 

445 target_url: str | None = None, 

446 ) -> dict[str, Any]: 

447 body = { 

448 "state": _BITBUCKET_STATE[state], 

449 "key": STATUS_CONTEXT, 

450 "name": "Governed Agent Harness", 

451 "description": description[:255], 

452 "url": target_url or "https://localhost.invalid/governed-harness", 

453 } 

454 result = _dict( 

455 self.transport.request("POST", f"{self._repo}/commit/{commit_sha}/statuses/build", body) 

456 ) 

457 return {"key": result.get("key", STATUS_CONTEXT), "state": body["state"]} 

458 

459 

460# ----- Azure DevOps ---------------------------------------------------------------------------- 

461_AZURE_STATE = {"success": "succeeded", "failure": "failed", "pending": "pending"} 

462_AZURE_VERSION = "api-version=7.1" 

463 

464 

465@dataclass 

466class AzureDevOpsForge(BaseForge): 

467 """``repository`` is ``organization/project/repository``; the transport's base URL is the 

468 organization (``https://dev.azure.com/<organization>``).""" 

469 

470 kind: str = "azure-devops" 

471 

472 @property 

473 def _repo(self) -> str: 

474 _organization, project, repository = self.repository.split("/", 2) 

475 return f"{quote(project)}/_apis/git/repositories/{quote(repository)}" 

476 

477 def _comments(self, number: int, page: int) -> list[dict[str, Any]]: 

478 if page > 1: 478 ↛ 479line 478 didn't jump to line 479 because the condition on line 478 was never true

479 return [] # threads are returned in one response 

480 value = _dict( 

481 self.transport.request( 

482 "GET", f"{self._repo}/pullRequests/{number}/threads?{_AZURE_VERSION}" 

483 ) 

484 ) 

485 threads = value.get("value") 

486 items: list[dict[str, Any]] = [] 

487 for thread in threads if isinstance(threads, list) else []: 

488 comments = _dict(thread).get("comments") 

489 first = comments[0] if isinstance(comments, list) and comments else None 

490 if isinstance(first, dict): 490 ↛ 487line 490 didn't jump to line 487 because the condition on line 490 was always true

491 items.append({"threadId": thread.get("id"), **first}) 

492 return items 

493 

494 def _comment_body(self, item: dict[str, Any]) -> str: 

495 return str(item.get("content", "")) 

496 

497 def _create_comment(self, number: int, body: str) -> dict[str, Any]: 

498 result = _dict( 

499 self.transport.request( 

500 "POST", 

501 f"{self._repo}/pullRequests/{number}/threads?{_AZURE_VERSION}", 

502 { 

503 "comments": [{"parentCommentId": 0, "content": body, "commentType": 1}], 

504 "status": 1, 

505 }, 

506 ) 

507 ) 

508 return {"id": result.get("id")} 

509 

510 def _update_comment(self, number: int, item: dict[str, Any], body: str) -> dict[str, Any]: 

511 result = _dict( 

512 self.transport.request( 

513 "PATCH", 

514 f"{self._repo}/pullRequests/{number}/threads/{item['threadId']}/comments/" 

515 f"{item['id']}?{_AZURE_VERSION}", 

516 {"content": body}, 

517 ) 

518 ) 

519 return {"id": item.get("threadId"), "commentId": result.get("id")} 

520 

521 def upload_reports( 

522 self, 

523 number: int, 

524 *, 

525 sarif: bytes | None, 

526 code_quality: bytes | None, 

527 commit_sha: str | None, 

528 ) -> dict[str, Any]: 

529 return { 

530 "status": "SKIPPED", 

531 "reason": "Azure DevOps reads SARIF from the pipeline: publish harness.sarif as the " 

532 "CodeAnalysisLogs build artifact", 

533 } 

534 

535 def create_pull_request( 

536 self, 

537 *, 

538 head: str, 

539 base: str, 

540 title: str, 

541 body: str, 

542 labels: tuple[str, ...] = (), 

543 draft: bool = False, 

544 ) -> dict[str, Any]: 

545 payload: dict[str, Any] = { 

546 "sourceRefName": f"refs/heads/{head}", 

547 "targetRefName": f"refs/heads/{base}", 

548 "title": title, 

549 "description": body[:4000], 

550 "isDraft": draft, 

551 } 

552 if labels: 552 ↛ 554line 552 didn't jump to line 554 because the condition on line 552 was always true

553 payload["labels"] = [{"name": item} for item in labels] 

554 result = _dict( 

555 self.transport.request("POST", f"{self._repo}/pullrequests?{_AZURE_VERSION}", payload) 

556 ) 

557 return {"number": result.get("pullRequestId"), "url": result.get("url")} 

558 

559 def set_status( 

560 self, 

561 *, 

562 commit_sha: str, 

563 state: StatusState, 

564 description: str, 

565 target_url: str | None = None, 

566 ) -> dict[str, Any]: 

567 body: dict[str, Any] = { 

568 "state": _AZURE_STATE[state], 

569 "description": description[:255], 

570 "context": {"name": STATUS_CONTEXT, "genre": "governance"}, 

571 } 

572 if target_url: 572 ↛ 573line 572 didn't jump to line 573 because the condition on line 572 was never true

573 body["targetUrl"] = target_url 

574 result = _dict( 

575 self.transport.request( 

576 "POST", f"{self._repo}/commits/{commit_sha}/statuses?{_AZURE_VERSION}", body 

577 ) 

578 ) 

579 return {"id": result.get("id"), "state": body["state"]} 

580 

581 

582# ----- Gitea and Forgejo ----------------------------------------------------------------------- 

583_GITEA_STATE = {"success": "success", "failure": "failure", "pending": "pending"} 

584 

585 

586@dataclass 

587class GiteaForge(BaseForge): 

588 kind: str = "gitea" 

589 

590 def _comments(self, number: int, page: int) -> list[dict[str, Any]]: 

591 value = self.transport.request( 

592 "GET", f"repos/{self.repository}/issues/{number}/comments?limit=50&page={page}" 

593 ) 

594 return [item for item in value if isinstance(item, dict)] if isinstance(value, list) else [] 

595 

596 def _create_comment(self, number: int, body: str) -> dict[str, Any]: 

597 result = _dict( 

598 self.transport.request( 

599 "POST", f"repos/{self.repository}/issues/{number}/comments", {"body": body} 

600 ) 

601 ) 

602 return {"id": result.get("id"), "url": result.get("html_url")} 

603 

604 def _update_comment(self, number: int, item: dict[str, Any], body: str) -> dict[str, Any]: 

605 result = _dict( 

606 self.transport.request( 

607 "PATCH", f"repos/{self.repository}/issues/comments/{item['id']}", {"body": body} 

608 ) 

609 ) 

610 return {"id": result.get("id"), "url": result.get("html_url")} 

611 

612 def _label_ids(self, labels: tuple[str, ...]) -> tuple[list[int], list[str]]: 

613 value = self.transport.request("GET", f"repos/{self.repository}/labels?limit=50") 

614 known = { 

615 str(item.get("name")): int(item["id"]) 

616 for item in (value if isinstance(value, list) else []) 

617 if isinstance(item, dict) and isinstance(item.get("id"), int) 

618 } 

619 return [known[name] for name in labels if name in known], [ 

620 name for name in labels if name not in known 

621 ] 

622 

623 def create_pull_request( 

624 self, 

625 *, 

626 head: str, 

627 base: str, 

628 title: str, 

629 body: str, 

630 labels: tuple[str, ...] = (), 

631 draft: bool = False, 

632 ) -> dict[str, Any]: 

633 payload: dict[str, Any] = { 

634 "head": head, 

635 "base": base, 

636 "title": f"WIP: {title}" if draft else title, 

637 "body": body, 

638 } 

639 unknown: list[str] = [] 

640 if labels: 640 ↛ 644line 640 didn't jump to line 644 because the condition on line 640 was always true

641 ids, unknown = self._label_ids(labels) 

642 if ids: 642 ↛ 644line 642 didn't jump to line 644 because the condition on line 642 was always true

643 payload["labels"] = ids 

644 result = _dict(self.transport.request("POST", f"repos/{self.repository}/pulls", payload)) 

645 report: dict[str, Any] = {"number": result.get("number"), "url": result.get("html_url")} 

646 if unknown: 646 ↛ 648line 646 didn't jump to line 648 because the condition on line 646 was always true

647 report["labelsUnknown"] = unknown 

648 return report 

649 

650 def set_status( 

651 self, 

652 *, 

653 commit_sha: str, 

654 state: StatusState, 

655 description: str, 

656 target_url: str | None = None, 

657 ) -> dict[str, Any]: 

658 body: dict[str, Any] = { 

659 "state": _GITEA_STATE[state], 

660 "context": STATUS_CONTEXT, 

661 "description": description[:255], 

662 } 

663 if target_url: 

664 body["target_url"] = target_url 

665 result = _dict( 

666 self.transport.request("POST", f"repos/{self.repository}/statuses/{commit_sha}", body) 

667 ) 

668 return {"id": result.get("id"), "state": body["state"]} 

669 

670 

671FORGES: dict[str, type[BaseForge]] = { 

672 "github": GitHubForge, 

673 "gitlab": GitLabForge, 

674 "bitbucket": BitbucketForge, 

675 "azure-devops": AzureDevOpsForge, 

676 "gitea": GiteaForge, 

677} 

678 

679 

680def forge_for(kind: str, transport: Transport, repository: str) -> BaseForge: 

681 try: 

682 return FORGES[kind](transport=transport, repository=repository) 

683 except KeyError as error: 

684 raise PublishError(f"unknown forge: {kind}") from error 

685 

686 

687__all__ = [ 

688 "FORGES", 

689 "STATUS_CONTEXT", 

690 "AzureDevOpsForge", 

691 "BaseForge", 

692 "BitbucketForge", 

693 "GitHubForge", 

694 "GitLabForge", 

695 "GiteaForge", 

696 "forge_for", 

697]