Skip to content

Instantly share code, notes, and snippets.

@tmcw
Forked from anonymous/core.clj
Created May 8, 2017 22:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcw/91fbdf2aedd9677b63ed3271b64a07b1 to your computer and use it in GitHub Desktop.
Save tmcw/91fbdf2aedd9677b63ed3271b64a07b1 to your computer and use it in GitHub Desktop.
Find all repositories owned by a user, generate redirects from their gh-pages branches on a custom domain to a bare pages domain.
(ns gh-finder.core
(:require [tentacles.repos :as repos])
(:require [clojure.java.io :as io])
(:require [clj-yaml.core :as yaml])
(:gen-class))
(defn auth
"get an authentication token from the environment"
[]
{:auth (System/getenv "GITHUB_TOKEN")})
(defn make-redirect
"given a repository, build a redirect to its pages.github.io address"
[repo]
(spit (str "2017-01-01-" (:name repo) ".md")
(str "---\n"
(yaml/generate-string {
:layout "redirect_external"
:categories "redirect"
:permalink (str "/" (:name repo) "/")
:redirect (str "https://tmcw.github.io/" (:name repo))
}) "---")))
(defn get-branches
"get branches given a repository map"
[repo]
(when
(some (partial = "gh-pages")
(map :name (repos/branches
(:login (:owner repo))
(:name repo)
(auth))))
repo))
(defn -main
"print a list of repositories you own that have gh-pages branches"
[& args]
(->>
(merge (auth) {:all-pages true})
(repos/user-repos "tmcw")
(map get-branches)
(remove nil?)
(map make-redirect)
;; (map (partial format "- %s"))
;; (clojure.string/join "\n")
;; (print)
))
(defproject gh-finder "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [
[clj-yaml "0.4.0"]
[hiccup "1.0.5"]
[irresponsible/tentacles "0.6.1"]
[org.clojure/clojure "1.8.0"]]
:plugins [[lein-cljfmt "0.5.6"]]
:main ^:skip-aot gh-finder.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment