|
| 1 | +import { WithPartial } from "@lib/utils/typeUtils/WithPartial"; |
| 2 | +import { Integration } from "./core"; |
| 3 | +import { |
| 4 | + EIntegrationParseError, |
| 5 | + EIntegrationParseErrorData, |
| 6 | + EIntegrationTargetError, |
| 7 | +} from "./error"; |
| 8 | +import { EErrorOptions } from "@lib/utils/error"; |
| 9 | +import _ from "lodash"; |
| 10 | + |
| 11 | +type EGitLabParseErrorData = EIntegrationParseErrorData & { |
| 12 | + integration: "GitLab"; |
| 13 | +}; |
| 14 | +export class EGitLabParseError extends EIntegrationParseError { |
| 15 | + constructor({ |
| 16 | + message = "Unable to extract repository or branch from URL", |
| 17 | + data, |
| 18 | + }: WithPartial<EErrorOptions<EGitLabParseErrorData>, "message">) { |
| 19 | + super({ message, data }); |
| 20 | + this.name = "EGitLabParseErrorData"; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function isPR(url: string | URL) { |
| 25 | + return /[/][^/]+[/][^/]+[/]-[/]merge_requests[/].+/.test(url.toString()); |
| 26 | +} |
| 27 | + |
| 28 | +export const GitLab: Integration = { |
| 29 | + platform: "GitLab", |
| 30 | + supports(url: string | URL) { |
| 31 | + return /^https?:[/][/]gitlab.com[/][^/]+[/][^/]+/.test(url.toString()); |
| 32 | + }, |
| 33 | + getButtonTarget(document: Document) { |
| 34 | + const node = |
| 35 | + document.querySelector<HTMLElement>(".project-code-holder") |
| 36 | + ?.parentElement ?? |
| 37 | + document.querySelector<HTMLElement>(".tree-controls > div") ?? |
| 38 | + document.querySelector<HTMLElement>(".detail-page-header-actions"); |
| 39 | + if (!node) { |
| 40 | + throw new EIntegrationTargetError({ |
| 41 | + message: "Unable to find button target", |
| 42 | + data: { |
| 43 | + integration: "GitLab", |
| 44 | + url: window.location.href, |
| 45 | + }, |
| 46 | + }); |
| 47 | + } |
| 48 | + return node; |
| 49 | + }, |
| 50 | + getRepo({ url }) { |
| 51 | + const results = |
| 52 | + /[/](?<repo>[^/]+[/][^/]+)([/]?tree[/](?<branch>[^?]+))?/.exec( |
| 53 | + url.toString(), |
| 54 | + )?.groups; |
| 55 | + if (!results) { |
| 56 | + throw new EGitLabParseError({ |
| 57 | + data: { |
| 58 | + url: url.toString(), |
| 59 | + integration: "GitLab", |
| 60 | + cause: "no match", |
| 61 | + process: "repo", |
| 62 | + }, |
| 63 | + }); |
| 64 | + } |
| 65 | + const { repo } = results; |
| 66 | + if (_.isEmpty(repo)) { |
| 67 | + throw new EGitLabParseError({ |
| 68 | + data: { |
| 69 | + url: url.toString(), |
| 70 | + integration: "GitLab", |
| 71 | + cause: "empty match", |
| 72 | + process: "repo", |
| 73 | + }, |
| 74 | + }); |
| 75 | + } |
| 76 | + return repo; |
| 77 | + }, |
| 78 | + getBranch({ url, document }) { |
| 79 | + if (isPR(url)) { |
| 80 | + const branchContainer = document.querySelector(".ref-container"); |
| 81 | + if (!branchContainer) { |
| 82 | + throw new EGitLabParseError({ |
| 83 | + data: { |
| 84 | + url: url.toString(), |
| 85 | + integration: "GitLab", |
| 86 | + cause: "no match", |
| 87 | + process: "branch", |
| 88 | + }, |
| 89 | + }); |
| 90 | + } |
| 91 | + const branch = branchContainer.textContent; |
| 92 | + if (!branch || _.isEmpty(branch)) { |
| 93 | + throw new EGitLabParseError({ |
| 94 | + data: { |
| 95 | + url: url.toString(), |
| 96 | + integration: "GitLab", |
| 97 | + cause: "empty match", |
| 98 | + process: "branch", |
| 99 | + }, |
| 100 | + }); |
| 101 | + } |
| 102 | + return branch; |
| 103 | + } else { |
| 104 | + const results = /[/][^/]+[/][^/]+[/]-[/]tree[/](?<branch>[^?]+)/.exec( |
| 105 | + url.toString(), |
| 106 | + )?.groups; |
| 107 | + if (!results) { |
| 108 | + throw new EGitLabParseError({ |
| 109 | + data: { |
| 110 | + url: window.location.href, |
| 111 | + integration: "GitLab", |
| 112 | + cause: "no match", |
| 113 | + process: "branch", |
| 114 | + }, |
| 115 | + }); |
| 116 | + } |
| 117 | + const { branch } = results; |
| 118 | + if (_.isEmpty(branch)) { |
| 119 | + throw new EGitLabParseError({ |
| 120 | + data: { |
| 121 | + url: url.toString(), |
| 122 | + integration: "GitLab", |
| 123 | + cause: "empty match", |
| 124 | + process: "branch", |
| 125 | + }, |
| 126 | + }); |
| 127 | + } |
| 128 | + return branch; |
| 129 | + } |
| 130 | + }, |
| 131 | + buttonClassOverride({ url }) { |
| 132 | + if (isPR(url)) { |
| 133 | + return "ml-2"; |
| 134 | + } |
| 135 | + }, |
| 136 | +}; |
0 commit comments