Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to ignore packages #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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