Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
First stab at an OWNERS implementation
Browse files Browse the repository at this point in the history
Nothing enforces it, but it should be automatically just adding and
removing a label based on 'OWNERS'

The kubernetes tree pointed to MUST have an OWNERS file in the root dir
or it will absolutely go off the rails.
  • Loading branch information
eparis committed Mar 20, 2016
1 parent 23b22e4 commit 784ac2d
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 11 deletions.
37 changes: 27 additions & 10 deletions mungegithub/features/repo-updates.go
Expand Up @@ -38,15 +38,15 @@ const (

type assignmentConfig struct {
Assignees []string `json:assignees yaml:assignees`
//Owners []string `json:owners`
Owners []string `json:owners yaml:owners`
}

// RepoInfo provides information about users in OWNERS files in a git repo
type RepoInfo struct {
enabled bool
kubernetesDir string
assignees map[string]sets.String
//owners map[string]sets.String
owners map[string]sets.String
}

func init() {
Expand Down Expand Up @@ -98,7 +98,7 @@ func (o *RepoInfo) walkFunc(path string, info os.FileInfo, err error) error {
}
path = filepath.Dir(path)
o.assignees[path] = sets.NewString(c.Assignees...)
//o.owners[path] = sets.NewString(c.Assignees...)
o.owners[path] = sets.NewString(c.Assignees...)
return nil
}

Expand Down Expand Up @@ -199,17 +199,34 @@ func (o *RepoInfo) LeafAssignees(path string) sets.String {
return peopleForPath(path, o.assignees, true)
}

// Assignees returns a set of users who are the closest assginees to the
// Assignees returns a set of users who are assignees anywhere along the path to the
// requested file. If pkg/OWNERS has user1 and pkg/util/OWNERS has user2 this
// will return both user1 and user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) Assignees(path string) sets.String {
return peopleForPath(path, o.assignees, false)
}

//func (o *RepoInfo) LeafOwners(path string) sets.String {
//return people(path, o.owners, true)
//}
// LeafOwners returns a set of users who are the closest owners to the
// requested file. If pkgOWNERS has user1 and pkg/util/OWNERS has user2 this
// will only return user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) LeafOwners(path string) sets.String {
return peopleForPath(path, o.owners, true)
}

// Assignees returns a set of users who are owners anywhere along the path to the
// requested file. If pkg/OWNERS has user1 and pkg/util/OWNERS has user2 this
// will return both user1 and user2 for the path pkg/util/sets/file.go
func (o *RepoInfo) Owners(path string) sets.String {
return peopleForPath(path, o.owners, false)
}

