Skip to content

Commit fd98f7f

Browse files
feat: populate api owners in operation extensions
1 parent e683947 commit fd98f7f

File tree

22 files changed

+175
-1
lines changed

22 files changed

+175
-1
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ require (
9595
github.com/google/s2a-go v0.1.7 // indirect
9696
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
9797
github.com/googleapis/gax-go/v2 v2.12.4 // indirect
98+
github.com/hairyhenderson/go-codeowners v0.5.0 // indirect
9899
github.com/hashicorp/hcl v1.0.0 // indirect
99100
github.com/invopop/yaml v0.3.1 // indirect
100101
github.com/jmespath/go-jmespath v0.4.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ github.com/googleapis/gax-go/v2 v2.12.4/go.mod h1:KYEYLorsnIGDi/rPC8b5TdlB9kbKoF
309309
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
310310
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
311311
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
312+
github.com/hairyhenderson/go-codeowners v0.5.0 h1:dpQB+hVHiRc2VVvc2BHxkuM+tmu9Qej/as3apqUbsWc=
313+
github.com/hairyhenderson/go-codeowners v0.5.0/go.mod h1:R3uW1OQXEj2Gu6/OvZ7bt6hr0qdkLvUWPiqNaWnexpo=
312314
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
313315
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
314316
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=

internal/simplebuild/build.go

+17
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9+
"os"
910
"path/filepath"
1011
"slices"
1112
"sort"
1213
"strings"
1314
"time"
1415

1516
"github.com/getkin/kin-openapi/openapi3"
17+
"github.com/hairyhenderson/go-codeowners"
1618
"github.com/tufin/oasdiff/checker"
1719
"github.com/tufin/oasdiff/diff"
1820
"github.com/tufin/oasdiff/load"
@@ -84,6 +86,9 @@ func Build(
8486
return err
8587
}
8688

89+
if doc.Doc.Extensions == nil {
90+
doc.Doc.Extensions = make(map[string]interface{})
91+
}
8792
doc.Doc.Extensions[vervet.ExtSnykApiVersion] = doc.VersionDate.Format(time.DateOnly)
8893

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

206211
func LoadPaths(ctx context.Context, api *config.API) (Operations, error) {
207212
operations := map[OpKey]VersionSet{}
213+
cwd, err := os.Getwd()
214+
if err != nil {
215+
return nil, err
216+
}
217+
ownerFinder, err := codeowners.FromFile(cwd)
218+
if err != nil {
219+
return nil, err
220+
}
208221

209222
for _, resource := range api.Resources {
210223
paths, err := ResourceSpecFiles(resource)
@@ -242,6 +255,10 @@ func LoadPaths(ctx context.Context, api *config.API) (Operations, error) {
242255
for _, pathName := range doc.T.Paths.InMatchingOrder() {
243256
pathDef := doc.T.Paths.Value(pathName)
244257
for opName, opDef := range pathDef.Operations() {
258+
if opDef.Extensions == nil {
259+
opDef.Extensions = make(map[string]interface{})
260+
}
261+
opDef.Extensions[vervet.ExtSnykApiOwner] = ownerFinder.Owners(path)
245262
k := OpKey{
246263
Path: pathName,
247264
Method: opName,

resource.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"os"
89
"path/filepath"
910
"strings"
1011
"time"
1112

1213
"github.com/bmatcuk/doublestar/v4"
1314
"github.com/getkin/kin-openapi/openapi3"
15+
"github.com/hairyhenderson/go-codeowners"
1416
"golang.org/x/exp/maps"
1517
)
1618

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

38+
// ExtSnykApiOwner is used to annotate an operation in a compiled OpenAPI spec
39+
// with the owners of the operation. This is useful to get to the owning github team.
40+
ExtSnykApiOwner = "x-snyk-api-owners"
41+
3642
// ExtSnykApiReleases is used to annotate a path in a compiled OpenAPI spec
3743
// with all the release versions containing a change in the path info. This
3844
// is useful for navigating changes in a particular path across versions.
@@ -208,7 +214,14 @@ func LoadResourceVersionsFileset(specYamls []string) (*ResourceVersions, error)
208214
path, operation string
209215
}
210216
opReleases := map[operationKey]VersionSlice{}
211-
217+
cwd, err := os.Getwd()
218+
if err != nil {
219+
return nil, err
220+
}
221+
ownerFinder, err := codeowners.FromFile(cwd)
222+
if err != nil {
223+
return nil, err
224+
}
212225
for i := range specYamls {
213226
specYamls[i], err = filepath.Abs(specYamls[i])
214227
if err != nil {
@@ -238,6 +251,7 @@ func LoadResourceVersionsFileset(specYamls []string) (*ResourceVersions, error)
238251
op.Extensions = make(map[string]any)
239252
}
240253
op.Extensions[ExtSnykApiVersion] = rc.Version.String()
254+
op.Extensions[ExtSnykApiOwner] = ownerFinder.Owners(specYamls[i])
241255
opKey := operationKey{path, opName}
242256
opReleases[opKey] = append(opReleases[opKey], rc.Version)
243257
}

testdata/output/2021-06-01~experimental/spec.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-01~experimental/spec.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-04~experimental/spec.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-04~experimental/spec.yaml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-07~experimental/spec.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-07~experimental/spec.yaml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-13~beta/spec.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-13~beta/spec.yaml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-13~experimental/spec.json

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-06-13~experimental/spec.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-08-20~experimental/spec.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/output/2021-08-20~experimental/spec.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)