Repository: kubesphere/ks-devops
Pull request #1005 (2023-10-11 09:17:47)

[release-3.4] add header info in api run-log
This is an automated cherry-pick of #1004

```release-note
none
```

ks-ci-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **linuxsuren** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @linuxsuren` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubesphere%2Fks-devops).

<details open>
Needs approval from an approver in each of these files:

- **[pkg/client/devops/OWNERS](https://github.com/kubesphere/ks-devops/blob/release-3.4/pkg/client/devops/OWNERS)**
- **[pkg/kapis/devops/v1alpha2/OWNERS](https://github.com/kubesphere/ks-devops/blob/release-3.4/pkg/kapis/devops/v1alpha2/OWNERS)**
- **[pkg/models/devops/OWNERS](https://github.com/kubesphere/ks-devops/blob/release-3.4/pkg/models/devops/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["linuxsuren"]} -->
chilianyi: /lgtm
/approve
ks-ci-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubesphere/ks-devops/pull/1005#issuecomment-1757263063" title="Approved">chilianyi</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubesphere%2Fks-devops).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[pkg/client/devops/OWNERS](https://github.com/kubesphere/ks-devops/blob/release-3.4/pkg/client/devops/OWNERS)~~ [chilianyi]
- ~~[pkg/kapis/devops/v1alpha2/OWNERS](https://github.com/kubesphere/ks-devops/blob/release-3.4/pkg/kapis/devops/v1alpha2/OWNERS)~~ [chilianyi]
- ~~[pkg/models/devops/OWNERS](https://github.com/kubesphere/ks-devops/blob/release-3.4/pkg/models/devops/OWNERS)~~ [chilianyi]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/client/devops/fake/fakedevops.go b/pkg/client/devops/fake/fakedevops.go
index 2346824..38c43bf 100644
--- a/pkg/client/devops/fake/fakedevops.go
+++ b/pkg/client/devops/fake/fakedevops.go
@@ -198,8 +198,8 @@ func (d *Devops) DownloadArtifact(projectName, pipelineName, runId, filename str
 	return nil, nil
 }
 
-func (d *Devops) GetRunLog(projectName, pipelineName, runId string, httpParameters *devops.HttpParameters) ([]byte, error) {
-	return nil, nil
+func (d *Devops) GetRunLog(projectName, pipelineName, runId string, httpParameters *devops.HttpParameters) ([]byte, http.Header, error) {
+	return nil, nil, nil
 }
 func (d *Devops) GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, httpParameters *devops.HttpParameters) ([]byte, http.Header, error) {
 	return nil, nil, nil
diff --git a/pkg/client/devops/jclient/pipeline.go b/pkg/client/devops/jclient/pipeline.go
index 818414f..6adec7b 100644
--- a/pkg/client/devops/jclient/pipeline.go
+++ b/pkg/client/devops/jclient/pipeline.go
@@ -81,7 +81,7 @@ func (j *JenkinsClient) DownloadArtifact(projectName, pipelineName, runID, filen
 }
 
 // GetRunLog returns the log output of a pipeline run
-func (j *JenkinsClient) GetRunLog(projectName, pipelineName, runID string, httpParameters *devops.HttpParameters) ([]byte, error) {
+func (j *JenkinsClient) GetRunLog(projectName, pipelineName, runID string, httpParameters *devops.HttpParameters) ([]byte, http.Header, error) {
 	return j.jenkins.GetRunLog(projectName, pipelineName, runID, httpParameters)
 }
 
diff --git a/pkg/client/devops/jenkins/jenkins.go b/pkg/client/devops/jenkins/jenkins.go
index b1d0f0e..3e9a08f 100644
--- a/pkg/client/devops/jenkins/jenkins.go
+++ b/pkg/client/devops/jenkins/jenkins.go
@@ -308,14 +308,13 @@ func (j *Jenkins) GetArtifacts(projectName, pipelineName, runId string, httpPara
 	return res, err
 }
 
-func (j *Jenkins) GetRunLog(projectName, pipelineName, runId string, httpParameters *devops.HttpParameters) ([]byte, error) {
+func (j *Jenkins) GetRunLog(projectName, pipelineName, runId string, httpParameters *devops.HttpParameters) ([]byte, http.Header, error) {
 	PipelineOjb := &Pipeline{
 		HttpParameters: httpParameters,
 		Jenkins:        j,
 		Path:           fmt.Sprintf(GetRunLogUrl+httpParameters.Url.RawQuery, projectName, pipelineName, runId),
 	}
-	res, err := PipelineOjb.GetRunLog()
-	return res, err
+	return PipelineOjb.GetRunLog()
 }
 
 func (j *Jenkins) GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, httpParameters *devops.HttpParameters) ([]byte, http.Header, error) {
diff --git a/pkg/client/devops/jenkins/pipeline.go b/pkg/client/devops/jenkins/pipeline.go
index e43706f..5145580 100644
--- a/pkg/client/devops/jenkins/pipeline.go
+++ b/pkg/client/devops/jenkins/pipeline.go
@@ -374,13 +374,13 @@ func (p *Pipeline) GetArtifacts() ([]devops.Artifacts, error) {
 	return artifacts, err
 }
 
-func (p *Pipeline) GetRunLog() ([]byte, error) {
-	res, err := p.Jenkins.SendPureRequest(p.Path, p.HttpParameters)
+func (p *Pipeline) GetRunLog() ([]byte, http.Header, error) {
+	res, header, err := p.Jenkins.SendPureRequestWithHeaderResp(p.Path, p.HttpParameters)
 	if err != nil {
 		klog.Error(err)
 	}
 
-	return res, err
+	return res, header, err
 }
 
 func (p *Pipeline) GetStepLog() ([]byte, http.Header, error) {
diff --git a/pkg/client/devops/pipeline.go b/pkg/client/devops/pipeline.go
index 8ba23ae..8c93d5c 100644
--- a/pkg/client/devops/pipeline.go
+++ b/pkg/client/devops/pipeline.go
@@ -1137,7 +1137,7 @@ type PipelineOperator interface {
 	RunPipeline(projectName, pipelineName string, httpParameters *HttpParameters) (*RunPipeline, error)
 	GetArtifacts(projectName, pipelineName, runId string, httpParameters *HttpParameters) ([]Artifacts, error)
 	DownloadArtifact(projectName, pipelineName, runId, filename string, isMultiBranch bool, branchName string) (io.ReadCloser, error)
-	GetRunLog(projectName, pipelineName, runId string, httpParameters *HttpParameters) ([]byte, error)
+	GetRunLog(projectName, pipelineName, runId string, httpParameters *HttpParameters) ([]byte, http.Header, error)
 	GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, httpParameters *HttpParameters) ([]byte, http.Header, error)
 	GetNodeSteps(projectName, pipelineName, runId, nodeId string, httpParameters *HttpParameters) ([]NodeSteps, error)
 	GetPipelineRunNodes(projectName, pipelineName, runId string, httpParameters *HttpParameters) ([]PipelineRunNodes, error)
diff --git a/pkg/kapis/devops/v1alpha2/devops.go b/pkg/kapis/devops/v1alpha2/devops.go
index 523dcb3..584a0fc 100644
--- a/pkg/kapis/devops/v1alpha2/devops.go
+++ b/pkg/kapis/devops/v1alpha2/devops.go
@@ -278,12 +278,18 @@ func (h *ProjectPipelineHandler) GetRunLog(req *restful.Request, resp *restful.R
 	pipelineName := req.PathParameter("pipeline")
 	runId := req.PathParameter("run")
 
-	res, err := h.devopsOperator.GetRunLog(projectName, pipelineName, runId, req.Request)
+	res, header, err := h.devopsOperator.GetRunLog(projectName, pipelineName, runId, req.Request)
 	if err != nil {
 		parseErr(err, resp)
 		return
 	}
 
+	for k, v := range header {
+		if strings.HasPrefix(k, jenkinsHeaderPre) {
+			resp.AddHeader(k, v[0])
+		}
+	}
+
 	resp.Write(res)
 }
 
diff --git a/pkg/models/devops/devops.go b/pkg/models/devops/devops.go
index 19a8408..bf684c8 100644
--- a/pkg/models/devops/devops.go
+++ b/pkg/models/devops/devops.go
@@ -83,7 +83,7 @@ type DevopsOperator interface {
 	ReplayPipeline(projectName, pipelineName, runId string, req *http.Request) (*devops.ReplayPipeline, error)
 	RunPipeline(projectName, pipelineName string, req *http.Request) (*devops.RunPipeline, error)
 	GetArtifacts(projectName, pipelineName, runId string, req *http.Request) ([]devops.Artifacts, error)
-	GetRunLog(projectName, pipelineName, runId string, req *http.Request) ([]byte, error)
+	GetRunLog(projectName, pipelineName, runId string, req *http.Request) ([]byte, http.Header, error)
 	GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error)
 	GetNodeSteps(projectName, pipelineName, runId, nodeId string, req *http.Request) ([]devops.NodeSteps, error)
 	GetPipelineRunNodes(projectName, pipelineName, runId string, req *http.Request) ([]devops.PipelineRunNodes, error)
@@ -550,15 +550,15 @@ func (d devopsOperator) GetArtifacts(projectName, pipelineName, runId string, re
 	return res, err
 }
 
-func (d devopsOperator) GetRunLog(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
+func (d devopsOperator) GetRunLog(projectName, pipelineName, runId string, req *http.Request) ([]byte, http.Header, error) {
 
-	res, err := d.devopsClient.GetRunLog(projectName, pipelineName, runId, convertToHttpParameters(req))
+	res, header, err := d.devopsClient.GetRunLog(projectName, pipelineName, runId, convertToHttpParameters(req))
 	if err != nil {
 		klog.Error(err)
-		return nil, err
+		return nil, nil, err
 	}
 
-	return res, err
+	return res, header, err
 }
 
 func (d devopsOperator) GetStepLog(projectName, pipelineName, runId, nodeId, stepId string, req *http.Request) ([]byte, http.Header, error) {


Test patch:
diff --git a/pkg/client/devops/fake/fakedevops_test.go b/pkg/client/devops/fake/fakedevops_test.go
index 2fe4854..375a339 100644
--- a/pkg/client/devops/fake/fakedevops_test.go
+++ b/pkg/client/devops/fake/fakedevops_test.go
@@ -193,8 +193,8 @@ func TestNotImplement(t *testing.T) {
 	assertNils(t, o1, o2)
 	o1, o2 = client.DownloadArtifact("", "", "", "", false, "")
 	assertNils(t, o1, o2)
-	o1, o2 = client.GetRunLog("", "", "", nil)
-	assertNils(t, o1, o2)
+	o1, o2, o3 = client.GetRunLog("", "", "", nil)
+	assertNils(t, o1, o2, o3)
 	o1, o2, o3 = client.GetStepLog("", "", "", "", "", nil)
 	assertNils(t, o1, o2, o3)
 	o1, o2 = client.RunPipeline("", "", nil)