-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
85 lines (73 loc) · 2.16 KB
/
main.go
File metadata and controls
85 lines (73 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"github.com/joho/godotenv"
"log"
"os"
)
type Options struct {
Query string `short:"q" long:"query" default:"type:issue is:open -label:Pending" description:"Query strings. For search Issues/Pull Requests."`
Duration int `short:"d" long:"duration" default:"30" description:"Duration. Issues would be closed if left over this duration. (days)"`
Comment string `short:"c" long:"comment" default:":alarm_clock: this Issue was left for a long time." description:"Comment. Would be posted before an Issue is closed."`
DryRun bool `short:"n" long:"dry-run" description:"If true, show target Issues without closing."`
RunOnce bool `short:"o" long:"run-once" description:"If true, close only one Issue."`
Limit int `short:"l" long:"limit" default:"0" description:"A maximum number of closed Issues."`
Debug bool `long:"debug"`
}
func main() {
godotenv.Load()
org := os.Getenv("GITHUB_ORGANIZATION")
repo := os.Getenv("GITHUB_REPO")
team := os.Getenv("GITHUB_TEAM")
token := os.Getenv("GITHUB_ACCESS_TOKEN")
var opts Options
p := flags.NewParser(&opts, flags.Default)
_, err := p.ParseArgs(os.Args[1:])
if err != nil {
os.Exit(1)
}
im := NewIssueManager(org, repo, team, token, opts.Duration)
dryRunMessage := ""
if opts.DryRun {
dryRunMessage = " (dryrun)"
}
issues, err := im.FindIssues(opts.Query)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
closedNumber := 0
for _, issue := range issues {
if im.isUpdatedWithinDuration(issue) {
if opts.Debug {
log.Println(fmt.Sprintf("#%d %s %s was updated recently. skipped.", *issue.Number, *issue.HTMLURL, *issue.Title))
}
continue
}
if !opts.DryRun {
if len(opts.Comment) > 0 {
im.Comment(issue, opts.Comment)
if err != nil {
log.Println(err)
continue
}
}
_, err = im.Close(issue)
if err != nil {
log.Println(err)
continue
}
}
if opts.Debug {
log.Println(fmt.Sprintf("#%d %s %s is closed.%s", *issue.Number, *issue.HTMLURL, *issue.Title, dryRunMessage))
}
closedNumber++
if opts.Limit > 0 && opts.Limit <= closedNumber {
break
}
if opts.RunOnce {
break
}
}
}