Skip to content

Commit

Permalink
fix: maintain backwards compatibility of RELEASES.md and ensure dir=.…
Browse files Browse the repository at this point in the history
… when .speakeasy/gen.lock in root of github repo (#174)
  • Loading branch information
ThomasRooney authored Oct 10, 2024
1 parent 6fd34c3 commit 5c29f96
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 17 deletions.
43 changes: 26 additions & 17 deletions internal/actions/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,7 @@ func Release() error {
}
}

for _, file := range files {
// Maintain Support for RELEASES.MD for backward compatibility with existing publishing actions
if strings.Contains(file, "RELEASES.md") {
dir = filepath.Dir(file)
logging.Info("Found RELEASES.md in %s\n", dir)
usingReleasesMd = true
break
}

if strings.Contains(file, "gen.lock") {
dir = filepath.Dir(file)
dir = strings.ReplaceAll(dir, "/.speakeasy", "")
dir = strings.ReplaceAll(dir, ".speakeasy", "")
logging.Info("Found gen.lock in %s\n", dir)
break
}
}
dir, usingReleasesMd = GetDirAndShouldUseReleasesMD(files, dir, usingReleasesMd)
}

var latestRelease *releases.ReleasesInfo
Expand Down Expand Up @@ -111,6 +95,31 @@ func Release() error {
return nil
}

func GetDirAndShouldUseReleasesMD(files []string, dir string, usingReleasesMd bool) (string, bool) {
for _, file := range files {
// Maintain Support for RELEASES.MD for backward compatibility with existing publishing actions
if strings.Contains(file, "RELEASES.md") {
// file = ./RELEASES.md
// dir = .
dir = filepath.Dir(file)
logging.Info("Found RELEASES.md in %s\n", dir)
usingReleasesMd = true
break
}

if strings.Contains(file, "gen.lock") {
// file = .speakeasy/gen.lock
dir = filepath.Dir(file)
if strings.Contains(dir, ".speakeasy") {
dir = filepath.Dir(dir)
}

logging.Info("Found gen.lock in %s\n", dir)
}
}
return dir, usingReleasesMd
}

func addPublishOutputs(dir string, outputs map[string]string) error {
wf, err := configuration.GetWorkflowAndValidateLanguages(false)
if err != nil {
Expand Down
89 changes: 89 additions & 0 deletions internal/actions/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package actions

import "testing"

func TestGetDirAndShouldUseReleasesMD(t *testing.T) {
type args struct {
files []string
dir string
usingReleasesMd bool
}
tests := []struct {
name string
args args
want string
want1 bool
}{
{
name: "RELEASES.md found",
args: args{
files: []string{"./RELEASES.md", "some/other/file.go"},
dir: ".",
usingReleasesMd: false,
},
want: ".",
want1: true,
},
{
name: "RELEASES.md found in subdirectory",
args: args{
files: []string{"subdir/RELEASES.md", "some/other/file.go"},
dir: ".",
usingReleasesMd: false,
},
want: "subdir",
want1: true,
},
{
name: "gen.lock found",
args: args{
files: []string{".speakeasy/gen.lock", "some/other/file.go"},
dir: ".",
usingReleasesMd: false,
},
want: ".",
want1: false,
},
{
name: "gen.lock found in subdirectory",
args: args{
files: []string{"subdir/.speakeasy/gen.lock", "some/other/file.go"},
dir: ".",
usingReleasesMd: false,
},
want: "subdir",
want1: false,
},
{
name: "no relevant files found",
args: args{
files: []string{"some/file.go", "another/file.js"},
dir: ".",
usingReleasesMd: false,
},
want: ".",
want1: false,
},
{
name: "RELEASES.md takes precedence over gen.lock",
args: args{
files: []string{".speakeasy/gen.lock", "RELEASES.md"},
dir: ".",
usingReleasesMd: false,
},
want: ".",
want1: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := GetDirAndShouldUseReleasesMD(tt.args.files, tt.args.dir, tt.args.usingReleasesMd)
if got != tt.want {
t.Errorf("GetDirAndShouldUseReleasesMD() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("GetDirAndShouldUseReleasesMD() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

0 comments on commit 5c29f96

Please sign in to comment.