golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/task/sync_private.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package task 6 7 import ( 8 "fmt" 9 10 wf "golang.org/x/build/internal/workflow" 11 ) 12 13 type PrivateMasterSyncTask struct { 14 Git *Git 15 PrivateGerritURL string 16 Ref string 17 } 18 19 func (t *PrivateMasterSyncTask) NewDefinition() *wf.Definition { 20 wd := wf.New() 21 // We use a Task, instead of an Action, even though we don't actually want 22 // to return any result, because nothing depends on the Action, and if we 23 // use an Action the definition will tell us we don't reference it anywhere 24 // and say it should be deleted. 25 synced := wf.Task0(wd, "Sync go-private master to public", func(ctx *wf.TaskContext) (string, error) { 26 repo, err := t.Git.Clone(ctx, t.PrivateGerritURL) 27 if err != nil { 28 return "", err 29 } 30 31 // NOTE: we assume this is generally safe in the case of a race between 32 // submitting a patch and resetting the master branch due to the ordering 33 // of operations at Gerrit. If the submit wins, we reset the master 34 // branch, and the submitted commit is orphaned, which is the expected 35 // behavior anyway. If the reset wins, the submission will either be 36 // cherry-picked onto the new base, which should either succeed, or fail 37 // due to a merge conflict, or Gerrit will reject the submission because 38 // something changed underneath it. Either case seems fine. 39 if _, err := repo.RunCommand(ctx.Context, "push", "--force", "origin", fmt.Sprintf("origin/%s:refs/heads/master", t.Ref)); err != nil { 40 return "", err 41 } 42 43 return "finished", nil 44 }) 45 wf.Output(wd, fmt.Sprintf("Reset master to %s", t.Ref), synced) 46 return wd 47 }