Skip to content

Instantly share code, notes, and snippets.

@lagenorhynque
Last active March 18, 2021 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagenorhynque/15ee2d206a59236e9d9b224e2b8a7106 to your computer and use it in GitHub Desktop.
Save lagenorhynque/15ee2d206a59236e9d9b224e2b8a7106 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bb
(ns list-repositories
(:require
[babashka.curl :as curl]
[cheshire.core :as cheshire]
[clojure.pprint :refer [pprint]]
[clojure.string :as str]))
(def auth-token (System/getenv "AUTH_TOKEN"))
(def query-repositories
"
query ($query: String!, $first: Int, $after: String) {
search(type: REPOSITORY, query: $query, first: $first, after: $after) {
nodes {
... on Repository {
nameWithOwner
isArchived
isDisabled
isFork
isPrivate
isTemplate
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
")
(defn run-query [query variables]
(-> (curl/post "https://api.github.com/graphql"
{:headers {"Content-Type" "application/json"
"Accept" "application/json"
"Authorization" (str "Bearer " auth-token)}
:body (cheshire/generate-string {:query query
:variables variables})})
:body
(cheshire/parse-string true)))
(defn all-repositories [variables]
(loop [{:keys [data errors]} (run-query query-repositories
variables)
repos []]
(if errors
(pprint errors)
(let [repos' (into repos (-> data :search :nodes))]
(if (-> data :search :pageInfo :hasNextPage)
(recur (run-query query-repositories
(assoc variables
:after (-> data :search :pageInfo :endCursor)))
repos')
repos')))))
(defn -main [& _]
(->> (all-repositories {:query "org:my-org"
:first 100})
(remove (some-fn :isArchived
:isDisabled
:isFork
(complement :isPrivate)
:isTemplate))
(map :nameWithOwner)
(str/join "\n")
println))
(when (= *file* (System/getProperty "babashka.file"))
(apply -main *command-line-args*))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment