Skip to content

Commit

Permalink
add both wrike and azure link check
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushgaud committed Sep 4, 2024
1 parent ee650fb commit 95223d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
23 changes: 10 additions & 13 deletions src/wrike_ist/azure.cljs
Original file line number Diff line number Diff line change
@@ -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))

Expand All @@ -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)
Expand Down
46 changes: 30 additions & 16 deletions src/wrike_ist/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"))))

0 comments on commit 95223d9

Please sign in to comment.