Skip to content

Commit a1a395d

Browse files
authored
feat(pod-utils): add pull author prow label (#31)
* feat(pod-utils): add CI annotations to Prow pods * feat(pod-utils): add pull author prow label * fix(pod-utils): skip empty pull author label * test(prowjob): expect pull author label
1 parent 5ba85b8 commit a1a395d

15 files changed

Lines changed: 146 additions & 0 deletions

cmd/deck/rerun_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ func TestLatestRerun(t *testing.T) {
596596
"prow.k8s.io/context": "",
597597
"prow.k8s.io/gerrit-report-label": "foo",
598598
"prow.k8s.io/job": "whoa",
599+
"prow.k8s.io/refs.author": tc.login,
599600
"prow.k8s.io/refs.base_ref": "master",
600601
"prow.k8s.io/refs.org": "org",
601602
"prow.k8s.io/refs.pull": "1",

pkg/kube/prowjob.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ const (
6464
// PullLabel is added in resources created by prow and
6565
// carries the PR number associated with the job, eg 321.
6666
PullLabel = "prow.k8s.io/refs.pull"
67+
// AuthorLabel is added in resources created by prow and
68+
// carries the PR author associated with the job, eg octocat.
69+
AuthorLabel = "prow.k8s.io/refs.author"
6770
// RetestLabel exposes if the job was created by a re-test request.
6871
RetestLabel = "prow.k8s.io/retest"
6972
// IsOptionalLabel is added in resources created by prow and

pkg/pjutil/pjutil_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ func TestNewPresubmitWithModifiers(t *testing.T) {
10641064
kube.RepoLabel: "repo-name",
10651065
kube.BaseRefLabel: "base-ref",
10661066
kube.PullLabel: "42",
1067+
kube.AuthorLabel: "user-login",
10671068
"extra-label": "foo",
10681069
},
10691070
Annotations: map[string]string{
@@ -1130,6 +1131,7 @@ func TestNewPresubmitWithModifiers(t *testing.T) {
11301131
kube.RepoLabel: "repo-name",
11311132
kube.BaseRefLabel: "base-ref",
11321133
kube.PullLabel: "42",
1134+
kube.AuthorLabel: "user-login",
11331135
"extra-label": "foo",
11341136
},
11351137
Annotations: map[string]string{

pkg/pod-utils/decorate/podspec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ func LabelsAndAnnotationsForSpec(spec prowapi.ProwJobSpec, extraLabels, extraAnn
141141
labels[kube.BaseRefLabel] = refs.BaseRef
142142
if len(refs.Pulls) > 0 {
143143
labels[kube.PullLabel] = strconv.Itoa(refs.Pulls[0].Number)
144+
if refs.Pulls[0].Author != "" {
145+
labels[kube.AuthorLabel] = refs.Pulls[0].Author
146+
}
144147
}
145148
}
146149

pkg/pod-utils/decorate/podspec_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"sigs.k8s.io/prow/pkg/gcsupload"
4040
"sigs.k8s.io/prow/pkg/github"
4141
"sigs.k8s.io/prow/pkg/initupload"
42+
"sigs.k8s.io/prow/pkg/kube"
4243
"sigs.k8s.io/prow/pkg/pod-utils/wrapper"
4344
"sigs.k8s.io/prow/pkg/sidecar"
4445
"sigs.k8s.io/prow/pkg/testutil"
@@ -65,6 +66,132 @@ func cookiePathOnly(secret string) string {
6566
return vp
6667
}
6768

69+
func TestLabelsAndAnnotationsForSpecAddsPullAuthorLabel(t *testing.T) {
70+
tests := []struct {
71+
name string
72+
spec prowapi.ProwJobSpec
73+
extraLabel map[string]string
74+
want string
75+
wantExists bool
76+
}{
77+
{
78+
name: "presubmit with pull author",
79+
spec: prowapi.ProwJobSpec{
80+
Job: "pull-test",
81+
Refs: &prowapi.Refs{
82+
Org: "pingcap",
83+
Repo: "tidb",
84+
BaseRef: "master",
85+
BaseSHA: "base-sha",
86+
Pulls: []prowapi.Pull{{
87+
Number: 123,
88+
Author: "pr-author",
89+
SHA: "pull-sha",
90+
}},
91+
},
92+
},
93+
want: "pr-author",
94+
wantExists: true,
95+
},
96+
{
97+
name: "postsubmit with refs but no pulls",
98+
spec: prowapi.ProwJobSpec{
99+
Job: "postsubmit-test",
100+
Refs: &prowapi.Refs{
101+
Org: "pingcap",
102+
Repo: "tidb",
103+
BaseRef: "master",
104+
BaseSHA: "base-sha",
105+
},
106+
},
107+
},
108+
{
109+
name: "presubmit with empty pull author",
110+
spec: prowapi.ProwJobSpec{
111+
Job: "pull-test",
112+
Refs: &prowapi.Refs{
113+
Org: "pingcap",
114+
Repo: "tidb",
115+
Pulls: []prowapi.Pull{{
116+
Number: 123,
117+
}},
118+
},
119+
},
120+
},
121+
{
122+
name: "invalid author is removed by label validation",
123+
spec: prowapi.ProwJobSpec{
124+
Job: "pull-test",
125+
Refs: &prowapi.Refs{
126+
Org: "pingcap",
127+
Repo: "tidb",
128+
Pulls: []prowapi.Pull{{
129+
Number: 123,
130+
Author: "bad author",
131+
}},
132+
},
133+
},
134+
},
135+
{
136+
name: "extra labels can override author",
137+
spec: prowapi.ProwJobSpec{
138+
Job: "pull-test",
139+
Refs: &prowapi.Refs{
140+
Org: "pingcap",
141+
Repo: "tidb",
142+
Pulls: []prowapi.Pull{{
143+
Number: 123,
144+
Author: "pr-author",
145+
}},
146+
},
147+
},
148+
extraLabel: map[string]string{
149+
kube.AuthorLabel: "custom-author",
150+
},
151+
want: "custom-author",
152+
wantExists: true,
153+
},
154+
}
155+
156+
for _, tc := range tests {
157+
t.Run(tc.name, func(t *testing.T) {
158+
got, _ := LabelsAndAnnotationsForSpec(tc.spec, tc.extraLabel, nil)
159+
value, exists := got[kube.AuthorLabel]
160+
if exists != tc.wantExists {
161+
t.Fatalf("expected author label existence %t, got %t", tc.wantExists, exists)
162+
}
163+
if value != tc.want {
164+
t.Fatalf("expected author label %q, got %q", tc.want, value)
165+
}
166+
})
167+
}
168+
}
169+
170+
func TestLabelsAndAnnotationsForJobAddsPullAuthorLabel(t *testing.T) {
171+
got, _ := LabelsAndAnnotationsForJob(prowapi.ProwJob{
172+
ObjectMeta: metav1.ObjectMeta{
173+
Name: "pj",
174+
},
175+
Spec: prowapi.ProwJobSpec{
176+
Job: "pull-test",
177+
Type: prowapi.PresubmitJob,
178+
Context: "pull-test",
179+
Refs: &prowapi.Refs{
180+
Org: "pingcap",
181+
Repo: "tidb",
182+
Pulls: []prowapi.Pull{{
183+
Number: 1,
184+
Author: "pr-author",
185+
}},
186+
},
187+
},
188+
})
189+
190+
if got[kube.AuthorLabel] != "pr-author" {
191+
t.Fatalf("expected author label %q, got %q", "pr-author", got[kube.AuthorLabel])
192+
}
193+
}
194+
68195
func TestCloneRefs(t *testing.T) {
69196
truth := true
70197
logMount := coreapi.VolumeMount{

pkg/pod-utils/decorate/testdata/TestProwJobToPod_0.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ metadata:
1010
prow.k8s.io/context: job-context
1111
prow.k8s.io/id: pod
1212
prow.k8s.io/job: job-name
13+
prow.k8s.io/refs.author: author-name
1314
prow.k8s.io/refs.base_ref: base-ref
1415
prow.k8s.io/refs.org: org-name
1516
prow.k8s.io/refs.pull: "1"

pkg/pod-utils/decorate/testdata/TestProwJobToPod_1.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ metadata:
1010
prow.k8s.io/context: job-context
1111
prow.k8s.io/id: pod
1212
prow.k8s.io/job: job-name
13+
prow.k8s.io/refs.author: author-name
1314
prow.k8s.io/refs.base_ref: base-ref
1415
prow.k8s.io/refs.org: org-name
1516
prow.k8s.io/refs.pull: "1"

pkg/pod-utils/decorate/testdata/TestProwJobToPod_10.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ metadata:
1010
prow.k8s.io/context: job-context
1111
prow.k8s.io/id: pod
1212
prow.k8s.io/job: job-name
13+
prow.k8s.io/refs.author: author-name
1314
prow.k8s.io/refs.base_ref: base-ref
1415
prow.k8s.io/refs.org: org-name
1516
prow.k8s.io/refs.pull: "1"

pkg/pod-utils/decorate/testdata/TestProwJobToPod_2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ metadata:
1010
prow.k8s.io/context: job-context
1111
prow.k8s.io/id: pod
1212
prow.k8s.io/job: job-name
13+
prow.k8s.io/refs.author: author-name
1314
prow.k8s.io/refs.base_ref: base-ref
1415
prow.k8s.io/refs.org: org-name
1516
prow.k8s.io/refs.pull: "1"

pkg/pod-utils/decorate/testdata/TestProwJobToPod_3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ metadata:
1010
prow.k8s.io/context: job-context
1111
prow.k8s.io/id: pod
1212
prow.k8s.io/job: job-name
13+
prow.k8s.io/refs.author: author-name
1314
prow.k8s.io/refs.base_ref: base-ref
1415
prow.k8s.io/refs.org: org-name
1516
prow.k8s.io/refs.pull: "1"

0 commit comments

Comments
 (0)