From acb98c6abbf566620e24f019ec41782c47cfe53c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Mon, 27 May 2024 12:10:51 +0200 Subject: [PATCH 1/3] Externalize function --- changelog/expand/expand.go | 12 ++++++++++++ changelog/generate.go | 9 ++------- 2 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 changelog/expand/expand.go diff --git a/changelog/expand/expand.go b/changelog/expand/expand.go new file mode 100644 index 0000000..59a4929 --- /dev/null +++ b/changelog/expand/expand.go @@ -0,0 +1,12 @@ +package expand + +import ( + "log/slog" + "regexp" +) + +func ExpandLinks(description string) string { + slog.Debug("Expanding links") + r := regexp.MustCompile(`(https://[^ \r\n]+)`) + return r.ReplaceAllString(description, "[$1]($1)") +} 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)") -} From cc98e9a62328ffbaa56d8e7f6f01a3b4e8135515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Mon, 27 May 2024 12:55:00 +0200 Subject: [PATCH 2/3] Expanding pull requests, changelogs and github users --- changelog/expand/expand.go | 18 +++++++++++++-- changelog/expand/expand_test.go | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 changelog/expand/expand_test.go diff --git a/changelog/expand/expand.go b/changelog/expand/expand.go index 59a4929..46f076d 100644 --- a/changelog/expand/expand.go +++ b/changelog/expand/expand.go @@ -7,6 +7,20 @@ import ( func ExpandLinks(description string) string { slog.Debug("Expanding links") - r := regexp.MustCompile(`(https://[^ \r\n]+)`) - return r.ReplaceAllString(description, "[$1]($1)") + + // 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) + } + }) + } +} From 3a30cbafc598f9e84d6d4afc3b56abaddbc6c361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Mon, 27 May 2024 12:55:26 +0200 Subject: [PATCH 3/3] Prepare for version 1.1 --- action.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 }}