grafana-cli: Diff generated ts directly instead of relying on git (#45815)

* Add diffing support to grafana-cli cue gen-ts

* Rely on diff comparison in cuetsify pipeline step

* Ignore *.gen.ts files with eslint

* Chore: Fix lint `sdboyer/cuetsify-compare` (#45818)

* Sync drone

(cherry picked from commit 40645ab19e39ff9b0a12b7ebb13a4dc4c5e1d472)

* Fix lint

(cherry picked from commit c95ece983984432fea029335b2b729b09d76c7eb)

* Sign drone

Co-authored-by: Dimitris Sotirakis <sotirakis.dim@gmail.com>
pull/45831/head^2
sam boyer 3 years ago committed by GitHub
parent 8d57318941
commit 60db643983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 122
      .drone.yml
  2. 3
      .eslintignore
  3. 5
      pkg/cmd/grafana-cli/commands/commands.go
  4. 51
      pkg/cmd/grafana-cli/commands/cuetsify_command.go
  5. 2
      public/app/plugins/panel/news/models.gen.ts
  6. 15
      scripts/drone/steps/lib.star

@ -159,22 +159,14 @@ steps:
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
name: validate-scuemata name: validate-scuemata
- commands: - commands:
- '# Make sure the git tree is clean.'
- '# Stashing changes, since packages that were produced in build-backend step are
needed.'
- git stash
- ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root .
- '# The above command generates Typescript files (*.gen.ts) from all appropriate
.cue files.'
- '# It is required that the generated Typescript be in sync with the input CUE - '# It is required that the generated Typescript be in sync with the input CUE
files.' files.'
- '# ...Modulo eslint auto-fixes...:' - '# To enforce this, the following command will attempt to generate Typescript
- yarn run eslint . --ext .gen.ts --fix from all'
- '# If any filenames are emitted by the below script, run the generator command - '# appropriate .cue files, then compare with the corresponding (*.gen.ts) file
`grafana-cli cue gen-ts` locally and commit the result.' the generated'
- ./scripts/clean-git-or-error.sh - '# code would have been written to. It exits 1 if any diffs are found.'
- '# Un-stash changes.' - ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root . --diff
- git stash pop
depends_on: depends_on:
- validate-scuemata - validate-scuemata
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
@ -686,22 +678,14 @@ steps:
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
name: validate-scuemata name: validate-scuemata
- commands: - commands:
- '# Make sure the git tree is clean.'
- '# Stashing changes, since packages that were produced in build-backend step are
needed.'
- git stash
- ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root .
- '# The above command generates Typescript files (*.gen.ts) from all appropriate
.cue files.'
- '# It is required that the generated Typescript be in sync with the input CUE - '# It is required that the generated Typescript be in sync with the input CUE
files.' files.'
- '# ...Modulo eslint auto-fixes...:' - '# To enforce this, the following command will attempt to generate Typescript
- yarn run eslint . --ext .gen.ts --fix from all'
- '# If any filenames are emitted by the below script, run the generator command - '# appropriate .cue files, then compare with the corresponding (*.gen.ts) file
`grafana-cli cue gen-ts` locally and commit the result.' the generated'
- ./scripts/clean-git-or-error.sh - '# code would have been written to. It exits 1 if any diffs are found.'
- '# Un-stash changes.' - ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root . --diff
- git stash pop
depends_on: depends_on:
- validate-scuemata - validate-scuemata
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
@ -1290,22 +1274,14 @@ steps:
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
name: validate-scuemata name: validate-scuemata
- commands: - commands:
- '# Make sure the git tree is clean.'
- '# Stashing changes, since packages that were produced in build-backend step are
needed.'
- git stash
- ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root .
- '# The above command generates Typescript files (*.gen.ts) from all appropriate
.cue files.'
- '# It is required that the generated Typescript be in sync with the input CUE - '# It is required that the generated Typescript be in sync with the input CUE
files.' files.'
- '# ...Modulo eslint auto-fixes...:' - '# To enforce this, the following command will attempt to generate Typescript
- yarn run eslint . --ext .gen.ts --fix from all'
- '# If any filenames are emitted by the below script, run the generator command - '# appropriate .cue files, then compare with the corresponding (*.gen.ts) file
`grafana-cli cue gen-ts` locally and commit the result.' the generated'
- ./scripts/clean-git-or-error.sh - '# code would have been written to. It exits 1 if any diffs are found.'
- '# Un-stash changes.' - ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root . --diff
- git stash pop
depends_on: depends_on:
- validate-scuemata - validate-scuemata
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
@ -1880,22 +1856,14 @@ steps:
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
name: validate-scuemata name: validate-scuemata
- commands: - commands:
- '# Make sure the git tree is clean.'
- '# Stashing changes, since packages that were produced in build-backend step are
needed.'
- git stash
- ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root .
- '# The above command generates Typescript files (*.gen.ts) from all appropriate
.cue files.'
- '# It is required that the generated Typescript be in sync with the input CUE - '# It is required that the generated Typescript be in sync with the input CUE
files.' files.'
- '# ...Modulo eslint auto-fixes...:' - '# To enforce this, the following command will attempt to generate Typescript
- yarn run eslint . --ext .gen.ts --fix from all'
- '# If any filenames are emitted by the below script, run the generator command - '# appropriate .cue files, then compare with the corresponding (*.gen.ts) file
`grafana-cli cue gen-ts` locally and commit the result.' the generated'
- ./scripts/clean-git-or-error.sh - '# code would have been written to. It exits 1 if any diffs are found.'
- '# Un-stash changes.' - ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root . --diff
- git stash pop
depends_on: depends_on:
- validate-scuemata - validate-scuemata
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
@ -3044,22 +3012,14 @@ steps:
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
name: validate-scuemata name: validate-scuemata
- commands: - commands:
- '# Make sure the git tree is clean.'
- '# Stashing changes, since packages that were produced in build-backend step are
needed.'
- git stash
- ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root .
- '# The above command generates Typescript files (*.gen.ts) from all appropriate
.cue files.'
- '# It is required that the generated Typescript be in sync with the input CUE - '# It is required that the generated Typescript be in sync with the input CUE
files.' files.'
- '# ...Modulo eslint auto-fixes...:' - '# To enforce this, the following command will attempt to generate Typescript
- yarn run eslint . --ext .gen.ts --fix from all'
- '# If any filenames are emitted by the below script, run the generator command - '# appropriate .cue files, then compare with the corresponding (*.gen.ts) file
`grafana-cli cue gen-ts` locally and commit the result.' the generated'
- ./scripts/clean-git-or-error.sh - '# code would have been written to. It exits 1 if any diffs are found.'
- '# Un-stash changes.' - ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root . --diff
- git stash pop
depends_on: depends_on:
- validate-scuemata - validate-scuemata
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
@ -3561,22 +3521,14 @@ steps:
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
name: validate-scuemata name: validate-scuemata
- commands: - commands:
- '# Make sure the git tree is clean.'
- '# Stashing changes, since packages that were produced in build-backend step are
needed.'
- git stash
- ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root .
- '# The above command generates Typescript files (*.gen.ts) from all appropriate
.cue files.'
- '# It is required that the generated Typescript be in sync with the input CUE - '# It is required that the generated Typescript be in sync with the input CUE
files.' files.'
- '# ...Modulo eslint auto-fixes...:' - '# To enforce this, the following command will attempt to generate Typescript
- yarn run eslint . --ext .gen.ts --fix from all'
- '# If any filenames are emitted by the below script, run the generator command - '# appropriate .cue files, then compare with the corresponding (*.gen.ts) file
`grafana-cli cue gen-ts` locally and commit the result.' the generated'
- ./scripts/clean-git-or-error.sh - '# code would have been written to. It exits 1 if any diffs are found.'
- '# Un-stash changes.' - ./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root . --diff
- git stash pop
depends_on: depends_on:
- validate-scuemata - validate-scuemata
image: grafana/build-container:1.5.1 image: grafana/build-container:1.5.1
@ -4305,6 +4257,6 @@ kind: secret
name: gcp_upload_artifacts_key name: gcp_upload_artifacts_key
--- ---
kind: signature kind: signature
hmac: b92ebbf48ca675f25c96f4b182a72c08b0e79f9c50d29caaacab124e97f32b4d hmac: fb2a26bf088c9ff2b7cc63cee4fa1f1da06baf560265df4703414f4db9c90708
... ...

@ -10,6 +10,9 @@ scripts/grafana-server/tmp
public/lib/monaco public/lib/monaco
deployment_tools_config.json deployment_tools_config.json
# TS generate from cue by cuetsy
**/*.gen.ts
# Auto-generated localisation files # Auto-generated localisation files
public/locales/_build/ public/locales/_build/
public/locales/**/*.js public/locales/**/*.js

@ -257,6 +257,11 @@ so must be recompiled to validate newly-added CUE files.`,
Name: "grafana-root", Name: "grafana-root",
Usage: "path to the root of a Grafana repository in which to generate TypeScript from CUE files", Usage: "path to the root of a Grafana repository in which to generate TypeScript from CUE files",
}, },
&cli.BoolFlag{
Name: "diff",
Usage: "diff results of codegen against files already on disk. Exits 1 if diff is non-empty",
Value: false,
},
}, },
}, },
} }

@ -17,6 +17,7 @@ import (
"cuelang.org/go/cue/errors" "cuelang.org/go/cue/errors"
cload "cuelang.org/go/cue/load" cload "cuelang.org/go/cue/load"
"cuelang.org/go/cue/parser" "cuelang.org/go/cue/parser"
"github.com/google/go-cmp/cmp"
"github.com/grafana/cuetsy" "github.com/grafana/cuetsy"
"github.com/grafana/grafana/pkg/cmd/grafana-cli/utils" "github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
"github.com/grafana/grafana/pkg/schema/load" "github.com/grafana/grafana/pkg/schema/load"
@ -63,6 +64,7 @@ var skipPaths = []string{
const prefix = "/" const prefix = "/"
//nolint: gocyclo
func (cmd Command) generateTypescript(c utils.CommandLine) error { func (cmd Command) generateTypescript(c utils.CommandLine) error {
root := c.String("grafana-root") root := c.String("grafana-root")
if root == "" { if root == "" {
@ -237,13 +239,46 @@ func (cmd Command) generateTypescript(c utils.CommandLine) error {
return gerrors.New(errors.Details(err, nil)) return gerrors.New(errors.Details(err, nil))
} }
diff := c.Bool("diff")
var derr bool
for of, b := range outfiles { for of, b := range outfiles {
err := os.WriteFile(filepath.Join(root, of), b, 0644) p := filepath.Join(root, of)
if err != nil { if diff {
return err if _, err := os.Stat(p); err != nil {
if errors.Is(err, os.ErrNotExist) {
fmt.Printf("%s: no generated code file to compare against\n", p)
derr = true
continue
}
return fmt.Errorf("%s: %w", p, err)
}
f, err := os.Open(filepath.Clean(p))
if err != nil {
return fmt.Errorf("%s: %w", p, err)
}
ob, err := io.ReadAll(f)
if err != nil {
return err
}
dstr := cmp.Diff(string(ob), string(b))
if dstr != "" {
derr = true
fmt.Printf("%s would have changed:\n%s\n", p, dstr)
}
} else {
err := os.WriteFile(p, b, 0644)
if err != nil {
return err
}
} }
} }
if derr {
return errors.New("some files changed")
}
return nil return nil
} }
@ -283,7 +318,7 @@ func toOverlay(prefix string, vfs fs.FS, overlay map[string]cload.Source) error
if !filepath.IsAbs(prefix) { if !filepath.IsAbs(prefix) {
return fmt.Errorf("must provide absolute path prefix when generating cue overlay, got %q", prefix) return fmt.Errorf("must provide absolute path prefix when generating cue overlay, got %q", prefix)
} }
err := fs.WalkDir(vfs, ".", (func(path string, d fs.DirEntry, err error) error { err := fs.WalkDir(vfs, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
@ -296,6 +331,12 @@ func toOverlay(prefix string, vfs fs.FS, overlay map[string]cload.Source) error
if err != nil { if err != nil {
return err return err
} }
defer func(f fs.File) {
err := f.Close()
if err != nil {
return
}
}(f)
b, err := io.ReadAll(f) b, err := io.ReadAll(f)
if err != nil { if err != nil {
@ -304,7 +345,7 @@ func toOverlay(prefix string, vfs fs.FS, overlay map[string]cload.Source) error
overlay[filepath.Join(prefix, path)] = cload.FromBytes(b) overlay[filepath.Join(prefix, path)] = cload.FromBytes(b)
return nil return nil
})) })
if err != nil { if err != nil {
return err return err

@ -2,8 +2,10 @@
// This file was autogenerated by cuetsy. DO NOT EDIT! // This file was autogenerated by cuetsy. DO NOT EDIT!
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export const modelVersion = Object.freeze([0, 0]); export const modelVersion = Object.freeze([0, 0]);
export interface PanelOptions { export interface PanelOptions {
feedUrl?: string; feedUrl?: string;
showImage?: boolean; showImage?: boolean;

@ -1134,18 +1134,11 @@ def ensure_cuetsified_step():
'validate-scuemata', 'validate-scuemata',
], ],
'commands': [ 'commands': [
'# Make sure the git tree is clean.',
'# Stashing changes, since packages that were produced in build-backend step are needed.',
'git stash',
'./bin/linux-amd64/grafana-cli cue gen-ts --grafana-root .',
'# The above command generates Typescript files (*.gen.ts) from all appropriate .cue files.',
'# It is required that the generated Typescript be in sync with the input CUE files.', '# It is required that the generated Typescript be in sync with the input CUE files.',
'# ...Modulo eslint auto-fixes...:', '# To enforce this, the following command will attempt to generate Typescript from all',
'yarn run eslint . --ext .gen.ts --fix', '# appropriate .cue files, then compare with the corresponding (*.gen.ts) file the generated',
'# If any filenames are emitted by the below script, run the generator command `grafana-cli cue gen-ts` locally and commit the result.', '# code would have been written to. It exits 1 if any diffs are found.',
'./scripts/clean-git-or-error.sh', './bin/linux-amd64/grafana-cli cue gen-ts --grafana-root . --diff',
'# Un-stash changes.',
'git stash pop',
], ],
} }

Loading…
Cancel
Save