Skip to content

Commit 033e3af

Browse files
committed
feat: inclusion, skip flag support
changes logcategory to handle unclassified(skipped) changes and also adds skip support for each and flag to print any combination of the skip and inclusion flag
1 parent 7f145ad commit 033e3af

File tree

2 files changed

+123
-35
lines changed

2 files changed

+123
-35
lines changed

logcategory.go

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,48 @@ import (
88
)
99

1010
// logContainer - Container of log strings
11-
type logContainer []string
11+
type logContainer struct {
12+
include bool
13+
commits []string
14+
}
1215

1316
// logsByCategory - Type to hold logs by each's category
1417
// to be left as ALLCAPS to be considered as symbols instead of selectors
1518
type logsByCategory struct {
16-
CI logContainer
17-
FIX logContainer
18-
REFACTOR logContainer
19-
FEATURE logContainer
20-
DOCS logContainer
21-
CHORE logContainer
22-
TEST logContainer
23-
OTHER logContainer
19+
CI logContainer
20+
FIX logContainer
21+
REFACTOR logContainer
22+
FEATURE logContainer
23+
DOCS logContainer
24+
CHORE logContainer
25+
TEST logContainer
26+
OTHER logContainer
27+
UNCLASSIFIED logContainer
28+
}
29+
30+
// Setup - Initialize all Log Containers
31+
func (logs logsByCategory) Setup() {
32+
logs.CI.include = true
33+
logs.FIX.include = true
34+
logs.REFACTOR.include = true
35+
logs.FEATURE.include = true
36+
logs.DOCS.include = true
37+
logs.CHORE.include = true
38+
logs.TEST.include = true
39+
logs.OTHER.include = true
40+
logs.UNCLASSIFIED.include = true
2441
}
2542

