Skip to content

Commit

Permalink
Tide: trigger jobs that are required by Gerrit
Browse files Browse the repository at this point in the history
Jobs required are different between GitHub and Gerrit. For GitHub they are defined on the job config, and for Gerrit it depends on whether the Gerrit label the prowjob votes on is also required by Gerrit
  • Loading branch information
chaodaiG committed Dec 1, 2022
1 parent fa0f77b commit e18823c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions prow/tide/codereview.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,8 @@ type provider interface {

refsForJob(sp subpool, prs []CodeReviewCommon) (prowapi.Refs, error)
labelsAndAnnotations(instance string, jobLabels, jobAnnotations map[string]string, changes ...CodeReviewCommon) (labels, annotations map[string]string)

// jobIsRequiredByTide is defined by each provider for figuring out whether
// a job is required by Tide or not.
jobIsRequiredByTide(ps *config.Presubmit, pr *CodeReviewCommon) bool
}
20 changes: 20 additions & 0 deletions prow/tide/gerrit.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,23 @@ func (p *GerritProvider) labelsAndAnnotations(instance string, jobLabels, jobAnn
labels, annotations = gerritadaptor.LabelsAndAnnotations(instance, jobLabels, jobAnnotations, changes...)
return
}

func (p *GerritProvider) jobIsRequiredByTide(ps *config.Presubmit, crc *CodeReviewCommon) bool {
if ps.RunBeforeMerge {
return true
}

pr := crc.Gerrit
requireLabels := sets.NewString()
for l, info := range pr.Labels {
if !info.Optional {
requireLabels.Insert(l)
}
}

val, ok := ps.Labels[kube.GerritReportLabel]
if !ok {
return false
}
return requireLabels.Has(val)
}
54 changes: 54 additions & 0 deletions prow/tide/gerrit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,3 +944,57 @@ func TestPrMergeMethod(t *testing.T) {
})
}
}

func TestJobIsRequiredByTide(t *testing.T) {
tests := []struct {
name string
ps *config.Presubmit
crc *CodeReviewCommon
want bool
}{
{
name: "default",
ps: &config.Presubmit{},
crc: &CodeReviewCommon{
Gerrit: &gerrit.ChangeInfo{Labels: map[string]gerrit.LabelInfo{}},
},
},
{
name: "run-before-merge",
ps: &config.Presubmit{RunBeforeMerge: true},
crc: &CodeReviewCommon{
Gerrit: &gerrit.ChangeInfo{Labels: map[string]gerrit.LabelInfo{}},
},
want: true,
},
{
name: "required-label",
ps: &config.Presubmit{JobBase: config.JobBase{Labels: map[string]string{
"prow.k8s.io/gerrit-report-label": "Verified-By-Prow",
}}},
crc: &CodeReviewCommon{
Gerrit: &gerrit.ChangeInfo{Labels: map[string]gerrit.LabelInfo{"Verified-By-Prow": {}}},
},
want: true,
},
{
name: "optional-label",
ps: &config.Presubmit{JobBase: config.JobBase{Labels: map[string]string{
"prow.k8s.io/gerrit-report-label": "Verified-By-Prow",
}}},
crc: &CodeReviewCommon{
Gerrit: &gerrit.ChangeInfo{Labels: map[string]gerrit.LabelInfo{"Verified-By-Prow": {Optional: true}}},
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
fc := &GerritProvider{}
if want, got := tc.want, fc.jobIsRequiredByTide(tc.ps, tc.crc); want != got {
t.Errorf("Wrong. Want: %v, got: %v", want, got)
}
})
}
}
4 changes: 4 additions & 0 deletions prow/tide/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ func (gi *GitHubProvider) labelsAndAnnotations(instance string, jobLabels, jobAn
return
}

func (gi *GitHubProvider) jobIsRequiredByTide(ps *config.Presubmit, pr *CodeReviewCommon) bool {
return ps.ContextRequired() || ps.RunBeforeMerge
}

// dateToken generates a GitHub search query token for the specified date range.
// See: https://help.github.com/articles/understanding-the-search-syntax/#query-for-dates
func dateToken(start, end time.Time) string {
Expand Down
10 changes: 8 additions & 2 deletions prow/tide/tide.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package tide
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sort"
Expand Down Expand Up @@ -1557,7 +1558,7 @@ func (c *syncController) presubmitsByPull(sp *subpool) (map[int][]config.Presubm
log.WithField("num_possible_presubmit", len(presubmitsForPull)).Debug("Found possible presubmits")

for _, ps := range presubmitsForPull {
if !ps.ContextRequired() {
if !c.provider.jobIsRequiredByTide(&ps, &pr) {
continue
}

Expand Down Expand Up @@ -1591,6 +1592,11 @@ func (c *syncController) presubmitsByPull(sp *subpool) (map[int][]config.Presubm
func (c *syncController) presubmitsForBatch(prs []CodeReviewCommon, org, repo, baseSHA, baseBranch string) ([]config.Presubmit, error) {
log := c.logger.WithFields(logrus.Fields{"repo": repo, "org": org, "base-sha": baseSHA, "base-branch": baseBranch})

if len(prs) == 0 {
log.Debug("No PRs, skip looking for presubmits for batch.")
return nil, errors.New("no PRs are provided")
}

var headRefGetters []config.RefGetter
for _, pr := range prs {
headRefGetters = append(headRefGetters, refGetterFactory(pr.HeadRefOID))
Expand All @@ -1604,7 +1610,7 @@ func (c *syncController) presubmitsForBatch(prs []CodeReviewCommon, org, repo, b

var result []config.Presubmit
for _, ps := range presubmits {
if !ps.ContextRequired() {
if !c.provider.jobIsRequiredByTide(&ps, &prs[0]) {
continue
}

Expand Down

0 comments on commit e18823c

Please sign in to comment.