changelog/service/service.go
jolheiser 70c955ae17
All checks were successful
goreleaser
Title-case PR title (#76)
Resolves #60

Extracts the previous trim into a new func as well to keep any title munging consistent.

Reviewed-on: #76
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.io>
Co-authored-by: jolheiser <john.olheiser@gmail.com>
Co-committed-by: jolheiser <john.olheiser@gmail.com>
2023-03-22 04:35:58 +08:00

102 lines
2.2 KiB
Go

// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package service
import (
"fmt"
"strings"
"unicode"
)
const defaultGitea = "https://gitea.com"
// New returns a service from a string
func New(serviceType, repo, baseURL, milestone, tag, token string, issues bool) (Service, error) {
if len(tag) == 0 {
tag = milestone
}
switch strings.ToLower(serviceType) {
case "github":
return &GitHub{
Milestone: milestone,
GitTag: tag,
Token: token,
Repo: repo,
Issues: issues,
}, nil
case "gitea":
ownerRepo := strings.Split(repo, "/")
if strings.TrimSpace(baseURL) == "" {
baseURL = defaultGitea
}
return &Gitea{
Milestone: milestone,
GitTag: tag,
Token: token,
BaseURL: baseURL,
Owner: ownerRepo[0],
Repo: ownerRepo[1],
Issues: issues,
}, nil
default:
return nil, fmt.Errorf("unknown service type %s", serviceType)
}
}
// Service defines how a struct can be a Changelog Service
type Service interface {
Generate() (string, []Entry, error)
Contributors() (ContributorList, error)
}
// Label is the minimum information needed for a PR label
type Label struct {
Name string
}
// Entry is the minimum information needed to make a changelog entry
type Entry struct {
Title string
Index int64
Labels []Label
}
// Contributor is a project contributor
type Contributor struct {
Name string
Profile string
}
// ContributorList is a slice of Contributors that can be sorted
type ContributorList []Contributor
// Len is the length of the ContributorList
func (cl ContributorList) Len() int {
return len(cl)
}
// Less determines whether a Contributor comes before another Contributor
func (cl ContributorList) Less(i, j int) bool {
return cl[i].Name < cl[j].Name
}
// Swap swaps Contributors in a ContributorList
func (cl ContributorList) Swap(i, j int) {
cl[i], cl[j] = cl[j], cl[i]
}
// CleanTitle returns the string with spaces trimmed and the first rune title-cased
func CleanTitle(s string) string {
s = strings.TrimSpace(s)
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
s = string(r)
return s
}