From 95223d9c829a002adaec41ce03ad6be9914a4c40 Mon Sep 17 00:00:00 2001 From: Ayush Gaud Date: Wed, 4 Sep 2024 17:49:13 +0900 Subject: [PATCH] add both wrike and azure link check --- src/wrike_ist/azure.cljs | 23 +++++++++----------- src/wrike_ist/core.cljs | 46 ++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/wrike_ist/azure.cljs b/src/wrike_ist/azure.cljs index 84e0fbd..4de35c0 100644 --- a/src/wrike_ist/azure.cljs +++ b/src/wrike_ist/azure.cljs @@ -1,11 +1,6 @@ (ns wrike-ist.azure (:require [httpurr.client.node :as http] [clojure.string :as str])) - -;; Define the variables -(def ^:private organization "rapyuta-robotics") ; Replace with your organization name -(def ^:private project "sootballs") ; Replace with your project name - (defn- azure-token [] (some-> js/process .-env .-AZURE_TOKEN .trim)) @@ -25,27 +20,29 @@ (defn find-task [url] - (let [pattern #"https://dev\.azure\.com/[^/]+/[^/]+/_workitems/edit/(\d+)" + (let [pattern #"https://dev\.azure\.com/([^/]+)/([^/]+)/_workitems/edit/(\d+)" matches (re-seq pattern url)] (js/Promise. (fn [resolve reject] (if (seq matches) - (resolve (-> (first matches) (second) (js/parseInt))) + (let [[_ organization project task-id] (first matches)] + (resolve {:organization organization + :project project + :task-id (js/parseInt task-id)})) (reject (js/Error. "No task ID found"))))))) - (defn link-pr [{:keys [pr-url permalink] :as details}] (.then (find-task permalink) - (fn [id] - (js/console.log (str "link-pr: Found task ID:" id)) + (fn [{:keys [organization project task-id]}] + (js/console.log (str "link-pr: Found organization:" organization " project:" project " task-id:" task-id)) (js/console.log (str "headers:" (headers))) - (let [uri (str "https://dev.azure.com/" organization "/" project "/_apis/wit/workitems/" id "/comments?api-version=7.0-preview.3")] - (js/console.log (str "uri:" uri)) + (let [uri (str "https://dev.azure.com/" organization "/" project "/_apis/wit/workitems/" task-id "/comments?api-version=7.0-preview.3")] + ;; (js/console.log (str "uri:" uri)) (-> (http/get uri {:headers (headers)}) (.then (fn find-existing-link [response] - (js/console.log (str "find-existing-link: response:" response)) + ;; (js/console.log (str "find-existing-link: response:" response)) (reduce (fn [ok comment] (if (.includes (get comment "text") pr-url) diff --git a/src/wrike_ist/core.cljs b/src/wrike_ist/core.cljs index 73341a9..8e6c4a5 100644 --- a/src/wrike_ist/core.cljs +++ b/src/wrike_ist/core.cljs @@ -4,15 +4,15 @@ [wrike-ist.wrike :as wrike] [wrike-ist.azure :as azure])) -(defn find-links - [text] - (not-empty (re-seq #"\bhttps://dev\.azure\.com/[^/]+/[^/]+/_workitems/edit/\d+\b" text))) ;; (defn find-links ;; [text] -;; (let [wrike-pattern #"\bhttps://www\.wrike\.com/open\.htm\?id=\d+\b" -;; azure-pattern #"\bhttps://dev\.azure\.com/[^/]+/[^/]+/_workitems/edit/\d+\b" -;; combined-pattern (re-pattern (str wrike-pattern "|" azure-pattern))] -;; (not-empty (re-seq combined-pattern text)))) +;; (not-empty (re-seq #"\bhttps://dev\.azure\.com/[^/]+/[^/]+/_workitems/edit/\d+\b" text))) +(defn find-links + [text] + (let [wrike-pattern #"\bhttps://www\.wrike\.com/open\.htm\?id=\d+\b" + azure-pattern #"\bhttps://dev\.azure\.com/[^/]+/[^/]+/_workitems/edit/\d+\b" + combined-pattern (re-pattern (str wrike-pattern "|" azure-pattern))] + (not-empty (re-seq combined-pattern text)))) (defn extract-details [pr-obj] @@ -39,21 +39,35 @@ :repository-name repository-name}) links))))) +(defn find-link-type + [url] + (cond + (re-find #"https://www\.wrike\.com/open\.htm\?id=\d+" url) :wrike + (re-find #"https://dev\.azure\.com/[^/]+/[^/]+/_workitems/edit/\d+" url) :azure + :else :unknown)) + (defn main [] (let [payload (.-payload (.-context github))] (if-let [pr (.-pull_request payload)] (loop [links (extract-details pr)] - (when-let [{:keys [state] :as details} (first links)] - (-> (case state - :draft - (azure/link-pr details) + (when-let [{:keys [state pr-url] :as details} (first links)] + (let [link-type (find-link-type pr-url)] + (-> (case state + :draft + (case link-type + :wrike (wrike/link-pr details) + :azure (azure/link-pr details) + (js/Promise.resolve)) - :open - (azure/link-pr details) + :open + (case link-type + :wrike (wrike/link-pr details) + :azure (azure/link-pr details) + (js/Promise.resolve)) - ;; else ignore - (js/Promise.resolve)) - (.catch #(core/setFailed (.-message %)))) + ;; else ignore + (js/Promise.resolve)) + (.catch #(core/setFailed (.-message %))))) (recur (rest links)))) (js/console.log "No pull_request in payload"))))