Skip to content

Commit

Permalink
Port docs-no-retest from munge to prow
Browse files Browse the repository at this point in the history
  • Loading branch information
paradigm committed Oct 5, 2017
1 parent 8f529fc commit 4af474f
Show file tree
Hide file tree
Showing 6 changed files with 442 additions and 96 deletions.
1 change: 0 additions & 1 deletion mungegithub/mungers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ go_library(
"comment-deleter.go",
"comment-deleter-jenkins.go",
"doc.go",
"docs-no-retest.go",
"inactive-review-handler.go",
"issue-categorizer.go",
"lgtm_after_commit.go",
Expand Down
95 changes: 0 additions & 95 deletions mungegithub/mungers/docs-no-retest.go

This file was deleted.

1 change: 1 addition & 0 deletions prow/plugins/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ filegroup(
"//prow/plugins/assign:all-srcs",
"//prow/plugins/cla:all-srcs",
"//prow/plugins/close:all-srcs",
"//prow/plugins/docs-no-retest:all-srcs",
"//prow/plugins/golint:all-srcs",
"//prow/plugins/heart:all-srcs",
"//prow/plugins/hold:all-srcs",
Expand Down
32 changes: 32 additions & 0 deletions prow/plugins/docs-no-retest/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["docs-no-retest.go"],
visibility = ["//visibility:public"],
deps = [
"//prow/github:go_default_library",
"//prow/plugins:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["docs-no-retest_test.go"],
library = ":go_default_library",
deps = ["//prow/github:go_default_library"],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
106 changes: 106 additions & 0 deletions prow/plugins/docs-no-retest/docs-no-retest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
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 docsnoretest contains a Prow plugin which manages a label indicating
// whether a given pull requests only changes documentation. In such cases it
// would not need to be retested.
package docsnoretest

import (
"fmt"
"path"
"regexp"

"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/plugins"
)

const (
pluginName = "docs-no-retest"
labelSkipRetest = "retest-not-required-docs-only"
)

var (
docFilesRegex = regexp.MustCompile("^.*\\.(md|png|svg|dia)$")
ownersFilesRegex = regexp.MustCompile("^OWNERS$")
)

func init() {
plugins.RegisterPullRequestHandler(pluginName, handlePullRequest)
}

func handlePullRequest(pc plugins.PluginClient, pe github.PullRequestEvent) error {
return handlePR(pc.GitHubClient, pe)
}

// Strict subset of *github.Client methods.
type githubClient interface {
AddLabel(owner, repo string, number int, label string) error
RemoveLabel(owner, repo string, number int, label string) error
GetIssueLabels(org, repo string, number int) ([]github.Label, error)
GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error)
}

func handlePR(gc githubClient, pe github.PullRequestEvent) error {
var (
owner = pe.PullRequest.Base.Repo.Owner.Login
repo = pe.PullRequest.Base.Repo.Name
num = pe.PullRequest.Number
)

changes, err := gc.GetPullRequestChanges(owner, repo, num)
if err != nil {
return fmt.Errorf("cannot get pull request changes for docs_no_retest plugin: %v", err)
}

docsOnly := true
for _, change := range changes {
_, basename := path.Split(change.Filename)
if docFilesRegex.MatchString(basename) {
continue
}
if ownersFilesRegex.MatchString(basename) {
continue
}
docsOnly = false
break
}

labels, err := gc.GetIssueLabels(owner, repo, num)
if err != nil {
return fmt.Errorf("cannot get labels for docs_no_retest plugin: %v", err)
}

hasTargetLabel := false
for _, label := range labels {
if label.Name == labelSkipRetest {
hasTargetLabel = true
break
}
}

if docsOnly && !hasTargetLabel {
if err := gc.AddLabel(owner, repo, num, labelSkipRetest); err != nil {
return fmt.Errorf("error adding label to %s/%s PR #%d: %v", owner, repo, num, err)
}
} else if !docsOnly && hasTargetLabel {
if err := gc.RemoveLabel(owner, repo, num, labelSkipRetest); err != nil {
return fmt.Errorf("error removing label from %s/%s PR #%d: %v", owner, repo, num, err)
}
}

return nil
}
Loading

0 comments on commit 4af474f

Please sign in to comment.