diff --git a/action.yaml b/action.yaml index 54650c1..b2e3fb5 100644 --- a/action.yaml +++ b/action.yaml @@ -21,7 +21,7 @@ inputs: default: 'true' runs: using: 'docker' - image: 'docker://ghcr.io/prodyna/changelog-json:v1.0' + image: 'docker://ghcr.io/prodyna/changelog-json:v1.1' env: GITHUB_TOKEN: ${{ inputs.github-token }} REPOSITORIES: ${{ inputs.repositories }} diff --git a/changelog/expand/expand.go b/changelog/expand/expand.go new file mode 100644 index 0000000..46f076d --- /dev/null +++ b/changelog/expand/expand.go @@ -0,0 +1,26 @@ +package expand + +import ( + "log/slog" + "regexp" +) + +func ExpandLinks(description string) string { + slog.Debug("Expanding links") + + // https://github.com/PRODYNA-YASM/yasm-backend/pull/549 -> [**#PR549**](https://github.com/PRODYNA-YASM/yasm-backend/pull/549) + r := regexp.MustCompile("https://github.com/(.*?)/pull/(\\d+)") + description = r.ReplaceAllString(description, "[**#PR$2**](https://github.com/$1/pull/$2)") + + // https://github.com/PRODYNA-YASM/yasm-backend/compare/1.16.4...1.19.0 -> [**#1.16.4...1.19.0**](https://github.com/PRODYNA-YASM/yasm-backend/compare/1.16.4...1.19.0) + r = regexp.MustCompile("https://github.com/(.*?)/compare/(.*)") + description = r.ReplaceAllString(description, "[**#$2**](https://github.com/$1/compare/$2)") + // r := regexp.MustCompile(`(https://[^ \r\n]+)`) + // return r.ReplaceAllString(description, "[$1]($1)") + + // @dkrizic -> [**@dkrizic**](https://github.com/dkrizic) + r = regexp.MustCompile("@(\\w+)") + description = r.ReplaceAllString(description, "[**@$1**](https://github.com/$1)") + + return description +} diff --git a/changelog/expand/expand_test.go b/changelog/expand/expand_test.go new file mode 100644 index 0000000..957d406 --- /dev/null +++ b/changelog/expand/expand_test.go @@ -0,0 +1,40 @@ +package expand + +import "testing" + +func TestExpandLinks(t *testing.T) { + tests := []struct { + name string + description string + want string + }{ + { + name: "no links", + description: "no links", + want: "no links", + }, + { + name: "pull request", + description: "this is a pull request https://github.com/PRODYNA-YASM/yasm-backend/pull/549", + want: "this is a pull request [**#PR549**](https://github.com/PRODYNA-YASM/yasm-backend/pull/549)", + }, + { + name: "changelog", + description: "this is a changelog https://github.com/PRODYNA-YASM/yasm-backend/compare/1.16.4...1.19.0", + want: "this is a changelog [**#1.16.4...1.19.0**](https://github.com/PRODYNA-YASM/yasm-backend/compare/1.16.4...1.19.0)", + }, + { + name: "github user", + description: "this is a github user @dkrizic", + want: "this is a github user [**@dkrizic**](https://github.com/dkrizic)", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ExpandLinks(tt.description); got != tt.want { + t.Errorf("ExpandLinks() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/changelog/generate.go b/changelog/generate.go index 4f8763f..0748233 100644 --- a/changelog/generate.go +++ b/changelog/generate.go @@ -2,11 +2,11 @@ package changelog import ( "context" + "github.com/prodyna/changelog-json/changelog/expand" "github.com/prodyna/changelog-json/changelog/output" "github.com/shurcooL/githubv4" "golang.org/x/oauth2" "log/slog" - "regexp" "strings" ) @@ -89,7 +89,7 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output. slog.Debug("Release", "tag", release.Tag.Name, "date", release.CreatedAt, "name", release.Name, "description.len", len(release.Description)) if clg.config.ExpandLinks { - release.Description = expandLinks(release.Description) + release.Description = expand.ExpandLinks(release.Description) } entry := output.Entry{ @@ -108,8 +108,3 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output. } // Replace all https:// to [https://](https://) -func expandLinks(description string) string { - slog.Debug("Expanding links") - r := regexp.MustCompile(`(https://[^ \r\n]+)`) - return r.ReplaceAllString(description, "[$1]($1)") -}