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

Commit

Permalink
Added commit zip support
Browse files Browse the repository at this point in the history
  • Loading branch information
rfloriano authored and scorphus committed Aug 11, 2014
1 parent 41735de commit b25a010
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 74 deletions.
100 changes: 31 additions & 69 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
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: "[email protected]",
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)
}
122 changes: 117 additions & 5 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"committer": "Barking Doge <[email protected]>",
"message": "Repository scaffold",
"author-name": "Doge Dog",
"author-email": "[email protected]",
"committer-name": "Doge Dog",
"committer-email": "[email protected]",
"branch": "master",
}
var files = []struct {
Name, Body string
Expand All @@ -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
}()
Expand All @@ -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": "[email protected]",
"date": "",
},
"committer": map[string]interface{}{
"name": "Doge Dog",
"email": "[email protected]",
"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": "[email protected]",
"committer-name": "Doge Dog",
"committer-email": "[email protected]",
}
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": "[email protected]",
"committer-name": "Doge Dog",
"committer-email": "[email protected]",
"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)
}

0 comments on commit b25a010

Please sign in to comment.