Skip to content

Commit

Permalink
feat(BREAKING): remove hashURL function in template render
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

before:

```bash
{{ hashURL .Hash}}
{{ hashURL .RevertCommitHash }}
```

after:

```bash
{{ .HashURL }}
{{ .RevertCommitHashURL }}
```
  • Loading branch information
axetroy committed Oct 3, 2021
1 parent ae56529 commit 35a348f
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 456 deletions.
75 changes: 68 additions & 7 deletions 3_transformer/transform.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package transformer

import (
"fmt"
"net/url"
"strings"

"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
extractor "github.com/whatchanged-community/whatchanged/2_extractor"
"github.com/whatchanged-community/whatchanged/internal/client"
"github.com/whatchanged-community/whatchanged/internal/commit/parser"
giturls "github.com/whilp/git-urls"
)

type Commit struct {
Hash string
Short string
Message string
Author *object.Signature
Committer *object.Signature
Field *parser.Message
RevertCommitHash *string // revert hash
Hash string
HashURL string
Short string
Message string
Author *object.Signature
Committer *object.Signature
Field *parser.Message
RevertCommitHash *string // revert hash
RevertCommitHashURL *string // revert hash URL
}

// https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit-message-header
Expand All @@ -36,9 +44,59 @@ type TemplateContext struct {
Commits []*Commit
}

func generateCommitHashURL(remoteURL *url.URL, longHash string) string {
shortHash := string(longHash[0:7])

if remoteURL != nil {
u, _ := url.Parse(remoteURL.String())

u.Path = u.Path + "/commit/" + longHash

return fmt.Sprintf("[`%s`](%s)", shortHash, u.String())
} else {
return shortHash
}
}

func Transform(g *client.GitClient, splices []*extractor.ExtractSplice) ([]*TemplateContext, error) {
context := make([]*TemplateContext, 0)

remote, err := g.GetRemote()

if err != nil {
return nil, errors.WithStack(err)
}

var remoteURL *url.URL

if remote != nil || len(remote.URLs) > 0 {
for _, urlStr := range remote.URLs {
if remoteURL, err = giturls.Parse(urlStr); err != nil {
return nil, errors.WithStack(err)
} else {
if remoteURL.Host == "github.com" || remoteURL.Host == "gitlab.com" {
break
}
}
}

if remoteURL != nil {
urlPath := strings.TrimSuffix(remoteURL.Path, ".git")
switch remoteURL.Scheme {
case "http":
fallthrough
case "https":
if remoteURL, err = url.Parse(fmt.Sprintf("%s://%s%s", remoteURL.Scheme, remoteURL.Host, urlPath)); err != nil {
return nil, errors.WithStack(err)
}
case "ssh":
if remoteURL, err = url.Parse(fmt.Sprintf("https://%s/%s", remoteURL.Host, urlPath)); err != nil {
return nil, errors.WithStack(err)
}
}
}
}

for _, splice := range splices {
ctx := &TemplateContext{
Version: splice.Name,
Expand All @@ -59,6 +117,7 @@ func Transform(g *client.GitClient, splices []*extractor.ExtractSplice) ([]*Temp
hash := commit.Hash.String()
c := &Commit{
Hash: hash,
HashURL: generateCommitHashURL(remoteURL, hash),
Short: string(hash[0:7]),
Message: commit.Message,
Author: &commit.Author,
Expand All @@ -79,6 +138,8 @@ func Transform(g *client.GitClient, splices []*extractor.ExtractSplice) ([]*Temp

if field.Revert != nil {
c.RevertCommitHash = field.Revert
revertHashURL := generateCommitHashURL(remoteURL, *field.Revert)
c.RevertCommitHashURL = &revertHashURL
}

ctx.Commits = append(ctx.Commits, c)
Expand Down
55 changes: 0 additions & 55 deletions 4_generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,17 @@ import (
"fmt"
"html/template"
"io/ioutil"
"net/url"
"strings"

"github.com/pkg/errors"
transformer "github.com/whatchanged-community/whatchanged/3_transformer"
"github.com/whatchanged-community/whatchanged/internal/client"
"github.com/whatchanged-community/whatchanged/option"
giturls "github.com/whilp/git-urls"
)

//go:embed template/*.tpl
var templateFS embed.FS

func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, format option.Format, preset option.Preset, templateFile string, templateStr string) ([]byte, error) {
remote, err := g.GetRemote()

if err != nil {
return nil, errors.WithStack(err)
}

var remoteURL *url.URL

if remote != nil || len(remote.URLs) > 0 {
for _, urlStr := range remote.URLs {
if remoteURL, err = giturls.Parse(urlStr); err != nil {
return nil, errors.WithStack(err)
} else {
if remoteURL.Host == "github.com" || remoteURL.Host == "gitlab.com" {
break
}
}
}

if remoteURL != nil {
urlPath := strings.TrimSuffix(remoteURL.Path, ".git")
switch remoteURL.Scheme {
case "http":
fallthrough
case "https":
if remoteURL, err = url.Parse(fmt.Sprintf("%s://%s%s", remoteURL.Scheme, remoteURL.Host, urlPath)); err != nil {
return nil, errors.WithStack(err)
}
case "ssh":
if remoteURL, err = url.Parse(fmt.Sprintf("https://%s/%s", remoteURL.Host, urlPath)); err != nil {
return nil, errors.WithStack(err)
}
}
}
}

switch format {
case option.FormatJSON:
output, err := json.Marshal(contexts)
Expand All @@ -69,10 +30,6 @@ func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, form
case option.FormatMarkdown:
var output []byte

if err != nil {
return nil, errors.WithStack(err)
}

if templateStr != "" {
// ignore
} else if templateFile == "" {
Expand Down Expand Up @@ -107,18 +64,6 @@ func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, form
"unescape": func(s string) template.HTML {
return template.HTML(s)
},
"hashURL": func(longHash string) string {
short := string(longHash[0:7])
if remoteURL == nil {
return short
}

u, _ := url.Parse(remoteURL.String())

u.Path = u.Path + "/commit/" + longHash

return fmt.Sprintf("[`%s`](%s)", short, u.String())
},
})

if t, err := t.Parse(templateStr); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions 4_generator/template/default.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{- define "body" -}}
{{range . -}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}}) (@{{ unescape .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ .Field.Footer.Closes }} {{- end }} {{- end}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{ .HashURL }}) (@{{ unescape .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ .Field.Footer.Closes }} {{- end }} {{- end}}
{{ end }}
{{- end -}}

Expand All @@ -24,7 +24,7 @@
{{if .Revert}}
### 🔙 Revert:
{{range .Revert -}}
- {{if .RevertCommitHash }}revert {{ hashURL .RevertCommitHash }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}})
- {{if .RevertCommitHash }}revert {{ .RevertCommitHashURL }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ .HashURL }})
{{ end }}
{{ end }}

