feat: get release tag from msg scope (#49)
All checks were successful
build / build (push) Successful in 1m46s
build / trigger-build-image (push) Successful in 6s

Check release tag from msg scope.

Co-authored-by: zzjc1234 <2359047351@qq.com>
Reviewed-on: #49
Reviewed-by: 张泊明518370910136 <bomingzh@sjtu.edu.cn>
Co-authored-by: 周赵嘉程521432910016 <zzjc123@sjtu.edu.cn>
Co-committed-by: 周赵嘉程521432910016 <zzjc123@sjtu.edu.cn>
This commit is contained in:
周赵嘉程521432910016 2024-10-08 11:16:14 +08:00 committed by 张泊明518370910136
parent 70012012c4
commit 3f9ec1a71d
2 changed files with 40 additions and 23 deletions

View File

@ -41,13 +41,11 @@ var Version string
func main() { func main() {
var gitWhitelist, metaFile []string var gitWhitelist, metaFile []string
showVersion := flag.Bool("version", false, "print current version") showVersion := flag.Bool("version", false, "print current version")
checkRelease := flag.Bool("checkRelease", true, "trigger release check")
rootDir := flag.String("root", "", "") rootDir := flag.String("root", "", "")
repo := flag.String("repo", "", "") repo := flag.String("repo", "", "")
localList := flag.String("localList", "", "") localList := flag.String("localList", "", "")
droneBranch := flag.String("droneBranch", "", "") droneBranch := flag.String("droneBranch", "", "")
releaseCategories := flag.String("releaseCategories", "", "")
releaseNumber := flag.Int("releaseNumber", 0, "")
mileStoneNumber := flag.Int("mileStoneNumber", 0, "")
checkFileNameList := flag.String("checkFileNameList", "", "Comma-separated list of files to check.") checkFileNameList := flag.String("checkFileNameList", "", "Comma-separated list of files to check.")
checkFileSumList := flag.String("checkFileSumList", "", "Comma-separated list of expected checksums.") checkFileSumList := flag.String("checkFileSumList", "", "Comma-separated list of expected checksums.")
parseMultiValueFlag(&gitWhitelist, "whitelist", "") parseMultiValueFlag(&gitWhitelist, "whitelist", "")
@ -79,7 +77,7 @@ func main() {
if err != nil { if err != nil {
fmt.Printf("### Non-ASCII Characters Commit Message Check Failed:\n%s\n", err.Error()) fmt.Printf("### Non-ASCII Characters Commit Message Check Failed:\n%s\n", err.Error())
} }
err = healthcheck.CheckTags(*rootDir, *releaseCategories, *releaseNumber, *mileStoneNumber) err = healthcheck.CheckTags(*rootDir, *checkRelease)
if err != nil { if err != nil {
fmt.Printf("### Release Tag Check Failed:\n%s\n", err.Error()) fmt.Printf("### Release Tag Check Failed:\n%s\n", err.Error())
} }

View File

@ -2,12 +2,42 @@ package healthcheck
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
"github.com/joint-online-judge/JOJ3/cmd/joj3/conf"
) )
func parseConventionalCommit(commit string) (*conf.ConventionalCommit, error) {
re := regexp.MustCompile(`(?s)^(\w+)(\(([^)]+)\))?!?: (.+?)(\n\n(.+?))?(\n\n(.+))?$`)
matches := re.FindStringSubmatch(strings.TrimSpace(commit))
if matches == nil {
return nil, fmt.Errorf("invalid conventional commit format")
}
cc := &conf.ConventionalCommit{
Type: matches[1],
Scope: matches[3],
Description: strings.TrimSpace(matches[4]),
Body: strings.TrimSpace(matches[6]),
Footer: strings.TrimSpace(matches[8]),
}
return cc, nil
}
func getTagFromMsg() (tag string, err error) {
msg, err := conf.GetCommitMsg()
if err != nil {
return "", err
}
conventionalCommit, err := parseConventionalCommit(msg)
if err != nil {
return "", err
}
return conventionalCommit.Scope, err
}
func getTagsFromRepo(repoPath string) ([]string, error) { func getTagsFromRepo(repoPath string) ([]string, error) {
repo, err := git.PlainOpen(repoPath) repo, err := git.PlainOpen(repoPath)
if err != nil { if err != nil {
@ -31,29 +61,18 @@ func getTagsFromRepo(repoPath string) ([]string, error) {
return tags, nil return tags, nil
} }
func CheckTags(repoPath string, category string, n int, m int) error { func CheckTags(repoPath string, skip bool) error {
// INFO: if category not specified, skipping this check by default if skip {
if category == "" {
return nil return nil
} }
tags, err := getTagsFromRepo(repoPath) tags, err := getTagsFromRepo(repoPath)
if err != nil { if err != nil {
return fmt.Errorf("error getting tags: %v", err) return fmt.Errorf("error getting tags from repo: %v", err)
} }
var prefix string
switch category { target, err := getTagFromMsg()
case "exam": if err != nil {
prefix = "e" return fmt.Errorf("error getting tag from msg scope: %v", err)
case "project":
prefix = "p"
case "homework":
prefix = "h"
default:
prefix = "a"
}
target := prefix + fmt.Sprintf("%d", n)
if category == "project" {
target += fmt.Sprintf("m%d", m)
} }
found := false found := false
for _, tag := range tags { for _, tag := range tags {
@ -63,7 +82,7 @@ func CheckTags(repoPath string, category string, n int, m int) error {
} }
} }
if !found { if !found {
return fmt.Errorf("Wrong release tag '%s' or missing release tags. Please use one of '%s'.", strings.Join(tags, "', '"), target) return fmt.Errorf("Wrong release tag in '%s' or missing release tags. Please use '%s'.", strings.Join(tags, "', '"), target)
} }
return nil return nil
} }