Skip to content

Commit

Permalink
Merge pull request #11448 from amwat/gerrit
Browse files Browse the repository at this point in the history
Add trigger support for gerrit.
  • Loading branch information
k8s-ci-robot committed Mar 22, 2019
2 parents d72fb4c + c406b50 commit f4b239b
Show file tree
Hide file tree
Showing 19 changed files with 1,157 additions and 602 deletions.
42 changes: 21 additions & 21 deletions prow/crier/controller.go
Expand Up @@ -39,7 +39,7 @@ import (
)

type reportClient interface {
Report(pj *v1.ProwJob) error
Report(pj *v1.ProwJob) ([]*v1.ProwJob, error)
GetName() string
ShouldReport(pj *v1.ProwJob) bool
}
Expand Down Expand Up @@ -240,37 +240,37 @@ func (c *Controller) processNextItem() bool {
}

logrus.WithField("prowjob", keyRaw).Infof("Will report state : %s", pj.Status.State)

if err := c.reporter.Report(pj); err != nil {
pjs, err := c.reporter.Report(pj)
if err != nil {
logrus.WithError(err).WithField("prowjob", keyRaw).Error("failed to report job")
return c.retry(key, err)
}

logrus.WithField("prowjob", keyRaw).Info("Updated job, now will update pj")
for _, pjob := range pjs {
if err := c.updateReportState(pjob); err != nil {
logrus.WithError(err).WithField("prowjob", keyRaw).Error("failed to update report state")

if err := c.updateReportState(pj); err != nil {
logrus.WithError(err).WithField("prowjob", keyRaw).Error("failed to update report state")
// theoretically patch should not have this issue, but in case:
// it might be out-dated, try to re-fetch pj and try again

// theoretically patch should not have this issue, but in case:
// it might be out-dated, try to re-fetch pj and try again
updatedPJ, err := c.pjclientset.Prow().ProwJobs(pjob.Namespace).Get(pjob.Name, metav1.GetOptions{})
if err != nil {
logrus.WithError(err).WithField("prowjob", keyRaw).Error("failed to get prowjob from apiserver")
c.queue.Forget(key)
return true
}

updatedPJ, err := c.pjclientset.Prow().ProwJobs(pj.Namespace).Get(pj.Name, metav1.GetOptions{})
if err != nil {
logrus.WithError(err).WithField("prowjob", keyRaw).Error("failed to get prowjob from apiserver")
c.queue.Forget(key)
return true
if err := c.updateReportState(updatedPJ); err != nil {
// shrug
logrus.WithError(err).WithField("prowjob", keyRaw).Error("failed to update report state again, give up")
c.queue.Forget(key)
return true
}
}

if err := c.updateReportState(updatedPJ); err != nil {
// shrug
logrus.WithError(err).WithField("prowjob", keyRaw).Error("failed to update report state again, give up")
c.queue.Forget(key)
return true
}
logrus.WithField("prowjob", keyRaw).Infof("Hunky Dory!, pj : %v, state : %s", pjob.Spec.Job, pjob.Status.State)
}

logrus.WithField("prowjob", keyRaw).Infof("Hunky Dory!, pj : %v, state : %s", pj.Spec.Job, pj.Status.State)

c.queue.Forget(key)
return true
}
5 changes: 4 additions & 1 deletion prow/gerrit/adapter/BUILD.bazel
Expand Up @@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["adapter.go"],
srcs = [
"adapter.go",
"trigger.go",
],
importpath = "k8s.io/test-infra/prow/gerrit/adapter",
visibility = ["//visibility:public"],
deps = [
Expand Down
37 changes: 28 additions & 9 deletions prow/gerrit/adapter/adapter.go
Expand Up @@ -278,15 +278,30 @@ func (c *Controller) ProcessChange(instance string, change client.ChangeInfo) er
case client.New:
presubmits := c.config().Presubmits[cloneURI.String()]
presubmits = append(presubmits, c.config().Presubmits[cloneURI.Host+"/"+cloneURI.Path]...)
for _, presubmit := range presubmits {
if shouldRun, err := presubmit.ShouldRun(change.Branch, changedFiles, false, false); err != nil {
return fmt.Errorf("failed to determine if presubmit %q should run: %v", presubmit.Name, err)
} else if shouldRun {
jobSpecs = append(jobSpecs, jobSpec{
spec: pjutil.PresubmitSpec(presubmit, refs),
labels: presubmit.Labels,
})
}
var filters []pjutil.Filter
filter, err := messageFilter(c.lastUpdate, change, presubmits)
if err != nil {
logger.WithError(err).Warn("failed to create filter on messages for presubmits")
} else {
filters = append(filters, filter)
}
latestRev := change.Revisions[change.CurrentRevision]
created, err := time.Parse(layout, latestRev.Created)
if err != nil {
logrus.WithError(err).Errorf("Parse time %v failed", latestRev.Created)
}
if created.After(c.lastUpdate) {
filters = append(filters, pjutil.TestAllFilter())
}
toTrigger, _, err := pjutil.FilterPresubmits(pjutil.AggregateFilter(filters), listChangedFiles(change), change.Branch, presubmits, logger)
if err != nil {
return fmt.Errorf("failed to filter presubmits: %v", err)
}
for _, presubmit := range toTrigger {
jobSpecs = append(jobSpecs, jobSpec{
spec: pjutil.PresubmitSpec(presubmit, refs),
labels: presubmit.Labels,
})
}
}

Expand All @@ -302,6 +317,10 @@ func (c *Controller) ProcessChange(instance string, change client.ChangeInfo) er
}
labels[client.GerritRevision] = change.CurrentRevision

if gerritLabel, ok := labels[client.GerritReportLabel]; !ok || gerritLabel == "" {
labels[client.GerritReportLabel] = client.CodeReview
}

pj := pjutil.NewProwJobWithAnnotation(jSpec.spec, labels, annotations)
if _, err := c.kc.CreateProwJob(pj); err != nil {
logger.WithError(err).Errorf("fail to create prowjob %v", pj)
Expand Down
119 changes: 106 additions & 13 deletions prow/gerrit/adapter/adapter_test.go
Expand Up @@ -21,7 +21,7 @@ import (
"testing"
"time"

gerrit "github.com/andygrunwald/go-gerrit"
"github.com/andygrunwald/go-gerrit"

"k8s.io/test-infra/prow/gerrit/client"

Expand All @@ -31,6 +31,8 @@ import (
"k8s.io/test-infra/prow/config"
)

var timeNow = time.Date(1234, time.May, 15, 1, 2, 3, 4, time.UTC)

type fca struct {
sync.Mutex
c *config.Config
Expand Down Expand Up @@ -195,7 +197,9 @@ func TestProcessChange(t *testing.T) {
Project: "woof",
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {},
"1": {
Created: timeNow.Format(layout),
},
},
},
},
Expand All @@ -207,7 +211,8 @@ func TestProcessChange(t *testing.T) {
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {
Ref: "refs/changes/00/1/1",
Ref: "refs/changes/00/1/1",
Created: timeNow.Format(layout),
},
},
},
Expand All @@ -222,10 +227,12 @@ func TestProcessChange(t *testing.T) {
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {
Ref: "refs/changes/00/2/1",
Ref: "refs/changes/00/2/1",
Created: timeNow.Format(layout),
},
"2": {
Ref: "refs/changes/00/2/2",
Ref: "refs/changes/00/2/2",
Created: timeNow.Format(layout),
},
},
},
Expand All @@ -240,7 +247,8 @@ func TestProcessChange(t *testing.T) {
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {
Ref: "refs/changes/00/1/1",
Ref: "refs/changes/00/1/1",
Created: timeNow.Format(layout),
},
},
},
Expand All @@ -255,7 +263,8 @@ func TestProcessChange(t *testing.T) {
Status: "MERGED",
Revisions: map[string]client.RevisionInfo{
"1": {
Ref: "refs/changes/00/1/1",
Ref: "refs/changes/00/1/1",
Created: timeNow.Format(layout),
},
},
},
Expand All @@ -270,7 +279,8 @@ func TestProcessChange(t *testing.T) {
Status: "MERGED",
Revisions: map[string]client.RevisionInfo{
"1": {
Ref: "refs/changes/00/1/1",
Ref: "refs/changes/00/1/1",
Created: timeNow.Format(layout),
},
},
},
Expand All @@ -288,6 +298,7 @@ func TestProcessChange(t *testing.T) {
"africa-lyrics.txt": {},
"important-code.go": {},
},
Created: timeNow.Format(layout),
},
},
},
Expand All @@ -306,6 +317,7 @@ func TestProcessChange(t *testing.T) {
"README.md": {},
"let-it-go.txt": {},
},
Created: timeNow.Format(layout),
},
},
},
Expand All @@ -319,7 +331,9 @@ func TestProcessChange(t *testing.T) {
Branch: "pony",
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {},
"1": {
Created: timeNow.Format(layout),
},
},
},
numPJ: 3,
Expand All @@ -332,7 +346,78 @@ func TestProcessChange(t *testing.T) {
Branch: "baz",
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {},
"1": {
Created: timeNow.Format(layout),
},
},
},
numPJ: 1,
},
{
name: "old presubmits don't run on old revision but trigger job does because new message",
change: client.ChangeInfo{
CurrentRevision: "1",
Project: "test-infra",
Branch: "baz",
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {
Number: 1,
Created: timeNow.Add(-time.Hour).Format(layout),
},
},
Messages: []gerrit.ChangeMessageInfo{
{
Message: "/test troll",
RevisionNumber: 1,
Date: timeNow.Add(time.Hour).Format(layout),
},
},
},
numPJ: 1,
},
{
name: "unrelated comment shouldn't trigger anything",
change: client.ChangeInfo{
CurrentRevision: "1",
Project: "test-infra",
Branch: "baz",
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {
Number: 1,
Created: timeNow.Add(-time.Hour).Format(layout),
},
},
Messages: []gerrit.ChangeMessageInfo{
{
Message: "/test diasghdgasudhkashdk",
RevisionNumber: 1,
Date: timeNow.Add(time.Hour).Format(layout),
},
},
},
numPJ: 0,
},
{
name: "trigger always run job on test all even if revision is old",
change: client.ChangeInfo{
CurrentRevision: "1",
Project: "test-infra",
Branch: "baz",
Status: "NEW",
Revisions: map[string]client.RevisionInfo{
"1": {
Number: 1,
Created: timeNow.Add(-time.Hour).Format(layout),
},
},
Messages: []gerrit.ChangeMessageInfo{
{
Message: "/test all",
RevisionNumber: 1,
Date: timeNow.Add(time.Hour).Format(layout),
},
},
},
numPJ: 1,
Expand Down Expand Up @@ -373,6 +458,13 @@ func TestProcessChange(t *testing.T) {
},
AlwaysRun: true,
},
{
JobBase: config.JobBase{
Name: "trigger-regex-all-branches",
},
Trigger: `.*/test\s*troll.*`,
RerunCommand: "/test troll",
},
}
if err := config.SetPresubmitRegexes(testInfraPresubmits); err != nil {
t.Fatalf("could not set regexes: %v", err)
Expand Down Expand Up @@ -408,9 +500,10 @@ func TestProcessChange(t *testing.T) {
fkc := &fkc{}

c := &Controller{
config: fca.Config,
kc: fkc,
gc: &fgc{},
config: fca.Config,
kc: fkc,
gc: &fgc{},
lastUpdate: timeNow.Add(-time.Minute),
}

err := c.ProcessChange("https://gerrit", tc.change)
Expand Down

0 comments on commit f4b239b

Please sign in to comment.