Skip to content

Commit

Permalink
Add ability to ignore packages
Browse files Browse the repository at this point in the history
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 joelanford#35
  • Loading branch information
mandre committed Sep 11, 2023
1 parent 0511785 commit 388242f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down Expand Up @@ -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")
```
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 14 additions & 4 deletions pkg/diff/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Options struct {
OldCommit string
NewCommit string
CompareImports bool
IgnoreList []string
}

func Run(opts Options) (*Diff, error) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 388242f

Please sign in to comment.