From ae370718c458f6f9ea5d32eb1d98d240320b1b10 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sun, 28 Apr 2019 00:52:51 +0000 Subject: [PATCH] Allow the caller to not update the published branch This change adds a way for the user to not update the published branch upon updating a problem. --- cmd/omegaup-update-problem/main.go | 5 ++ cmd/omegaup-update-problem/main_test.go | 4 ++ ziphandler.go | 74 +++++++++++++------------ 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/cmd/omegaup-update-problem/main.go b/cmd/omegaup-update-problem/main.go index c0bf30e..dcc8532 100644 --- a/cmd/omegaup-update-problem/main.go +++ b/cmd/omegaup-update-problem/main.go @@ -33,6 +33,7 @@ var ( zipPath = flag.String("zip-path", "", "Path of the .zip file") mergeStrategyName = flag.String("merge-strategy", "theirs", "Merge strategy to use. Valid values are 'ours', 'theirs', 'statement-ours', and 'recursive-theirs'") acceptsSubmissions = flag.Bool("accepts-submissions", true, "Problem accepts submissions") + updatePublished = flag.Bool("update-published", false, "Update the published branch") libinteractivePath = flag.String("libinteractive-path", "/usr/share/java/libinteractive.jar", "Path of libinteractive.jar") // Flags that are used when updating a repository with a []BlobUpdate. @@ -60,6 +61,7 @@ func commitZipFile( problemSettings *common.ProblemSettings, zipMergeStrategy gitserver.ZipMergeStrategy, acceptsSubmissions bool, + updatePublished bool, log log15.Logger, ) (*gitserver.UpdateResult, error) { ctx := request.NewContext(context.Background(), &base.NoOpMetrics{}) @@ -91,6 +93,7 @@ func commitZipFile( problemSettings, zipMergeStrategy, acceptsSubmissions, + updatePublished, protocol, log, ) @@ -435,6 +438,7 @@ func main() { problemSettings, zipMergeStrategy, *acceptsSubmissions, + *updatePublished, log, ) if err != nil { @@ -504,6 +508,7 @@ func main() { problemSettings, gitserver.ZipMergeStrategyOurs, *acceptsSubmissions, + *updatePublished, log, ) if err != nil { diff --git a/cmd/omegaup-update-problem/main_test.go b/cmd/omegaup-update-problem/main_test.go index 01c47af..f734a6d 100644 --- a/cmd/omegaup-update-problem/main_test.go +++ b/cmd/omegaup-update-problem/main_test.go @@ -63,6 +63,7 @@ func getTreeOid(t *testing.T, extraFileContents map[string]io.Reader, log log15. nil, gitserver.ZipMergeStrategyTheirs, true, + true, log, ); err != nil { t.Fatalf("Failed to commit zip: %v", err) @@ -236,6 +237,7 @@ func TestProblemUpdateZip(t *testing.T) { nil, gitserver.ZipMergeStrategyTheirs, true, + true, log, ) if err != nil { @@ -291,6 +293,7 @@ func TestProblemUpdateZip(t *testing.T) { nil, gitserver.ZipMergeStrategyTheirs, true, + true, log, ) if err != nil { @@ -367,6 +370,7 @@ func TestProblemUpdateBlobs(t *testing.T) { nil, gitserver.ZipMergeStrategyTheirs, true, + true, log, ) if err != nil { diff --git a/ziphandler.go b/ziphandler.go index 044291c..312155a 100644 --- a/ziphandler.go +++ b/ziphandler.go @@ -1223,6 +1223,7 @@ func PushZip( problemSettings *common.ProblemSettings, zipMergeStrategy ZipMergeStrategy, acceptsSubmissions bool, + updatePublished bool, protocol *githttp.GitProtocol, log log15.Logger, ) (*UpdateResult, error) { @@ -1315,45 +1316,47 @@ func PushZip( } // Update the published ref. - publishedUpdatedRef := githttp.UpdatedRef{ - Name: "refs/heads/published", - } - masterNewOid := &git.Oid{} - for _, ref := range updatedRefs { - if ref.Name == "refs/heads/master" { - publishedUpdatedRef.To = ref.To - publishedUpdatedRef.ToTree = ref.ToTree - masterNewOid, err = git.NewOid(ref.To) - if err != nil { + if updatePublished { + publishedUpdatedRef := githttp.UpdatedRef{ + Name: "refs/heads/published", + } + masterNewOid := &git.Oid{} + for _, ref := range updatedRefs { + if ref.Name == "refs/heads/master" { + publishedUpdatedRef.To = ref.To + publishedUpdatedRef.ToTree = ref.ToTree + masterNewOid, err = git.NewOid(ref.To) + if err != nil { + return nil, errors.Wrap( + err, + "failed to parse the updated ID", + ) + } + break + } + } + if masterNewOid.IsZero() { + log.Error("could not find the updated reference for master") + } else { + if publishedBranch, err := repo.LookupBranch("published", git.BranchLocal); err == nil { + publishedUpdatedRef.From = publishedBranch.Target().String() + publishedBranch.Free() + } + if ref, err := repo.References.Create( + publishedUpdatedRef.Name, + masterNewOid, + true, + "", + ); err != nil { return nil, errors.Wrap( err, - "failed to parse the updated ID", + "failed to update the published ref", ) + } else { + ref.Free() } - break - } - } - if masterNewOid.IsZero() { - log.Error("could not find the updated reference for master") - } else { - if publishedBranch, err := repo.LookupBranch("published", git.BranchLocal); err == nil { - publishedUpdatedRef.From = publishedBranch.Target().String() - publishedBranch.Free() - } - if ref, err := repo.References.Create( - publishedUpdatedRef.Name, - masterNewOid, - true, - "", - ); err != nil { - return nil, errors.Wrap( - err, - "failed to update the published ref", - ) - } else { - ref.Free() + updatedRefs = append(updatedRefs, publishedUpdatedRef) } - updatedRefs = append(updatedRefs, publishedUpdatedRef) } return &UpdateResult{ @@ -1443,6 +1446,8 @@ func (h *zipUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } acceptsSubmissions := (paramValue("acceptsSubmissions") == "" || paramValue("acceptsSubmissions") == "true") + updatePublished := (paramValue("updatePublished") == "" || + paramValue("updatePublished") == "true") zipMergeStrategy, err := ParseZipMergeStrategy(paramValue("mergeStrategy")) if err != nil { h.log.Error("invalid merge strategy", "mergeStrategy", paramValue("mergeStrategy")) @@ -1569,6 +1574,7 @@ func (h *zipUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { problemSettings, zipMergeStrategy, acceptsSubmissions, + updatePublished, h.protocol, h.log, )