| 1 |
// Copyright 2023 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/json" |
| 20 |
"flag" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"os/exec" |
| 24 |
"strings" |
| 25 |
|
| 26 |
"cloud.google.com/go/internal/actions/logg" |
| 27 |
) |
| 28 |
|
| 29 |
var ( |
| 30 |
dir = flag.String("dir", "", "the root directory to evaluate") |
| 31 |
format = flag.String("format", "plain", "output format, one of [plain|github], defaults to 'plain'") |
| 32 |
ghVarName = flag.String("gh-var", "submodules", "github format's variable name to set output for, defaults to 'submodules'.") |
| 33 |
base = flag.String("base", "origin/main", "the base ref to compare to, defaults to 'origin/main'") |
| 34 |
) |
| 35 |
|
| 36 |
func main() { |
| 37 |
flag.BoolVar(&logg.Quiet, "q", false, "quiet mode, minimal logging") |
| 38 |
flag.Parse() |
| 39 |
rootDir, err := os.Getwd() |
| 40 |
if err != nil { |
| 41 |
logg.Fatal(err) |
| 42 |
} |
| 43 |
if *dir != "" { |
| 44 |
rootDir = *dir |
| 45 |
} |
| 46 |
logg.Printf("Root dir: %q", rootDir) |
| 47 |
|
| 48 |
submodulesDirs, err := modDirs(rootDir) |
| 49 |
if err != nil { |
| 50 |
logg.Fatal(err) |
| 51 |
} |
| 52 |
|
| 53 |
changes, err := gitFilesChanges(rootDir) |
| 54 |
if err != nil { |
| 55 |
logg.Fatalf("unable to get files changed: %v", err) |
| 56 |
} |
| 57 |
|
| 58 |
modulesSeen := map[string]bool{} |
| 59 |
updatedSubmoduleDirs := []string{} |
| 60 |
for _, change := range changes { |
| 61 |
if strings.HasPrefix(change, "internal") { |
| 62 |
continue |
| 63 |
} |
| 64 |
submodDir, ok := owner(change, submodulesDirs) |
| 65 |
if !ok { |
| 66 |
logg.Printf("no module for: %s", change) |
| 67 |
continue |
| 68 |
} |
| 69 |
if _, seen := modulesSeen[submodDir]; !seen { |
| 70 |
logg.Printf("changes in submodule: %s", submodDir) |
| 71 |
updatedSubmoduleDirs = append(updatedSubmoduleDirs, submodDir) |
| 72 |
modulesSeen[submodDir] = true |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
output(updatedSubmoduleDirs) |
| 77 |
} |
| 78 |
|
| 79 |
func output(s []string) error { |
| 80 |
switch *format { |
| 81 |
case "github": |
| 82 |
b, err := json.Marshal(s) |
| 83 |
if err != nil { |
| 84 |
logg.Fatalf("unable to marshal submodules: %v", err) |
| 85 |
} |
| 86 |
fmt.Printf("::set-output name=%s::%s", *ghVarName, b) |
| 87 |
case "plain": |
| 88 |
fallthrough |
| 89 |
default: |
| 90 |
fmt.Println(strings.Join(s, "\n")) |
| 91 |
} |
| 92 |
return nil |
| 93 |
} |
| 94 |
|
| 95 |
func owner(file string, submoduleDirs []string) (string, bool) { |
| 96 |
submod := "" |
| 97 |
for _, mod := range submoduleDirs { |
| 98 |
if strings.HasPrefix(file, mod) && len(mod) > len(submod) { |
| 99 |
submod = mod |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return submod, submod != "" |
| 104 |
} |
| 105 |
|
| 106 |
func modDirs(dir string) (submodulesDirs []string, err error) { |
| 107 |
c := exec.Command("go", "list", "-m", "-f", "{{.Dir}}") |
| 108 |
c.Dir = dir |
| 109 |
b, err := c.Output() |
| 110 |
if err != nil { |
| 111 |
return submodulesDirs, err |
| 112 |
} |
| 113 |
// Skip the root mod |
| 114 |
list := strings.Split(strings.TrimSpace(string(b)), "\n")[1:] |
| 115 |
|
| 116 |
submodulesDirs = []string{} |
| 117 |
for _, modPath := range list { |
| 118 |
// Skip non-submodule or internal submodules. |
| 119 |
if strings.Contains(modPath, "internal") { |
| 120 |
continue |
| 121 |
} |
| 122 |
logg.Printf("found module: %s", modPath) |
| 123 |
modPath = strings.TrimPrefix(modPath, dir+"/") |
| 124 |
submodulesDirs = append(submodulesDirs, modPath) |
| 125 |
} |
| 126 |
|
| 127 |
return submodulesDirs, nil |
| 128 |
} |
| 129 |
|
| 130 |
func gitFilesChanges(dir string) ([]string, error) { |
| 131 |
c := exec.Command("git", "diff", "--name-only", *base) |
| 132 |
c.Dir = dir |
| 133 |
b, err := c.Output() |
| 134 |
if err != nil { |
| 135 |
return nil, err |
| 136 |
} |
| 137 |
b = bytes.TrimSpace(b) |
| 138 |
logg.Printf("Files changed:\n%s", b) |
| 139 |
return strings.Split(string(b), "\n"), nil |
| 140 |
} |