forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
79 lines (69 loc) · 2.51 KB
/
proxy.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
package vcs
import (
"github.com/hootsuite/atlantis/server/events/models"
"github.com/pkg/errors"
)
//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_proxy.go ClientProxy
// ClientProxy proxies calls to the correct VCS client depending on which
// VCS host is required.
type ClientProxy interface {
GetModifiedFiles(repo models.Repo, pull models.PullRequest, host Host) ([]string, error)
CreateComment(repo models.Repo, pull models.PullRequest, comment string, host Host) error
PullIsApproved(repo models.Repo, pull models.PullRequest, host Host) (bool, error)
UpdateStatus(repo models.Repo, pull models.PullRequest, state CommitStatus, description string, host Host) error
}
// DefaultClientProxy proxies calls to the correct VCS client depending on which
// VCS host is required.
type DefaultClientProxy struct {
GithubClient Client
GitlabClient Client
}
func NewDefaultClientProxy(githubClient Client, gitlabClient Client) *DefaultClientProxy {
if githubClient == nil {
githubClient = &NotConfiguredVCSClient{}
}
if gitlabClient == nil {
gitlabClient = &NotConfiguredVCSClient{}
}
return &DefaultClientProxy{
GitlabClient: gitlabClient,
GithubClient: githubClient,
}
}
var invalidVCSErr = errors.New("Invalid VCS Host. This is a bug!")
func (d *DefaultClientProxy) GetModifiedFiles(repo models.Repo, pull models.PullRequest, host Host) ([]string, error) {
switch host {
case Github:
return d.GithubClient.GetModifiedFiles(repo, pull)
case Gitlab:
return d.GitlabClient.GetModifiedFiles(repo, pull)
}
return nil, invalidVCSErr
}
func (d *DefaultClientProxy) CreateComment(repo models.Repo, pull models.PullRequest, comment string, host Host) error {
switch host {
case Github:
return d.GithubClient.CreateComment(repo, pull, comment)
case Gitlab:
return d.GitlabClient.CreateComment(repo, pull, comment)
}
return invalidVCSErr
}
func (d *DefaultClientProxy) PullIsApproved(repo models.Repo, pull models.PullRequest, host Host) (bool, error) {
switch host {
case Github:
return d.GithubClient.PullIsApproved(repo, pull)
case Gitlab:
return d.GitlabClient.PullIsApproved(repo, pull)
}
return false, invalidVCSErr
}
func (d *DefaultClientProxy) UpdateStatus(repo models.Repo, pull models.PullRequest, state CommitStatus, description string, host Host) error {
switch host {
case Github:
return d.GithubClient.UpdateStatus(repo, pull, state, description)
case Gitlab:
return d.GitlabClient.UpdateStatus(repo, pull, state, description)
}
return invalidVCSErr
}