-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
151 lines (114 loc) · 3.86 KB
/
main.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"log"
"os"
"sync"
"time"
"github.com/Namchee/conventional-pr/internal"
"github.com/Namchee/conventional-pr/internal/constants"
"github.com/Namchee/conventional-pr/internal/entity"
"github.com/Namchee/conventional-pr/internal/formatter"
"github.com/Namchee/conventional-pr/internal/service"
"github.com/Namchee/conventional-pr/internal/utils"
"github.com/Namchee/conventional-pr/internal/validator"
"github.com/Namchee/conventional-pr/internal/whitelist"
)
var (
infoLogger *log.Logger
errorLogger *log.Logger
defaultLogger *log.Logger
)
func init() {
infoLogger = log.New(os.Stdout, "[INFO] ", log.Ldate|log.Ltime|log.Lmsgprefix)
errorLogger = log.New(os.Stderr, "[ERROR] ", log.Ldate|log.Ltime|log.Lmsgprefix)
defaultLogger = log.New(os.Stdout, "", 0)
}
func main() {
infoLogger.Println("Initializing conventional-pr")
start := time.Now()
ctx := context.Background()
var config *entity.Configuration
var meta *entity.Meta
var event *entity.Event
var err error
infoLogger.Println("Reading configuration from environment variables")
config, err = entity.ReadConfig()
if err != nil {
errorLogger.Fatalln(err)
}
infoLogger.Println("Reading repository metadata")
meta, err = entity.CreateMeta(
utils.ReadEnvString("GITHUB_REPOSITORY"),
)
if err != nil {
errorLogger.Fatalf("Failed to read repository metadata: %s", err.Error())
}
infoLogger.Println("Initializing GitHub Client")
client := internal.NewGithubClient(config)
infoLogger.Println("Reading pull request metadata")
event, err = entity.ReadEvent(os.DirFS("/"))
if err != nil {
errorLogger.Fatalf("Failed to read repository event: %s", err.Error())
}
infoLogger.Println("Validating pull request sub-events")
if !utils.ContainsString(constants.Events, event.Action) {
infoLogger.Println("Incompatible sub-events detected. Exiting...")
os.Exit(0)
}
pullRequest, err := client.GetPullRequest(ctx, meta, event.Number)
if err != nil {
errorLogger.Fatalf("Failed to fetch pull request data: %s", err.Error())
}
var vgResult []*entity.ValidationResult
sync := &sync.WaitGroup{}
infoLogger.Println("Testing pull request for whitelists")
wg := whitelist.NewWhitelistGroup(client, config, sync)
wgResult := wg.Process(ctx, pullRequest)
if !whitelist.IsWhitelisted(wgResult) {
infoLogger.Println("Testing pull request validity")
vg := validator.NewValidatorGroup(client, config, sync)
vgResult = vg.Process(ctx, pullRequest)
}
svc := service.NewGithubService(client, config, meta)
infoLogger.Println("Writing run report")
results := &entity.PullRequestResult{
Whitelist: wgResult,
Validation: vgResult,
}
resultLog := formatter.FormatResultToConsole(results)
defaultLogger.Println(resultLog)
if config.Verbose {
err = svc.WriteReport(ctx, pullRequest, results, time.Now())
if err != nil {
errorLogger.Fatalf("Failed to write report: %s", err.Error())
}
}
if !validator.IsValid(vgResult) {
infoLogger.Println("Processing invalid pull request")
if config.Label != "" {
infoLogger.Println("Attaching label to invalid pull request")
err = svc.AttachLabel(ctx, pullRequest)
if err != nil {
errorLogger.Fatalf("Failed to attach invalid pull request label: %s", err.Error())
}
}
if config.Message != "" {
infoLogger.Println("Writing custom error message to invalid pull request")
err = svc.WriteMessage(ctx, pullRequest)
if err != nil {
errorLogger.Fatalf("Failed to write message: %s", err.Error())
}
}
if config.Close {
infoLogger.Println("Closing invalid pull request")
err = svc.ClosePullRequest(ctx, pullRequest)
if err != nil {
errorLogger.Fatalf("Failed to close invalid pull request: %s", err.Error())
}
}
infoLogger.Printf("Finished processing on %.2fs", time.Since(start).Seconds())
os.Exit(1)
}
infoLogger.Printf("Finished processing on %.2fs", time.Since(start).Seconds())
}