Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[patch] fix: switch pending status to pr trigger step #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/v1/pullrequesttrigger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ type PullRequestTriggerCondition struct {
type PullRequestTriggerConditionType string

const (
// PullRequestTriggerCondPendingStatusSent means the pull request trigger has been sent the pending status
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// PullRequestTriggerCondPendingStatusSent means the pull request trigger has been sent the pending status
// PullRequestTriggerCondPendingStatusSent means the pull request trigger has been sent the pending status when start running tests

PullRequestTriggerCondPendingStatusSent PullRequestTriggerConditionType = "PendingStatusSent"
// PullRequestTriggerCondFailed means the pull request trigger failed to retrieve the image from the registry
PullRequestTriggerCondFailed PullRequestTriggerConditionType = "Failed"
)
Expand Down Expand Up @@ -153,3 +155,12 @@ type PullRequestTriggerList struct {
func init() {
SchemeBuilder.Register(&PullRequestTrigger{}, &PullRequestTriggerList{})
}

func (prStatus *PullRequestTriggerStatus) IsContains(conditionType PullRequestTriggerConditionType) bool {
for _, c := range prStatus.Conditions {
if c.Type == conditionType {
return true
}
}
return false
}
2 changes: 0 additions & 2 deletions api/v1/queue_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ const (
QueueDeployed QueueConditionType = "QueueDeployed"
// QueueTestTriggered means the queue has been triggered testing
QueueTestTriggered QueueConditionType = "QueueTestTriggered"
// QueueTestPendingStatusSent means the queue has been sent the test pipeline pending status while testing
QueueTestPendingStatusSent QueueConditionType = "QueueTestPendingStatusSent"
// QueueTested means the queue has been finished testing
QueueTested QueueConditionType = "QueueTested"
// QueueTeamcityTestResult means the test result of Teamcity
Expand Down
8 changes: 8 additions & 0 deletions internal/pullrequest/trigger/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ func (c *controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
maxRetry := prConfig.Trigger.MaxRetry
gitRepo := prConfig.GitRepository

// send pending status while test is running
isSentPendingStatus := prTrigger.Status.IsContains(s2hv1.PullRequestTriggerCondPendingStatusSent)
if !isSentPendingStatus {
if err := c.sendTestPendingResult(prTrigger); err != nil {
return reconcile.Result{}, errors.Wrapf(err, "cannot get pull request config of team: %s", c.teamName)
}
}

var tearDownDuration s2hv1.PullRequestTearDownDuration
// overwrite tearDownDuration
if prTrigger.Spec.TearDownDuration != nil {
Expand Down
51 changes: 51 additions & 0 deletions internal/pullrequest/trigger/send_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package trigger

import (
"context"
"net/http"

s2hv1 "github.com/agoda-com/samsahai/api/v1"
"github.com/agoda-com/samsahai/internal"
samsahairpc "github.com/agoda-com/samsahai/pkg/samsahai/rpc"
"github.com/twitchtv/twirp"
v1 "k8s.io/api/core/v1"
)

const sendTestPendingRetry = 3

func (c *controller) sendTestPendingResult(prTrigger *s2hv1.PullRequestTrigger) error {
headers := make(http.Header)
headers.Set(internal.SamsahaiAuthHeader, c.authToken)
ctx, err := twirp.WithHTTPRequestHeaders(context.TODO(), headers)
if err != nil {
logger.Error(err, "cannot set request header")
return err
}

for retry := 0; retry <= sendTestPendingRetry; retry++ {
if _, err = c.s2hClient.RunPostPullRequestQueueTestRunnerTrigger(ctx, &samsahairpc.TeamWithPullRequest{
TeamName: c.teamName,
Namespace: internal.GenStagingNamespace(c.teamName),
BundleName: internal.GenPullRequestBundleName(prTrigger.Spec.BundleName, prTrigger.Spec.PRNumber),
}); err != nil {
logger.Error(err,
"cannot send pull request trigger pending status report,",
"team", c.teamName, "component", prTrigger.Spec.BundleName, "pr number", prTrigger.Spec.PRNumber)
// set state, cannot send test pending status
prTrigger.Status.SetCondition(
s2hv1.PullRequestTriggerCondPendingStatusSent,
v1.ConditionFalse,
"cannot send pull request trigger pending status")
continue
}
logger.Info("sent pull request test runner pending status successfully",
"team", c.teamName, "component", prTrigger.Spec.BundleName, "pr number", prTrigger.Spec.PRNumber)
// set state, test pending status has been sent
prTrigger.Status.SetCondition(
s2hv1.PullRequestTriggerCondPendingStatusSent,
v1.ConditionTrue,
"pull request trigger pending status has been sent")
break
}
return err
}
59 changes: 1 addition & 58 deletions internal/staging/start_testrunner.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package staging

import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"time"

"github.com/twitchtv/twirp"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand All @@ -18,7 +15,6 @@ import (
"github.com/agoda-com/samsahai/internal/staging/testrunner/gitlab"
"github.com/agoda-com/samsahai/internal/staging/testrunner/teamcity"
"github.com/agoda-com/samsahai/internal/staging/testrunner/testmock"
samsahairpc "github.com/agoda-com/samsahai/pkg/samsahai/rpc"
)

type testResult string
Expand All @@ -28,8 +24,7 @@ const (
testPolling = 5 * time.Second // 5 secs
testTriggerTimeout = 1 * time.Minute // 1 minutes

testResultRetry = 3 // 3 times
sendTestPendingRetry = 3 // 3 times
testResultRetry = 3 // 3 times

testResultSuccess testResult = "PASSED"
testResultFailure testResult = "FAILED"
Expand Down Expand Up @@ -78,14 +73,6 @@ func (c *controller) startTesting(queue *s2hv1.Queue) error {
return err
}

// send pending status while test is running
isSentPendingStatus := queue.Status.IsContains(s2hv1.QueueTestPendingStatusSent)
if queue.Spec.Type == s2hv1.QueueTypePullRequest && !isSentPendingStatus {
if err := c.sendTestPendingResult(queue); err != nil {
return err
}
}

// get result from tests (polling check)
testCondition := v1.ConditionTrue
message := "queue testing succeeded"
Expand Down Expand Up @@ -331,47 +318,3 @@ func (c *controller) setTestResultCondition(queue *s2hv1.Queue, testRunnerName s
// update queue back to k8s
return c.updateQueue(queue)
}

func (c *controller) sendTestPendingResult(queue *s2hv1.Queue) error {
headers := make(http.Header)
headers.Set(internal.SamsahaiAuthHeader, c.authToken)
ctx, err := twirp.WithHTTPRequestHeaders(context.TODO(), headers)
if err != nil {
logger.Error(err, "cannot set request header")
return err
}

for retry := 0; retry <= sendTestPendingRetry; retry++ {
if _, err = c.s2hClient.RunPostPullRequestQueueTestRunnerTrigger(ctx, &samsahairpc.TeamWithPullRequest{
TeamName: c.teamName,
Namespace: internal.GenStagingNamespace(c.teamName),
BundleName: internal.GenPullRequestBundleName(queue.Spec.Name, queue.Spec.PRNumber),
}); err != nil {
logger.Error(err,
"cannot send pull request test runner pending status report,",
"team", c.teamName, "component", queue.Spec.Name, "pr number", queue.Spec.PRNumber)
// set state, cannot send test pending status
queue.Status.SetCondition(
s2hv1.QueueTestPendingStatusSent,
v1.ConditionFalse,
"cannot send queue test pending status")
if err := c.updateQueue(queue); err != nil {
logger.Error(err, "cannot update queue", "name", queue.Name)
}
continue
}
logger.Info("sent pull request test runner pending status successfully",
"team", c.teamName, "component", queue.Spec.Name, "pr number", queue.Spec.PRNumber)
// set state, test pending status has been sent
queue.Status.SetCondition(
s2hv1.QueueTestPendingStatusSent,
v1.ConditionTrue,
"queue test pending status has been sent")
if err = c.updateQueue(queue); err != nil {
logger.Error(err, "cannot update queue", "name", queue.Name)
return err
}
break
}
return err
}