-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathatlantis.go
More file actions
119 lines (92 loc) · 3.08 KB
/
Copy pathatlantis.go
File metadata and controls
119 lines (92 loc) · 3.08 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
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
package main
import (
"fmt"
"os"
"path"
atlantiscmd "github.com/runatlantis/atlantis/cmd"
"github.com/runatlantis/atlantis/server"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/vcs"
"github.com/runatlantis/atlantis/server/logging"
"github.com/spf13/viper"
)
var atlantisLogger logging.SimpleLogging
type atlantisFlags struct {
AtlantisDB string
AtlantisURL string
ExecutableName string
}
func getAtlantisFlags(atlantisConfig string) (*atlantisFlags, error) {
srvCreator := &serverConfigRecorder{}
// safe to run without change of data-dir because serverConfigRecorder only records the userConfig
// it does not construct or start the server.
args := []string{"--config", atlantisConfig}
err := startAtlantis(srvCreator, args, atlantisConfig)
if err != nil {
return nil, err
}
return &atlantisFlags{
AtlantisDB: path.Join(srvCreator.userConfig.DataDir, "atlantis.db"),
AtlantisURL: srvCreator.userConfig.AtlantisURL,
ExecutableName: srvCreator.userConfig.ExecutableName,
}, nil
}
func getCommentPoster(atlantisConfig string) (*commentPoster, error) {
srvCreator := &serverCreatorRecorder{}
// don't reuse data-dir (including db), we only need this to get the vcs client
dir := os.TempDir()
defer os.RemoveAll(dir)
args := []string{"--data-dir", dir, "--log-level", "error", "--config", atlantisConfig}
err := startAtlantis(srvCreator, args, atlantisConfig)
if err != nil {
return nil, err
}
return &commentPoster{client: srvCreator.vcsClient}, nil
}
type commentPoster struct {
client vcs.Client
}
func (p commentPoster) postComment(repo models.Repo, pullNum int, body string) error {
return p.client.CreateComment(atlantisLogger, repo, pullNum, body, "post-workflow-hook")
}
func startAtlantis(creator atlantiscmd.ServerCreator, args []string, atlantisConfig string) error {
if atlantisConfig == "" {
return fmt.Errorf("-atlantis-config flag is required")
}
atlantisLogger, _ = logging.NewStructuredLogger()
atlantisLogger.SetLevel(logging.Error)
c := &atlantiscmd.ServerCmd{
ServerCreator: creator,
Viper: viper.New(),
SilenceOutput: true,
Logger: atlantisLogger,
}
cmd := c.Init()
cmd.SetArgs(args)
// won't actually start the server, only construct all inner workings
return cmd.Execute()
}
type serverConfigRecorder struct {
userConfig server.UserConfig
}
func (s *serverConfigRecorder) NewServer(userConfig server.UserConfig, _ server.Config) (atlantiscmd.ServerStarter, error) {
// ran only once, no need for lock
s.userConfig = userConfig
return &serverStarterMock{}, nil
}
type serverCreatorRecorder struct {
vcsClient vcs.Client
}
func (s *serverCreatorRecorder) NewServer(userConfig server.UserConfig, config server.Config) (atlantiscmd.ServerStarter, error) {
srv, err := server.NewServer(userConfig, config)
if err != nil {
return nil, err
}
// ran only once, no need for lock
s.vcsClient = srv.VCSEventsController.VCSClient
return &serverStarterMock{}, nil
}
type serverStarterMock struct{}
func (s *serverStarterMock) Start() error {
return nil
}