Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #136 from marcelometal/links
Browse files Browse the repository at this point in the history
Added zipArchive and tarArchive in the GetForEachRef
  • Loading branch information
andrewsmedina committed Jul 28, 2014
2 parents d744a9c + b97e379 commit 3ec070d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 22 deletions.
26 changes: 20 additions & 6 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,15 +974,19 @@ func (s *S) TestGetTreeWhenCommandFails(c *gocheck.C) {

func (s *S) TestGetBranch(c *gocheck.C) {
url := "/repository/repo/branch?:name=repo"
refs := make([]map[string]string, 1)
refs[0] = make(map[string]string)
refs := make([]map[string]interface{}, 1)
refs[0] = map[string]interface{}{}
refs[0]["ref"] = "a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9"
refs[0]["name"] = "doge_barks"
refs[0]["commiterName"] = "doge"
refs[0]["commiterEmail"] = "<much@email.com>"
refs[0]["authorName"] = "doge"
refs[0]["authorEmail"] = "<much@email.com>"
refs[0]["subject"] = "will bark"
links := map[string]string{}
links["zipArchive"] = repository.GetArchiveUrl("repo", "doge_barks", "zip")
links["tarArchive"] = repository.GetArchiveUrl("repo", "doge_barks", "tar.gz")
refs[0]["_links"] = links
mockRetriever := repository.MockContentRetriever{
Refs: refs,
}
Expand All @@ -995,7 +999,7 @@ func (s *S) TestGetBranch(c *gocheck.C) {
recorder := httptest.NewRecorder()
GetBranch(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
var obj []map[string]string
var obj []map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &obj)
c.Assert(len(obj), gocheck.Equals, 1)
c.Assert(obj[0]["ref"], gocheck.Equals, refs[0]["ref"])
Expand All @@ -1005,6 +1009,9 @@ func (s *S) TestGetBranch(c *gocheck.C) {
c.Assert(obj[0]["authorName"], gocheck.Equals, refs[0]["authorName"])
c.Assert(obj[0]["authorEmail"], gocheck.Equals, refs[0]["authorEmail"])
c.Assert(obj[0]["subject"], gocheck.Equals, refs[0]["subject"])
objLinks := obj[0]["_links"].(map[string]interface{})
c.Assert(objLinks["zipArchive"], gocheck.Equals, links["zipArchive"])
c.Assert(objLinks["tarArchive"], gocheck.Equals, links["tarArchive"])
}

func (s *S) TestGetBranchWhenRepoNotSupplied(c *gocheck.C) {
Expand Down Expand Up @@ -1049,15 +1056,19 @@ func (s *S) TestGetBranchWhenCommandFails(c *gocheck.C) {

func (s *S) TestGetTag(c *gocheck.C) {
url := "/repository/repo/tag?:name=repo"
tags := make([]map[string]string, 1)
tags[0] = make(map[string]string)
tags := make([]map[string]interface{}, 1)
tags[0] = map[string]interface{}{}
tags[0]["ref"] = "a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9"
tags[0]["name"] = "0.1"
tags[0]["commiterName"] = "doge"
tags[0]["commiterEmail"] = "<much@email.com>"
tags[0]["authorName"] = "doge"
tags[0]["authorEmail"] = "<much@email.com>"
tags[0]["subject"] = "will bark"
links := map[string]string{}
links["zipArchive"] = repository.GetArchiveUrl("repo", "0.1", "zip")
links["tarArchive"] = repository.GetArchiveUrl("repo", "0.1", "tar.gz")
tags[0]["_links"] = links
mockRetriever := repository.MockContentRetriever{
Refs: tags,
}
Expand All @@ -1070,7 +1081,7 @@ func (s *S) TestGetTag(c *gocheck.C) {
recorder := httptest.NewRecorder()
GetTag(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
var obj []map[string]string
var obj []map[string]interface{}
json.Unmarshal(recorder.Body.Bytes(), &obj)
c.Assert(len(obj), gocheck.Equals, 1)
c.Assert(obj[0]["ref"], gocheck.Equals, tags[0]["ref"])
Expand All @@ -1080,4 +1091,7 @@ func (s *S) TestGetTag(c *gocheck.C) {
c.Assert(obj[0]["authorName"], gocheck.Equals, tags[0]["authorName"])
c.Assert(obj[0]["authorEmail"], gocheck.Equals, tags[0]["authorEmail"])
c.Assert(obj[0]["subject"], gocheck.Equals, tags[0]["subject"])
objLinks := obj[0]["_links"].(map[string]interface{})
c.Assert(objLinks["zipArchive"], gocheck.Equals, links["zipArchive"])
c.Assert(objLinks["tarArchive"], gocheck.Equals, links["tarArchive"])
}
8 changes: 4 additions & 4 deletions repository/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type MockContentRetriever struct {
LastPath string
ResultContents []byte
Tree []map[string]string
Refs []map[string]string
Refs []map[string]interface{}
LookPathError error
OutputError error
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (r *MockContentRetriever) GetTree(repo, ref, path string) ([]map[string]str
return r.Tree, nil
}

func (r *MockContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]string, error) {
func (r *MockContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]interface{}, error) {
if r.LookPathError != nil {
return nil, r.LookPathError
}
Expand All @@ -261,7 +261,7 @@ func (r *MockContentRetriever) GetForEachRef(repo, pattern string) ([]map[string
return r.Refs, nil
}

func (r *MockContentRetriever) GetBranch(repo string) ([]map[string]string, error) {
func (r *MockContentRetriever) GetBranch(repo string) ([]map[string]interface{}, error) {
if r.LookPathError != nil {
return nil, r.LookPathError
}
Expand All @@ -281,7 +281,7 @@ func (r *MockContentRetriever) GetDiff(repo, previousCommit, lastCommit string)
return r.ResultContents, nil
}

func (r *MockContentRetriever) GetTag(repo string) ([]map[string]string, error) {
func (r *MockContentRetriever) GetTag(repo string) ([]map[string]interface{}, error) {
if r.LookPathError != nil {
return nil, r.LookPathError
}
Expand Down
31 changes: 20 additions & 11 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ func RevokeAccess(rNames, uNames []string) error {
return err
}

func GetArchiveUrl(repo, ref, format string) string {
url := "/repository/%s/archive/%s.%s"
return fmt.Sprintf(url, repo, ref, format)
}

type ArchiveFormat int

const (
Expand All @@ -249,10 +254,10 @@ type ContentRetriever interface {
GetContents(repo, ref, path string) ([]byte, error)
GetArchive(repo, ref string, format ArchiveFormat) ([]byte, error)
GetTree(repo, ref, path string) ([]map[string]string, error)
GetForEachRef(repo, pattern string) ([]map[string]string, error)
GetBranch(repo string) ([]map[string]string, error)
GetForEachRef(repo, pattern string) ([]map[string]interface{}, error)
GetBranch(repo string) ([]map[string]interface{}, error)
GetDiff(repo, lastCommit, previousCommit string) ([]byte, error)
GetTag(repo string) ([]map[string]string, error)
GetTag(repo string) ([]map[string]interface{}, error)
}

var Retriever ContentRetriever
Expand Down Expand Up @@ -353,7 +358,7 @@ func (*GitContentRetriever) GetTree(repo, ref, path string) ([]map[string]string
return objects, nil
}

func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]string, error) {
func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]interface{}, error) {
var ref, name, commiterName, commiterEmail, commiterDate, authorName, authorEmail, authorDate, subject string
gitPath, err := exec.LookPath("git")
if err != nil {
Expand All @@ -376,7 +381,7 @@ func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]st
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
objectCount := len(lines)
objects := make([]map[string]string, objectCount)
objects := make([]map[string]interface{}, objectCount)
objectCount = 0
for _, line := range lines {
if strings.TrimSpace(line) == "" {
Expand All @@ -396,7 +401,7 @@ func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]st
} else {
return nil, fmt.Errorf("Error when trying to obtain the refs of repository %s (Invalid git for-each-ref output [%s]).", repo, out)
}
object := make(map[string]string)
object := map[string]interface{}{}
object["ref"] = ref
object["name"] = name
object["commiterName"] = commiterName
Expand All @@ -406,13 +411,17 @@ func (*GitContentRetriever) GetForEachRef(repo, pattern string) ([]map[string]st
object["authorEmail"] = authorEmail
object["authorDate"] = authorDate
object["subject"] = subject
links := map[string]string{}
links["zipArchive"] = GetArchiveUrl(repo, name, "zip")
links["tarArchive"] = GetArchiveUrl(repo, name, "tar.gz")
object["_links"] = links
objects[objectCount] = object
objectCount++
}
return objects, nil
}

func (*GitContentRetriever) GetBranch(repo string) ([]map[string]string, error) {
func (*GitContentRetriever) GetBranch(repo string) ([]map[string]interface{}, error) {
branches, err := retriever().GetForEachRef(repo, "refs/heads/")
return branches, err
}
Expand All @@ -436,7 +445,7 @@ func (*GitContentRetriever) GetDiff(repo, previousCommit, lastCommit string) ([]
return out, nil
}

func (*GitContentRetriever) GetTag(repo string) ([]map[string]string, error) {
func (*GitContentRetriever) GetTag(repo string) ([]map[string]interface{}, error) {
tags, err := retriever().GetForEachRef(repo, "refs/tags/")
return tags, err
}
Expand Down Expand Up @@ -464,18 +473,18 @@ func GetTree(repo, ref, path string) ([]map[string]string, error) {
return retriever().GetTree(repo, ref, path)
}

func GetForEachRef(repo, pattern string) ([]map[string]string, error) {
func GetForEachRef(repo, pattern string) ([]map[string]interface{}, error) {
return retriever().GetForEachRef(repo, pattern)
}

func GetBranch(repo string) ([]map[string]string, error) {
func GetBranch(repo string) ([]map[string]interface{}, error) {
return retriever().GetBranch(repo)
}

func GetDiff(repo, previousCommit, lastCommit string) ([]byte, error) {
return retriever().GetDiff(repo, previousCommit, lastCommit)
}

func GetTag(repo string) ([]map[string]string, error) {
func GetTag(repo string) ([]map[string]interface{}, error) {
return retriever().GetTag(repo)
}
23 changes: 22 additions & 1 deletion repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,20 +833,29 @@ func (s *S) TestGetBranchIntegration(c *gocheck.C) {
c.Assert(branches[0]["authorName"], gocheck.Equals, "doge")
c.Assert(branches[0]["authorEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(branches[0]["subject"], gocheck.Equals, "will bark")
links := branches[0]["_links"].(map[string]string)
c.Assert(links["zipArchive"], gocheck.Equals, GetArchiveUrl(repo, "doge_barks", "zip"))
c.Assert(links["tarArchive"], gocheck.Equals, GetArchiveUrl(repo, "doge_barks", "tar.gz"))
c.Assert(branches[1]["ref"], gocheck.Matches, "[a-f0-9]{40}")
c.Assert(branches[1]["name"], gocheck.Equals, "doge_bites")
c.Assert(branches[1]["commiterName"], gocheck.Equals, "doge")
c.Assert(branches[1]["commiterEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(branches[1]["authorName"], gocheck.Equals, "doge")
c.Assert(branches[1]["authorEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(branches[1]["subject"], gocheck.Equals, "will bark")
links = branches[1]["_links"].(map[string]string)
c.Assert(links["zipArchive"], gocheck.Equals, GetArchiveUrl(repo, "doge_bites", "zip"))
c.Assert(links["tarArchive"], gocheck.Equals, GetArchiveUrl(repo, "doge_bites", "tar.gz"))
c.Assert(branches[2]["ref"], gocheck.Matches, "[a-f0-9]{40}")
c.Assert(branches[2]["name"], gocheck.Equals, "master")
c.Assert(branches[2]["commiterName"], gocheck.Equals, "doge")
c.Assert(branches[2]["commiterEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(branches[2]["authorName"], gocheck.Equals, "doge")
c.Assert(branches[2]["authorEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(branches[2]["subject"], gocheck.Equals, "will bark")
links = branches[2]["_links"].(map[string]string)
c.Assert(links["zipArchive"], gocheck.Equals, GetArchiveUrl(repo, "master", "zip"))
c.Assert(links["tarArchive"], gocheck.Equals, GetArchiveUrl(repo, "master", "tar.gz"))
}

func (s *S) TestGetForEachRefIntegrationWithSubjectEmpty(c *gocheck.C) {
Expand Down Expand Up @@ -930,7 +939,8 @@ func (s *S) TestGetForEachRefIntegrationWhenPatternEmpty(c *gocheck.C) {
refs, err := GetForEachRef("gandalf-test-repo", "")
c.Assert(err, gocheck.IsNil)
c.Assert(refs, gocheck.HasLen, 1)
c.Assert(refs[0], gocheck.HasLen, 9)
c.Assert(refs[0], gocheck.HasLen, 10)
c.Assert(refs[0]["_links"], gocheck.HasLen, 2)
}

func (s *S) TestGetForEachRefIntegrationWhenPatternNonExistent(c *gocheck.C) {
Expand Down Expand Up @@ -1094,11 +1104,22 @@ func (s *S) TestGetTagIntegration(c *gocheck.C) {
c.Assert(tags[0]["authorName"], gocheck.Equals, "doge")
c.Assert(tags[0]["authorEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(tags[0]["subject"], gocheck.Equals, "much WOW")
links := tags[0]["_links"].(map[string]string)
c.Assert(links["zipArchive"], gocheck.Equals, GetArchiveUrl(repo, "0.1", "zip"))
c.Assert(links["tarArchive"], gocheck.Equals, GetArchiveUrl(repo, "0.1", "tar.gz"))
c.Assert(tags[1]["ref"], gocheck.Matches, "[a-f0-9]{40}")
c.Assert(tags[1]["name"], gocheck.Equals, "0.2")
c.Assert(tags[1]["commiterName"], gocheck.Equals, "doge")
c.Assert(tags[1]["commiterEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(tags[1]["authorName"], gocheck.Equals, "doge")
c.Assert(tags[1]["authorEmail"], gocheck.Equals, "<much@email.com>")
c.Assert(tags[1]["subject"], gocheck.Equals, "")
links = tags[1]["_links"].(map[string]string)
c.Assert(links["zipArchive"], gocheck.Equals, GetArchiveUrl(repo, "0.2", "zip"))
c.Assert(links["tarArchive"], gocheck.Equals, GetArchiveUrl(repo, "0.2", "tar.gz"))
}

func (s *S) TestGetArchiveUrl(c *gocheck.C) {
url := GetArchiveUrl("repo", "ref", "zip")
c.Assert(url, gocheck.Equals, fmt.Sprintf("/repository/%s/archive/%s.%s", "repo", "ref", "zip"))
}

0 comments on commit 3ec070d

Please sign in to comment.