Skip to content

Commit 329171e

Browse files
More flexibly handle github repo references. (#10)
1 parent 0d3a12e commit 329171e

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>eamonnsullivan</groupId>
55
<artifactId>github-api-lib</artifactId>
6-
<version>0.1.18</version>
6+
<version>0.1.19</version>
77
<name>github-api-lib</name>
88
<description>Library of Github API calls that I happen to need.</description>
99
<url>https://github.com/eamonnsullivan/github-api-lib</url>

src/eamonnsullivan/github_api_lib/repos.clj

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
(ns eamonnsullivan.github-api-lib.repos
2-
(:require [eamonnsullivan.github-api-lib.core :as core]))
2+
(:require [clojure.string :as string]
3+
[eamonnsullivan.github-api-lib.core :as core]))
34

45
(def ^:dynamic *default-page-size* 10)
56

67
(defn parse-repo
78
"Parse a repository url (a full url or just the owner/name part) and
89
return a map with :owner and :name keys."
910
[url]
10-
(let [matches (re-matches #"(https://github.com/)?([^/]*)/([^/]*).*$" url)
11-
[_ _ owner name] matches]
11+
(let [matches (re-matches #"(https://github.com/|[email protected]:)?([^/]*)/([^/]*)(.git)?.*$" url)
12+
[_ _ owner name _] matches]
1213
(if (and owner name (not-empty owner) (not-empty name))
13-
{:owner owner :name name}
14+
{:owner owner
15+
:name (if (string/ends-with? name ".git")
16+
(string/replace name #".git$" "")
17+
name)}
1418
(throw (ex-info (format "Could not parse repository from url: %s" url) {})))))
1519

1620
(defn get-repo-id

test/eamonnsullivan/github_api_lib/repos_test.clj

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
(is (= {:owner "eamonnsullivan" :name "github-search"} (sut/parse-repo "eamonnsullivan/github-search/blob/master/src/eamonnsullivan/github_search.clj")))
2020
(is (= {:owner "eamonnsullivan" :name "github-pr-lib"} (sut/parse-repo "eamonnsullivan/github-pr-lib/pull/1")))
2121
(is (= {:owner "bbc" :name "optimo"} (sut/parse-repo "bbc/optimo/pull/1277"))))
22+
(testing "handles ssh urls as well"
23+
(is (= {:owner "eamonnsullivan" :name "emacs.d"} (sut/parse-repo "[email protected]:eamonnsullivan/emacs.d.git")))
24+
(is (= {:owner "eamonnsullivan" :name "github-search"} (sut/parse-repo "[email protected]:eamonnsullivan/github-search.git")))
25+
(is (= {:owner "eamonnsullivan" :name "github-pr-lib"} (sut/parse-repo "[email protected]:eamonnsullivan/github-pr-lib.git"))))
2226
(testing "Throws an exception when the url is incomplete or unrecognised"
2327
(is (thrown-with-msg? RuntimeException
2428
#"Could not parse repository from url: something else"

0 commit comments

Comments
 (0)