Skip to content

Commit

Permalink
Allow the caller to not update the published branch
Browse files Browse the repository at this point in the history
This change adds a way for the user to not update the published branch
upon updating a problem.
  • Loading branch information
lhchavez committed Apr 28, 2019
1 parent 4ce9884 commit ae37071
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
5 changes: 5 additions & 0 deletions cmd/omegaup-update-problem/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{})
Expand Down Expand Up @@ -91,6 +93,7 @@ func commitZipFile(
problemSettings,
zipMergeStrategy,
acceptsSubmissions,
updatePublished,
protocol,
log,
)
Expand Down Expand Up @@ -435,6 +438,7 @@ func main() {
problemSettings,
zipMergeStrategy,
*acceptsSubmissions,
*updatePublished,
log,
)
if err != nil {
Expand Down Expand Up @@ -504,6 +508,7 @@ func main() {
problemSettings,
gitserver.ZipMergeStrategyOurs,
*acceptsSubmissions,
*updatePublished,
log,
)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/omegaup-update-problem/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -236,6 +237,7 @@ func TestProblemUpdateZip(t *testing.T) {
nil,
gitserver.ZipMergeStrategyTheirs,
true,
true,
log,
)
if err != nil {
Expand Down Expand Up @@ -291,6 +293,7 @@ func TestProblemUpdateZip(t *testing.T) {
nil,
gitserver.ZipMergeStrategyTheirs,
true,
true,
log,
)
if err != nil {
Expand Down Expand Up @@ -367,6 +370,7 @@ func TestProblemUpdateBlobs(t *testing.T) {
nil,
gitserver.ZipMergeStrategyTheirs,
true,
true,
log,
)
if err != nil {
Expand Down
74 changes: 40 additions & 34 deletions ziphandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,7 @@ func PushZip(
problemSettings *common.ProblemSettings,
zipMergeStrategy ZipMergeStrategy,
acceptsSubmissions bool,
updatePublished bool,
protocol *githttp.GitProtocol,
log log15.Logger,
) (*UpdateResult, error) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -1569,6 +1574,7 @@ func (h *zipUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
problemSettings,
zipMergeStrategy,
acceptsSubmissions,
updatePublished,
h.protocol,
h.log,
)
Expand Down

0 comments on commit ae37071

Please sign in to comment.