//func (o *RepoInfo) Owners(path string) sets.String {
//return people(path, o.owners, false)
//}
// TestFakeRepo returns a repo with the specified values. It should only be used in
// _test.go
func TestFakeRepo(assignees, owners map[string]sets.String) *RepoInfo {
info := RepoInfo{
enabled: true,
assignees: assignees,
owners: owners,
}
return &info
}
1 change: 1 addition & 0 deletions mungegithub/github/github.go
Expand Up @@ -487,6 +487,7 @@ func (obj *MungeObject) LastModifiedTime() *time.Time {
var lastModified *time.Time
commits, err := obj.GetCommits()
if err != nil {
glog.Errorf("Unable to determine LastModifiedTime for #%d: %v", *obj.Issue.Number, err)
return lastModified
}
for _, commit := range commits {
Expand Down
126 changes: 126 additions & 0 deletions mungegithub/mungers/owner-approval.go
@@ -0,0 +1,126 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 mungers

import (
"strings"

"k8s.io/contrib/mungegithub/features"
"k8s.io/contrib/mungegithub/github"
"k8s.io/kubernetes/pkg/util/sets"

"github.com/golang/glog"
"github.com/spf13/cobra"
)

const (
ownerApproval = "has-owner-approval"
)

// OwnerApproval it a github munger which labels PRs based on 'owner' approval.
// Owners are listed in files (called OWNERS) in the git tree. An owner owns
// everything beneath the directory. So a person who 'owns' the root directory
// can approve everything
type OwnerApproval struct {
features *features.Features
}

func init() {
RegisterMungerOrDie(&OwnerApproval{})
}

// Name is the name usable in --pr-mungers
func (o *OwnerApproval) Name() string { return "owner-approval" }

// RequiredFeatures specifies that we need the features which loads assignees and owners
func (o *OwnerApproval) RequiredFeatures() []string { return []string{features.RepoFeatureName} }

// Initialize does just that
func (o *OwnerApproval) Initialize(config *github.Config, features *features.Features) error {
o.features = features
return nil
}

// EachLoop is called at the start of every munge loop
func (o *OwnerApproval) EachLoop() error { return nil }

// AddFlags will add any request flags to the cobra `cmd`
func (o *OwnerApproval) AddFlags(cmd *cobra.Command, config *github.Config) {}

// Munge is the workhorse the will actually make updates to the PR
func (o *OwnerApproval) Munge(obj *github.MungeObject) {
if !obj.IsPR() {
return
}
commits, err := obj.GetCommits()
if err != nil {
return
}

neededApprovals := map[string]sets.String{}
for _, c := range commits {
for _, f := range c.Files {
file := *f.Filename
neededApprovals[file] = o.features.Repos.Owners(file)
}
}

lastModified := obj.LastModifiedTime()
if lastModified == nil {
return
}

comments, err := obj.ListComments()
if err != nil {
return
}

approvalsGiven := sets.String{}
for _, comment := range comments {
if lastModified.After(*comment.UpdatedAt) {
continue
}
lines := strings.Split(*comment.Body, "\n")
for _, line := range lines {
line = strings.TrimPrefix(line, "@k8s-merge-bot")
line = strings.TrimSpace(line)
line = strings.ToLower(line)
switch line {
case "i approve":
approvalsGiven.Insert(*comment.User.Login)
case "approved":
approvalsGiven.Insert(*comment.User.Login)
}
}
}

missingApprovals := sets.NewString()
for _, needed := range neededApprovals {
intersection := needed.Intersection(approvalsGiven)
if intersection.Len() != 0 {
// Someone who approved covered this area
continue
}
missingApprovals = missingApprovals.Union(needed)
}

if missingApprovals.Len() == 0 && !obj.HasLabel(ownerApproval) {
obj.AddLabel(ownerApproval)
} else if missingApprovals.Len() != 0 && obj.HasLabel(ownerApproval) {
obj.RemoveLabel(ownerApproval)
}
}
180 changes: 180 additions & 0 deletions mungegithub/mungers/owner-approval_test.go
@@ -0,0 +1,180 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 mungers

import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"strings"
"testing"
"time"

github_features "k8s.io/contrib/mungegithub/features"
github_util "k8s.io/contrib/mungegithub/github"
github_test "k8s.io/contrib/mungegithub/github/testing"
"k8s.io/kubernetes/pkg/util/sets"

"github.com/golang/glog"
"github.com/google/go-github/github"
)

var (
_ = fmt.Printf
_ = glog.Errorf
)

func comment(body string, user string, t int64) github.IssueComment {
return github.IssueComment{
Body: &body,
User: &github.User{
Login: &user,
},
CreatedAt: timePtr(time.Unix(t, 0)),
UpdatedAt: timePtr(time.Unix(t, 0)),
}
}

func TestOwnerMunge(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())

tests := []struct {
name string // because when the fail, counting is hard
commits []github.RepositoryCommit
comments []github.IssueComment
pass bool
}{
{
name: "Approved by lowest level",
commits: pathsCommit([]string{"hack/file.sh", "hack/after-build/file.sh", "pkg/util/file.go"}), // Modified at time.Unix(7), 8, and 9
comments: []github.IssueComment{
comment("hello", "root2", 11),
comment("i ApproVe", "hack1", 11),
comment("ApproVed", "afterbuild1", 11),
comment("ApproVed", "pkg2", 11),
},
pass: true,
},
{
name: "Approved by root",
commits: pathsCommit([]string{"hack/file.sh", "hack/after-build/file.sh", "pkg/util/file.go"}), // Modified at time.Unix(7), 8, and 9
comments: []github.IssueComment{
comment("ApproVed", "root2", 11),
},
pass: true,
},
{
name: "Missing pkg approval",
commits: pathsCommit([]string{"hack/file.sh", "hack/after-build/file.sh", "pkg/util/file.go"}), // Modified at time.Unix(7), 8, and 9
comments: []github.IssueComment{
comment("i ApproVe", "hack1", 11),
comment("ApproVed", "afterbuild1", 11),
},
pass: false,
},
{
name: "Early approval",
commits: pathsCommit([]string{"hack/file.sh", "hack/after-build/file.sh", "pkg/util/file.go"}), // Modified at time.Unix(7), 8, and 9
comments: []github.IssueComment{
comment("i ApproVe", "hack1", 6),
comment("ApproVed", "afterbuild1", 11),
comment("approved", "pkg1", 12),
},
pass: false,
},
}
owners := map[string]sets.String{
"": sets.NewString("root1", "root2"),
"hack": sets.NewString("hack1", "hack2"),
"hack/after-build": sets.NewString("afterbuild1", "afterbuild2"),
"pkg": sets.NewString("pkg1", "pkg2"),
}
assignees := map[string]sets.String{}

for testNum, test := range tests {
pr := ValidPR()
issue := NoOKToMergeIssue()
issueNum := testNum + 1
issue.Number = &issueNum
events := NewLGTMEvents()

client, server, mux := github_test.InitServer(t, issue, pr, events, test.commits, nil)

config := &github_util.Config{}
config.Org = "o"
config.Project = "r"
config.SetClient(client)

path := fmt.Sprintf("/repos/o/r/issues/%d/labels", issueNum)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method != "POST" {
t.Errorf("Unexpected method: %s", r.Method)
}
data, err := json.Marshal([]github.Label{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})
path = fmt.Sprintf("/repos/o/r/issues/%d/comments", issueNum)
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
if r.Method == "GET" {
data, err := json.Marshal(test.comments)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
return
}
if r.Method != "POST" {
t.Errorf("Unexpected method: %s", r.Method)
}

type comment struct {
Body string `json:"body"`
}
c := new(comment)
json.NewDecoder(r.Body).Decode(c)
msg := c.Body
if strings.HasPrefix(msg, "@k8s-bot test this") {
glog.Errorf("WTF")
}
data, err := json.Marshal(github.IssueComment{})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
w.Write(data)
})

