-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port docs-no-retest from munge to prow
- Loading branch information
Showing
6 changed files
with
442 additions
and
96 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 was deleted.
Oops, something went wrong.
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,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"], | ||
) |
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,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 | ||
} |
Oops, something went wrong.