Skip to content

Commit

Permalink
Add PR ref to postsubmit jobs in mkpj command
Browse files Browse the repository at this point in the history
The mkpj command now includes a reference to PR in the generated
postsubmit job specs.

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
  • Loading branch information
adshmh committed Apr 8, 2020
1 parent d84b106 commit b430c63
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
1 change: 1 addition & 0 deletions prow/cmd/mkpj/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ go_test(
embed = [":go_default_library"],
deps = [
"//prow/apis/prowjobs/v1:go_default_library",
"//prow/config:go_default_library",
"//prow/github:go_default_library",
"//prow/github/fakegithub:go_default_library",
],
Expand Down
33 changes: 16 additions & 17 deletions prow/cmd/mkpj/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,7 @@ func (o *options) genJobSpec(conf *config.Config, name string) (config.JobBase,
}
for _, p := range ps {
if p.Name == o.jobName {
return p.JobBase, pjutil.PresubmitSpec(p, prowapi.Refs{
Org: org,
Repo: repo,
BaseRef: o.baseRef,
BaseSHA: o.baseSha,
Pulls: []prowapi.Pull{{
Author: o.pullAuthor,
Number: o.pullNumber,
SHA: o.pullSha,
}},
})
return p.JobBase, pjutil.PresubmitSpec(p, o.createRefs(org, repo))
}
}
}
Expand All @@ -85,12 +75,7 @@ func (o *options) genJobSpec(conf *config.Config, name string) (config.JobBase,
}
for _, p := range ps {
if p.Name == o.jobName {
return p.JobBase, pjutil.PostsubmitSpec(p, prowapi.Refs{
Org: org,
Repo: repo,
BaseRef: o.baseRef,
BaseSHA: o.baseSha,
})
return p.JobBase, pjutil.PostsubmitSpec(p, o.createRefs(org, repo))
}
}
}
Expand Down Expand Up @@ -171,6 +156,20 @@ func (o *options) defaultBaseRef(pjs *prowapi.ProwJobSpec) error {
return nil
}

func (o *options) createRefs(org, repo string) prowapi.Refs {
return prowapi.Refs{
Org: org,
Repo: repo,
BaseRef: o.baseRef,
BaseSHA: o.baseSha,
Pulls: []prowapi.Pull{{
Author: o.pullAuthor,
Number: o.pullNumber,
SHA: o.pullSha,
}},
}
}

type githubClient interface {
GetPullRequest(org, repo string, number int) (*github.PullRequest, error)
GetRef(org, repo, ref string) (string, error)
Expand Down
64 changes: 64 additions & 0 deletions prow/cmd/mkpj/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/github/fakegithub"
)
Expand Down Expand Up @@ -126,3 +127,66 @@ func TestDefaultBaseRef(t *testing.T) {
}

}

func TestGenJobSpec(t *testing.T) {
author := "PR_Author"
sha := "PR_SHA"
testCases := []struct {
name string
jobName string
}{
{
name: "presubmit job has PR reference",
jobName: "presubmit-test",
},
{
name: "postsubmit job has PR reference",
jobName: "postsubmit-test",
},
}
conf := &config.Config{
JobConfig: config.JobConfig{
PresubmitsStatic: map[string][]config.Presubmit{
"org/repo": {
config.Presubmit{
JobBase: config.JobBase{Name: "presubmit-test"},
},
},
},
PostsubmitsStatic: map[string][]config.Postsubmit{
"org/repo": {
config.Postsubmit{
JobBase: config.JobBase{Name: "postsubmit-test"},
},
},
},
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
o := &options{
jobName: test.jobName,
pullAuthor: author,
pullNumber: 1,
pullSha: sha,
}
_, jobSpec := o.genJobSpec(conf, test.jobName)
if jobSpec.Refs == nil {
t.Fatalf("expected Refs to be set on the job %s, found nil", test.jobName)
}
pulls := jobSpec.Refs.Pulls
if len(pulls) != 1 {
t.Fatalf("expected Pulls to be 1 in job %s Refs, found %d", test.jobName, len(pulls))
}
if pulls[0].Author != author {
t.Errorf("expected %s as Pull author, found %s", author, pulls[0].Author)
}
if pulls[0].SHA != sha {
t.Errorf("expected %s as Pull SHA, found %s", sha, pulls[0].SHA)
}
if pulls[0].Number != 1 {
t.Errorf("expected 1 as Pull Number, found %d", pulls[0].Number)
}
})
}
}

0 comments on commit b430c63

Please sign in to comment.