Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow branches to be excluded in tide queries #7456

Merged
Merged
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
5 changes: 5 additions & 0 deletions prow/config/tide.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (t *Tide) MergeMethod(org, repo string) github.PullRequestMergeType {
type TideQuery struct {
Repos []string `json:"repos,omitempty"`

ExcludedBranches []string `json:"excludedBranches,omitempty"`

Labels []string `json:"labels,omitempty"`
MissingLabels []string `json:"missingLabels,omitempty"`

Expand All @@ -96,6 +98,9 @@ func (tq *TideQuery) Query() string {
for _, r := range tq.Repos {
toks = append(toks, fmt.Sprintf("repo:\"%s\"", r))
}
for _, b := range tq.ExcludedBranches {
toks = append(toks, fmt.Sprintf("-base:\"%s\"", b))
}
for _, l := range tq.Labels {
toks = append(toks, fmt.Sprintf("label:\"%s\"", l))
}
Expand Down
14 changes: 13 additions & 1 deletion prow/tide/tide.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ func byRepoAndNumber(prs []PullRequest) map[string]PullRequest {
// requirementDiff calculates the diff between a PR and a TideQuery.
// This diff is defined with a string that describes some subset of the
// differences and an integer counting the total number of differences.
// The diff count should always reflect the total number of differences between
// the current state of the PR and the query, but the message returned need not
// attempt to convey all of that information if some differences are more severe.
// For instance, we need to convey that a PR is open against a forbidden branch
// more than we need to detail which status contexts are failed against the PR.
// Note: an empty diff can be returned if the reason that the PR does not match
// the TideQuery is unknown. This can happen happen if this function's logic
// does not match GitHub's and does not indicate that the PR matches the query.
Expand All @@ -206,6 +211,13 @@ func requirementDiff(pr *PullRequest, q *config.TideQuery) (string, int) {
return labels[:i]
}

for _, excludedBranch := range q.ExcludedBranches {
if string(pr.BaseRef.Name) == excludedBranch {
desc = fmt.Sprintf(" Merging to branch %s is forbidden.", pr.BaseRef.Name)
diff = 1
}
}

var missingLabels []string
for _, l1 := range q.Labels {
var found bool
Expand All @@ -220,7 +232,7 @@ func requirementDiff(pr *PullRequest, q *config.TideQuery) (string, int) {
}
}
diff += len(missingLabels)
if len(missingLabels) > 0 {
if desc == "" && len(missingLabels) > 0 {
sort.Strings(missingLabels)
trunced := truncate(missingLabels)
if len(trunced) == 1 {
Expand Down
21 changes: 19 additions & 2 deletions prow/tide/tide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ func TestExpectedStatus(t *testing.T) {
testcases := []struct {
name string

baseref string
labels []string
contexts []Context
inPool bool
Expand Down Expand Up @@ -457,6 +458,15 @@ func TestExpectedStatus(t *testing.T) {
state: github.StatusPending,
desc: fmt.Sprintf(statusNotInPool, " Needs need-1 label."),
},
{
name: "against excluded branch",
baseref: "bad",
labels: neededLabels,
inPool: false,

state: github.StatusPending,
desc: fmt.Sprintf(statusNotInPool, " Merging to branch bad is forbidden."),
},
{
name: "only failed tide context",
labels: neededLabels,
Expand Down Expand Up @@ -508,8 +518,9 @@ func TestExpectedStatus(t *testing.T) {
queriesByRepo := map[string]config.TideQueries{
"": {
config.TideQuery{
Labels: neededLabels,
MissingLabels: forbiddenLabels,
ExcludedBranches: []string{"bad"},
Labels: neededLabels,
MissingLabels: forbiddenLabels,
},
config.TideQuery{
Labels: []string{"1", "2", "3", "4", "5", "6", "7"}, // lots of requirements
Expand All @@ -520,6 +531,12 @@ func TestExpectedStatus(t *testing.T) {
for _, tc := range testcases {
t.Logf("Test Case: %q\n", tc.name)
var pr PullRequest
pr.BaseRef = struct {
Name githubql.String
Prefix githubql.String
}{
Name: githubql.String(tc.baseref),
}
for _, label := range tc.labels {
pr.Labels.Nodes = append(
pr.Labels.Nodes,
Expand Down