From f763553739d5f916d1809fb45125d0fdbdb524b9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrian=20G=C3=B6risch?= <github@ago-dev.org>
Date: Mon, 27 Apr 2020 10:57:20 +0200
Subject: [PATCH] Add support for Gitlab merge-request and issues (#133)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Adrian Görisch <adrian.goerisch@10m.de>
---
 src/js/remoteServices.js      | 24 +++++++++++++++++++++++
 test/utils/urlMatcher.test.js | 37 +++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/src/js/remoteServices.js b/src/js/remoteServices.js
index 71dd55f..59ea145 100644
--- a/src/js/remoteServices.js
+++ b/src/js/remoteServices.js
@@ -118,4 +118,28 @@ export default {
         ?.textContent?.trim(),
     projectId: projectIdentifierBySelector(".taskItem.selected .taskItem-titleWrapper-title"),
   },
+
+  "gitlab-mr": {
+    name: "gitlab",
+    urlPatterns: [
+      "https\\://gitlab.com/:org/:group/:projectId/-/merge_requests/:id",
+      "https\\://gitlab.com/:org/:projectId/-/merge_requests/:id",
+    ],
+    description: (document, service, { id }) => {
+      const title = document.querySelector("h2.title")?.textContent?.trim()
+      return `#${id} ${title || ""}`.trim()
+    },
+  },
+
+  "gitlab-issues": {
+    name: "gitlab",
+    urlPatterns: [
+      "https\\://gitlab.com/:org/:group/:projectId/-/issues/:id",
+      "https\\://gitlab.com/:org/:projectId/-/issues/:id",
+    ],
+    description: (document, service, { id }) => {
+      const title = document.querySelector("h2.title")?.textContent?.trim()
+      return `#${id} ${title || ""}`.trim()
+    },
+  },
 }
diff --git a/test/utils/urlMatcher.test.js b/test/utils/urlMatcher.test.js
index 3d00d78..5b84686 100644
--- a/test/utils/urlMatcher.test.js
+++ b/test/utils/urlMatcher.test.js
@@ -108,6 +108,43 @@ describe("utils", () => {
           ).id,
         ).toEqual("1234")
       })
+
+      it("should match gitlab-mergerequest url", () => {
+        const service = matcher(
+          "https://gitlab.com/testorganisatzion/testproject/-/merge_requests/1",
+        )
+        expect(service.id).toEqual("1")
+        expect(service.match.id).toEqual("1")
+        expect(service.name).toEqual("gitlab")
+        expect(service.projectId).toEqual("testproject")
+      })
+      it("should match gitlab-mergerequest url with group", () => {
+        const service = matcher(
+          "https://gitlab.com/testorganisatzion/test-group/testproject/-/merge_requests/1",
+        )
+        expect(service.id).toEqual("1")
+        expect(service.match.id).toEqual("1")
+        expect(service.name).toEqual("gitlab")
+        expect(service.projectId).toEqual("testproject")
+      })
+
+      it("should match gitlab-issue url", () => {
+        const service = matcher("https://gitlab.com/testorganisatzion/testproject/-/issues/1")
+        expect(service.id).toEqual("1")
+        expect(service.match.id).toEqual("1")
+        expect(service.name).toEqual("gitlab")
+        expect(service.projectId).toEqual("testproject")
+      })
+
+      it("should match gitlab-issue url with group", () => {
+        const service = matcher(
+          "https://gitlab.com/testorganisatzion/test-group/testproject/-/issues/1",
+        )
+        expect(service.id).toEqual("1")
+        expect(service.match.id).toEqual("1")
+        expect(service.name).toEqual("gitlab")
+        expect(service.projectId).toEqual("testproject")
+      })
     })
 
     describe("createEnhancer", () => {