2643
// printLog - loops through the collected logs to write them to string builder
2744
func (logs logContainer) printLog(out *strings.Builder, title string, skipped bool) {
28-
if len(logs) > 0 {
45+
if !logs.include {
46+
return
47+
}
48+
if len(logs.commits) > 0 {
2949
if !skipped {
3050
out.WriteString(fmt.Sprintf("\n\n## %s \n", title))
3151
}
32-
for _, item := range logs {
52+
for _, item := range logs.commits {
3353
out.WriteString(item + "\n")
3454
}
3555
}
@@ -51,8 +71,68 @@ func (logs *logsByCategory) ToMarkdown(skipped bool) string {
5171
logs.TEST.printLog(&markdownString, "Tests", skipped)
5272
logs.OTHER.printLog(&markdownString, "Other Changes", skipped)
5373
} else {
54-
logs.OTHER.printLog(&markdownString, "Other Changes", skipped)
74+
logs.UNCLASSIFIED.include = true
75+
logs.UNCLASSIFIED.printLog(&markdownString, "Unclassified Changes", skipped)
5576
}
5677

5778
return markdownString.String()
5879
}
80+
81+
// AddCommit - Add a commit to the needed logContainer based on skip and include flag
82+
func (logs *logsByCategory) AddCommit(key, commitHash string, skip bool) {
83+
var addCommitToContainer *logContainer
84+
switch key {
85+
case "ci":
86+
if logs.CI.include && !skip {
87+
addCommitToContainer = &logs.CI
88+
} else if skip && logs.CI.include {
89+
addCommitToContainer = &logs.UNCLASSIFIED
90+
}
91+
case "fix":
92+
if logs.FIX.include && !skip {
93+
addCommitToContainer = &logs.FIX
94+
} else if skip && logs.FIX.include {
95+
addCommitToContainer = &logs.UNCLASSIFIED
96+
}
97+
case "refactor":
98+
if logs.REFACTOR.include && !skip {
99+
addCommitToContainer = &logs.REFACTOR
100+
} else if skip && logs.REFACTOR.include {
101+
addCommitToContainer = &logs.UNCLASSIFIED
102+
}
103+
case "feat", "feature":
104+
if logs.FEATURE.include && !skip {
105+
addCommitToContainer = &logs.FEATURE
106+
} else if skip && logs.FEATURE.include {
107+
addCommitToContainer = &logs.UNCLASSIFIED
108+
}
109+
case "docs":
110+
if logs.DOCS.include && !skip {
111+
addCommitToContainer = &logs.DOCS
112+
} else if skip && logs.DOCS.include {
113+
addCommitToContainer = &logs.UNCLASSIFIED
114+
}
115+
case "test":
116+
if logs.TEST.include && !skip {
117+
addCommitToContainer = &logs.TEST
118+
} else if skip && logs.TEST.include {
119+
addCommitToContainer = &logs.UNCLASSIFIED
120+
}
121+
case "chore":
122+
if logs.CHORE.include && !skip {
123+
addCommitToContainer = &logs.CHORE
124+
} else if skip && logs.CHORE.include {
125+
addCommitToContainer = &logs.UNCLASSIFIED
126+
}
127+
default:
128+
if logs.OTHER.include && !skip {
129+
addCommitToContainer = &logs.OTHER
130+
} else if skip && logs.OTHER.include {
131+
addCommitToContainer = &logs.UNCLASSIFIED
132+
}
133+
}
134+
135+
if addCommitToContainer != nil {
136+
addCommitToContainer.commits = append(addCommitToContainer.commits, commitHash)
137+
}
138+
}

main.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package main
44

55
import (
6+
"bytes"
67
"flag"
78
"fmt"
89
"log"
@@ -18,11 +19,13 @@ type ErrMessage struct {
1819
err error
1920
}
2021

22+
type commitTypeInclusions [][]byte
23+
2124
func main() {
2225
var repoPath = flag.String("p", ".", "path to the repository, points to the current working directory by default")
2326
var startCommit = flag.String("s", "", "commit hash string start collecting commit messages from")
2427
var endCommit = flag.String("e", "", "commit hash string to stop collecting commit message at")
25-
var inclusionFlags = flag.String("i", "", "commit types to be includes, defaults to CI FIX REFACTOR FEATURE DOCS CHORE TEST OTHER")
28+
var inclusionFlags = flag.String("i", "ci,docs,fix,feat,test,chore,other", "commit types to be includes, defaults to CI FIX REFACTOR FEATURE DOCS CHORE TEST OTHER")
2629
var skipClassification = flag.Bool("skip", false, "if true will skip trying to classify and just give a list of changes")
2730

2831
flag.Parse()
@@ -76,32 +79,26 @@ func CommitLog(path string, startCommitString string, endCommitString string, in
7679
return ErrMessage{"Error getting commits : ", err}
7780
}
7881

82+
var inclusions commitTypeInclusions = bytes.SplitN([]byte(inclusionFlags), []byte(","), -1)
83+
7984
logContainer := logsByCategory{}
8085

86+
logContainer.Setup()
87+
88+
logContainer.CI.include = inclusions.checkInclusion("ci")
89+
logContainer.FIX.include = inclusions.checkInclusion("fix")
90+
logContainer.REFACTOR.include = inclusions.checkInclusion("refactor")
91+
logContainer.FEATURE.include = inclusions.checkInclusion("feat")
92+
logContainer.DOCS.include = inclusions.checkInclusion("docs")
93+
logContainer.CHORE.include = inclusions.checkInclusion("chore")
94+
logContainer.TEST.include = inclusions.checkInclusion("test")
95+
logContainer.OTHER.include = inclusions.checkInclusion("other")
96+
8197
for _, c := range commits {
8298
normalizedHash := c.Hash.String() + " - " + normalizeCommit(c.Message)
83-
if !skipClassification {
84-
switch strings.SplitN(strings.TrimSpace(c.Message), ":", 2)[0] {
85-
case "ci":
86-
logContainer.CI = append(logContainer.CI, normalizedHash)
87-
case "fix":
88-
logContainer.FIX = append(logContainer.FIX, normalizedHash)
89-
case "refactor":
90-
logContainer.REFACTOR = append(logContainer.REFACTOR, normalizedHash)
91-
case "feat", "feature":
92-
logContainer.FEATURE = append(logContainer.FEATURE, normalizedHash)
93-
case "docs":
94-
logContainer.DOCS = append(logContainer.DOCS, normalizedHash)
95-
case "test":
96-
logContainer.TEST = append(logContainer.TEST, normalizedHash)
97-
case "chore":
98-
logContainer.CHORE = append(logContainer.CHORE, normalizedHash)
99-
default:
100-
logContainer.OTHER = append(logContainer.OTHER, normalizedHash)
101-
}
102-
} else {
103-
logContainer.OTHER = append(logContainer.OTHER, normalizedHash)
104-
}
99+
key := strings.SplitN(strings.TrimSpace(c.Message), ":", 2)[0]
100+
101+
logContainer.AddCommit(key, normalizedHash, skipClassification)
105102

106103
if endHash == nil && isCommitToNearestTag(currentRepository, c) {
107104
break
@@ -113,3 +110,14 @@ func CommitLog(path string, startCommitString string, endCommitString string, in
113110

114111
return ErrMessage{}
115112
}
113+
114+
func (inclusions *commitTypeInclusions) checkInclusion(flagToCheck string) bool {
115+
if inclusions != nil {
116+
for _, flag := range *inclusions {
117+
if string(flag) == flagToCheck {
118+
return true
119+
}
120+
}
121+
}
122+
return false
123+
}

0 commit comments

Comments
 (0)