Skip to content

Commit

Permalink
Merge pull request #2201 from spxtr/newmetric
Browse files Browse the repository at this point in the history
Add retests/pr transform plugin.
  • Loading branch information
spxtr authored Mar 9, 2017
2 parents 0bc656a + 8be4464 commit 6e1a79e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions velodrome/transform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ go_library(
"plugins.go",
"prstats.go",
"rebase.go",
"retest.go",
"transform.go",
],
tags = ["automanaged"],
Expand Down
1 change: 1 addition & 0 deletions velodrome/transform/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func NewPlugins(idb InfluxDatabase) Plugins {
NewLGTMToMergedPlugin(idb),
NewFirstCommentPlugin(idb),
NewRebasePlugin(idb),
NewRetestPlugin(idb),
}

return plugins
Expand Down
88 changes: 88 additions & 0 deletions velodrome/transform/retest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"regexp"
"time"

"github.com/golang/glog"

"k8s.io/test-infra/velodrome/sql"
)

var (
// Matches "@k8s-bot foo bar test this" with the capture group "foo bar".
testThisRe = regexp.MustCompile(`@k8s-bot (\w*(?: \w+)*) ?test this`)

robotNames = map[string]bool{
"k8s-merge-robot": true,
"k8s-ci-robot": true,
}
)

type Retest struct {
DB InfluxDatabase
last time.Time
// Number of requests for retest per PR.
retests map[int]int
}

// NewRetestPlugin initializes the plugin.
func NewRetestPlugin(DB InfluxDatabase) *Retest {
last, err := DB.GetLastMeasurement("retest")
if err != nil {
glog.Fatal("Failed to create Retest plugin: ", err)
}
return &Retest{
DB: DB,
last: last,
retests: make(map[int]int),
}
}

// ReceiveIssue is not used for this metric.
func (*Retest) ReceiveIssue(sql.Issue) error {
return nil
}

// ReceiveComment counts occurences of "@k8s-bot test this"-style comments.
func (r *Retest) ReceiveComment(comment sql.Comment) error {
if robotNames[comment.User] {
return nil
}
if matches := testThisRe.MatchString(comment.Body); !matches {
return nil
}
r.retests[comment.IssueID]++
return nil
}

// ReceiveIssueEvent adds to the DB when a PR is merged.
func (r *Retest) ReceiveIssueEvent(event sql.IssueEvent) error {
if !event.EventCreatedAt.After(r.last) {
return nil
}
if event.Event != "merged" {
return nil
}
return r.DB.Push(
"retest",
nil,
map[string]interface{}{"value": r.retests[event.IssueId]},
event.EventCreatedAt)
}

0 comments on commit 6e1a79e

Please sign in to comment.