| 1 |
// Copyright 2023 Google LLC |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style license that can be |
| 4 |
// found in the LICENSE file. |
| 5 |
// |
| 6 |
// This task driver runs various Go linters via Bazel, and fails on any errors or Git diffs. |
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"path/filepath" |
| 14 |
"strings" |
| 15 |
|
| 16 |
sk_exec "go.skia.org/infra/go/exec" |
| 17 |
"go.skia.org/infra/task_driver/go/lib/bazel" |
| 18 |
"go.skia.org/infra/task_driver/go/lib/os_steps" |
| 19 |
"go.skia.org/infra/task_driver/go/td" |
| 20 |
"go.skia.org/skia/infra/bots/task_drivers/common" |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
// Required properties for this task. |
| 25 |
// We want the cache to be on a bigger disk than default. The root disk, where the home |
| 26 |
// directory (and default Bazel cache) lives, is only 15 GB on our GCE VMs. |
| 27 |
gitPath = flag.String("git_path", "", "Location of git binary to use for diffs.") |
| 28 |
projectId = flag.String("project_id", "", "ID of the Google Cloud project.") |
| 29 |
taskId = flag.String("task_id", "", "ID of this task.") |
| 30 |
taskName = flag.String("task_name", "", "Name of the task.") |
| 31 |
workdir = flag.String("workdir", ".", "Working directory, the root directory of a full Skia checkout") |
| 32 |
// Optional flags. |
| 33 |
local = flag.Bool("local", false, "True if running locally (as opposed to on the CI/CQ)") |
| 34 |
output = flag.String("o", "", "If provided, dump a JSON blob of step data to the given file. Prints to stdout if '-' is given.") |
| 35 |
) |
| 36 |
|
| 37 |
func main() { |
| 38 |
bazelFlags := common.MakeBazelFlags(common.MakeBazelFlagsOpts{ |
| 39 |
AdditionalArgs: true, |
| 40 |
}) |
| 41 |
|
| 42 |
// StartRun calls flag.Parse() |
| 43 |
ctx := td.StartRun(projectId, taskId, taskName, output, local) |
| 44 |
defer td.EndRun(ctx) |
| 45 |
|
| 46 |
bazelFlags.Validate(ctx) |
| 47 |
|
| 48 |
if *gitPath == "" { |
| 49 |
td.Fatal(ctx, fmt.Errorf("--git_path is required")) |
| 50 |
} |
| 51 |
|
| 52 |
wd, err := os_steps.Abs(ctx, *workdir) |
| 53 |
if err != nil { |
| 54 |
td.Fatal(ctx, err) |
| 55 |
} |
| 56 |
absGit, err := os_steps.Abs(ctx, *gitPath) |
| 57 |
if err != nil { |
| 58 |
td.Fatal(ctx, err) |
| 59 |
} |
| 60 |
if _, err := os_steps.Stat(ctx, absGit); err != nil { |
| 61 |
fmt.Printf("Cannot stat git binary %s\n", absGit) |
| 62 |
td.Fatal(ctx, err) |
| 63 |
} |
| 64 |
skiaPath := filepath.Join(wd, "skia") |
| 65 |
|
| 66 |
// When running on the CI, there is not a git checkout here, so we make a temp one. |
| 67 |
if !*local { |
| 68 |
if err := gitInit(ctx, absGit, skiaPath); err != nil { |
| 69 |
td.Fatal(ctx, err) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
opts := bazel.BazelOptions{ |
| 74 |
CachePath: *bazelFlags.CacheDir, |
| 75 |
} |
| 76 |
if err := bazel.EnsureBazelRCFile(ctx, opts); err != nil { |
| 77 |
td.Fatal(ctx, err) |
| 78 |
} |
| 79 |
|
| 80 |
if err := bazelRun(ctx, skiaPath, "//:go", append(*bazelFlags.AdditionalArgs, "--", "fmt", "./...")...); err != nil { |
| 81 |
td.Fatal(ctx, err) |
| 82 |
} |
| 83 |
|
| 84 |
if err := checkGitDiff(ctx, absGit, skiaPath); err != nil { |
| 85 |
td.Fatal(ctx, err) |
| 86 |
} |
| 87 |
|
| 88 |
if !*local { |
| 89 |
if err := common.BazelCleanIfLowDiskSpace(ctx, *bazelFlags.CacheDir, skiaPath, "bazelisk"); err != nil { |
| 90 |
td.Fatal(ctx, err) |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// bazelRun runs the given Bazel label from the Skia path with any given args using Bazelisk. |
| 96 |
func bazelRun(ctx context.Context, skiaPath, label string, args ...string) error { |
| 97 |
return td.Do(ctx, td.Props("bazel run "+label), func(ctx context.Context) error { |
| 98 |
runCmd := &sk_exec.Command{ |
| 99 |
Name: "bazelisk", |
| 100 |
Args: append([]string{"run", label}, args...), |
| 101 |
InheritEnv: true, // Need to make sure bazelisk is on the path |
| 102 |
Dir: skiaPath, |
| 103 |
LogStdout: true, |
| 104 |
LogStderr: true, |
| 105 |
} |
| 106 |
_, err := sk_exec.RunCommand(ctx, runCmd) |
| 107 |
if err != nil { |
| 108 |
return err |
| 109 |
} |
| 110 |
return nil |
| 111 |
}) |
| 112 |
} |
| 113 |
|
| 114 |
// gitInit creates a temporary git repository with all files in the Skia path. This allows the later |
| 115 |
// git diff call to work properly. This is necessary because our current Swarming setup does not |
| 116 |
// include the .git folder when copying down files. |
| 117 |
func gitInit(ctx context.Context, gitPath, skiaPath string) error { |
| 118 |
step := fmt.Sprintf("Setting git baseline in %s", skiaPath) |
| 119 |
err := td.Do(ctx, td.Props(step), func(ctx context.Context) error { |
| 120 |
initCmd := &sk_exec.Command{ |
| 121 |
Name: gitPath, |
| 122 |
Args: []string{"init"}, |
| 123 |
InheritEnv: false, |
| 124 |
Dir: skiaPath, |
| 125 |
LogStdout: true, |
| 126 |
LogStderr: true, |
| 127 |
} |
| 128 |
if _, err := sk_exec.RunCommand(ctx, initCmd); err != nil { |
| 129 |
return err |
| 130 |
} |
| 131 |
addCmd := &sk_exec.Command{ |
| 132 |
Name: gitPath, |
| 133 |
Args: []string{"add", "."}, |
| 134 |
InheritEnv: false, |
| 135 |
Dir: skiaPath, |
| 136 |
LogStdout: true, |
| 137 |
LogStderr: true, |
| 138 |
} |
| 139 |
if _, err := sk_exec.RunCommand(ctx, addCmd); err != nil { |
| 140 |
return err |
| 141 |
} |
| 142 |
commitCmd := &sk_exec.Command{ |
| 143 |
Name: gitPath, |
| 144 |
Args: []string{"commit", "-m", "baseline commit"}, |
| 145 |
InheritEnv: false, |
| 146 |
Dir: skiaPath, |
| 147 |
LogStdout: true, |
| 148 |
LogStderr: true, |
| 149 |
} |
| 150 |
if _, err := sk_exec.RunCommand(ctx, commitCmd); err != nil { |
| 151 |
return err |
| 152 |
} |
| 153 |
return nil |
| 154 |
}) |
| 155 |
return err |
| 156 |
} |
| 157 |
|
| 158 |
// checkGitDiff runs git diff and returns error if the diff is non-empty. |
| 159 |
func checkGitDiff(ctx context.Context, gitPath, skiaPath string) error { |
| 160 |
step := fmt.Sprintf("git diff %s", skiaPath) |
| 161 |
return td.Do(ctx, td.Props(step), func(ctx context.Context) error { |
| 162 |
runCmd := &sk_exec.Command{ |
| 163 |
Name: gitPath, |
| 164 |
Args: []string{"diff", "--no-ext-diff"}, |
| 165 |
InheritEnv: false, |
| 166 |
Dir: skiaPath, |
| 167 |
LogStdout: true, |
| 168 |
LogStderr: true, |
| 169 |
} |
| 170 |
rv, err := sk_exec.RunCommand(ctx, runCmd) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
if strings.TrimSpace(rv) != "" { |
| 175 |
return fmt.Errorf("Non-empty diff:\n" + rv) |
| 176 |
} |
| 177 |
return nil |
| 178 |
}) |
| 179 |
} |