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

Path Based Labeler: Use a config file for the mapping #87

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,19 @@ func GetLabelsWithPrefix(labels []github.Label, prefix string) []string {
return ret
}

func GetLabelSet(labels []github.Label) util.StringSet {
set := util.NewStringSet()
for _, label := range labels {
if label.Name != nil {
set.Insert(*label.Name)
}
}
return set
}

func (config *GithubConfig) AddLabels(prNum int, labels []string) error {
if config.DryRun {
glog.Infof("Would have added labels %v to PR %d --dry-run is set", labels, prNum)
glog.Infof("Would have added labels %v to PR %d but --dry-run is set", labels, prNum)
return nil
}
if _, _, err := config.client.Issues.AddLabelsToIssue(config.Org, config.Project, prNum, labels); err != nil {
Expand All @@ -175,7 +185,7 @@ func (config *GithubConfig) AddLabels(prNum int, labels []string) error {

func (config *GithubConfig) RemoveLabel(prNum int, label string) error {
if config.DryRun {
glog.Infof("Would have removed label %q to PR %d --dry-run is set", label, prNum)
glog.Infof("Would have removed label %q to PR %d but --dry-run is set", label, prNum)
return nil
}
if _, err := config.client.Issues.RemoveLabelForIssue(config.Org, config.Project, prNum, label); err != nil {
Expand Down
1 change: 1 addition & 0 deletions mungegithub/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ RUN apt-get update
RUN apt-get install -y -qq ca-certificates
ADD mungegithub /mungegithub
ADD blunderbuss.yml /blunderbuss.yml
ADD path-label.txt /path-label.txt
ADD run.sh /run.sh
RUN chmod a+x /run.sh
7 changes: 7 additions & 0 deletions mungegithub/path-label.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is used by the path-label munger and is of the form:
# PATHPREFIX LABEL
docs/proposals kind/design
pkg/api/register.go kind/new-api
pkg/expapi/register.go kind/new-api
pkg/api/types.go kind/api-change
pkg/expapi/types.go kind/api-change
109 changes: 109 additions & 0 deletions mungegithub/pulls/label_based_on_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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 pulls

import (
"bufio"
"fmt"
"os"
"strings"

github_util "k8s.io/contrib/github"
"k8s.io/contrib/mungegithub/config"
"k8s.io/kubernetes/pkg/util"

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

var (
_ = fmt.Print
)

type PathLabelMunger struct {
labelMap *map[string]string
pathLabelFile string
}

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

func (p *PathLabelMunger) Name() string { return "path-label" }

func (p *PathLabelMunger) AddFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&p.pathLabelFile, "path-label-config", "path-label.txt", "file containing the pathname to label mappings")
}

func (p *PathLabelMunger) loadPathMap() error {
out := map[string]string{}
p.labelMap = &out
file := p.pathLabelFile
if len(file) == 0 {
glog.Infof("No --path-label-config= supplied, applying no labels")
return nil
}
fp, err := os.Open(file)
if err != nil {
return err
}
defer fp.Close()
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) != 2 {
glog.Errorf("Invalid line in path based label munger config %s: %q", file, line)
continue
}
file := fields[0]
label := fields[1]
out[file] = label
}
return scanner.Err()
}

func (p *PathLabelMunger) MungePullRequest(config *config.MungeConfig, pr *github.PullRequest, issue *github.Issue, commits []github.RepositoryCommit, events []github.IssueEvent) {
glog.V(8).Infof("Checking out PR %d\n", *pr.Number)

if p.labelMap == nil {
if err := p.loadPathMap(); err != nil {
return
}
}
currentLabels := github_util.GetLabelSet(issue.Labels)

mungerLabels := util.StringSet{}
for _, c := range commits {
for _, f := range c.Files {
for prefix, label := range *p.labelMap {
if strings.HasPrefix(*f.Filename, prefix) {
mungerLabels.Insert(label)
}
}
}
}

needsLabels := mungerLabels.Difference(currentLabels)
if len(needsLabels) != 0 {
config.AddLabels(*pr.Number, needsLabels.List())
}
}
2 changes: 1 addition & 1 deletion mungegithub/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

while true; do
/mungegithub \
--pr-mungers=blunderbuss,needs-rebase,ok-to-test,ping-ci,size \
--pr-mungers=blunderbuss,needs-rebase,ok-to-test,ping-ci,size,path-label \
--blunderbuss-config=/blunderbuss.yml \
--token-file=/etc/secret-volume/token
sleep 600
Expand Down