0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-21 22:10:39 +00:00

Merge pull request #28 from bvieira/lint

CI: add golangci to github actions
This commit is contained in:
Beatriz Vieira 2021-07-31 17:56:29 -03:00 committed by GitHub
commit d76920bb5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 146 additions and 104 deletions

View File

@ -8,6 +8,16 @@ on:
jobs:
golangci:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
build:
name: Build
runs-on: ubuntu-latest
@ -16,7 +26,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.13
go-version: ^1.16
id: go
- name: Check out code into the Go module directory

View File

@ -1,26 +1,35 @@
linters:
# disable-all: true
# enable:
# - megacheck
# - govet
enable-all: true
disable:
- scopelint
- paralleltest
- staticcheck
- noctx
- wsl
- lll
- forbidigo
# - prealloc
# presets:
# - bugs
# - unused
# bugs|comment|complexity|error|format|import|metalinter|module|performance|sql|style|test|unused
fast: true
enable:
- tagliatelle
run:
skip-dirs:
- build
- artifacts
linters-settings:
tagliatelle:
case:
use-field-name: true
rules:
json: camel
yaml: kebab
xml: camel
bson: camel
avro: snake
mapstructure: kebab
issues:
exclude-rules:
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
- gochecknoglobals
- testpackage
- path: cmd/git-sv/main.go
linters:
- gochecknoglobals
- funlen

View File

