Skip to content

Commit

Permalink
Merge pull request #2 from PRODYNA/1-enable-links-in-md-format
Browse files Browse the repository at this point in the history
1 enable links in md format
  • Loading branch information
dkrizic authored May 27, 2024
2 parents aae4783 + efa34cb commit 2ebc0c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ inputs:
description: 'The output file to write the changelog to'
required: true
default: 'CHANGELOG.json'
expand-links:
description: 'Expand the links in the changelog'
required: false
default: 'true'
runs:
using: 'docker'
image: 'docker://ghcr.io/prodyna/changelog-json:v0.9'
image: 'docker://ghcr.io/prodyna/changelog-json:v1.0'
env:
GITHUB_TOKEN: ${{ inputs.github-token }}
REPOSITORIES: ${{ inputs.repositories }}
ORGANIZATION: ${{ inputs.organization }}
OUTPUT_FILE: ${{ inputs.output-file }}
EXPAND_LINKS: ${{ inputs.expand-links }}
14 changes: 14 additions & 0 deletions changelog/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
"log/slog"
"regexp"
"strings"
)

type Config struct {
GitHubToken string
Repositories string
Organization string
ExpandLinks bool
}

type Tag struct {
Expand Down Expand Up @@ -85,6 +87,11 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output.

for _, release := range query.Organization.Repository.Releases.Nodes {
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)
}

entry := output.Entry{
Tag: release.Tag.Name,
Name: release.Name,
Expand All @@ -99,3 +106,10 @@ func (clg *ChangelogGenerator) Generate(ctx context.Context) (changelog *output.

return changelog, nil
}

// Replace all https://<whatever> to [https://<whatever>](https://<whatever>)
func expandLinks(description string) string {
slog.Debug("Expanding links")
r := regexp.MustCompile(`(https://[^ \r\n]+)`)
return r.ReplaceAllString(description, "[$1]($1)")
}
15 changes: 15 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
keyOrganizationEnvironment = "ORGANIZATION"
keyOutputFile = "output-file"
keyOutputFileEnvironment = "OUTPUT_FILE"
keyExpandLinks = "expand-links"
keyExpandLinksEnvironment = "EXPAND_LINKS"
)

type Config struct {
Expand All @@ -28,6 +30,7 @@ type Config struct {
Repositories string
Organization string
OutputFile string
ExpandLinks bool
}

func New() (*Config, error) {
Expand All @@ -37,6 +40,7 @@ func New() (*Config, error) {
flag.StringVar(&c.Repositories, keyRepositories, lookupEnvOrString(keyRepositoriesEnvironment, ""), "The repositories to generate changelog for.")
flag.StringVar(&c.Organization, keyOrganization, lookupEnvOrString(keyOrganizationEnvironment, ""), "The organization to generate changelog for.")
flag.StringVar(&c.OutputFile, keyOutputFile, lookupEnvOrString(keyOutputFileEnvironment, ""), "The output file to write the changelog to.")
flag.BoolVar(&c.ExpandLinks, keyExpandLinks, lookupEnvOrBool(keyExpandLinksEnvironment, "true"), "Expand links in the changelog.")
flag.Parse()

level := slog.LevelError
Expand Down Expand Up @@ -90,3 +94,14 @@ func lookupEnvOrInt(key string, defaultVal int) int {
}
return defaultVal
}

func lookupEnvOrBool(key string, defaultVal string) bool {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("LookupEnvOrBool[%s]: %v", key, err)
}
return v
}
return defaultVal == "true"
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func main() {
GitHubToken: c.GithubToken,
Repositories: c.Repositories,
Organization: c.Organization,
ExpandLinks: c.ExpandLinks,
})
if err != nil {
slog.Error("unable to create changelog generator", "error", err)
Expand Down

0 comments on commit 2ebc0c0

Please sign in to comment.