-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutputs.go
132 lines (107 loc) · 3.5 KB
/
outputs.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
package prplanner
import (
"context"
"strconv"
"strings"
"github.com/redis/go-redis/v9"
"github.com/utilitywarehouse/git-mirror/pkg/giturl"
tfaplv1beta1 "github.com/utilitywarehouse/terraform-applier/api/v1beta1"
)
func (p *Planner) uploadRequestOutput(ctx context.Context, pr *pr) {
// Go through PR comments in reverse order
for i := len(pr.Comments.Nodes) - 1; i >= 0; i-- {
comment := pr.Comments.Nodes[i]
cluster, moduleNamespacedName, path, commitID, requestedAt := parseRequestAcknowledgedMsg(comment.Body)
if requestedAt == nil {
continue
}
if cluster != p.ClusterEnvName {
continue
}
// ger run output from Redis
run, err := p.RedisClient.PRRun(ctx, moduleNamespacedName, pr.Number, commitID)
if err != nil {
continue
}
if run.Output == "" {
continue
}
payload := prComment{
Body: runOutputMsg(p.ClusterEnvName, moduleNamespacedName.String(), path, run),
}
_, err = p.github.postComment(pr.BaseRepository.Owner.Login, pr.BaseRepository.Name, comment.DatabaseID, pr.Number, payload)
if err != nil {
p.Log.Error("error posting PR comment:", "error", err)
continue
}
p.Log.Info("run output uploaded", "module", moduleNamespacedName, "pr", pr.Number)
}
}
func (p *Planner) processRedisKeySetMsg(ctx context.Context, ch <-chan *redis.Message) {
p.Log.Info("starting redis update watcher")
defer p.Log.Error("stopping redis update watcher")
for msg := range ch {
// make sure we only process `set` keyevent
if msg.Channel != "__keyevent@0__:set" {
continue
}
key := msg.Payload
// skip non run related keys
// and process default output only once
if !strings.Contains(key, ":default:lastRun") &&
!strings.Contains(key, ":PR:") {
continue
}
run, err := p.RedisClient.Run(ctx, key)
if err != nil {
p.Log.Error("unable to get run output", "key", key, "err", err)
continue
}
if run.Output == "" {
continue
}
var prNum, CommentID int
if run.Request.PR != nil {
prNum = run.Request.PR.Number
CommentID = run.Request.PR.CommentID
}
// if its not a PR run then also
// check if there is pending task for output upload
if prNum == 0 && strings.Contains(key, "default:lastRun") {
if pr, err := p.RedisClient.PendingApplyUploadPR(ctx, run.Module, run.CommitHash); err == nil {
prNum, _ = strconv.Atoi(pr)
}
}
// this is required in case this run is not a PR run && not apply run
if prNum == 0 {
continue
}
var module tfaplv1beta1.Module
err = p.ClusterClt.Get(ctx, run.Module, &module)
if err != nil {
p.Log.Error("unable to get module", "module", run.Module, "pr", prNum, "error", err)
continue
}
comment := prComment{
Body: runOutputMsg(p.ClusterEnvName, run.Module.String(), module.Spec.Path, run),
}
repo, err := giturl.Parse(module.Spec.RepoURL)
if err != nil {
p.Log.Error("unable to parse repo url", "module", run.Module, "pr", prNum, "error", err)
continue
}
_, err = p.github.postComment(repo.Path, strings.TrimSuffix(repo.Repo, ".git"), CommentID, prNum, comment)
if err != nil {
p.Log.Error("error posting PR comment:", "module", run.Module, "pr", prNum, "error", err)
continue
}
p.Log.Info("run output posted", "module", run.Module, "pr", prNum)
// if apply output is posted then clean up PR runs
if strings.Contains(key, "default:lastRun") {
if err := p.RedisClient.CleanupPRKeys(ctx, run.Module, prNum, run.CommitHash); err != nil {
p.Log.Error("error cleaning PR keys:", "module", run.Module, "pr", prNum, "error", err)
continue
}
}
}
}