Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion plugins/source/azuredevops/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ gen-docs:
rm -rf ./docs/tables/*
go run main.go doc ./docs/tables

.PHONY: gen-code
gen-code:
grep -rl '// Code generated by codegen; DO NOT EDIT.' resources/services/* | xargs rm
go run codegen/main.go

.PHONY: gen
gen: gen-docs
gen: gen-code gen-docs
18 changes: 18 additions & 0 deletions plugins/source/azuredevops/codegen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"log"

"github.com/cloudquery/cloudquery/plugins/source/azuredevops/codegen/recipes"
)

func main() {
for _, resource := range recipes.Resources {
if err := resource.Generate(); err != nil {
Comment thread
candiduslynx marked this conversation as resolved.
log.Fatal(err)
}
}
if err := recipes.GenerateTablesList(recipes.Resources); err != nil {
log.Fatal(err)
}
}
120 changes: 120 additions & 0 deletions plugins/source/azuredevops/codegen/recipes/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package recipes

import (
"bytes"
"embed"
"fmt"
"go/format"
"os"
"path"
"reflect"
"runtime"
"text/template"

"github.com/cloudquery/plugin-sdk/codegen"
"github.com/cloudquery/plugin-sdk/schema"
"github.com/google/uuid"
"github.com/iancoleman/strcase"
)

//go:embed templates/*.go.tpl
var templatesFS embed.FS
var Resources []*Resource

type Resource struct {
Service string
Comment thread
candiduslynx marked this conversation as resolved.
SubService string
Struct interface{}
Table *codegen.TableDefinition
}

func writeTemplateContentToFile(dir string, filePath string, buff bytes.Buffer) error {
outputPath := path.Join(dir, "../..", filePath)
outputDir := path.Dir(outputPath)
if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
return fmt.Errorf("failed to create directory %s: %w", outputDir, err)
}

content := buff.Bytes()
formattedContent, err := format.Source(content)
if err != nil {
fmt.Printf("failed to format source: %s: %v\n", filePath, err)
} else {
content = formattedContent
}

if err := os.WriteFile(outputPath, content, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %w", filePath, err)
}
return nil
}

func renderTemplate(name string, filePath string, data interface{}) error {
_, filename, _, ok := runtime.Caller(0)
if !ok {
return fmt.Errorf("failed to get caller information")
}
dir := path.Dir(filename)

tpl, err := template.New(name).Funcs(template.FuncMap{"ToCamel": strcase.ToCamel}).ParseFS(templatesFS, "templates/*.go.tpl")
Comment thread
candiduslynx marked this conversation as resolved.
if err != nil {
return fmt.Errorf("failed to parse azureDevops templates: %w", err)
}
tpl, err = tpl.ParseFS(codegen.TemplatesFS, "templates/*.go.tpl")
if err != nil {
return fmt.Errorf("failed to parse sdk template: %w", err)
}

var buff bytes.Buffer
if err := tpl.Execute(&buff, data); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}

return writeTemplateContentToFile(dir, filePath, buff)
}

func (resource *Resource) generate() error {
return renderTemplate("resource.go.tpl", path.Join("resources", "services", resource.Service, resource.SubService+".go"), resource)
}

func isUUID(fieldType reflect.Type) bool {
fieldKind := fieldType.Kind()

if fieldKind == reflect.Ptr {
return isUUID(fieldType.Elem())
}

return fieldType == reflect.TypeOf(uuid.UUID{})
}

func typeTransformer(field reflect.StructField) (schema.ValueType, error) {
if isUUID(field.Type) {
return schema.TypeUUID, nil
}

return codegen.DefaultTypeTransformer(field)
Comment thread
candiduslynx marked this conversation as resolved.
}

func (resource *Resource) Generate() error {
var err error
resource.Table, err = codegen.NewTableFromStruct(
fmt.Sprintf("azuredevops_%s_%s", resource.Service, resource.SubService),
Comment thread
erezrokah marked this conversation as resolved.
resource.Struct,
codegen.WithTypeTransformer(typeTransformer),
Comment thread
candiduslynx marked this conversation as resolved.
)

if err != nil {
return err
}

resource.Table.Resolver = "fetch" + strcase.ToCamel(resource.SubService)
if err := resource.generate(); err != nil {
return err
}

return nil
}

func GenerateTablesList(resources []*Resource) error {
return renderTemplate("tables.go.tpl", path.Join("resources", "plugin", "tables.go"), resources)
}
20 changes: 20 additions & 0 deletions plugins/source/azuredevops/codegen/recipes/core.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package recipes

import (
"github.com/microsoft/azure-devops-go-api/azuredevops/v6/core"
)

func init() {
resources := []*Resource{
{
SubService: "projects",
Struct: &core.TeamProjectReference{},
},
}

for _, resource := range resources {
resource.Service = "core"
}

Resources = append(Resources, resources...)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Code generated by codegen; DO NOT EDIT.

package {{.Service}}

import (
"github.com/cloudquery/plugin-sdk/schema"
)

func {{.SubService | ToCamel}}() *schema.Table {
Comment thread
candiduslynx marked this conversation as resolved.
return &schema.Table{{template "table.go.tpl" .Table}}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Code generated by codegen; DO NOT EDIT.

package plugin

import (
{{- range .}}
"github.com/cloudquery/cloudquery/plugins/source/azuredevops/resources/services/{{.Service}}"
{{- end}}
"github.com/cloudquery/plugin-sdk/schema"
)

func tables() []*schema.Table {
return []*schema.Table{
{{- range .}}
{{.Service}}.{{.SubService | ToCamel}}(),
{{- end}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ The primary key for this table is **_cq_id**.
|_cq_sync_time|Timestamp|
|_cq_id (PK)|UUID|
|_cq_parent_id|UUID|
|id|UUID|
|abbreviation|String|
|default_team_image_url|String|
|description|String|
|id|UUID|
|last_update_time|JSON|
|name|String|
|revision|Int|
|state|String|
|url|String|
|visibility|String|
4 changes: 3 additions & 1 deletion plugins/source/azuredevops/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.19

require (
github.com/cloudquery/plugin-sdk v1.12.0
github.com/google/uuid v1.3.0
github.com/iancoleman/strcase v0.2.0
github.com/microsoft/azure-devops-go-api/azuredevops/v6 v6.0.1
github.com/rs/zerolog v1.28.0
)
Expand All @@ -12,7 +14,6 @@ require (
github.com/getsentry/sentry-go v0.15.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/providers/zerolog/v2 v2.0.0-rc.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.3 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand All @@ -22,6 +23,7 @@ require (
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/thoas/go-funk v0.9.2 // indirect
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.2.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions plugins/source/azuredevops/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.3 h1:o95KDiV/b1xdkumY5
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.0-rc.3/go.mod h1:hTxjzRcX49ogbTGVJ1sM5mz5s+SSgiGIyL3jjPxl32E=
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/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand Down Expand Up @@ -202,6 +204,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
6 changes: 5 additions & 1 deletion plugins/source/azuredevops/resources/plugin/tables.go

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

77 changes: 47 additions & 30 deletions plugins/source/azuredevops/resources/services/core/projects.go

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

Loading