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
- This also adds the message fields to the expected.yaml.
- Instead of using `testhelpers.AssertTextFilesEqual` we're now just comparing the generated pa and expected pa.
- Also removes `t.TempDir()`
- Adds comments to help understand the steps

related to jenkins-x/jx-ui#23

Signed-off-by: rajatgupta24 <[email protected]>
  • Loading branch information
rajatgupta24 committed Jul 13, 2022
1 parent d9dea25 commit 7d50b6c
Show file tree
Hide file tree
Showing 12 changed files with 5,509 additions and 167 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
28 changes: 28 additions & 0 deletions pkg/pipelines/pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,34 @@ 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 k := range pr.Status.TaskRuns {
msg := ""

for k2 := range pr.Status.TaskRuns[k].Status.Conditions {
msg = pr.Status.TaskRuns[k].Status.Conditions[k2].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
110 changes: 82 additions & 28 deletions pkg/pipelines/pipelines_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package pipelines_test

import (
"os"
"path/filepath"
"testing"

"github.com/jenkins-x-plugins/jx-pipeline/pkg/pipelines"
"github.com/jenkins-x-plugins/jx-pipeline/pkg/testpipelines"
v1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-helpers/v3/pkg/testhelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/stretchr/testify/require"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/yaml"
)

const (
Expand All @@ -36,60 +33,61 @@ func TestCreatePipelineActivity(t *testing.T) {
}

func AssertPipelineActivityMapping(t *testing.T, folder string) {
// getting path of prfile
prFile := filepath.Join("testdata", folder, "pipelinerun.yaml")
require.FileExists(t, prFile)

tmpDir := t.TempDir()

data, err := os.ReadFile(prFile)
require.NoError(t, err, "failed to load %s", prFile)

// loading a prfile
pr := &v1beta1.PipelineRun{}
err = yaml.Unmarshal(data, pr)
err := yamls.LoadFile(prFile, pr)
require.NoError(t, err, "failed to unmarshal %s", prFile)

// generating pa from pr
pa := &v1.PipelineActivity{}
pipelines.ToPipelineActivity(pr, pa, false)

// removing timestamps
testpipelines.ClearTimestamps(pa)

paFile := filepath.Join(tmpDir, "pa.yaml")
err = yamls.SaveFile(pa, paFile)
require.NoError(t, err, "failed to save %s", paFile)
// expected pa
expectedFile := filepath.Join("testdata", folder, "expected.yaml")
require.FileExists(t, expectedFile)

t.Logf("created PipelineActivity %s\n", paFile)
expectedPa := &v1.PipelineActivity{}
err = yamls.LoadFile(expectedFile, expectedPa)
require.NoError(t, err, "failed to load %s", expectedPa)

testhelpers.AssertTextFilesEqual(t, filepath.Join("testdata", folder, "expected.yaml"), paFile, "generated git credentials file")
require.Equal(t, expectedPa, pa)
}

func TestMergePipelineActivity(t *testing.T) {
// copying pr path
prFile := filepath.Join("testdata", "merge", "pipelinerun.yaml")
require.FileExists(t, prFile)

paFile := filepath.Join("testdata", "merge", "pa.yaml")
require.FileExists(t, prFile)

tmpDir := t.TempDir()
// copying expected pa path
expectedFile := filepath.Join("testdata", "merge", "expected.yaml")
require.FileExists(t, expectedFile)

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

pa := &v1.PipelineActivity{}
err = yamls.LoadFile(paFile, pa)
require.NoError(t, err, "failed to load %s", paFile)
// loading expectedPa
expectedPa := &v1.PipelineActivity{}
err = yamls.LoadFile(expectedFile, expectedPa)
require.NoError(t, err, "failed to load %s", expectedPa)

// creating pa from pr
pa := &v1.PipelineActivity{}
pipelines.ToPipelineActivity(pr, pa, false)

// removing the timestamp from pa
testpipelines.ClearTimestamps(pa)

paFile = filepath.Join(tmpDir, "pa.yaml")
err = yamls.SaveFile(pa, paFile)
require.NoError(t, err, "failed to save %s", paFile)

t.Logf("created PipelineActivity %s\n", paFile)

testhelpers.AssertTextFilesEqual(t, filepath.Join("testdata", "merge", "expected.yaml"), paFile, "generated git credentials file")
// compare the pa with expected.yaml (expectedPa)
require.Equal(t, expectedPa, pa)
}

func generatePipelineRunWithLabels(branch, org, repo, buildNum string) *v1beta1.PipelineRun {
Expand Down Expand Up @@ -273,3 +271,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())
}
}
64 changes: 33 additions & 31 deletions pkg/pipelines/testdata/create/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,40 @@ spec:
gitRepository: nodey510
gitUrl: https://github.com/jstrachan/nodey510.git
lastCommitSHA: 5a694a615045f2821aa40aefe86aab9192667193
message: "Tasks Completed: 0 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0"
pipeline: jstrachan/nodey510/master
status: Running
steps:
- kind: Stage
stage:
name: from build pack
status: Running
steps:
- name: Git Clone
status: Succeeded
- name: Git Setup
status: Succeeded
- name: Next Version
status: Succeeded
- name: Tag Version
status: Succeeded
- name: Setup Builder Home
status: Succeeded
- name: Git Merge
status: Succeeded
- name: Build Npmrc
status: Succeeded
- name: Build Npm Install
- kind: Stage
stage:
message: Not all Steps in the Task have finished executing
name: from build pack
status: Running
- name: Build Npm Test
status: Pending
- name: Build Container Build
status: Pending
- name: Promote Changelog
status: Pending
- name: Promote Helm Release
status: Pending
- name: Promote Jx Promote
status: Pending
status: {}
steps:
- name: Git Clone
status: Succeeded
- name: Git Setup
status: Succeeded
- name: Next Version
status: Succeeded
- name: Tag Version
status: Succeeded
- name: Setup Builder Home
status: Succeeded
- name: Git Merge
status: Succeeded
- name: Build Npmrc
status: Succeeded
- name: Build Npm Install
status: Running
- name: Build Npm Test
status: Pending
- name: Build Container Build
status: Pending
- name: Promote Changelog
status: Pending
- name: Promote Helm Release
status: Pending
- name: Promote Jx Promote
status: Pending
status: {}
10 changes: 8 additions & 2 deletions pkg/pipelines/testdata/initial/expected.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: jenkins.io/v1
apiVersion: jenkins.io/v1
kind: PipelineActivity
metadata:
annotations:
Expand Down Expand Up @@ -31,11 +31,17 @@ spec:
gitRepository: nodey510
gitUrl: https://github.com/jstrachan/nodey510.git
lastCommitSHA: f25284383035dd4b1ed23bd65aa8e1af8da25488
message: 'Tasks Completed: 0 (Failed: 0, Cancelled 0), Incomplete: 1, Skipped: 0'
pipeline: jstrachan/nodey510/master
status: Pending
steps:
- kind: Stage
stage:
message: 'pod status "Ready":"False"; message: "containers with unready status:
[step-git-clone step-git-setup step-git-merge step-next-version step-tag-version
step-setup-builder-home step-jx-variables step-build-npmrc step-build-npm-install
step-build-npm-test step-build-container-build step-promote-changelog step-promote-helm-release
step-promote-jx-promote]"'
name: from build pack
status: Pending
steps:
Expand Down Expand Up @@ -67,4 +73,4 @@ spec:
status: Pending
- name: Promote Jx Promote
status: Pending
status: {}
status: {}
Loading

0 comments on commit 7d50b6c

Please sign in to comment.