From d72177807b14f108c7264fda96499c04a945ea0c Mon Sep 17 00:00:00 2001 From: Joao Paulo Vieira Date: Mon, 28 Jul 2014 18:39:33 -0300 Subject: [PATCH 1/2] add GetDiff Handler --- api/handler.go | 23 +++++++++++++++++++ api/handler_test.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/api/handler.go b/api/handler.go index 5f533b8..872dc69 100644 --- a/api/handler.go +++ b/api/handler.go @@ -355,3 +355,26 @@ func GetBranch(w http.ResponseWriter, r *http.Request) { } w.Write(b) } + +func GetDiff(w http.ResponseWriter, r *http.Request) { + repo := r.URL.Query().Get(":name") + previousCommit := r.URL.Query().Get("previous_commit") + lastCommit := r.URL.Query().Get("last_commit") + if repo == "" { + err := fmt.Errorf("Error when trying to obtain diff between hash commits of repository %s (repository is required).", repo) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + if previousCommit == "" || lastCommit == "" { + err := fmt.Errorf("Error when trying to obtain diff between hash commits of repository %s (Hash Commit(s) are required).", repo) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + diff, err := repository.GetDiff(repo, previousCommit, lastCommit) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + w.Header().Set("Content-Length", strconv.Itoa(len(diff))) + w.Write(diff) +} diff --git a/api/handler_test.go b/api/handler_test.go index b0a8e85..f8f55eb 100644 --- a/api/handler_test.go +++ b/api/handler_test.go @@ -1046,3 +1046,59 @@ func (s *S) TestGetBranchWhenCommandFails(c *gocheck.C) { c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest) c.Assert(recorder.Body.String(), gocheck.Equals, "Error when trying to obtain the branches of repository repo (output error).\n") } + +func (s *S) TestGetDiff(c *gocheck.C) { + url := "/repository/repo/diff/commits?:name=repo&previous_commit=1b970b076bbb30d708e262b402d4e31910e1dc10&last_commit=545b1904af34458704e2aa06ff1aaffad5289f8f" + expected := "test_diff" + repository.Retriever = &repository.MockContentRetriever{ + ResultContents: []byte(expected), + } + defer func() { + repository.Retriever = nil + }() + request, err := http.NewRequest("GET", url, nil) + c.Assert(err, gocheck.IsNil) + recorder := httptest.NewRecorder() + GetDiff(recorder, request) + c.Assert(recorder.Code, gocheck.Equals, http.StatusOK) + c.Assert(recorder.Body.String(), gocheck.Equals, expected) +} + +func (s *S) TestGetDiffWhenCommandFails(c *gocheck.C) { + url := "/repository/repo/diff/commits?:name=repo&previous_commit=1b970b076bbb30d708e262b402d4e31910e1dc10&last_commit=545b1904af34458704e2aa06ff1aaffad5289f8f" + outputError := fmt.Errorf("command error") + repository.Retriever = &repository.MockContentRetriever{ + OutputError: outputError, + } + defer func() { + repository.Retriever = nil + }() + request, err := http.NewRequest("GET", url, nil) + c.Assert(err, gocheck.IsNil) + recorder := httptest.NewRecorder() + GetDiff(recorder, request) + c.Assert(recorder.Code, gocheck.Equals, http.StatusNotFound) + c.Assert(recorder.Body.String(), gocheck.Equals, "command error\n") +} + +func (s *S) TestGetDiffWhenNoRepository(c *gocheck.C) { + url := "/repository//diff/commits?:name=&previous_commit=1b970b076bbb30d708e262b402d4e31910e1dc10&last_commit=545b1904af34458704e2aa06ff1aaffad5289f8f" + request, err := http.NewRequest("GET", url, nil) + c.Assert(err, gocheck.IsNil) + recorder := httptest.NewRecorder() + GetDiff(recorder, request) + expected := "Error when trying to obtain diff between hash commits of repository (repository is required).\n" + c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest) + c.Assert(recorder.Body.String(), gocheck.Equals, expected) +} + +func (s *S) TestGetDiffWhenNoCommits(c *gocheck.C) { + url := "/repository/repo/diff/commits?:name=repo&previous_commit=&last_commit=" + request, err := http.NewRequest("GET", url, nil) + c.Assert(err, gocheck.IsNil) + recorder := httptest.NewRecorder() + GetDiff(recorder, request) + expected := "Error when trying to obtain diff between hash commits of repository repo (Hash Commit(s) are required).\n" + c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest) + c.Assert(recorder.Body.String(), gocheck.Equals, expected) +} From 94e2b74daca647c70e6e4b0581e5f960737f52e1 Mon Sep 17 00:00:00 2001 From: Joao Paulo Vieira Date: Mon, 28 Jul 2014 18:41:34 -0300 Subject: [PATCH 2/2] URL for GetDiff Handler --- webserver/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/webserver/main.go b/webserver/main.go index f135fc7..5b868e3 100644 --- a/webserver/main.go +++ b/webserver/main.go @@ -50,6 +50,7 @@ For an example conf check gandalf/etc/gandalf.conf file.\n %s` router.Get("/repository/:name/tree/:path", http.HandlerFunc(api.GetTree)) router.Get("/repository/:name/tree", http.HandlerFunc(api.GetTree)) router.Get("/repository/:name/branch", http.HandlerFunc(api.GetBranch)) + router.Get("/repository/:name/diff/commits", http.HandlerFunc(api.GetDiff)) router.Get("/healthcheck/", http.HandlerFunc(api.HealthCheck)) router.Post("/hook/:name", http.HandlerFunc(api.AddHook))