Skip to content

Commit e615365

Browse files
committed
MINOR: add push event detection and proces just last commit for it
1 parent be7fb9a commit e615365

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

check-commit/.golangci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@ linters-settings:
33
lines: 70
44
testpackage:
55
skip-regexp: check_test.go
6-
cyclop:
7-
max-complexity: 12

check-commit/check.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ TagOrder:
7070
- HAProxy Standard Feature Commit
7171
`
7272

73-
minSubjectParts = 3
74-
maxSubjectParts = 15
75-
minSubjectLen = 15
76-
maxSubjectLen = 100
73+
MINSUBJECTPARTS = 3
74+
MAXSUBJECTPARTS = 15
75+
MINSUBJECTLEN = 15
76+
MAXSUBJECTLEN = 100
77+
78+
GITHUB = "Github"
7779
)
7880

7981
var ErrSubjectMessageFormat = errors.New("invalid subject message format")
@@ -89,16 +91,16 @@ func checkSubjectText(subject string) error {
8991
subject, ErrSubjectMessageFormat)
9092
}
9193

92-
if subjectPartsLen < minSubjectParts || subjectPartsLen > maxSubjectParts {
94+
if subjectPartsLen < MINSUBJECTPARTS || subjectPartsLen > MAXSUBJECTPARTS {
9395
return fmt.Errorf(
9496
"subject word count out of bounds [words %d < %d < %d] '%s': %w",
95-
minSubjectParts, subjectPartsLen, maxSubjectParts, subjectParts, ErrSubjectMessageFormat)
97+
MINSUBJECTPARTS, subjectPartsLen, MAXSUBJECTPARTS, subjectParts, ErrSubjectMessageFormat)
9698
}
9799

98-
if subjectLen < minSubjectLen || subjectLen > maxSubjectLen {
100+
if subjectLen < MINSUBJECTLEN || subjectLen > MAXSUBJECTLEN {
99101
return fmt.Errorf(
100102
"subject length out of bounds [len %d < %d < %d] '%s': %w",
101-
minSubjectLen, subjectLen, maxSubjectLen, subject, ErrSubjectMessageFormat)
103+
MINSUBJECTLEN, subjectLen, MAXSUBJECTLEN, subject, ErrSubjectMessageFormat)
102104
}
103105

104106
return nil
@@ -206,7 +208,7 @@ var ErrGitEnvironment = errors.New("git environment error")
206208

207209
func readGitEnvironment() (*gitEnv, error) {
208210
knownVars := []gitEnvVars{
209-
{"Github", "GITHUB_EVENT_NAME", "GITHUB_SHA", "GITHUB_BASE_REF"},
211+
{GITHUB, "GITHUB_EVENT_NAME", "GITHUB_SHA", "GITHUB_BASE_REF"},
210212
{"Gitlab", "CI_PIPELINE_SOURCE", "CI_MERGE_REQUEST_SOURCE_BRANCH_NAME", "CI_MERGE_REQUEST_TARGET_BRANCH_NAME"},
211213
{"Gitlab-commit", "CI_PIPELINE_SOURCE", "CI_COMMIT_SHA", "CI_DEFAULT_BRANCH"},
212214
}
@@ -218,9 +220,9 @@ func readGitEnvironment() (*gitEnv, error) {
218220
ref = os.Getenv(vars.RefVar)
219221
base = os.Getenv(vars.BaseVar)
220222

221-
if ref != "" && base != "" {
223+
if !(ref == "" && base == "") || (vars.EnvName == GITHUB && event == "push") {
222224
log.Printf("detected %s environment\n", vars.EnvName)
223-
log.Printf("using %s with %s and %s\n", event, ref, base)
225+
log.Printf("using event '%s' with refs '%s' and '%s'\n", event, ref, base)
224226

225227
return &gitEnv{
226228
EnvName: vars.EnvName,
@@ -231,7 +233,7 @@ func readGitEnvironment() (*gitEnv, error) {
231233
}
232234
}
233235

234-
return nil, fmt.Errorf("no suitable git environment variables found %w", ErrGitEnvironment)
236+
return nil, fmt.Errorf("no suitable git environment variables found: %w", ErrGitEnvironment)
235237
}
236238

237239
func LoadCommitPolicy(filename string) (CommitPolicyConfig, error) {
@@ -254,12 +256,13 @@ func LoadCommitPolicy(filename string) (CommitPolicyConfig, error) {
254256
return commitPolicy, nil
255257
}
256258

257-
var ErrReachedMergeBase = errors.New("reached Merge Base")
258-
259-
func getCommitSubjects(repo *git.Repository, repoEnv *gitEnv) ([]string, error) {
259+
func hashesFromRefs(repo *git.Repository, repoEnv *gitEnv) ([]*plumbing.Hash, []*object.Commit) {
260260
var refStrings []string
261261
refStrings = append(refStrings, repoEnv.Ref)
262-
refStrings = append(refStrings, repoEnv.Base)
262+
263+
if !(repoEnv.EnvName == GITHUB && repoEnv.Event == "push") { // for Github push we only have the last commit
264+
refStrings = append(refStrings, repoEnv.Base)
265+
}
263266

264267
hashes := make([]*plumbing.Hash, 0, 2)
265268

@@ -283,6 +286,18 @@ func getCommitSubjects(repo *git.Repository, repoEnv *gitEnv) ([]string, error)
283286
commits = append(commits, commit)
284287
}
285288

289+
return hashes, commits
290+
}
291+
292+
var ErrReachedMergeBase = errors.New("reached Merge Base")
293+
294+
func getCommitSubjects(repo *git.Repository, repoEnv *gitEnv) ([]string, error) {
295+
hashes, commits := hashesFromRefs(repo, repoEnv)
296+
297+
if len(commits) == 1 { // just the last commit
298+
return []string{strings.Split(commits[0].Message, "\n")[0]}, nil
299+
}
300+
286301
mergeBase, err := commits[0].MergeBase(commits[1])
287302
if err != nil {
288303
log.Fatalf("repo history error %s", err)
@@ -307,7 +322,7 @@ func getCommitSubjects(repo *git.Repository, repoEnv *gitEnv) ([]string, error)
307322
}
308323
subjectOnly := strings.Split(c.Message, "\n")[0]
309324

310-
if !(repoEnv.EnvName == "Github" && repoEnv.Event == "pull_request" && gitlabMergeRegex.Match([]byte(c.Message))) {
325+
if !(repoEnv.EnvName == GITHUB && repoEnv.Event == "pull_request" && gitlabMergeRegex.Match([]byte(c.Message))) {
311326
// ignore github pull request commits with subject "Merge x into y", these get added automatically by github
312327
subjects = append(subjects, subjectOnly)
313328
log.Printf("collected commit hash %s, subject '%s'", c.Hash, subjectOnly)
@@ -367,7 +382,7 @@ func main() {
367382

368383
gitEnv, err := readGitEnvironment()
369384
if err != nil {
370-
log.Fatalf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually")
385+
log.Fatalf("couldn't auto-detect running environment, please set GITHUB_REF and GITHUB_BASE_REF manually: %s", err)
371386
}
372387

373388
repo, err := git.PlainOpen(repoPath)

0 commit comments

Comments
 (0)