Skip to content

Commit

Permalink
fix: reduce spurious releases (#72)
Browse files Browse the repository at this point in the history
* fix: reduce spurious releases

* chore: better messaging

* chore: also pass in docs checksum
  • Loading branch information
ThomasRooney authored Nov 21, 2023
1 parent 162eb17 commit fbd4d04
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 167 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/google/go-github/v54 v54.0.0
github.com/hashicorp/go-version v1.6.0
github.com/pb33f/libopenapi v0.13.8
github.com/speakeasy-api/git-diff-parser v0.0.3
github.com/speakeasy-api/sdk-gen-config v1.2.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.12.1 h1:DRhgbg1t32OSYoHT/Bk3stVruqquAkKj+S+OqOQIbBc=
github.com/pb33f/libopenapi v0.12.1/go.mod h1:s8uj6S0DjWrwZVj20ianJBz+MMjHAbeeRYNyo9ird74=
github.com/pb33f/libopenapi v0.13.8 h1:PzBTpN1jdTi/XJpHGiRomxSHfQHdabcasGXW8yEhIjw=
github.com/pb33f/libopenapi v0.13.8/go.mod h1:Lv2eEtsAtbRFlF8hjH82L8SIGoUNgemMVoKoB6A9THk=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand All @@ -129,8 +127,10 @@ github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/speakeasy-api/sdk-gen-config v1.1.0 h1:gSrzInsnw8tQh/FtZQJYyNo3gQ1Nu6Sm2OUp0BazMYI=
github.com/speakeasy-api/sdk-gen-config v1.1.0/go.mod h1:94mmqrM5tP6/O8triQo9PIb6YYqihtKeoH70uwQ+JF4=
github.com/speakeasy-api/git-diff-parser v0.0.2 h1:88LebHKNede37yhQbODE+jVkNmvd33GqOTM3YEzNsJY=
github.com/speakeasy-api/git-diff-parser v0.0.2/go.mod h1:P46HmmVVmwA9P8h2wa0fDpmRM8/grbVQ+uKhWDtpkIY=
github.com/speakeasy-api/git-diff-parser v0.0.3 h1:LL12d+HMtSyj6O/hQqIn/lgDPYI6ci/DEhk0la/xA+0=
github.com/speakeasy-api/git-diff-parser v0.0.3/go.mod h1:P46HmmVVmwA9P8h2wa0fDpmRM8/grbVQ+uKhWDtpkIY=
github.com/speakeasy-api/sdk-gen-config v1.2.0 h1:u5UCrS63TXpNPgsBqU4iSmG0ZnFjKg7gDC8ICKVviu0=
github.com/speakeasy-api/sdk-gen-config v1.2.0/go.mod h1:94mmqrM5tP6/O8triQo9PIb6YYqihtKeoH70uwQ+JF4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
4 changes: 4 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ func Suggest(docPath, maxSuggestions, docOutputPath string) (string, error) {
}

func Generate(docPath, lang, outputDir, installationURL string, published, outputTests bool, repoURL, repoSubDirectory string) error {
outputDir, err := filepath.Abs(outputDir)
if err != nil {
return err
}
args := []string{
"generate",
"sdk",
Expand Down
9 changes: 6 additions & 3 deletions internal/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
)

type Git interface {
CheckDirDirty(dir string, ignoreMap map[string]string) (bool, error)
CheckDirDirty(dir string, ignoreMap map[string]string) (bool, string, error)
}

func Generate(g Git) (*GenerationInfo, map[string]string, error) {
Expand Down Expand Up @@ -158,15 +158,18 @@ func Generate(g Git) (*GenerationInfo, map[string]string, error) {

outputs[fmt.Sprintf("%s_directory", lang)] = dirForOutput

dirty, err := g.CheckDirDirty(dir, map[string]string{
previousVersion: newVersion,
dirty, dirtyMsg, err := g.CheckDirDirty(dir, map[string]string{
sdkVersion: newVersion,
cfg.Config.Management.GenerationVersion: generationVersion.String(),
cfg.Config.Management.DocChecksum: docChecksum,
})
if err != nil {
return nil, outputs, err
}

if dirty {
langGenerated[lang] = true
fmt.Printf("Regenerating %s SDK resulted in significant changes %s\n", lang, dirtyMsg)
} else {
langCfg.Version = sdkVersion
cfg.Config.Languages[lang] = langCfg
Expand Down
72 changes: 20 additions & 52 deletions internal/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,37 @@ package git

import (
"fmt"
"regexp"
"strings"

diffParser "github.com/speakeasy-api/git-diff-parser"
"github.com/speakeasy-api/sdk-generation-action/internal/environment"
)

var (
fileBoundaryRegex = regexp.MustCompile(`(?m)^diff --git a\/.*? b\/.*?$`)
fileNameRegex = regexp.MustCompile(`(?m)^--- a\/(.*?)$`)
versionChangeRegex = regexp.MustCompile(`_?(sdk|gen|Gen|SDK)_?[vV]ersion`)
userAgentRegex = regexp.MustCompile(`speakeasy-sdk/`)
)

func IsGitDiffSignificant(diff string, ignoreChangePatterns map[string]string) bool {
func IsGitDiffSignificant(diff string, ignoreChangePatterns map[string]string) (bool, string, error) {
if environment.ForceGeneration() {
return true
return true, "", nil
}

diffs := fileBoundaryRegex.Split(diff, -1)

significantChanges := false

outer:
for _, diff := range diffs {
if strings.TrimSpace(diff) == "" {
continue
isSignificant, signifanceMsg, err := diffParser.SignificantChange(diff, func(diff *diffParser.FileDiff, change *diffParser.ContentChange) (bool, string) {
if diff.ToFile == "gen.yaml" || diff.ToFile == "RELEASES.md" {
return false, ""
}

matches := fileNameRegex.FindStringSubmatch(diff)
if len(matches) != 2 {
continue
}

filename := fileNameRegex.FindStringSubmatch(diff)[1]
if strings.Contains(filename, "gen.yaml") {
continue
if change.Type == diffParser.ContentChangeTypeNOOP {
return false, ""
}

lines := strings.Split(diff, "\n")
for i, line := range lines {
isAddition := strings.HasPrefix(line, "+ ") || strings.HasPrefix(line, "+\t")
isSpecificPatternIgnored := false
if i > 1 && isAddition && strings.HasPrefix(lines[i-1], "- ") {
priorLine := lines[i-1]
for fromPattern, toPattern := range ignoreChangePatterns {
if strings.Contains(priorLine, fromPattern) && strings.Contains(line, toPattern) {
isSpecificPatternIgnored = true
break
}
}

}
isNotVersionChange := !versionChangeRegex.MatchString(line)
isNotUAChange := !userAgentRegex.MatchString(line)

significantChanges = isAddition && isNotVersionChange && isNotUAChange && !isSpecificPatternIgnored

if significantChanges {
fmt.Println(line)
break outer
for pattern, replacement := range ignoreChangePatterns {
if strings.Contains(change.From, pattern) && strings.Contains(change.To, replacement) {
return false, ""
}
}
}
if diff.Type == diffParser.FileDiffTypeModified {
return true, fmt.Sprintf("significant diff %#v", diff)
}

return significantChanges
return true, fmt.Sprintf("significant change %#v in %s", change, diff.ToFile)
})
if err != nil {
return true, "", fmt.Errorf("failed to parse diff: %w", err)
}
return isSignificant, signifanceMsg, nil
}
Loading

0 comments on commit fbd4d04

Please sign in to comment.