-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub.go
127 lines (88 loc) · 2.83 KB
/
sub.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
package cmd
import (
"context"
"os"
"sync"
"time"
pubsub "cloud.google.com/go/pubsub"
"github.com/devopsext/notifier/common"
"github.com/devopsext/utils"
"github.com/spf13/cobra"
gitlab "github.com/xanzy/go-gitlab"
"google.golang.org/api/option"
)
var subEnv = utils.GetEnvironment()
var subLog = utils.GetLog()
var subOpts = common.SubOptions{
Subscription: subEnv.Get("NOTIFIER_PUBSUB_SUBSCRIPTION", "").(string),
}
func startSubscriber(wg *sync.WaitGroup) {
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
subLog.Info("Start subscriber...")
ctx := context.Background()
var o option.ClientOption
if _, err := os.Stat(subOpts.Account.Credentials); err == nil {
o = option.WithCredentialsFile(subOpts.Account.Credentials)
} else {
o = option.WithCredentialsJSON([]byte(subOpts.Account.Credentials))
}
client, err := pubsub.NewClient(ctx, subOpts.Account.ProjectID, o)
if err != nil {
subLog.Panic(err)
}
if client == nil {
subLog.Panic("Client is not found")
}
sub := client.Subscription(subOpts.Subscription)
subLog.Info("Subscriber is up. Listening...")
err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
t := subLog.Info("Got message from %s: %s", subOpts.Subscription, m.Data)
if !utils.IsEmpty(subOpts.Gitlab.BaseURL) && !utils.IsEmpty(subOpts.Gitlab.ProjectID) {
git, err := gitlab.NewClient(subOpts.Gitlab.Token, gitlab.WithBaseURL(subOpts.Gitlab.BaseURL))
if err != nil {
subLog.Error("Failed to create gitlab client: %s", err.Error())
}
ref := subOpts.Gitlab.ProjectRef
if utils.IsEmpty(ref) {
ref = "master"
}
vars := make(map[string]string)
vars[subOpts.Gitlab.Variable] = string(m.Data[:])
opt := &gitlab.RunPipelineTriggerOptions{Ref: &ref, Token: &subOpts.Gitlab.TriggerToken, Variables: vars}
pipeline, _, err := git.PipelineTriggers.RunPipelineTrigger(subOpts.Gitlab.ProjectID, opt)
if err != nil {
subLog.Error("Failed to create pipeline: %s", err.Error())
}
if pipeline != nil {
var spent = (time.Now().UnixNano() - t) / 1000000
subLog.Info("Pipeline started with id: %d, spent: %v", pipeline.ID, spent)
}
}
m.Ack()
})
if err != nil {
subLog.Panic(err)
}
}(wg)
}
func sub(cmd *cobra.Command, args []string) {
var wg sync.WaitGroup
//startMetrics(&wg)
startSubscriber(&wg)
wg.Wait()
}
// GetSubCmd implements CLI interface to Subscription
func GetSubCmd(pubSubAccount *common.PubSubAccount, gitlabOptions *common.GitlabOptions) *cobra.Command {
rootCmd := cobra.Command{
Use: "sub",
Short: "Sub command",
Run: sub,
}
subOpts.Account = pubSubAccount
subOpts.Gitlab = gitlabOptions
flags := rootCmd.PersistentFlags()
flags.StringVar(&subOpts.Subscription, "pubsub-subscription", subOpts.Subscription, "Pub/Sub subscription")
return &rootCmd
}