golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/task/updateproxytestrepo.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  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	wf "golang.org/x/build/internal/workflow"
    14  	"golang.org/x/mod/modfile"
    15  )
    16  
    17  type UpdateProxyTestRepoTasks struct {
    18  	Git       *Git
    19  	GerritURL string
    20  	Branch    string
    21  }
    22  
    23  func (t *UpdateProxyTestRepoTasks) UpdateProxyTestRepo(ctx *wf.TaskContext, published Published) (string, error) {
    24  	repo, err := t.Git.Clone(ctx, t.GerritURL)
    25  	if err != nil {
    26  		return "", err
    27  	}
    28  
    29  	// Read the file and check if version is higher.
    30  	modFile := filepath.Join(repo.dir, "go.mod")
    31  	contents, err := os.ReadFile(modFile)
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  	f, err := modfile.ParseLax(modFile, contents, nil)
    36  	if err != nil {
    37  		return "", err
    38  	}
    39  	// If the published version is lower than the current go.mod version, don't update.
    40  	// If we could parse the go.mod file, assume we should update.
    41  	if f.Go != nil && compareGoVersions(published.Version, "go"+f.Go.Version) < 0 {
    42  		return "no update", nil
    43  	}
    44  
    45  	version := strings.TrimPrefix(published.Version, "go")
    46  	// Update the go.mod file for the new release.
    47  	if err := os.WriteFile(modFile, []byte(fmt.Sprintf("module test\n\ngo %s\n", version)), 0666); err != nil {
    48  		return "", err
    49  	}
    50  	if _, err := repo.RunCommand(ctx, "commit", "-am", fmt.Sprintf("update go version to %s", version)); err != nil {
    51  		return "", fmt.Errorf("git commit error: %v", err)
    52  	}
    53  	// Force move the tag.
    54  	if _, err := repo.RunCommand(ctx, "tag", "-af", "v1.0.0", "-m", fmt.Sprintf("moving tag to include go version %s", version)); err != nil {
    55  		return "", fmt.Errorf("git tag error: %v", err)
    56  	}
    57  	if _, err := repo.RunCommand(ctx, "push", "--force", "--tags", "origin", t.Branch); err != nil {
    58  		return "", fmt.Errorf("git push --tags error: %v", err)
    59  	}
    60  	return "updated", nil
    61  }