Skip to content

Commit

Permalink
Publish comment from clojure script
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jun 14, 2022
1 parent 4f80b93 commit 5c4f04f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 45 deletions.
18 changes: 1 addition & 17 deletions .github/workflows/comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,7 @@ jobs:

- name: Add comment to PR
env:
URL: ${{ github.event.pull_request.comments_url }}
PR: ${{ github.event.pull_request.number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
./script/comment.sh --pr $PR
# run: |
# set -x
# echo $URL
# echo $PR
# git fetch origin pull/$PR/head:pr-$PR
# git branch -a
# git diff remotes/origin/main..pr-$PR
# DIFF=`git diff remotes/origin/main..pr-$PR`
# curl --fail --silent --location \
# -X POST \
# -H "Content-Type: application/json" \
# -H "Accept: application/vnd.github.v3+json" \
# -H "Authorization: token $GITHUB_TOKEN" \
# https://api.github.com/repos/Roam-Research/roam-marketplace/issues/$PR/comments \
# --data '{ "body": "$DIFF" }'
./script/comment.sh --pr $PR --token $GITHUB_TOKEN
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:deps
{cheshire/cheshire {:mvn/version "5.11.0"}}
{clj-http/clj-http {:mvn/version "3.12.3"}
cheshire/cheshire {:mvn/version "5.11.0"}}
:aliases
{:firebase
{:extra-deps
Expand Down
58 changes: 31 additions & 27 deletions src/community_extensions/comment.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns community-extensions.comment
(:require
[cheshire.core :as json]
[clj-http.client :as http]
[clojure.java.io :as io]
[clojure.java.shell :as shell]
[clojure.string :as str]
Expand All @@ -8,14 +10,9 @@
(def args-map
(apply array-map *command-line-args*))

; (defn build [ext-id data]
; (let [dir (io/file "checkout" ext-id)
; exists? (.exists dir)
; {repo "source_repo"
; commit "source_commit"} data]


(defn -main [& {pr "--pr" :as args-map}]
(defn -main [& {pr "--pr"
token "--token"
:as args-map}]
(let [branch (str "pr-" pr)
_ (core/sh "git" "fetch" "origin" (str "pull/" pr "/head:" branch))
changes (vec
Expand All @@ -33,25 +30,32 @@
after (into {}
(for [[mode path] changes
:when (.exists (io/file path))]
[path (core/slurp-json path)]))]
(println
(str/join "\n"
(concat
["Here’s your link to the diff:"]
(for [[mode path] changes
:let [[_ user repo] (re-matches #"extensions/([^/]+)/([^/]+)\.json" path)
commit-before (:source_commit (before path))
commit-after (:source_commit (after path))
url (or (:source_url (before path))
(:source_url (after path)))]
:when (not= commit-before commit-after)]
(cond
(nil? commit-before)
(str "Added [" user "/" repo "](" url ") [" (subs commit-after 0 7) "](" url "/tree/" commit-after ")")
[path (core/slurp-json path)]))
message (str/join "\n"
(concat
["Here’s your link to the diff:\n"]
(for [[mode path] changes
:let [[_ user repo] (re-matches #"extensions/([^/]+)/([^/]+)\.json" path)
commit-before (:source_commit (before path))
commit-after (:source_commit (after path))
url (or (:source_url (before path))
(:source_url (after path)))]
:when (not= commit-before commit-after)]
(cond
(nil? commit-before)
(str "Added [" user "/" repo "](" url ") [" (subs commit-after 0 7) "](" url "/tree/" commit-after ")")

(nil? commit-after)
(str "Added [" user "/" repo "](" url ") [" (subs commit-before 0 7) "](" url "/tree/" commit-before ")")
(nil? commit-after)
(str "Added [" user "/" repo "](" url ") [" (subs commit-before 0 7) "](" url "/tree/" commit-before ")")

:else
(str "Changed [" user "/" repo "](" url ") [" (subs commit-before 0 7) "" (subs commit-after 0 7) "](" url "/compare/" commit-before ".." commit-after ")"))))))
:else
(str "Changed [" user "/" repo "](" url ") [" (subs commit-before 0 7) "" (subs commit-after 0 7) "](" url "/compare/" commit-before ".." commit-after ")")))))]
(println message)
(when token
(println
(http/post (str "https://api.github.com/repos/Roam-Research/roam-marketplace/issues/" pr "/comments")
{:headers {"Content-Type" "application/json"
"Accept" "application/vnd.github.v3+json"
"Authorization" (str "token " token)}
:body (json/generate-string {:body message})})))
(shutdown-agents)))

0 comments on commit 5c4f04f

Please sign in to comment.