This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
changelog.go
91 lines (75 loc) · 1.83 KB
/
changelog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package changelog
import (
"fmt"
"github.com/NoUseFreak/letitgo/internal/app/action"
"github.com/NoUseFreak/letitgo/internal/app/config"
"github.com/NoUseFreak/letitgo/internal/app/ui"
"github.com/NoUseFreak/letitgo/internal/app/utils"
gitclient "github.com/NoUseFreak/letitgo/internal/app/utils/git"
"gopkg.in/src-d/go-git.v4"
)
// New returns an action for changelog
func New() action.Action {
return &changelog{}
}
type changelog struct {
File string
Message string
}
func (a *changelog) Name() string {
return "changelog"
}
func (a *changelog) GetInitConfig() map[string]interface{} {
return map[string]interface{}{
"file": "CHANGELOG.md",
}
}
func (a *changelog) Weight() int {
return 5
}
func (a *changelog) Execute(cfg config.LetItGoConfig) error {
if a.File == "" {
a.File = "CHANGELOG.md"
}
if a.Message == "" {
a.Message = "Update changelog\n[skip ci]"
}
r, err := git.PlainOpen(".")
if err != nil {
return fmt.Errorf("unable to find git repo - %s", err.Error())
}
if lastCommitIsChangelog(r, a.Message, a.File) {
ui.Info("Skipping changelog")
return nil
}
tree, err := buildReleaseBlocks(r, []string{a.Message})
if err != nil {
return fmt.Errorf("unable to build release blocks - %s", err.Error())
}
vars := struct {
Blocks []releaseBlock
}{
Blocks: *tree,
}
out, err := templateChangelog(vars)
if err != nil {
return fmt.Errorf("unable to template changelog - %s", err.Error())
}
repo, err := gitclient.GetRemote(".")
if err != nil {
return fmt.Errorf("unable to resolve remote - %s", err.Error())
}
ui.Trace(out)
ui.Step("Publishing %s", a.File)
if err := utils.WriteFile(a.File, out); err != nil {
return err
}
client, err := gitclient.NewClient(
repo,
utils.DryRun.IsEnabled(),
)
if err != nil {
return err
}
return client.PublishFile(a.File, out, a.Message, nil)
}