Skip to content

Commit

Permalink
feat: link commit for generation
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 23, 2020
1 parent 7553570 commit b9432db
Show file tree
Hide file tree
Showing 4 changed files with 793 additions and 732 deletions.
33 changes: 33 additions & 0 deletions 4_generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package generator
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/url"
"os"
"path"
"strings"
Expand All @@ -15,6 +17,25 @@ import (
)

func Generate(g *client.GitClient, contexts []*transformer.TemplateContext, format string, preset string, templateFile 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 {
// prefer github and gitlab
if strings.HasPrefix(urlStr, "https://github.com") || strings.HasPrefix(urlStr, "https://gitlab.com") {
if remoteURL, err = url.Parse(urlStr); err != nil {
return nil, errors.WithStack(err)
}
break
}
}
}

switch format {
case "json":
Expand Down Expand Up @@ -66,6 +87,18 @@ 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
5 changes: 2 additions & 3 deletions 4_generator/template_default.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package generator

const DEFAULT_TEMPLATE = `# {{ .Version }}
{{- define "body" -}}
{{range . -}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{.Short}}) (thanks @{{ unescape .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ stringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end}}
- {{if .Field.Header.Scope }}**{{ unescape .Field.Header.Scope }}**: {{ end }}{{ unescape .Field.Header.Subject }}({{ hashURL .Hash}}) (thanks @{{ unescape .Author.Name }}){{if .Field.Footer }} {{if .Field.Footer.Closes }}, Closes: {{ stringsJoin .Field.Footer.Closes "," }} {{- end }} {{- end}}
{{ end }}
{{- end -}}
Expand Down Expand Up @@ -33,5 +32,5 @@ const DEFAULT_TEMPLATE = `# {{ .Version }}
### Commits({{ len .Commits }}):
{{range .Commits -}}
- **{{ .Short }}** {{ unescape .Field.Title }}
- {{ hashURL .Hash }} {{ unescape .Field.Title }}
{{ end }}`
Loading

0 comments on commit b9432db

Please sign in to comment.