@ -10,13 +10,12 @@ import (
"strings"
"github.com/bvieira/sv4git/sv"
"github.com/imdario/mergo"
"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v3"
)
// EnvConfig env vars for cli configuration
// EnvConfig env vars for cli configuration.
type EnvConfig struct {
Home string `envconfig:"SV4GIT_HOME" default:""`
}
@ -30,7 +29,7 @@ func loadEnvConfig() EnvConfig {
return c
}
// Config cli yaml config
// Config cli yaml config.
type Config struct {
Version string `yaml:"version"`
Versioning sv.VersioningConfig `yaml:"versioning"`
@ -77,8 +76,8 @@ func defaultConfig() Config {
Tag: sv.TagConfig{Pattern: "%d.%d.%d"},
ReleaseNotes: sv.ReleaseNotesConfig{Headers: map[string]string{"fix": "Bug Fixes", "feat": "Features", "breaking-change": "Breaking Changes"}},
Branches: sv.BranchesConfig{
PrefixRegex: "([a-z]+\\/)?",
SuffixRegex: "(-.*)?",
Prefix: "([a-z]+\\/)?",
Suffix: "(-.*)?",
DisableIssue: false,
Skip: []string{"master", "main", "developer"},
SkipDetached: &skipDetached,

View File

@ -10,9 +10,8 @@ import (
"strings"
"time"
"github.com/bvieira/sv4git/sv"
"github.com/Masterminds/semver/v3"
"github.com/bvieira/sv4git/sv"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
@ -73,7 +72,7 @@ func nextVersionHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) f
}
}
func commitLogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *cli.Context) error {
func commitLogHandler(git sv.Git) func(c *cli.Context) error {
return func(c *cli.Context) error {
var commits []sv.GitCommitLog
var err error
@ -149,8 +148,11 @@ func commitNotesHandler(git sv.Git, rnProcessor sv.ReleaseNoteProcessor, outputF
date, _ = time.Parse("2006-01-02", commits[0].Date)
}
releasenote := rnProcessor.Create(nil, date, commits)
fmt.Println(outputFormatter.FormatReleaseNote(releasenote))
output, err := outputFormatter.FormatReleaseNote(rnProcessor.Create(nil, date, commits))
if err != nil {
return fmt.Errorf("could not format release notes, message: %v", err)
}
fmt.Println(output)
return nil
}
}
@ -163,7 +165,7 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
var err error
if tag := c.String("t"); tag != "" {
rnVersion, date, commits, err = getTagVersionInfo(git, semverProcessor, tag)
rnVersion, date, commits, err = getTagVersionInfo(git, tag)
} else {
// TODO: should generate release notes if version was not updated?
rnVersion, _, date, commits, err = getNextVersionInfo(git, semverProcessor)
@ -174,12 +176,16 @@ func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor,
}
releasenote := rnProcessor.Create(&rnVersion, date, commits)
fmt.Println(outputFormatter.FormatReleaseNote(releasenote))
output, err := outputFormatter.FormatReleaseNote(releasenote)
if err != nil {
return fmt.Errorf("could not format release notes, message: %v", err)
}
fmt.Println(output)
return nil
}
}
func getTagVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, tag string) (semver.Version, time.Time, []sv.GitCommitLog, error) {
func getTagVersionInfo(git sv.Git, tag string) (semver.Version, time.Time, []sv.GitCommitLog, error) {
tagVersion, err := sv.ToVersion(tag)
if err != nil {
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error parsing version: %s from tag, message: %v", tag, err)
@ -281,7 +287,7 @@ func getCommitScope(cfg Config, p sv.MessageProcessor, input string, noScope boo
return input, p.ValidateScope(input)
}
func getCommitDescription(cfg Config, p sv.MessageProcessor, input string) (string, error) {
func getCommitDescription(p sv.MessageProcessor, input string) (string, error) {
if input == "" {
return promptSubject()
}
@ -366,7 +372,7 @@ func commitHandler(cfg Config, git sv.Git, messageProcessor sv.MessageProcessor)
return err
}
subject, err := getCommitDescription(cfg, messageProcessor, inputDescription)
subject, err := getCommitDescription(messageProcessor, inputDescription)
if err != nil {
return err
}
@ -443,7 +449,11 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP
releaseNotes = append(releaseNotes, rnProcessor.Create(&currentVer, tag.Date, commits))
}
fmt.Println(formatter.FormatChangelog(releaseNotes))
output, err := formatter.FormatChangelog(releaseNotes)
if err != nil {
return fmt.Errorf("could not format changelog, message: %v", err)
}
fmt.Println(output)
return nil
}
@ -455,12 +465,12 @@ func validateCommitMessageHandler(git sv.Git, messageProcessor sv.MessageProcess
detached, derr := git.IsDetached()
if messageProcessor.SkipBranch(branch, derr == nil && detached) {
warn("commit message validation skipped, branch in ignore list or detached...")
warnf("commit message validation skipped, branch in ignore list or detached...")
return nil
}
if source := c.String("source"); source == "merge" {
warn("commit message validation skipped, ignoring source: %s...", source)
warnf("commit message validation skipped, ignoring source: %s...", source)
return nil
}
@ -477,7 +487,7 @@ func validateCommitMessageHandler(git sv.Git, messageProcessor sv.MessageProcess
msg, err := messageProcessor.Enhance(branch, commitMessage)
if err != nil {
warn("could not enhance commit message, %s", err.Error())
warnf("could not enhance commit message, %s", err.Error())
return nil
}
if msg == "" {

View File

@ -2,6 +2,6 @@ package main
import "fmt"
func warn(format string, values ...interface{}) {
func warnf(format string, values ...interface{}) {
fmt.Printf("WARN: "+format+"\n", values...)
}

View File

@ -6,11 +6,10 @@ import (
"path/filepath"
"github.com/bvieira/sv4git/sv"
"github.com/urfave/cli/v2"
)
// Version for git-sv
// Version for git-sv.
var Version = ""
const (
@ -92,7 +91,7 @@ func main() {
Aliases: []string{"cl"},
Usage: "list all commit logs according to range as jsons",
Description: "The range filter is used based on git log filters, check https://git-scm.com/docs/git-log for more info. When flag range is \"tag\" and start is empty, last tag created will be used instead. When flag range is \"date\", if \"end\" is YYYY-MM-DD the range will be inclusive.",
Action: commitLogHandler(git, semverProcessor),
Action: commitLogHandler(git),
Flags: []cli.Flag{
&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"},
&cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash", Value: string(sv.TagRange)},
@ -165,8 +164,7 @@ func main() {
},
}
apperr := app.Run(os.Args)
if apperr != nil {
if apperr := app.Run(os.Args); apperr != nil {
log.Fatal(apperr)
}
}

View File

@ -40,8 +40,8 @@ type CommitMessageIssueConfig struct {
// BranchesConfig branches preferences.
type BranchesConfig struct {
PrefixRegex string `yaml:"prefix"`
SuffixRegex string `yaml:"suffix"`
Prefix string `yaml:"prefix"`
Suffix string `yaml:"suffix"`
DisableIssue bool `yaml:"disable-issue"`
Skip []string `yaml:"skip,flow"`
SkipDetached *bool `yaml:"skip-detached"`

View File

@ -51,8 +51,8 @@ const (
// OutputFormatter output formatter interface.
type OutputFormatter interface {
FormatReleaseNote(releasenote ReleaseNote) string
FormatChangelog(releasenotes []ReleaseNote) string
FormatReleaseNote(releasenote ReleaseNote) (string, error)
FormatChangelog(releasenotes []ReleaseNote) (string, error)
}
// OutputFormatterImpl formater for release note and changelog.
@ -72,31 +72,35 @@ func NewOutputFormatter() *OutputFormatterImpl {
}
// FormatReleaseNote format a release note.
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) string {
func (p OutputFormatterImpl) FormatReleaseNote(releasenote ReleaseNote) (string, error) {
var b bytes.Buffer
p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote))
return b.String()
if err := p.releasenoteTemplate.Execute(&b, releaseNoteVariables(releasenote)); err != nil {
return "", err
}
return b.String(), nil
}
// FormatChangelog format a changelog
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) string {
var templateVars []releaseNoteTemplateVariables
for _, v := range releasenotes {
templateVars = append(templateVars, releaseNoteVariables(v))
// FormatChangelog format a changelog.
func (p OutputFormatterImpl) FormatChangelog(releasenotes []ReleaseNote) (string, error) {
templateVars := make([]releaseNoteTemplateVariables, len(releasenotes))
for i, v := range releasenotes {
templateVars[i] = releaseNoteVariables(v)
}
var b bytes.Buffer
p.changelogTemplate.Execute(&b, templateVars)
return b.String()
if err := p.changelogTemplate.Execute(&b, templateVars); err != nil {
return "", err
}
return b.String(), nil
}
func releaseNoteVariables(releasenote ReleaseNote) releaseNoteTemplateVariables {
var date = ""
date := ""
if !releasenote.Date.IsZero() {
date = releasenote.Date.Format("2006-01-02")
}
var version = ""
version := ""
if releasenote.Version != nil {
version = releasenote.Version.String()
}

View File

@ -9,10 +9,13 @@ import (
var dateChangelog = `## v1.0.0 (2020-05-01)
`
var emptyDateChangelog = `## v1.0.0
`
var emptyVersionChangelog = `## 2020-05-01
`
var fullChangeLog = `## v1.0.0 (2020-05-01)
### Features
@ -39,17 +42,23 @@ func TestOutputFormatterImpl_FormatReleaseNote(t *testing.T) {
name string
input ReleaseNote
want string
wantErr bool
}{
{"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog},
{"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog},
{"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog},
{"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog},
{"with date", emptyReleaseNote("1.0.0", date.Truncate(time.Minute)), dateChangelog, false},
{"without date", emptyReleaseNote("1.0.0", time.Time{}.Truncate(time.Minute)), emptyDateChangelog, false},
{"without version", emptyReleaseNote("", date.Truncate(time.Minute)), emptyVersionChangelog, false},
{"full changelog", fullReleaseNote("1.0.0", date.Truncate(time.Minute)), fullChangeLog, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewOutputFormatter().FormatReleaseNote(tt.input); got != tt.want {
got, err := NewOutputFormatter().FormatReleaseNote(tt.input)
if got != tt.want {
t.Errorf("OutputFormatterImpl.FormatReleaseNote() = %v, want %v", got, tt.want)
}
if (err != nil) != tt.wantErr {
t.Errorf("OutputFormatterImpl.FormatReleaseNote() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

View File

@ -18,7 +18,7 @@ const (
endLine = "~~"
)
// Git commands
// Git commands.
type Git interface {
LastTag() string
Log(lr LogRange) ([]GitCommitLog, error)
@ -29,48 +29,48 @@ type Git interface {
IsDetached() (bool, error)
}
// GitCommitLog description of a single commit log
// GitCommitLog description of a single commit log.
type GitCommitLog struct {
Date string `json:"date,omitempty"`
Hash string `json:"hash,omitempty"`
Message CommitMessage `json:"message,omitempty"`
}
// GitTag git tag info
// GitTag git tag info.
type GitTag struct {
Name string
Date time.Time
}
// LogRangeType type of log range
// LogRangeType type of log range.
type LogRangeType string
// constants for log range type
// constants for log range type.
const (
TagRange LogRangeType = "tag"
DateRange = "date"
HashRange = "hash"
DateRange LogRangeType = "date"
HashRange LogRangeType = "hash"
)
// LogRange git log range
// LogRange git log range.
type LogRange struct {
rangeType LogRangeType
start string
end string
}
// NewLogRange LogRange constructor
// NewLogRange LogRange constructor.
func NewLogRange(t LogRangeType, start, end string) LogRange {
return LogRange{rangeType: t, start: start, end: end}
}
// GitImpl git command implementation
// GitImpl git command implementation.
type GitImpl struct {
messageProcessor MessageProcessor
tagCfg TagConfig
}
// NewGit constructor
// NewGit constructor.
func NewGit(messageProcessor MessageProcessor, cfg TagConfig) *GitImpl {
return &GitImpl{
messageProcessor: messageProcessor,
@ -78,7 +78,7 @@ func NewGit(messageProcessor MessageProcessor, cfg TagConfig) *GitImpl {
}
}
// LastTag get last tag, if no tag found, return empty
// LastTag get last tag, if no tag found, return empty.
func (GitImpl) LastTag() string {
cmd := exec.Command("git", "for-each-ref", "refs/tags", "--sort", "-creatordate", "--format", "%(refname:short)", "--count", "1")
out, err := cmd.CombinedOutput()
@ -88,7 +88,7 @@ func (GitImpl) LastTag() string {
return strings.TrimSpace(strings.Trim(string(out), "\n"))
}
// Log return git log
// Log return git log.
func (g GitImpl) Log(lr LogRange) ([]GitCommitLog, error) {
format := "--pretty=format:\"%ad" + logSeparator + "%h" + logSeparator + "%s" + logSeparator + "%b" + endLine + "\""
params := []string{"log", "--date=short", format}
@ -114,7 +114,7 @@ func (g GitImpl) Log(lr LogRange) ([]GitCommitLog, error) {
return parseLogOutput(g.messageProcessor, string(out)), nil
}
// Commit runs git commit
// Commit runs git commit.
func (g GitImpl) Commit(header, body, footer string) error {
cmd := exec.Command("git", "commit", "-m", header, "-m", "", "-m", body, "-m", "", "-m", footer)
cmd.Stdout = os.Stdout
@ -122,7 +122,7 @@ func (g GitImpl) Commit(header, body, footer string) error {
return cmd.Run()
}
// Tag create a git tag
// Tag create a git tag.
func (g GitImpl) Tag(version semver.Version) error {
tag := fmt.Sprintf(g.tagCfg.Pattern, version.Major(), version.Minor(), version.Patch())
tagMsg := fmt.Sprintf("Version %d.%d.%d", version.Major(), version.Minor(), version.Patch())
@ -136,7 +136,7 @@ func (g GitImpl) Tag(version semver.Version) error {
return pushCommand.Run()
}
// Tags list repository tags
// Tags list repository tags.
func (g GitImpl) Tags() ([]GitTag, error) {
cmd := exec.Command("git", "for-each-ref", "--sort", "creatordate", "--format", "%(creatordate:iso8601)#%(refname:short)", "refs/tags")
out, err := cmd.CombinedOutput()
@ -146,7 +146,7 @@ func (g GitImpl) Tags() ([]GitTag, error) {
return parseTagsOutput(string(out))
}
// Branch get git branch
// Branch get git branch.
func (GitImpl) Branch() string {
cmd := exec.Command("git", "symbolic-ref", "--short", "HEAD")
out, err := cmd.CombinedOutput()

View File

@ -23,7 +23,7 @@ type CommitMessage struct {
Metadata map[string]string `json:"metadata,omitempty"`
}
// NewCommitMessage commit message constructor
// NewCommitMessage commit message constructor.
func NewCommitMessage(ctype, scope, description, body, issue, breakingChanges string) CommitMessage {
metadata := make(map[string]string)
if issue != "" {
@ -58,7 +58,7 @@ type MessageProcessor interface {
Parse(subject, body string) CommitMessage
}
// NewMessageProcessor MessageProcessorImpl constructor
// NewMessageProcessor MessageProcessorImpl constructor.
func NewMessageProcessor(mcfg CommitMessageConfig, bcfg BranchesConfig) *MessageProcessorImpl {
return &MessageProcessorImpl{
messageCfg: mcfg,
@ -82,7 +82,7 @@ func (p MessageProcessorImpl) Validate(message string) error {
subject, body := splitCommitMessageContent(message)
msg := p.Parse(subject, body)
if !regexp.MustCompile("^[a-z+]+(\\(.+\\))?!?: .+$").MatchString(subject) {
if !regexp.MustCompile(`^[a-z+]+(\(.+\))?!?: .+$`).MatchString(subject) {
return fmt.Errorf("subject [%s] should be valid according with conventional commits", subject)
}
@ -160,7 +160,7 @@ func (p MessageProcessorImpl) IssueID(branch string) (string, error) {
return "", nil
}
rstr := fmt.Sprintf("^%s(%s)%s$", p.branchesCfg.PrefixRegex, p.messageCfg.Issue.Regex, p.branchesCfg.SuffixRegex)
rstr := fmt.Sprintf("^%s(%s)%s$", p.branchesCfg.Prefix, p.messageCfg.Issue.Regex, p.branchesCfg.Suffix)
r, err := regexp.Compile(rstr)
if err != nil {
return "", fmt.Errorf("could not compile issue regex: %s, error: %v", rstr, err.Error())
@ -229,7 +229,7 @@ func (p MessageProcessorImpl) Parse(subject, body string) CommitMessage {
}
func parseSubjectMessage(message string) (string, string, string, bool) {
regex := regexp.MustCompile("([a-z]+)(\\((.*)\\))?(!)?: (.*)")
regex := regexp.MustCompile(`([a-z]+)(\((.*)\))?(!)?: (.*)`)
result := regex.FindStringSubmatch(message)
if len(result) != 6 {
return "", "", message, false

View File

@ -55,14 +55,14 @@ var ccfgWithScope = CommitMessageConfig{
func newBranchCfg(skipDetached bool) BranchesConfig {
return BranchesConfig{
PrefixRegex: "([a-z]+\\/)?",
SuffixRegex: "(-.*)?",
Prefix: "([a-z]+\\/)?",
Suffix: "(-.*)?",
Skip: []string{"develop", "master"},
SkipDetached: &skipDetached,
}
}
// messages samples start
// messages samples start.
var fullMessage = `fix: correct minor typos in code
see the issue for details
@ -71,6 +71,7 @@ on typos fixed.
Reviewed-by: Z
Refs #133`
var fullMessageWithJira = `fix: correct minor typos in code
see the issue for details
@ -80,6 +81,7 @@ on typos fixed.
Reviewed-by: Z
Refs #133
jira: JIRA-456`
var fullMessageRefs = `fix: correct minor typos in code
see the issue for details
@ -87,11 +89,13 @@ see the issue for details
on typos fixed.
Refs #133`
var subjectAndBodyMessage = `fix: correct minor typos in code
see the issue for details
on typos fixed.`
var subjectAndFooterMessage = `refactor!: drop support for Node 6
BREAKING CHANGE: refactor to use JavaScript features not available in Node 6.`
@ -470,7 +474,6 @@ func Test_splitCommitMessageContent(t *testing.T) {
}
}
//commitType, scope, description, hasBreakingChange
func Test_parseSubjectMessage(t *testing.T) {
tests := []struct {
name string

View File

@ -55,7 +55,7 @@ type ReleaseNote struct {
BreakingChanges BreakingChangeSection
}
// BreakingChangeSection breaking change section
// BreakingChangeSection breaking change section.
type BreakingChangeSection struct {
Name string
Messages []string

View File

@ -11,7 +11,7 @@ const (
major
)
// ToVersion parse string to semver.Version
// ToVersion parse string to semver.Version.
func ToVersion(value string) (semver.Version, error) {
version := value
if version == "" {
@ -24,12 +24,12 @@ func ToVersion(value string) (semver.Version, error) {
return *v, nil
}
// SemVerCommitsProcessor interface
// SemVerCommitsProcessor interface.
type SemVerCommitsProcessor interface {
NextVersion(version semver.Version, commits []GitCommitLog) (semver.Version, bool)
}
// SemVerCommitsProcessorImpl process versions using commit log
// SemVerCommitsProcessorImpl process versions using commit log.
type SemVerCommitsProcessorImpl struct {
MajorVersionTypes map[string]struct{}
MinorVersionTypes map[string]struct{}
@ -38,7 +38,7 @@ type SemVerCommitsProcessorImpl struct {
IncludeUnknownTypeAsPatch bool
}
// NewSemVerCommitsProcessor SemanticVersionCommitsProcessorImpl constructor
// NewSemVerCommitsProcessor SemanticVersionCommitsProcessorImpl constructor.
func NewSemVerCommitsProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig) *SemVerCommitsProcessorImpl {
return &SemVerCommitsProcessorImpl{
IncludeUnknownTypeAsPatch: !vcfg.IgnoreUnknown,
@ -49,9 +49,9 @@ func NewSemVerCommitsProcessor(vcfg VersioningConfig, mcfg CommitMessageConfig)
}
}
// NextVersion calculates next version based on commit log
// NextVersion calculates next version based on commit log.
func (p SemVerCommitsProcessorImpl) NextVersion(version semver.Version, commits []GitCommitLog) (semver.Version, bool) {
var versionToUpdate = none
versionToUpdate := none
for _, commit := range commits {
if v := p.versionTypeToUpdate(commit); v > versionToUpdate {
versionToUpdate = v