forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ci): add third party dep check (googleapis#9343)
- Loading branch information
Showing
5 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
name: third_party_check | ||
on: | ||
pull_request: | ||
paths: | ||
- '**/go.mod' | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
changed_gomods: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 2 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version: 1.21.x | ||
- name: Find modified go.mod files | ||
id: modfiles | ||
run: | | ||
dirs=$(go run ./internal/actions/cmd/changefinder -q -internal --base=HEAD~1 --diff-filter=d --path-filter='*go.mod') | ||
if [ -z "$dirs" ] | ||
then | ||
echo "skip=1" >> "$GITHUB_OUTPUT" | ||
echo "No new/modified go.mod files!" | ||
else | ||
for d in $dirs; do list=${list},\"${d}\"; done | ||
echo "mods={\"mods\":[${list#,}]}" >> "$GITHUB_OUTPUT" | ||
echo "skip=" >> "$GITHUB_OUTPUT" | ||
fi | ||
outputs: | ||
mods: ${{ steps.modfiles.outputs.mods }} | ||
skip: ${{ steps.modfiles.outputs.skip }} | ||
check_deps: | ||
needs: changed_gomods | ||
runs-on: ubuntu-latest | ||
if: "!needs.changed_gomods.outputs.skip" | ||
continue-on-error: true | ||
strategy: | ||
matrix: ${{ fromJson(needs.changed_gomods.outputs.mods) }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: go run ./internal/actions/cmd/thirdpartycheck -q -mod ${{ matrix.mods }}/go.mod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path" | ||
"strings" | ||
|
||
"cloud.google.com/go/internal/actions/logg" | ||
"golang.org/x/mod/modfile" | ||
) | ||
|
||
var ( | ||
dir = flag.String("dir", "", "the root directory to evaluate") | ||
mod = flag.String("mod", "", "path to go.mod file relative to root directory") | ||
|
||
// List of allowlist module prefixes. | ||
allowlist = []string{ | ||
// First party deps. | ||
"cloud.google.com/go", | ||
"github.com/google/", | ||
"github.com/googleapis/", | ||
"github.com/golang/", | ||
"google.golang.org/", | ||
"golang.org/", | ||
|
||
// Third party deps (allowed). | ||
"go.opencensus.io", | ||
"go.opentelemetry.io/", | ||
|
||
// Third party deps (temporary exception(s)). | ||
"go.einride.tech/aip", // https://github.com/googleapis/google-cloud-go/issues/9338 | ||
} | ||
) | ||
|
||
func main() { | ||
flag.BoolVar(&logg.Quiet, "q", false, "quiet mode, minimal logging") | ||
flag.Parse() | ||
if *mod == "" { | ||
logg.Fatalf("missing required flag: -mod") | ||
} | ||
|
||
rootDir, err := os.Getwd() | ||
if err != nil { | ||
logg.Fatal(err) | ||
} | ||
if *dir != "" { | ||
rootDir = *dir | ||
} | ||
|
||
modPath := path.Join(rootDir, *mod) | ||
findings, err := check(modPath) | ||
if err != nil { | ||
logg.Fatal(err) | ||
} | ||
|
||
if len(findings) != 0 { | ||
fmt.Println("found disallowed module(s) in direct dependencies:") | ||
fmt.Println() | ||
fmt.Printf("\t%s\n", strings.Join(findings, "\n\t")) | ||
fmt.Println() | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// check reads & parses the specified go.mod, then validates that | ||
// all direct dependencies are part of the allowlist. | ||
func check(file string) ([]string, error) { | ||
data, err := os.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
m, err := modfile.ParseLax(*mod, data, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var disallowed []string | ||
for _, r := range m.Require { | ||
if r.Indirect { | ||
continue | ||
} | ||
var allowed bool | ||
for _, a := range allowlist { | ||
if strings.HasPrefix(r.Mod.Path, a) { | ||
allowed = true | ||
break | ||
} | ||
} | ||
if !allowed { | ||
disallowed = append(disallowed, r.Mod.Path) | ||
} | ||
} | ||
return disallowed, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module cloud.google.com/go/internal/actions | ||
|
||
go 1.19 | ||
|
||
require golang.org/x/mod v0.14.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= | ||
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= |