Skip to content

Commit

Permalink
Re/open (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
valerauko committed Jun 2, 2023
2 parents e97b3ce + 75e8891 commit 2fab9ae
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 81 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/develop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
- closed
- edited
- opened
- ready_for_review
- reopened
- synchronize

Expand All @@ -27,3 +28,5 @@ jobs:
env:
WRIKE_TOKEN: ${{ secrets.WRIKE_TOKEN }}
uses: ./
with:
opened: "In review"
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ jobs:
env:
WRIKE_TOKEN: ${{ secrets.WRIKE_TOKEN }}
uses: ./
with:
opened: "In review"
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ jobs:
env:
WRIKE_TOKEN: ${{ secrets.WRIKE_TOKEN }}
uses: ./
with:
opened: "In review"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ on:
- edited # link in description might be updated
- opened # yay new pr
- reopened # someone must've whoops'ed a pr
- ready_for_review # a pr is no longer draft
# - synchronize # new commit to pr

jobs:
Expand Down
3 changes: 3 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ branding:
color: green
icon: check-square
inputs:
opened:
required: false
description: Status to move task to when a PR is (re)opened or marked as ready for review. No action is taken unless specified.
merged:
required: false
description: Status to move task to when the PR is merged. Defaults to the first Completed status in your workflow. Can be disabled by setting it to "-".
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wrike-ist",
"version": "1.5.0",
"version": "1.6.0",
"description": "GitHub Action for Wrike automation",
"main": "resources/main.js",
"scripts": {
Expand Down
64 changes: 33 additions & 31 deletions resources/index.js

Large diffs are not rendered by default.

64 changes: 33 additions & 31 deletions resources/main.js

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions src/wrike_ist/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
(let [state (cond
^boolean (.-merged pr-obj) :merged
(= (.-state pr-obj) "closed") :closed
;; (= (.-mergeable_state pr-obj) "draft") :draft
^boolean (.-draft pr-obj) :draft
:else :open)
url ^String (.-html_url pr-obj)
title ^String (.-title pr-obj)]
Expand All @@ -33,9 +33,21 @@
(loop [links (extract-details pr)]
(when-let [{:keys [state] :as details} (first links)]
(-> (case state
:open (wrike/link-pr details)
:merged (wrike/complete-task details (core/getInput "merged"))
:closed (wrike/cancel-task details (core/getInput "closed"))
:draft
(wrike/link-pr details)

:open
(js/Promise.all
[(wrike/link-pr details)
(wrike/progress-task details (core/getInput "opened"))])

:merged
(wrike/complete-task details (core/getInput "merged"))

:closed
(wrike/cancel-task details (core/getInput "closed"))

;; else ignore
(js/Promise.resolve))
(.catch #(core/setFailed (.-message %))))
(recur (rest links))))
Expand Down
46 changes: 32 additions & 14 deletions src/wrike_ist/wrike.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,35 @@
(.then (fn [{statuses "customStatuses"}]
(filter #(= (get % "hidden") false) statuses))))))

(defn find-status
[statuses {:keys [wanted-status wanted-group]}]
(reduce
(fn [{current-group "group" :as candidate} {:strs [name group] :as status}]
(if (= name wanted-status)
;; if an exact name match is found, that's it
(reduced status)
;; if wanted-group isn't set then only exact name matches are acceptable
(when wanted-group
(cond
;; if the current candidate is already in the right group use it
(= current-group wanted-group)
candidate

;; else if the new status is in the right group use that
(= group wanted-group)
status))))
nil
statuses))

(defn next-status
[folder-id {:keys [wanted-status wanted-group]}]
;; {:keys [wanted-status wanted-group] :as opts}
[folder-id opts]
(.then
(folder-statuses folder-id)
(fn [statuses]
(reduce
(fn [candidate {:strs [name] :as status}]
;; use the status with the desired name
;; or if not found use the first status in the desired group
;; or if not found use the last status found
(if (= name wanted-status)
(reduced status)
(if (= (get candidate "group") wanted-group)
candidate
status)))
;; start with empty so that the very first status can be candidate too
{}
statuses))))
(if-let [match (find-status statuses opts)]
match
(js/Promise.reject (str "No appropriate status found" opts))))))

(defn update-task-status
[{task-id "id" [folder-id] "parentIds"} wanted]
Expand All @@ -110,6 +121,13 @@
(http/put uri {:headers (headers)
:body (js/JSON.stringify params)})))))

(defn progress-task
[{:keys [permalink]} wanted-status]
(when (not-empty wanted-status)
(.then
(find-task permalink)
#(update-task-status % {:wanted-status wanted-status}))))

(defn complete-task
[{:keys [permalink]} wanted-status]
(if-not (= "-" wanted-status)
Expand Down
20 changes: 20 additions & 0 deletions test/wrike_ist/wrike_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [cljs.test :refer-macros [deftest is testing]]
[wrike-ist.wrike :refer [cancel-task
complete-task
find-status
link-html]]))

(deftest link-html-test
Expand All @@ -16,6 +17,25 @@
(is (= url (re-find (re-pattern url) (link-html data))))
(is (= title (re-find (re-pattern title) (link-html data)))))))

(deftest find-status-test
(let [haystack [{"name" "bar"
"group" "fuga"}
{"name" "foo"
"group" "fuga"}
{"name" "baz"
"group" "hoge"}]]
(testing "If there's an exact name match"
(is (= {"name" "foo" "group" "fuga"}
(find-status haystack {:wanted-status "foo" :wanted-group "hoge"}))
"The name match is prioritized over group match"))
(testing "If there's no name match and :wanted-group isn't specified"
(is (= nil (find-status haystack {:wanted-status "asd"}))))
(testing "If there's no name match but :wanted-group matches"
(is (= {"name" "baz" "group" "hoge"}
(find-status haystack {:wanted-status "asd" :wanted-group "hoge"}))))
(testing "If there's no match at all"
(is (= nil (find-status haystack {:wanted-status "asd" :wanted-group "fgh"}))))))

(deftest cancel-task-test
(testing "Does nothing if `merged` is configured explicitly as \"-\""
(is (= nil (cancel-task {} "-")))))
Expand Down

0 comments on commit 2fab9ae

Please sign in to comment.