Expand All @@ -45,7 +45,7 @@

### 💪 Commits({{ len .Commits }}):
{{range .Commits -}}
- {{ hashURL .Hash}} - {{ unescape .Field.Title }}
- {{ .HashURL }} - {{ unescape .Field.Title }}
{{ end }}

{{ end }}
6 changes: 3 additions & 3 deletions 4_generator/template/full.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{- define "body" -}}
{{range . -}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}}) (@{{ unescape .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ .Field.Footer.Closes }} {{- end }} {{- end}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{ .HashURL }}) (@{{ unescape .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ .Field.Footer.Closes }} {{- end }} {{- end}}
{{ end }}
{{- end -}}

Expand Down Expand Up @@ -59,7 +59,7 @@
{{if .Revert}}
### 🔙 Revert:
{{range .Revert -}}
- {{if .RevertCommitHash }}revert {{ hashURL .RevertCommitHash }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}})
- {{if .RevertCommitHash }}revert {{ .RevertCommitHashURL }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ .HashURL }})
{{ end }}
{{ end }}

Expand All @@ -80,7 +80,7 @@

### 💪 Commits({{ len .Commits }}):
{{range .Commits -}}
- {{ hashURL .Hash}} - {{ unescape .Field.Title }}
- {{ .HashURL }} - {{ unescape .Field.Title }}
{{ end }}

{{ end }}
4 changes: 2 additions & 2 deletions 4_generator/template/simple.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{{- define "body" -}}
{{range . -}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}}) {{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ .Field.Footer.Closes }} {{- end }} {{- end}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{ .HashURL }}) {{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ .Field.Footer.Closes }} {{- end }} {{- end}}
{{ end }}
{{- end -}}

Expand All @@ -19,7 +19,7 @@
{{if .Revert}}
### 🔙 Revert:
{{range .Revert -}}
- {{if .RevertCommitHash }}revert {{ hashURL .RevertCommitHash }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}})
- {{if .RevertCommitHash }}revert {{ .RevertCommitHashURL }}, {{ end }}{{ unescape .Field.Header.Subject }}({{ .HashURL }})
{{ end }}
{{ end }}

Expand Down
Loading

0 comments on commit 35a348f

Please sign in to comment.