github.com/brandonmanuel/git-chglog@v0.0.0-20200903004639-7a62fa08787a/cmd/git-chglog/config.go (about)

     1  package main
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	chglog "github.com/BrandonManuel/git-chglog"
     8  	"github.com/imdario/mergo"
     9  )
    10  
    11  // Info ...
    12  type Info struct {
    13  	Title         string `yaml:"title"`
    14  	RepositoryURL string `yaml:"repository_url"`
    15  }
    16  
    17  // CommitOptions ...
    18  type CommitOptions struct {
    19  	Filters map[string][]string `yaml:"filters"`
    20  	SortBy  string              `yaml:"sort_by"`
    21  }
    22  
    23  // CommitGroupOptions ...
    24  type CommitGroupOptions struct {
    25  	GroupBy   string            `yaml:"group_by"`
    26  	SortBy    string            `yaml:"sort_by"`
    27  	TitleMaps map[string]string `yaml:"title_maps"`
    28  }
    29  
    30  // PatternOptions ...
    31  type PatternOptions struct {
    32  	Pattern     string   `yaml:"pattern"`
    33  	PatternMaps []string `yaml:"pattern_maps"`
    34  }
    35  
    36  // IssueOptions ...
    37  type IssueOptions struct {
    38  	Prefix []string `yaml:"prefix"`
    39  }
    40  
    41  // RefOptions ...
    42  type RefOptions struct {
    43  	Actions []string `yaml:"actions"`
    44  }
    45  
    46  // NoteOptions ...
    47  type NoteOptions struct {
    48  	Keywords []string `yaml:"keywords"`
    49  }
    50  
    51  // Options ...
    52  type Options struct {
    53  	Commits      CommitOptions      `yaml:"commits"`
    54  	CommitGroups CommitGroupOptions `yaml:"commit_groups"`
    55  	Header       PatternOptions     `yaml:"header"`
    56  	Issues       IssueOptions       `yaml:"issues"`
    57  	Refs         RefOptions         `yaml:"refs"`
    58  	Merges       PatternOptions     `yaml:"merges"`
    59  	Reverts      PatternOptions     `yaml:"reverts"`
    60  	Notes        NoteOptions        `yaml:"notes"`
    61  }
    62  
    63  // Config ...
    64  type Config struct {
    65  	Bin      string  `yaml:"bin"`
    66  	Template string  `yaml:"template"`
    67  	Style    string  `yaml:"style"`
    68  	Info     Info    `yaml:"info"`
    69  	Options  Options `yaml:"options"`
    70  }
    71  
    72  // Normalize ...
    73  func (config *Config) Normalize(ctx *CLIContext) error {
    74  	err := mergo.Merge(config, &Config{
    75  		Bin:      "git",
    76  		Template: "CHANGELOG.tpl.md",
    77  		Info: Info{
    78  			Title: "CHANGELOG",
    79  		},
    80  		Options: Options{
    81  			Commits: CommitOptions{
    82  				SortBy: "Scope",
    83  			},
    84  			CommitGroups: CommitGroupOptions{
    85  				GroupBy: "Type",
    86  				SortBy:  "Title",
    87  			},
    88  		},
    89  	})
    90  
    91  	if err != nil {
    92  		return err
    93  	}
    94  
    95  	config.Info.RepositoryURL = strings.TrimRight(config.Info.RepositoryURL, "/")
    96  
    97  	if !filepath.IsAbs(config.Template) {
    98  		config.Template = filepath.Join(filepath.Dir(ctx.ConfigPath), config.Template)
    99  	}
   100  
   101  	config.normalizeStyle()
   102  
   103  	return nil
   104  }
   105  
   106  // Normalize style
   107  func (config *Config) normalizeStyle() {
   108  	switch config.Style {
   109  	case "github":
   110  		config.normalizeStyleOfGitHub()
   111  	case "gitlab":
   112  		config.normalizeStyleOfGitLab()
   113  	case "bitbucket":
   114  		config.normalizeStyleOfBitbucket()
   115  	}
   116  }
   117  
   118  // For GitHub
   119  func (config *Config) normalizeStyleOfGitHub() {
   120  	opts := config.Options
   121  
   122  	if len(opts.Issues.Prefix) == 0 {
   123  		opts.Issues.Prefix = []string{
   124  			"#",
   125  			"gh-",
   126  		}
   127  	}
   128  
   129  	if len(opts.Refs.Actions) == 0 {
   130  		opts.Refs.Actions = []string{
   131  			"close",
   132  			"closes",
   133  			"closed",
   134  			"fix",
   135  			"fixes",
   136  			"fixed",
   137  			"resolve",
   138  			"resolves",
   139  			"resolved",
   140  		}
   141  	}
   142  
   143  	if opts.Merges.Pattern == "" && len(opts.Merges.PatternMaps) == 0 {
   144  		opts.Merges.Pattern = "^Merge pull request #(\\d+) from (.*)$"
   145  		opts.Merges.PatternMaps = []string{
   146  			"Ref",
   147  			"Source",
   148  		}
   149  	}
   150  
   151  	config.Options = opts
   152  }
   153  
   154  // For GitLab
   155  func (config *Config) normalizeStyleOfGitLab() {
   156  	opts := config.Options
   157  
   158  	if len(opts.Issues.Prefix) == 0 {
   159  		opts.Issues.Prefix = []string{
   160  			"#",
   161  		}
   162  	}
   163  
   164  	if len(opts.Refs.Actions) == 0 {
   165  		opts.Refs.Actions = []string{
   166  			"close",
   167  			"closes",
   168  			"closed",
   169  			"closing",
   170  			"fix",
   171  			"fixes",
   172  			"fixed",
   173  			"fixing",
   174  			"resolve",
   175  			"resolves",
   176  			"resolved",
   177  			"resolving",
   178  		}
   179  	}
   180  
   181  	if opts.Merges.Pattern == "" && len(opts.Merges.PatternMaps) == 0 {
   182  		opts.Merges.Pattern = "^Merge branch '.*' into '(.*)'$"
   183  		opts.Merges.PatternMaps = []string{
   184  			"Source",
   185  		}
   186  	}
   187  
   188  	config.Options = opts
   189  }
   190  
   191  // For Bitbucket
   192  func (config *Config) normalizeStyleOfBitbucket() {
   193  	opts := config.Options
   194  
   195  	if len(opts.Issues.Prefix) == 0 {
   196  		opts.Issues.Prefix = []string{
   197  			"#",
   198  		}
   199  	}
   200  
   201  	if len(opts.Refs.Actions) == 0 {
   202  		opts.Refs.Actions = []string{
   203  			"close",
   204  			"closes",
   205  			"closed",
   206  			"closing",
   207  			"fix",
   208  			"fixed",
   209  			"fixes",
   210  			"fixing",
   211  			"resolve",
   212  			"resolves",
   213  			"resolved",
   214  			"resolving",
   215  			"eopen",
   216  			"reopens",
   217  			"reopening",
   218  			"hold",
   219  			"holds",
   220  			"holding",
   221  			"wontfix",
   222  			"invalidate",
   223  			"invalidates",
   224  			"invalidated",
   225  			"invalidating",
   226  			"addresses",
   227  			"re",
   228  			"references",
   229  			"ref",
   230  			"refs",
   231  			"see",
   232  		}
   233  	}
   234  
   235  	if opts.Merges.Pattern == "" && len(opts.Merges.PatternMaps) == 0 {
   236  		opts.Merges.Pattern = "^Merged in (.*) \\(pull request #(\\d+)\\)$"
   237  		opts.Merges.PatternMaps = []string{
   238  			"Source",
   239  			"Ref",
   240  		}
   241  	}
   242  
   243  	config.Options = opts
   244  }
   245  
   246  // Convert ...
   247  func (config *Config) Convert(ctx *CLIContext) *chglog.Config {
   248  	info := config.Info
   249  	opts := config.Options
   250  
   251  	return &chglog.Config{
   252  		Bin:        config.Bin,
   253  		WorkingDir: ctx.WorkingDir,
   254  		Template:   config.Template,
   255  		Info: &chglog.Info{
   256  			Title:         info.Title,
   257  			RepositoryURL: info.RepositoryURL,
   258  		},
   259  		Options: &chglog.Options{
   260  			NextTag:              ctx.NextTag,
   261  			TagFilterPattern:     ctx.TagFilterPattern,
   262  			NoCaseSensitive:      ctx.NoCaseSensitive,
   263  			CommitFilters:        opts.Commits.Filters,
   264  			CommitSortBy:         opts.Commits.SortBy,
   265  			CommitGroupBy:        opts.CommitGroups.GroupBy,
   266  			CommitGroupSortBy:    opts.CommitGroups.SortBy,
   267  			CommitGroupTitleMaps: opts.CommitGroups.TitleMaps,
   268  			HeaderPattern:        opts.Header.Pattern,
   269  			HeaderPatternMaps:    opts.Header.PatternMaps,
   270  			IssuePrefix:          opts.Issues.Prefix,
   271  			RefActions:           opts.Refs.Actions,
   272  			MergePattern:         opts.Merges.Pattern,
   273  			MergePatternMaps:     opts.Merges.PatternMaps,
   274  			RevertPattern:        opts.Reverts.Pattern,
   275  			RevertPatternMaps:    opts.Reverts.PatternMaps,
   276  			NoteKeywords:         opts.Notes.Keywords,
   277  		},
   278  	}
   279  }