From 388242f7969658df2db5b689245c0803eda4f452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Andr=C3=A9?= Date: Mon, 11 Sep 2023 16:00:38 +0200 Subject: [PATCH] Add ability to ignore packages By passing the `--ignore` flag to go-apidiff it is now possible to ignore incompatible changes in a list of packages. This flag can be specified multiple times. This is passed via the `ignore-list` input parameter in the github action and must be specified as a comma-separated list. Fixes #35 --- README.md | 5 +++++ action.yml | 6 +++++- main.go | 1 + pkg/diff/run.go | 18 ++++++++++++++---- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 50be327..a778f3d 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ Version of go-apidiff to use (default: `latest`) Compare exported API differences in the imports of the repo (default: `false`) +#### `ignore-list` + +Comma-separated list of packages prefixes to ignore (default: `''`) + #### `print-compatible` Print compatible API changes (default: `true`) @@ -80,6 +84,7 @@ Usage: Flags: --compare-imports Compare exported API differences of the imports in the repo. -h, --help help for go-apidiff + -i, --ignore strings Ignore packages starting with prefix. Can be repeated. --print-compatible Print compatible API changes --repo-path string Path to root of git repository to compare (default "/home/myuser/myproject") ``` diff --git a/action.yml b/action.yml index e03019a..4674d6f 100644 --- a/action.yml +++ b/action.yml @@ -24,6 +24,10 @@ inputs: description: 'Path to root of git repository to compare' required: false default: '.' + ignore-list: + description: 'Comma-separated list of packages prefixes to ignore' + required: false + default: '' outputs: semver-type: description: "Returns the type (patch, minor, major) of the sementic version that would be required if producing a release." @@ -48,7 +52,7 @@ runs: set -x GOPATH=$(go env GOPATH) set +e - OUTPUT=$($GOPATH/bin/go-apidiff ${{ inputs.base-ref }} --compare-imports=${{ inputs.compare-imports }} --print-compatible=${{ inputs.print-compatible }} --repo-path=${{ inputs.repo-path }}) + OUTPUT=$($GOPATH/bin/go-apidiff ${{ inputs.base-ref }} --compare-imports=${{ inputs.compare-imports }} --print-compatible=${{ inputs.print-compatible }} --repo-path=${{ inputs.repo-path }} --ignore=${{ inputs.ignore-list }} ) GOAPIDIFF_RC=$? set -e if [ $GOAPIDIFF_RC -eq 0 ]; then diff --git a/main.go b/main.go index 7fb5e3c..19c2c59 100644 --- a/main.go +++ b/main.go @@ -93,6 +93,7 @@ and HEAD is used for newCommit."`, cmd.Flags().StringVar(&opts.RepoPath, "repo-path", cwd, "Path to root of git repository to compare") cmd.Flags().BoolVar(&opts.CompareImports, "compare-imports", false, "Compare exported API differences of the imports in the repo. ") cmd.Flags().BoolVar(&printCompatible, "print-compatible", false, "Print compatible API changes") + cmd.Flags().StringSliceVarP(&opts.IgnoreList, "ignore", "i", []string{}, "Ignore packages starting with prefix. Can be repeated.") return cmd } diff --git a/pkg/diff/run.go b/pkg/diff/run.go index 0afc8b9..1279345 100644 --- a/pkg/diff/run.go +++ b/pkg/diff/run.go @@ -37,6 +37,7 @@ type Options struct { OldCommit string NewCommit string CompareImports bool + IgnoreList []string } func Run(opts Options) (*Diff, error) { @@ -97,12 +98,12 @@ func Run(opts Options) (*Diff, error) { } }() - selfOld, importsOld, err := getPackages(*wt, *oldHash) + selfOld, importsOld, err := getPackages(*wt, *oldHash, opts.IgnoreList) if err != nil { return nil, fmt.Errorf("failed to get packages from old commit %q (%s): %w", opts.OldCommit, oldHash, err) } - selfNew, importsNew, err := getPackages(*wt, *newHash) + selfNew, importsNew, err := getPackages(*wt, *newHash, opts.IgnoreList) if err != nil { return nil, fmt.Errorf("failed to get packages from new commit %q (%s): %w", opts.NewCommit, newHash, err) } @@ -192,7 +193,16 @@ func getHashes(repo *git.Repository, oldRev, newRev plumbing.Revision) (*plumbin return oldCommitHash, newCommitHash, nil } -func getPackages(wt git.Worktree, hash plumbing.Hash) (map[string]*packages.Package, map[string]*packages.Package, error) { +func ignorePackage(pkg string, ignoreList []string) bool { + for _, ignore := range ignoreList { + if strings.HasPrefix(pkg, ignore) { + return true + } + } + return false +} + +func getPackages(wt git.Worktree, hash plumbing.Hash, ignoreList []string) (map[string]*packages.Package, map[string]*packages.Package, error) { if err := wt.Checkout(&git.CheckoutOptions{Hash: hash, Force: true}); err != nil { return nil, nil, err } @@ -222,7 +232,7 @@ func getPackages(wt git.Worktree, hash plumbing.Hash) (map[string]*packages.Pack importPkgs := make(map[string]*packages.Package) for _, pkg := range pkgs { // skip internal packages since they do not contain public APIs - if strings.HasSuffix(pkg.PkgPath, "/internal") || strings.Contains(pkg.PkgPath, "/internal/") { + if strings.HasSuffix(pkg.PkgPath, "/internal") || strings.Contains(pkg.PkgPath, "/internal/") || ignorePackage(pkg.PkgPath, ignoreList) { continue } selfPkgs[pkg.PkgPath] = pkg