Skip to content

Commit

Permalink
🐛 [patch] Fix getting result from private gitlab project (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pohfy123 authored Jul 23, 2021
1 parent 579c7b5 commit e644600
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 14 deletions.
1 change: 1 addition & 0 deletions cmd/samsahai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func startCtrlCmd() *cobra.Command {
GithubToken: viper.GetString(s2h.VKGithubToken),
TeamcityUsername: viper.GetString(s2h.VKTeamcityUsername),
TeamcityPassword: viper.GetString(s2h.VKTeamcityPassword),
GitlabToken: viper.GetString(s2h.VKGitlabToken),
MSTeams: s2h.MSTeamsCredential{
TenantID: viper.GetString(s2h.VKMSTeamsTenantID),
ClientID: viper.GetString(s2h.VKMSTeamsClientID),
Expand Down
8 changes: 5 additions & 3 deletions cmd/staging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ func startCtrlCmd() *cobra.Command {
tcUsername := viper.GetString(s2h.VKTeamcityUsername)
tcPassword := viper.GetString(s2h.VKTeamcityPassword)
glBaseURL := viper.GetString(s2h.VKGitlabURL)
glToken := viper.GetString(s2h.VKGitlabToken)
maxQueueHistDays := viper.GetInt(s2h.VKQueueMaxHistoryDays)
stagingCtrl := stagingctrl.NewController(teamName, namespace, authToken, samsahaiClient, mgr,
queueCtrl, configCtrl, tcBaseURL, tcUsername, tcPassword, glBaseURL,
queueCtrl, configCtrl, tcBaseURL, tcUsername, tcPassword, glBaseURL, glToken,
s2h.StagingConfig{MaxHistoryDays: maxQueueHistDays})

prQueueCtrl := prqueuectrl.New(teamName, namespace, mgr, authToken, samsahaiClient,
Expand Down Expand Up @@ -194,10 +195,11 @@ func startCtrlCmd() *cobra.Command {
cmd.Flags().String(s2h.VKS2HTeamName, "", "Samsahai Team Name.")
cmd.Flags().String(s2h.VKS2HServerURL, "", "Samsahai server endpoint.")
cmd.Flags().String(s2h.VKS2HAuthToken, "", "Samsahai server authentication token.")
cmd.Flags().String(s2h.VKTeamcityURL, "", "Teamcity api base url.")
cmd.Flags().String(s2h.VKTeamcityURL, "", "Teamcity API base url.")
cmd.Flags().String(s2h.VKTeamcityUsername, "", "Teamcity username.")
cmd.Flags().String(s2h.VKTeamcityPassword, "", "Teamcity password.")
cmd.Flags().String(s2h.VKGitlabURL, "", "Gitlab api base url.")
cmd.Flags().String(s2h.VKGitlabURL, "", "Gitlab API base url.")
cmd.Flags().String(s2h.VKGitlabToken, "", "Gitlab private token for API.")
cmd.Flags().String(s2h.VKServerHTTPPort, "8090", "The port for http server to listens to.")
cmd.Flags().String(s2h.VKMetricHTTPPort, "8091", "The port for prometheus metric to binds to.")
cmd.Flags().Int(s2h.VKQueueMaxHistoryDays, 7, "Max stored queue histories in day.")
Expand Down
1 change: 1 addition & 0 deletions internal/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
VKTeamcityUsername = "teamcity-username"
VKTeamcityPassword = "teamcity-password"
VKGitlabURL = "gitlab-url"
VKGitlabToken = "gitlab-token"
VKSlackToken = "slack-token"
VKGithubURL = "github-url"
VKGithubToken = "github-token"
Expand Down
1 change: 1 addition & 0 deletions internal/samsahai.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type SamsahaiCredential struct {
MSTeams MSTeamsCredential
TeamcityUsername string
TeamcityPassword string
GitlabToken string
}

type MSTeamsCredential struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/samsahai/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ func (c *controller) createEnvironmentObjects(teamComp *s2hv1.Team, namespace st
Key: internal.VKTeamcityPassword,
Value: intstr.FromString(c.configs.SamsahaiCredential.TeamcityPassword),
},
{
Key: internal.VKGitlabToken,
Value: intstr.FromString(c.configs.SamsahaiCredential.GitlabToken),
},
{
Key: internal.VKTeamcityURL,
Value: intstr.FromString(c.configs.TeamcityURL),
Expand Down
6 changes: 5 additions & 1 deletion internal/staging/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ type controller struct {
teamcityPassword string

gitlabBaseURL string
gitlabToken string

configs internal.StagingConfig
}

// TODO: move test runner config to be optional
func NewController(
teamName string,
namespace string,
Expand All @@ -81,6 +83,7 @@ func NewController(
teamcityUsername string,
teamcityPassword string,
gitlabBaseURL string,
gitlabToken string,
configs internal.StagingConfig,
) internal.StagingController {
if queueCtrl == nil {
Expand Down Expand Up @@ -108,6 +111,7 @@ func NewController(
teamcityUsername: teamcityUsername,
teamcityPassword: teamcityPassword,
gitlabBaseURL: gitlabBaseURL,
gitlabToken: gitlabToken,
configs: configs,
}

Expand Down Expand Up @@ -253,7 +257,7 @@ func (c *controller) loadTestRunners() {
}

if c.gitlabBaseURL != "" {
testRunners = append(testRunners, gitlab.New(c.client, c.gitlabBaseURL))
testRunners = append(testRunners, gitlab.New(c.client, c.gitlabBaseURL, gitlab.WithGitlabToken(c.gitlabToken)))
}

for _, r := range testRunners {
Expand Down
27 changes: 24 additions & 3 deletions internal/staging/testrunner/gitlab/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,39 @@ type ResultResponse struct {
}

type testRunner struct {
baseURL string
client client.Client
baseURL string
privateToken string
client client.Client
}

type triggerBuildReq struct {
Variables map[string]string `json:"variables"`
Token string `json:"token"`
}

// NewOption allows specifying various configuration
type NewOption func(*testRunner)

// WithGitlabToken specifies a gitlab private token to override when creating Gitlab test runner
// This will be used to get test result from the pipeline
func WithGitlabToken(token string) NewOption {
return func(r *testRunner) {
r.privateToken = token
}
}

// New creates a new gitlab test runner
func New(client client.Client, baseURL string) internal.StagingTestRunner {
func New(client client.Client, baseURL string, opts ...NewOption) internal.StagingTestRunner {
t := &testRunner{
client: client,
baseURL: baseURL,
}

// apply the new options
for _, opt := range opts {
opt(t)
}

return t
}

Expand Down Expand Up @@ -183,6 +200,10 @@ func (t *testRunner) GetResult(testConfig *s2hv1.ConfigTestRunner, currentQueue
http.WithSkipTLSVerify(),
}

if t.privateToken != "" {
opts = append(opts, http.WithHeader("PRIVATE-TOKEN", t.privateToken))
}

_, resp, err := http.Get(apiURL, opts...)
if err != nil {
logger.Error(err, "The HTTP request failed", "URL", apiURL)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/pullrequest/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func setupStaging(namespace string) (internal.StagingController, internal.QueueC
qctrl := queue.New(namespace, client)
stagingCtrl := staging.NewController(teamName, namespace, samsahaiAuthToken, samsahaiClient,
stagingMgr, qctrl, stagingCfgCtrl, "", "", "", "",
internal.StagingConfig{})
"", internal.StagingConfig{})

prQueueCtrl := prqueuectrl.New(teamName, namespace, stagingMgr, samsahaiAuthToken, samsahaiClient,
prqueuectrl.WithClient(client))
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/samsahai/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ var _ = Describe("[e2e] Main controller", func() {
stagingCfgCtrl := configctrl.New(stagingMgr)
qctrl := queue.New(preActiveNs, client)
stagingPreActiveCtrl := staging.NewController(teamName, preActiveNs, samsahaiAuthToken, samsahaiClient,
stagingMgr, qctrl, stagingCfgCtrl, "", "", "", "",
internal.StagingConfig{})
stagingMgr, qctrl, stagingCfgCtrl, "", "", "",
"", "", internal.StagingConfig{})
go func() {
defer GinkgoRecover()
Expect(stagingMgr.Start(chStop)).NotTo(HaveOccurred())
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/staging/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ var _ = Describe("[e2e] Staging controller", func() {
authToken := "12345"
stagingCfgCtrl := configctrl.New(mgr)
stagingCtrl = staging.NewController(teamName, namespace, authToken, nil, mgr, queueCtrl,
stagingCfgCtrl, "", "", "", "", internal.StagingConfig{})
stagingCfgCtrl, "", "", "", "", "", internal.StagingConfig{})

go stagingCtrl.Start(chStop)

Expand Down Expand Up @@ -661,7 +661,7 @@ var _ = Describe("[e2e] Staging controller", func() {

stagingCfgCtrl := configctrl.New(mgr)
stagingCtrl = staging.NewController(teamName, namespace, authToken, samsahaiClient, mgr, queueCtrl,
stagingCfgCtrl, "", "", "", "", internal.StagingConfig{})
stagingCfgCtrl, "", "", "", "", "", internal.StagingConfig{})
go stagingCtrl.Start(chStop)

By("Creating Config")
Expand Down Expand Up @@ -760,7 +760,7 @@ var _ = Describe("[e2e] Staging controller", func() {

stagingCfgCtrl := configctrl.New(mgr)
stagingCtrl = staging.NewController(teamName, namespace, authToken, samsahaiClient, mgr, queueCtrl,
stagingCfgCtrl, "", "", "", "", internal.StagingConfig{})
stagingCfgCtrl, "", "", "", "", "", internal.StagingConfig{})
go stagingCtrl.Start(chStop)

redis := queue.NewQueue(teamName, namespace, redisCompName, "",
Expand Down Expand Up @@ -801,7 +801,7 @@ var _ = Describe("[e2e] Staging controller", func() {
defer close(done)

stagingCtrl = staging.NewController(teamName, namespace, "", nil, mgr, queueCtrl,
nil, "", "", "", "", internal.StagingConfig{})
nil, "", "", "", "", "", internal.StagingConfig{})

server := httptest.NewServer(stagingCtrl)
defer server.Close()
Expand Down

0 comments on commit e644600

Please sign in to comment.