Skip to content

Commit

Permalink
Add clone repository function (#8)
Browse files Browse the repository at this point in the history
* Add clone repository function

* Update src/clj_github/repository.clj

Co-authored-by: Rui Fernando Hayashi <[email protected]>

* Create temp dir to download git repo

* Remove master as default branch. Let github itself decides the default

* Add link to API documentation

* rename tag param to ref

* Remove destination path param

* Update src/clj_github/repository.clj

Co-authored-by: Rui Fernando Hayashi <[email protected]>

* Fix error on returned repo path

* Update src/clj_github/repository.clj

Co-authored-by: Rui Fernando Hayashi <[email protected]>

* Update changelog

Co-authored-by: Rui Fernando Hayashi <[email protected]>
  • Loading branch information
marianabm and rfhayashi authored Jul 5, 2021
1 parent 48a1994 commit 91edb17
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.4.0
- Add function that downloads repository content

## 0.3.0
- Support content types other than json

Expand Down
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject dev.nubank/clj-github "0.3.0"
(defproject dev.nubank/clj-github "0.4.0"
:description "A Clojure library for interacting with the github developer API"
:url "https://github.com/nubank/clj-github"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand All @@ -19,7 +19,8 @@
[clj-commons/clj-yaml "0.7.106"]
[http-kit "2.5.3"]
[nubank/clj-github-app "0.1.4"]
[nubank/state-flow "5.11.3"]]
[nubank/state-flow "5.11.3"]
[clj-commons/fs "1.6.307"]]

:cljfmt {:indents {flow [[:block 1]]
assoc-some [[:block 0]]}}
Expand Down
34 changes: 32 additions & 2 deletions src/clj_github/repository.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
(ns clj-github.repository
"Provides auxiliary functions to work with repositories via github api."
(:require [clojure.string :as string]
(:require [clj-github.client-utils :refer [fetch-body!]]
[clj-github.httpkit-client :as client]
[clj-github.client-utils :refer [fetch-body!]])
[clojure.java.io :as io]
[clojure.string :as string]
[clojure.string :as str]
[me.raynes.fs :as fs]
[me.raynes.fs.compression :as fs-compression])
(:import [clojure.lang ExceptionInfo]
[java.util Base64]))

Expand Down Expand Up @@ -195,3 +199,29 @@
(fetch-body! client {:path url
:method :put
:body params})))

(defn- find-repo-path
[clone-path]
(->> (fs/list-dir clone-path)
(map str)
(remove #(str/ends-with? % ".zip"))
first))

(defn clone
"Download github repository and put its content on a temporary dir. Returns the path of the temporary dir.
For details about the parameters and response format, look at https://docs.github.com/en/rest/reference/repos#download-a-repository-archive-zip."
([client org repo]
(clone client org repo ""))
([client org repo ref]
(let [clone-path (fs/temp-dir "clone-repo")
url (format "/repos/%s/%s/zipball/%s" org repo ref)
git-response (client/request client {:path url
:method :get
:as :byte-array})]
(-> git-response
:body
(io/input-stream)
(io/copy (io/file clone-path "git-response.zip")))
(fs-compression/unzip (str clone-path "/git-response.zip") clone-path)
(find-repo-path clone-path))))

0 comments on commit 91edb17

Please sign in to comment.