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 7, 2022
1 parent 2327041 commit f7130d5
Show file tree
Hide file tree
Showing 6 changed files with 5,397 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkg/pipelines/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,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 f7130d5

Please sign in to comment.