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

WIP: [mungegithub] OWNERS implementation #474

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions mungegithub/Makefile
Expand Up @@ -21,8 +21,11 @@ READONLY ?= true
mungegithub:
CGO_ENABLED=0 GOOS=linux godep go build -a -installsuffix cgo -ldflags '-w' -o mungegithub

test: mungegithub
CGO_ENABLED=0 GOOS=linux godep go test ./...

# build the container with the binary
container: mungegithub
container: test
docker build -t $(CONTAINER) -f Dockerfile-$(APP) .

# push the container
Expand Down Expand Up @@ -98,4 +101,4 @@ help:
@echo " clean: deletes the binary and local files (does not delete old containers)"


.PHONY: all mungegithub container push dryrun cleandryrun local_dryrun rc secret clean help
.PHONY: all mungegithub test container push dryrun cleandryrun local_dryrun rc secret clean help
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
}
5 changes: 3 additions & 2 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 Expand Up @@ -1195,8 +1196,8 @@ func (obj *MungeObject) MergePR(who string) error {
return nil
}

// ListComments returns all comments for the issue/PR in question
func (obj *MungeObject) ListComments(number int) ([]github.IssueComment, error) {
// ListComments returns all comments for the object
func (obj *MungeObject) ListComments() ([]github.IssueComment, error) {
config := obj.config
issueNum := *obj.Issue.Number
allComments := []github.IssueComment{}
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)
}
}