Skip to content

Commit

Permalink
Merge pull request #380 from snyk/feat/populate-api-owners
Browse files Browse the repository at this point in the history
feat: populate api owners in operation extensions
  • Loading branch information
tinygrasshopper authored Sep 24, 2024
2 parents e683947 + fd98f7f commit c2e29c1
Show file tree
Hide file tree
Showing 22 changed files with 175 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ require (
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
github.com/hairyhenderson/go-codeowners v0.5.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/invopop/yaml v0.3.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ github.com/googleapis/gax-go/v2 v2.12.4/go.mod h1:KYEYLorsnIGDi/rPC8b5TdlB9kbKoF
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hairyhenderson/go-codeowners v0.5.0 h1:dpQB+hVHiRc2VVvc2BHxkuM+tmu9Qej/as3apqUbsWc=
github.com/hairyhenderson/go-codeowners v0.5.0/go.mod h1:R3uW1OQXEj2Gu6/OvZ7bt6hr0qdkLvUWPiqNaWnexpo=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down
17 changes: 17 additions & 0 deletions internal/simplebuild/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"time"

"github.com/getkin/kin-openapi/openapi3"
"github.com/hairyhenderson/go-codeowners"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
Expand Down Expand Up @@ -84,6 +86,9 @@ func Build(
return err
}

if doc.Doc.Extensions == nil {
doc.Doc.Extensions = make(map[string]interface{})
}
doc.Doc.Extensions[vervet.ExtSnykApiVersion] = doc.VersionDate.Format(time.DateOnly)

refResolver := NewRefResolver()
Expand Down Expand Up @@ -205,6 +210,14 @@ func (ops Operations) VersionDates() []time.Time {

func LoadPaths(ctx context.Context, api *config.API) (Operations, error) {
operations := map[OpKey]VersionSet{}
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
ownerFinder, err := codeowners.FromFile(cwd)
if err != nil {
return nil, err
}

for _, resource := range api.Resources {
paths, err := ResourceSpecFiles(resource)
Expand Down Expand Up @@ -242,6 +255,10 @@ func LoadPaths(ctx context.Context, api *config.API) (Operations, error) {
for _, pathName := range doc.T.Paths.InMatchingOrder() {
pathDef := doc.T.Paths.Value(pathName)
for opName, opDef := range pathDef.Operations() {
if opDef.Extensions == nil {
opDef.Extensions = make(map[string]interface{})
}
opDef.Extensions[vervet.ExtSnykApiOwner] = ownerFinder.Owners(path)
k := OpKey{
Path: pathName,
Method: opName,
Expand Down
16 changes: 15 additions & 1 deletion resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/bmatcuk/doublestar/v4"
"github.com/getkin/kin-openapi/openapi3"
"github.com/hairyhenderson/go-codeowners"
"golang.org/x/exp/maps"
)

Expand All @@ -33,6 +35,10 @@ const (
// overall version of the compiled spec at the document level.
ExtSnykApiVersion = "x-snyk-api-version"

// ExtSnykApiOwner is used to annotate an operation in a compiled OpenAPI spec
// with the owners of the operation. This is useful to get to the owning github team.
ExtSnykApiOwner = "x-snyk-api-owners"

// ExtSnykApiReleases is used to annotate a path in a compiled OpenAPI spec
// with all the release versions containing a change in the path info. This
// is useful for navigating changes in a particular path across versions.
Expand Down Expand Up @@ -208,7 +214,14 @@ func LoadResourceVersionsFileset(specYamls []string) (*ResourceVersions, error)
path, operation string
}
opReleases := map[operationKey]VersionSlice{}

cwd, err := os.Getwd()
if err != nil {
return nil, err
}
ownerFinder, err := codeowners.FromFile(cwd)
if err != nil {
return nil, err
}
for i := range specYamls {
specYamls[i], err = filepath.Abs(specYamls[i])
if err != nil {
Expand Down Expand Up @@ -238,6 +251,7 @@ func LoadResourceVersionsFileset(specYamls []string) (*ResourceVersions, error)
op.Extensions = make(map[string]any)
}
op.Extensions[ExtSnykApiVersion] = rc.Version.String()
op.Extensions[ExtSnykApiOwner] = ownerFinder.Owners(specYamls[i])
opKey := operationKey{path, opName}
opReleases[opKey] = append(opReleases[opKey], rc.Version)
}
Expand Down
3 changes: 3 additions & 0 deletions testdata/output/2021-06-01~experimental/spec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions testdata/output/2021-06-01~experimental/spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions testdata/output/2021-06-04~experimental/spec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions testdata/output/2021-06-04~experimental/spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions testdata/output/2021-06-07~experimental/spec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions testdata/output/2021-06-07~experimental/spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions testdata/output/2021-06-13~beta/spec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions testdata/output/2021-06-13~beta/spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions testdata/output/2021-06-13~experimental/spec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions testdata/output/2021-06-13~experimental/spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions testdata/output/2021-08-20~experimental/spec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions testdata/output/2021-08-20~experimental/spec.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c2e29c1

Please sign in to comment.