Skip to content

Commit

Permalink
fix: discover version number changes (and ignore them) even when in a…
Browse files Browse the repository at this point in the history
…n unusual location (#70)
  • Loading branch information
ThomasRooney authored Nov 17, 2023
1 parent e7c9717 commit 573f142
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
8 changes: 5 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) (bool, error)
CheckDirDirty(dir string, ignoreMap map[string]string) (bool, error)
}

func Generate(g Git) (*GenerationInfo, map[string]string, error) {
Expand Down Expand Up @@ -145,7 +145,9 @@ func Generate(g Git) (*GenerationInfo, map[string]string, error) {

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

dirty, err := g.CheckDirDirty(dir)
dirty, err := g.CheckDirDirty(dir, map[string]string{
previousVersion: newVersion,
})
if err != nil {
return nil, outputs, err
}
Expand Down Expand Up @@ -321,7 +323,7 @@ func GenerateDocs(g Git) (*GenerationInfo, map[string]string, error) {

outputs["docs_directory"] = rootDir

dirty, err := g.CheckDirDirty(rootDir)
dirty, err := g.CheckDirDirty(rootDir, map[string]string{})
if err != nil {
return nil, outputs, err
}
Expand Down
17 changes: 14 additions & 3 deletions internal/git/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
userAgentRegex = regexp.MustCompile(`speakeasy-sdk/`)
)

func IsGitDiffSignificant(diff string) bool {
func IsGitDiffSignificant(diff string, ignoreChangePatterns map[string]string) bool {
if environment.ForceGeneration() {
return true
}
Expand All @@ -41,12 +41,23 @@ outer:
}

lines := strings.Split(diff, "\n")
for _, line := range lines {
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
significantChanges = isAddition && isNotVersionChange && isNotUAChange && !isSpecificPatternIgnored

if significantChanges {
fmt.Println(line)
Expand Down
19 changes: 18 additions & 1 deletion internal/git/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,27 @@ index b26db52..fdc01f4 100755
},
want: true,
},
{
name: "ignores a version number change, even when compiled into an unusual line",
args: args{
// Important: Preserve tabs in the follow diff
diff: `diff --git a/gen.yaml b/gen.yaml
index 322c845..585bc5b 100644
--- a/useragent.go
+++ b/useragent.go
- useragent := "%s/go 1.3.2 2.155.1 0.1.0-alpha openapi"
+ useragent := "%s/go 1.3.3 2.155.1 0.1.0-alpha openapi"
`,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsGitDiffSignificant(tt.args.diff)
got := IsGitDiffSignificant(tt.args.diff, map[string]string{
// example version number change
"1.3.2": "1.3.3",
})
assert.Equal(t, tt.want, got)
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (g *Git) CloneRepo() error {
return nil
}

func (g *Git) CheckDirDirty(dir string) (bool, error) {
func (g *Git) CheckDirDirty(dir string, ignoreChangePatterns map[string]string) (bool, error) {
if g.repo == nil {
return false, fmt.Errorf("repo not cloned")
}
Expand Down Expand Up @@ -146,7 +146,7 @@ func (g *Git) CheckDirDirty(dir string) (bool, error) {
return false, fmt.Errorf("error running git diff: %w", err)
}

return IsGitDiffSignificant(diffOutput), nil
return IsGitDiffSignificant(diffOutput, ignoreChangePatterns), nil
}

func (g *Git) FindExistingPR(branchName string, action environment.Action) (string, *github.PullRequest, error) {
Expand Down

0 comments on commit 573f142

Please sign in to comment.