Skip to content

Commit

Permalink
fix: add msg field to pipelineActivity Spec and Steps
Browse files Browse the repository at this point in the history
- Tekton PipelineRun provides the message & this PR will add that Message field to Spec and Steps in the pipelineActivity.
- This PR also adds test for 4 testcases:
	- timeout
	- cancelled
	- success
	- timedout-success

related to jenkins-x/jx-ui#23

Signed-off-by: rajatgupta24 <[email protected]>
  • Loading branch information
rajatgupta24 committed Jul 9, 2022
1 parent d9dea25 commit c0fde1b
Show file tree
Hide file tree
Showing 8 changed files with 5,400 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/jenkins-x-plugins/jx-gitops v0.7.22
github.com/jenkins-x/go-scm v1.11.16
github.com/jenkins-x/jx-api/v4 v4.3.7
github.com/jenkins-x/jx-api/v4 v4.5.1
github.com/jenkins-x/jx-helpers/v3 v3.2.16
github.com/jenkins-x/jx-kube-client/v3 v3.0.3
github.com/jenkins-x/jx-logging/v3 v3.0.9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1058,8 +1058,8 @@ github.com/jenkins-x/jx-api/v4 v4.1.5/go.mod h1:l11kHlFy40UGu9pdhCRxDiJcEgRubSVz
github.com/jenkins-x/jx-api/v4 v4.3.3/go.mod h1:l11kHlFy40UGu9pdhCRxDiJcEgRubSVzybWB2jXjLcs=
github.com/jenkins-x/jx-api/v4 v4.3.5/go.mod h1:iHK40sCT1ct/C5cIa+Uo+MDFFAjXfnA88MImhPfiTmY=
github.com/jenkins-x/jx-api/v4 v4.3.6/go.mod h1:BjLd7lmBB0CpqA1U2IZ2AyEQ2A66U1lDUBz/lsvld3A=
github.com/jenkins-x/jx-api/v4 v4.3.7 h1:SHDH+Z7kN8YBS/LzQTa69u5qWaYxraXIYjPSTmNnad8=
github.com/jenkins-x/jx-api/v4 v4.3.7/go.mod h1:1ZPMGLTfWeDz4CoB+QBScVQM+AjS780osJ4W6mHJWlk=
github.com/jenkins-x/jx-api/v4 v4.5.1 h1:39vflsM69LgOPSF7MdyI+VJH4h78KKDocIlBcPeb3Mo=
github.com/jenkins-x/jx-api/v4 v4.5.1/go.mod h1:1ZPMGLTfWeDz4CoB+QBScVQM+AjS780osJ4W6mHJWlk=
github.com/jenkins-x/jx-helpers/v3 v3.0.127/go.mod h1:0U5fcXnqSv5ugx+XMZ2rYT+VU3o+pyJeXJEiNMAVeSU=
github.com/jenkins-x/jx-helpers/v3 v3.2.5/go.mod h1:w0isjFV0n85jYJogjTW9cdm8vM/8k1mLhTfLXrb4lCs=
github.com/jenkins-x/jx-helpers/v3 v3.2.16 h1:DwN0vxSJHz1L8yaZaJxqYOjDZ0evLpdRosWxW8JvQsI=
Expand Down
24 changes: 24 additions & 0 deletions pkg/pipelines/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,30 @@ func ToPipelineActivity(pr *v1beta1.PipelineRun, pa *v1.PipelineActivity, overwr
}

activities.UpdateStatus(pa, false, nil)

for k := range pr.Status.Conditions {
msg := pr.Status.Conditions[k].GetMessage()

if strings.Contains(msg, "PipelineRun") {
msg = strings.ReplaceAll(msg, "PipelineRun", "PipelineActivity")
}

if strings.Contains(msg, pa.Labels["tekton.dev/pipeline"]) {
msg = strings.ReplaceAll(msg, pa.Labels["tekton.dev/pipeline"], pa.Name)
}

pa.Spec.Message = v1.ActivityMessageType(msg)
}

k1 := 0
for k2 := range pr.Status.TaskRuns {
msg := pr.Status.TaskRuns[k2].Status.Conditions[0].Message

msg = strings.ReplaceAll(msg, "TaskRun", "Stage")
pa.Spec.Steps[k1].Stage.Message = v1.ActivityMessageType(msg)

k1++
}
}

// Humanize splits into words and capitalises
Expand Down
56 changes: 56 additions & 0 deletions pkg/pipelines/pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,59 @@ func TestPipelineActivityStatus(t *testing.T) {
require.Equal(t, v.expectedStatus, pa.Spec.Status.String())
}
}

var activityMessageTestCases = []struct {
description string
folder string
name string
expectedMessage string
}{
{
description: "Jenkins X PipelineActivity has been successful run and message exists",
folder: "message/success",
name: "rajatgupta24-quickstart-004-pr-1-7",
expectedMessage: `Tasks Completed: 1 (Failed: 0, Cancelled 0), Skipped: 0`,
},
{
description: "Jenkins X PipelineActivity has been timedout and message exists",
folder: "message/timeout",
name: "rajatgupta24-quickstart-004-pr-1-7",
expectedMessage: `PipelineActivity "rajatgupta24-quickstart-004-pr-1-7" failed to finish within "1h0m0s"`,
},
{
description: "Jenkins X PipelineActivity has been cancelled and message exists",
folder: "message/cancelled",
name: "rajatgupta24-quickstart-004-pr-2-7",
expectedMessage: `PipelineActivity "rajatgupta24-quickstart-004-pr-2-7" was cancelled`,
},
{
description: "Jenkins X PipelineActivity has two steps, one step that been timedout and other Succeeded",
folder: "message/timedout-succeeded",
name: "rajatgupta24-quickstart-004-pr-2-7",
expectedMessage: `PipelineActivity "rajatgupta24-quickstart-004-pr-2-7" failed to finish within "1m0s"`,
},
}

func TestPipelineActivityMessage(t *testing.T) {
for k, v := range activityMessageTestCases {
t.Logf("Running test case %d: %s", k+1, v.description)
prFile := filepath.Join("testdata", v.folder, "pr.yaml")
require.FileExists(t, prFile)

pr := &v1beta1.PipelineRun{}
err := yamls.LoadFile(prFile, pr)
require.NoError(t, err, "failed to load %s", prFile)

pa := &v1.PipelineActivity{}

pa.Name = v.name
pipelines.ToPipelineActivity(pr, pa, false)

for k2 := range pa.Spec.Steps {
require.NotEqual(t, "", pa.Spec.Steps[k2].Stage.Message)
}

require.NotEqual(t, "", pa.Spec.Message.String())
require.Equal(t, v.expectedMessage, pa.Spec.Message.String())
}
}
Loading

0 comments on commit c0fde1b

Please sign in to comment.