o := OwnerApproval{}
f := &github_features.Features{
Repos: github_features.TestFakeRepo(assignees, owners),
}
o.features = f
obj := github_util.TestObject(config, issue, pr, test.commits, events)
o.Munge(obj)
if test.pass && !obj.HasLabel(ownerApproval) {
t.Errorf("%d:%q should have label but doesn't", testNum, test.name)
} else if !test.pass && obj.HasLabel(ownerApproval) {
t.Errorf("%d:%q should not have label but does", testNum, test.name)
}
server.Close()
}
}
2 changes: 1 addition & 1 deletion mungegithub/submit-queue/rc.yaml
Expand Up @@ -23,7 +23,7 @@ spec:
- command:
- /mungegithub
- --token-file=/etc/secret-volume/token
- --pr-mungers=blunderbuss,lgtm-after-commit,label-unapproved-picks,needs-rebase,ok-to-test,rebuild-request,path-label,size,stale-pending-ci,stale-green-ci,block-path,submit-queue
- --pr-mungers=blunderbuss,lgtm-after-commit,label-unapproved-picks,needs-rebase,ok-to-test,rebuild-request,path-label,size,stale-pending-ci,stale-green-ci,block-path,owner-approval,submit-queue
- --dry-run=true
image: gcr.io/google_containers/submit-queue:2015-10-26-2a7f8fd
imagePullPolicy: IfNotPresent
Expand Down

0 comments on commit 784ac2d

Please sign in to comment.