Skip to content

Commit b2ff62f

Browse files
committed
add new flag
1 parent 77eec28 commit b2ff62f

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

main.go

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import (
2020

2121
// AppConfig holds the application configuration
2222
type AppConfig struct {
23-
Token string
24-
RepoDir string
25-
PR int
26-
Repo string
27-
Verbose bool
23+
Token string
24+
RepoDir string
25+
PR int
26+
Repo string
27+
Verbose bool
28+
AddComments bool
2829
}
2930

3031
// App represents the application with its dependencies
@@ -38,20 +39,22 @@ type App struct {
3839

3940
// Flags holds the command line flags
4041
type Flags struct {
41-
Token *string
42-
RepoDir *string
43-
PR *int
44-
Repo *string
45-
Verbose *bool
42+
Token *string
43+
RepoDir *string
44+
PR *int
45+
Repo *string
46+
Verbose *bool
47+
AddComments *bool
4648
}
4749

4850
var (
4951
flags = &Flags{
50-
Token: flag.String("token", getEnv("INPUT_GITHUB-TOKEN", ""), "GitHub authentication token"),
51-
RepoDir: flag.String("dir", getEnv("GITHUB_WORKSPACE", "/"), "Path to local Git repo"),
52-
PR: flag.Int("pr", ignoreError(strconv.Atoi(getEnv("INPUT_PR", ""))), "Pull Request number"),
53-
Repo: flag.String("repo", getEnv("INPUT_REPOSITORY", ""), "GitHub repo name"),
54-
Verbose: flag.Bool("v", ignoreError(strconv.ParseBool(getEnv("INPUT_VERBOSE", "0"))), "Verbose output"),
52+
Token: flag.String("token", getEnv("INPUT_GITHUB-TOKEN", ""), "GitHub authentication token"),
53+
RepoDir: flag.String("dir", getEnv("GITHUB_WORKSPACE", "/"), "Path to local Git repo"),
54+
PR: flag.Int("pr", ignoreError(strconv.Atoi(getEnv("INPUT_PR", ""))), "Pull Request number"),
55+
Repo: flag.String("repo", getEnv("INPUT_REPOSITORY", ""), "GitHub repo name"),
56+
Verbose: flag.Bool("v", ignoreError(strconv.ParseBool(getEnv("INPUT_VERBOSE", "0"))), "Verbose output"),
57+
AddComments: flag.Bool("comments", ignoreError(strconv.ParseBool(getEnv("INPUT_ADD_COMMENTS", "1"))), "Add comments to Pull Request"),
5558
}
5659
WarningBuffer = bytes.NewBuffer([]byte{})
5760
InfoBuffer = bytes.NewBuffer([]byte{})
@@ -212,51 +215,49 @@ func (a *App) processApprovalsAndReviewers() (bool, string, error) {
212215
}
213216

214217
// Add comments to the PR if necessary
215-
if !QUIET_MODE_TEST {
216-
if len(unapprovedOwners) > 0 {
217-
// Comment on the PR with the codeowner teams that have not approved the PR
218-
comment := allRequiredOwners.ToCommentString()
219-
hasHighPriority, err := a.client.IsInLabels(a.conf.HighPriorityLabels)
218+
if a.config.AddComments && len(unapprovedOwners) > 0 {
219+
// Comment on the PR with the codeowner teams that have not approved the PR
220+
comment := allRequiredOwners.ToCommentString()
221+
hasHighPriority, err := a.client.IsInLabels(a.conf.HighPriorityLabels)
222+
if err != nil {
223+
fmt.Fprintf(WarningBuffer, "WARNING: Error checking high priority labels: %v\n", err)
224+
} else if hasHighPriority {
225+
comment = "❗High Prio❗\n\n" + comment
226+
}
227+
if maxReviewsMet {
228+
comment += "\n\n"
229+
comment += "The PR has received the max number of required reviews. No further action is required."
230+
}
231+
fiveDaysAgo := time.Now().AddDate(0, 0, -5)
232+
found, err := a.client.IsInComments(comment, &fiveDaysAgo)
233+
if err != nil {
234+
return false, message, fmt.Errorf("IsInComments Error: %v\n", err)
235+
}
236+
if !found {
237+
err = a.client.AddComment(comment)
220238
if err != nil {
221-
fmt.Fprintf(WarningBuffer, "WARNING: Error checking high priority labels: %v\n", err)
222-
} else if hasHighPriority {
223-
comment = "❗High Prio❗\n\n" + comment
224-
}
225-
if maxReviewsMet {
226-
comment += "\n\n"
227-
comment += "The PR has received the max number of required reviews. No further action is required."
239+
return false, message, fmt.Errorf("AddComment Error: %v\n", err)
228240
}
229-
fiveDaysAgo := time.Now().AddDate(0, 0, -5)
230-
found, err := a.client.IsInComments(comment, &fiveDaysAgo)
241+
}
242+
}
243+
if a.config.AddComments && len(allOptionalReviewerNames) > 0 {
244+
var isInCommentsError error = nil
245+
// Add CC comment to the PR with the optional reviewers that have not already been mentioned in the PR comments
246+
viewersToPing := f.Filtered(allOptionalReviewerNames, func(name string) bool {
247+
found, err := a.client.IsSubstringInComments(name, nil)
231248
if err != nil {
232-
return false, message, fmt.Errorf("IsInComments Error: %v\n", err)
233-
}
234-
if !found {
235-
err = a.client.AddComment(comment)
236-
if err != nil {
237-
return false, message, fmt.Errorf("AddComment Error: %v\n", err)
238-
}
249+
isInCommentsError = err
239250
}
251+
return !found
252+
})
253+
if isInCommentsError != nil {
254+
return false, message, fmt.Errorf("IsInComments Error: %v\n", err)
240255
}
241-
if len(allOptionalReviewerNames) > 0 {
242-
var isInCommentsError error = nil
243-
// Add CC comment to the PR with the optional reviewers that have not already been mentioned in the PR comments
244-
viewersToPing := f.Filtered(allOptionalReviewerNames, func(name string) bool {
245-
found, err := a.client.IsSubstringInComments(name, nil)
246-
if err != nil {
247-
isInCommentsError = err
248-
}
249-
return !found
250-
})
251-
if isInCommentsError != nil {
252-
return false, message, fmt.Errorf("IsInComments Error: %v\n", err)
253-
}
254-
if len(viewersToPing) > 0 {
255-
comment := fmt.Sprintf("cc %s", strings.Join(viewersToPing, " "))
256-
err = a.client.AddComment(comment)
257-
if err != nil {
258-
return false, message, fmt.Errorf("AddComment Error: %v\n", err)
259-
}
256+
if len(viewersToPing) > 0 {
257+
comment := fmt.Sprintf("cc %s", strings.Join(viewersToPing, " "))
258+
err = a.client.AddComment(comment)
259+
if err != nil {
260+
return false, message, fmt.Errorf("AddComment Error: %v\n", err)
260261
}
261262
}
262263
}
@@ -422,11 +423,12 @@ func main() {
422423
}
423424

424425
cfg := AppConfig{
425-
Token: *flags.Token,
426-
RepoDir: *flags.RepoDir,
427-
PR: *flags.PR,
428-
Repo: *flags.Repo,
429-
Verbose: *flags.Verbose,
426+
Token: *flags.Token,
427+
RepoDir: *flags.RepoDir,
428+
PR: *flags.PR,
429+
Repo: *flags.Repo,
430+
Verbose: *flags.Verbose,
431+
AddComments: *flags.AddComments,
430432
}
431433

432434
app, err := NewApp(cfg)

0 commit comments

Comments
 (0)