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

fix(checkmarx) Improve project branching process #4180

Merged
merged 2 commits into from
Jan 6, 2023
Merged
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
57 changes: 53 additions & 4 deletions pkg/checkmarx/checkmarx.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ type ProjectCreateResult struct {
Link Link `json:"link"`
}

// ProjectBranchingResponse - ProjectBranchingResponse Structure
type ProjectBranchingResponse struct {
ID int `json:"id"`
Link Link `json:"link"`
}

type BranchingStatus struct {
ID int `json:"id"`
Value string `json:"value"`
}

// ProjectBranchingStatusResponse - ProjectBranchingStatusResponse Structure
type ProjectBranchingStatusResponse struct {
ID int `json:"id"`
OriginalProjectId int `json:"originalProjectId"`
BranchedProjectId int `json:"branchedProjectId"`
Status BranchingStatus `json:"status"`
ErrorMessage string `json:"errorMessage"`
}

// Report - Report Structure
type Report struct {
ReportID int `json:"reportId"`
Expand Down Expand Up @@ -432,10 +452,39 @@ func (sys *SystemInstance) CreateBranch(projectID int, branchName string) int {
return 0
}

var scan Scan

json.Unmarshal(data, &scan)
return scan.ID
var branchingResponse ProjectBranchingResponse
json.Unmarshal(data, &branchingResponse)
branchedProjectId := branchingResponse.ID

branchingStatusId := 0 // 0 Started, 1 InProgress, 2 Completed, 3 Failed
i := 0
for branchingStatusId != 2 {
dataBranchingStatus, err := sendRequest(sys, http.MethodGet, fmt.Sprintf("/projects/branch/%v", branchedProjectId), nil, header)
if err != nil {
sys.logger.Warnf("Failed to poll status of branching process: %s", err)
} else {
var branchingStatusResponse ProjectBranchingStatusResponse
json.Unmarshal(dataBranchingStatus, &branchingStatusResponse)
branchingStatusId = branchingStatusResponse.Status.ID
branchingStatusValue := branchingStatusResponse.Status.Value
sys.logger.Debugf("Branching process status: %s", branchingStatusValue)
if branchingStatusId == 2 {
sys.logger.Debug("Branching process completed successfuly")
break
} else if branchingStatusId == 3 {
sys.logger.Errorf("Branching process failed. Error is: %s", branchingStatusResponse.ErrorMessage)
return 0
}
}
if i >= 30 {
// time out after 5 minutes
sys.logger.Errorf("Branching process timed out.")
return 0
}
i++
time.Sleep(10 * time.Second)
}
return branchedProjectId
}

// UploadProjectSourceCode zips and uploads the project sources for scanning
Expand Down
16 changes: 0 additions & 16 deletions pkg/checkmarx/checkmarx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,22 +557,6 @@ func TestDownloadReport(t *testing.T) {
})
}

func TestCreateBranch(t *testing.T) {
logger := log.Entry().WithField("package", "SAP/jenkins-library/pkg/checkmarx_test")
opts := piperHttp.ClientOptions{}
t.Run("test success", func(t *testing.T) {
myTestClient := senderMock{responseBody: `{"id": 13, "link": {}}`, httpStatusCode: 201}
sys := SystemInstance{serverURL: "https://cx.server.com", client: &myTestClient, logger: logger}
myTestClient.SetOptions(opts)

result := sys.CreateBranch(6, "PR-17")
assert.Equal(t, "https://cx.server.com/cxrestapi/projects/6/branch", myTestClient.urlCalled, "Called url incorrect")
assert.Equal(t, "POST", myTestClient.httpMethod, "HTTP method incorrect")
assert.Equal(t, `{"name":"PR-17"}`, myTestClient.requestBody, "Request body incorrect")
assert.Equal(t, 13, result, "result incorrect")
})
}

func TestGetProjectByID(t *testing.T) {
logger := log.Entry().WithField("package", "SAP/jenkins-library/pkg/checkmarx_test")
opts := piperHttp.ClientOptions{}
Expand Down