Skip to content

Commit fc612e8

Browse files
committed
enhance: add PipelineRun name in pr json marshalling
when a PipelineRun fails json marshalling in match.go we don't know which PipelineRun has syntax error if there are multiple PipelineRuns. this PR adds PipelineRun name along with Json marshalling error so that it is easy to track the issue. Signed-off-by: Zaki Shaikh <zashaikh@redhat.com>
1 parent 269d2a7 commit fc612e8

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

pkg/pipelineascode/match.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,14 @@ func filterRunningPipelineRunOnTargetTest(testPipeline string, prs []*tektonv1.P
396396
// - the template variable with the one from the event (this includes the remote pipeline that has template variables).
397397
func (p *PacRun) changePipelineRun(ctx context.Context, repo *v1alpha1.Repository, prs []*tektonv1.PipelineRun) error {
398398
for k, pr := range prs {
399+
prName := pr.GetName()
400+
if prName == "" {
401+
prName = pr.GetGenerateName()
402+
}
403+
399404
b, err := json.Marshal(pr)
400405
if err != nil {
401-
return err
406+
return fmt.Errorf("failed to marshal PipelineRun %s: %w", prName, err)
402407
}
403408

404409
name := secrets.GenerateBasicAuthSecretName()
@@ -410,7 +415,7 @@ func (p *PacRun) changePipelineRun(ctx context.Context, repo *v1alpha1.Repositor
410415
var np *tektonv1.PipelineRun
411416
err = json.Unmarshal([]byte(processed), &np)
412417
if err != nil {
413-
return err
418+
return fmt.Errorf("failed to unmarshal PipelineRun %s: %w", prName, err)
414419
}
415420
// don't crash when we don't have any annotations
416421
if np.Annotations == nil {

pkg/pipelineascode/match_test.go

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ func TestPacRun_checkNeedUpdate(t *testing.T) {
5858
}
5959

6060
func TestChangePipelineRun(t *testing.T) {
61+
repo := &v1alpha1.Repository{
62+
ObjectMeta: metav1.ObjectMeta{
63+
Name: "testrepo",
64+
Namespace: "test",
65+
},
66+
}
67+
6168
prs := []*tektonv1.PipelineRun{
6269
{
6370
ObjectMeta: metav1.ObjectMeta{
@@ -66,21 +73,59 @@ func TestChangePipelineRun(t *testing.T) {
6673
},
6774
},
6875
}
69-
event := info.NewEvent()
70-
event.Repository = "testrepo"
71-
p := &PacRun{event: event}
72-
repo := &v1alpha1.Repository{
73-
ObjectMeta: metav1.ObjectMeta{
74-
Name: "testrepo",
75-
Namespace: "test",
76+
77+
jsonErrorPRs := []*tektonv1.PipelineRun{
78+
{
79+
ObjectMeta: metav1.ObjectMeta{
80+
Name: "json-error-pr",
81+
Namespace: "test",
82+
},
83+
Spec: tektonv1.PipelineRunSpec{
84+
Params: []tektonv1.Param{
85+
{
86+
Name: "my-param",
87+
Value: tektonv1.ParamValue{
88+
// We are intentionally leaving this empty:
89+
// Type: tektonv1.ParamTypeString,
90+
StringVal: "some-value",
91+
},
92+
},
93+
},
94+
},
95+
},
96+
}
97+
98+
tests := []struct {
99+
name string
100+
prs []*tektonv1.PipelineRun
101+
expectedError string
102+
}{
103+
{
104+
name: "test with params",
105+
prs: prs,
76106
},
107+
{
108+
name: "test with json error",
109+
prs: jsonErrorPRs,
110+
expectedError: "failed to marshal PipelineRun json-error-pr: json: error calling MarshalJSON for type v1.ParamValue: impossible ParamValues.Type: \"\"",
111+
},
112+
}
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
event := info.NewEvent()
116+
event.Repository = "testrepo"
117+
p := &PacRun{event: event}
118+
ctx, _ := rtesting.SetupFakeContext(t)
119+
err := p.changePipelineRun(ctx, repo, tt.prs)
120+
if tt.expectedError != "" {
121+
assert.Error(t, err, tt.expectedError)
122+
return
123+
}
124+
assert.Assert(t, strings.HasPrefix(tt.prs[0].GetName(), "pac-gitauth"), tt.prs[0].GetName(), "has no pac-gitauth prefix")
125+
assert.Assert(t, tt.prs[0].GetAnnotations()[apipac.GitAuthSecret] != "")
126+
assert.Assert(t, tt.prs[0].GetNamespace() == "testrepo", "namespace should be testrepo: %v", tt.prs[0].GetNamespace())
127+
})
77128
}
78-
ctx, _ := rtesting.SetupFakeContext(t)
79-
err := p.changePipelineRun(ctx, repo, prs)
80-
assert.NilError(t, err)
81-
assert.Assert(t, strings.HasPrefix(prs[0].GetName(), "pac-gitauth"), prs[0].GetName(), "has no pac-gitauth prefix")
82-
assert.Assert(t, prs[0].GetAnnotations()[apipac.GitAuthSecret] != "")
83-
assert.Assert(t, prs[0].GetNamespace() == "testrepo", "namespace should be testrepo: %v", prs[0].GetNamespace())
84129
}
85130

86131
func TestFilterRunningPipelineRunOnTargetTest(t *testing.T) {

0 commit comments

Comments
 (0)