This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First stab at an OWNERS implementation
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
Showing
5 changed files
with
335 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters