diff --git a/api/handler.go b/api/handler.go index 9c419f4..16840c9 100644 --- a/api/handler.go +++ b/api/handler.go @@ -453,84 +453,46 @@ func Commit(w http.ResponseWriter, r *http.Request) { } err := r.ParseMultipartForm(int64(maxMemoryValue())) if err != nil { - err := fmt.Errorf("Error when trying to commit to repository %s (%s).", repo, err) http.Error(w, err.Error(), http.StatusBadRequest) return } - committer := repository.GitUser{ - Name: "committer", - Email: "committer@globo.com", - Date: "", - } - // committer, err := multipart.GetCommitter(r.MultipartForm.File["zipfile"][0]) - // if err != nil { - // err := fmt.Errorf("Error when trying to commit to repository %s (%s).", repo, err) - // http.Error(w, err.Error(), http.StatusBadRequest) - // return - // } - author := repository.GitUser{ - Name: "author", - Email: "author@globo.com", - Date: "", - } - // author, err := multipart.GetAuthor(r.MultipartForm) - // if err != nil { - // err := fmt.Errorf("Error when trying to commit to repository %s (%s).", repo, err) - // http.Error(w, err.Error(), http.StatusBadRequest) - // return - // } - message := "commit message" - // message, err := multipart.GetMessage(r.MultipartForm) - // if err != nil { - // err := fmt.Errorf("Error when trying to commit to repository %s (%s).", repo, err) - // http.Error(w, err.Error(), http.StatusBadRequest) - // return - // } - branch := "master" - // branch, err := multipart.GetBranch(r.MultipartForm) - // if err != nil { - // err := fmt.Errorf("Error when trying to commit to repository %s (%s).", repo, err) - // http.Error(w, err.Error(), http.StatusBadRequest) - // return - // } - cloneDir, _, err := repository.TempClone(repo) - // cloneDir, cleanUp, err := repository.TempClone(repo) - // if cleanUp != nil { - // defer cleanUp() - // } - if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - err = repository.SetCommitter(cloneDir, committer) - if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - err = repository.Checkout(cloneDir, branch) - if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - err = multipartzip.ExtractZip(r.MultipartForm.File["zipfile"][0], cloneDir) - if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) - return + form := r.MultipartForm + data := map[string]string{ + "branch": "", + "message": "", + "author-name": "", + "author-email": "", + "committer-name": "", + "committer-email": "", } - err = repository.AddAll(cloneDir) - if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) - return + for key, _ := range data { + data[key], err = multipartzip.ValueField(form, key) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } } - err = repository.Commit(cloneDir, message, author) + commit := repository.GitCommit{ + Branch: data["branch"], + Message: data["message"], + Author: repository.GitUser{ + Name: data["author-name"], + Email: data["author-email"], + }, + Committer: repository.GitUser{ + Name: data["committer-name"], + Email: data["committer-email"], + }, + } + ref, err := repository.CommitZip(repo, r.MultipartForm.File["zipfile"][0], commit) if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) + http.Error(w, err.Error(), http.StatusBadRequest) return } - err = repository.Push(cloneDir, branch) + b, err := json.Marshal(ref) if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) + http.Error(w, err.Error(), http.StatusBadRequest) return } - fmt.Fprintf(w, "Commit successfully applied to: %s\n", cloneDir) + w.Write(b) } diff --git a/api/handler_test.go b/api/handler_test.go index 23830fa..b33c27c 100644 --- a/api/handler_test.go +++ b/api/handler_test.go @@ -1288,9 +1288,12 @@ func (s *S) TestGetDiffWhenNoCommits(c *gocheck.C) { func (s *S) TestPostNewCommit(c *gocheck.C) { url := "/repository/repo/commit/?:name=repo" params := map[string]string{ - "message": "Repository scaffold", - "author": "Doge Dog ", - "committer": "Barking Doge ", + "message": "Repository scaffold", + "author-name": "Doge Dog", + "author-email": "doge@much.com", + "committer-name": "Doge Dog", + "committer-email": "doge@much.com", + "branch": "master", } var files = []struct { Name, Body string @@ -1303,7 +1306,27 @@ func (s *S) TestPostNewCommit(c *gocheck.C) { c.Assert(err, gocheck.IsNil) reader, writer := io.Pipe() go multipartzip.StreamWriteMultipartForm(params, "zipfile", "scaffold.zip", "muchBOUNDARY", writer, buf) - repository.Retriever = &repository.MockContentRetriever{} + mockRetriever := repository.MockContentRetriever{ + Ref: repository.Ref{ + Ref: "some-random-ref", + Name: "master", + CreatedAt: "Mon Jul 28 10:13:27 2014 -0300", + Committer: &repository.GitUser{ + Name: params["committer-name"], + Email: params["committer-email"], + }, + Author: &repository.GitUser{ + Name: params["author-name"], + Email: params["author-email"], + }, + Subject: params["message"], + Links: &repository.Links{ + ZipArchive: repository.GetArchiveUrl("repo", "master", "zip"), + TarArchive: repository.GetArchiveUrl("repo", "master", "tar.gz"), + }, + }, + } + repository.Retriever = &mockRetriever defer func() { repository.Retriever = nil }() @@ -1313,5 +1336,94 @@ func (s *S) TestPostNewCommit(c *gocheck.C) { recorder := httptest.NewRecorder() Commit(recorder, request) c.Assert(recorder.Code, gocheck.Equals, http.StatusOK) - fmt.Println(recorder.Body) + var data map[string]interface{} + body, err := ioutil.ReadAll(recorder.Body) + err = json.Unmarshal(body, &data) + c.Assert(err, gocheck.IsNil) + expected := map[string]interface{}{ + "ref": "some-random-ref", + "name": "master", + "author": map[string]interface{}{ + "name": "Doge Dog", + "email": "doge@much.com", + "date": "", + }, + "committer": map[string]interface{}{ + "name": "Doge Dog", + "email": "doge@much.com", + "date": "", + }, + "_links": map[string]interface{}{ + "tarArchive": "/repository/repo/archive?ref=master\u0026format=tar.gz", + "zipArchive": "/repository/repo/archive?ref=master\u0026format=zip", + }, + "subject": "Repository scaffold", + "createdAt": "Mon Jul 28 10:13:27 2014 -0300", + } + c.Assert(data, gocheck.DeepEquals, expected) +} + +func (s *S) TestPostNewCommitWithoutBranch(c *gocheck.C) { + url := "/repository/repo/commit/?:name=repo" + params := map[string]string{ + "message": "Repository scaffold", + "author-name": "Doge Dog", + "author-email": "doge@much.com", + "committer-name": "Doge Dog", + "committer-email": "doge@much.com", + } + var files = []struct { + Name, Body string + }{ + {"doge.txt", "Much doge"}, + {"much.txt", "Much mucho"}, + {"WOW/WOW.WOW", "WOW\nWOW"}, + } + buf, err := multipartzip.CreateZipBuffer(files) + c.Assert(err, gocheck.IsNil) + reader, writer := io.Pipe() + go multipartzip.StreamWriteMultipartForm(params, "zipfile", "scaffold.zip", "muchBOUNDARY", writer, buf) + repository.Retriever = &repository.MockContentRetriever{} + defer func() { + repository.Retriever = nil + }() + request, err := http.NewRequest("POST", url, reader) + request.Header.Set("Content-Type", "multipart/form-data;boundary=muchBOUNDARY") + c.Assert(err, gocheck.IsNil) + recorder := httptest.NewRecorder() + Commit(recorder, request) + c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest) +} + +func (s *S) TestPostNewCommitWithEmptyBranch(c *gocheck.C) { + url := "/repository/repo/commit/?:name=repo" + params := map[string]string{ + "message": "Repository scaffold", + "author-name": "Doge Dog", + "author-email": "doge@much.com", + "committer-name": "Doge Dog", + "committer-email": "doge@much.com", + "branch": "", + } + var files = []struct { + Name, Body string + }{ + {"doge.txt", "Much doge"}, + {"much.txt", "Much mucho"}, + {"WOW/WOW.WOW", "WOW\nWOW"}, + } + buf, err := multipartzip.CreateZipBuffer(files) + c.Assert(err, gocheck.IsNil) + reader, writer := io.Pipe() + go multipartzip.StreamWriteMultipartForm(params, "zipfile", "scaffold.zip", "muchBOUNDARY", writer, buf) + repository.Retriever = &repository.MockContentRetriever{} + defer func() { + repository.Retriever = nil + }() + request, err := http.NewRequest("POST", url, reader) + request.Header.Set("Content-Type", "multipart/form-data;boundary=muchBOUNDARY") + c.Assert(err, gocheck.IsNil) + recorder := httptest.NewRecorder() + Commit(recorder, request) + c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest) }