Skip to content

Commit dc03760

Browse files
committed
feat: add label when pr has conflicts
1 parent a75701c commit dc03760

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

pkg/keeper/keeper.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"context"
2525
"encoding/json"
2626
"fmt"
27+
"github.com/jenkins-x/lighthouse/pkg/labels"
2728
"net/http"
2829
"os"
2930
"sort"
@@ -79,6 +80,7 @@ type scmProviderClient interface {
7980
GetFile(string, string, string, string) ([]byte, error)
8081
ListFiles(string, string, string, string) ([]*scm.FileEntry, error)
8182
GetIssueLabels(string, string, int, bool) ([]*scm.Label, error)
83+
AddLabel(string, string, int, string, bool) error
8284
}
8385

8486
type contextChecker interface {
@@ -596,16 +598,20 @@ func (c *DefaultController) initSubpoolData(sp *subpool) error {
596598
// should be deleted.
597599
func filterSubpool(spc scmProviderClient, sp *subpool) *subpool {
598600
var toKeep []PullRequest
601+
var conflicting []int
599602
for _, pr := range sp.prs {
600603
p := pr
601604
if !filterPR(spc, sp, &p) {
602605
toKeep = append(toKeep, pr)
606+
} else if pr.Mergeable == githubql.MergeableStateConflicting && !hasLabel(&pr, labels.HasConflicts) {
607+
conflicting = append(conflicting, int(pr.Number))
603608
}
604609
}
605610
if len(toKeep) == 0 {
606611
return nil
607612
}
608613
sp.prs = toKeep
614+
sp.conflictingPRs = conflicting
609615
return sp
610616
}
611617

@@ -834,7 +840,7 @@ func accumulate(presubmits map[int][]job.Presubmit, prs []PullRequest, pjs []v1a
834840
missingTests = map[int][]job.Presubmit{}
835841
for _, pr := range prs {
836842
// Accumulate the best result for each job (Passing > Pending > Failing/Unknown)
837-
// We can ignore the baseSHA here because the subPool only contains PipelineActivitys with the correct baseSHA
843+
// We can ignore the baseSHA here because the subPool only contains LighthouseJobs with the correct baseSHA
838844
psStates := make(map[string]simpleState)
839845
for _, pj := range pjs {
840846
if pj.Spec.Type != job.PresubmitJob {
@@ -950,6 +956,11 @@ func (c *DefaultController) pickBatch(sp subpool, cc contextChecker) ([]PullRequ
950956
}
951957
}
952958
}
959+
for _, prNumber := range sp.conflictingPRs {
960+
if err := c.spc.AddLabel(sp.org, sp.repo, prNumber, labels.HasConflicts, true); err != nil {
961+
sp.log.Warnf("error while adding Label %q: %v", labels.HasConflicts, err)
962+
}
963+
}
953964
return res, nil
954965
}
955966

@@ -1479,13 +1490,13 @@ func prMeta(prs ...PullRequest) []v1alpha1.Pull {
14791490

14801491
func sortPools(pools []Pool) {
14811492
sort.Slice(pools, func(i, j int) bool {
1482-
if string(pools[i].Org) != string(pools[j].Org) {
1483-
return string(pools[i].Org) < string(pools[j].Org)
1493+
if pools[i].Org != pools[j].Org {
1494+
return pools[i].Org < pools[j].Org
14841495
}
1485-
if string(pools[i].Repo) != string(pools[j].Repo) {
1486-
return string(pools[i].Repo) < string(pools[j].Repo)
1496+
if pools[i].Repo != pools[j].Repo {
1497+
return pools[i].Repo < pools[j].Repo
14871498
}
1488-
return string(pools[i].Branch) < string(pools[j].Branch)
1499+
return pools[i].Branch < pools[j].Branch
14891500
})
14901501

14911502
sortPRs := func(prs []PullRequest) {
@@ -1510,8 +1521,9 @@ type subpool struct {
15101521

15111522
// ljs contains all LighthouseJobs of type Presubmit or Batch
15121523
// that have the same baseSHA as the subpool
1513-
ljs []v1alpha1.LighthouseJob
1514-
prs []PullRequest
1524+
ljs []v1alpha1.LighthouseJob
1525+
prs []PullRequest
1526+
conflictingPRs []int
15151527

15161528
cc contextChecker
15171529
// presubmit contains all required presubmits for each PR
@@ -1900,13 +1912,13 @@ func scmPRToGraphQLPR(scmPR *scm.PullRequest, scmRepo *scm.Repository) *PullRequ
19001912
mergeable = githubql.MergeableStateConflicting
19011913
}
19021914

1903-
labels := struct {
1915+
qlLabels := struct {
19041916
Nodes []struct {
19051917
Name githubql.String
19061918
}
19071919
}{}
19081920
for _, l := range scmPR.Labels {
1909-
labels.Nodes = append(labels.Nodes, struct{ Name githubql.String }{Name: githubql.String(l.Name)})
1921+
qlLabels.Nodes = append(qlLabels.Nodes, struct{ Name githubql.String }{Name: githubql.String(l.Name)})
19101922
}
19111923

19121924
return &PullRequest{
@@ -1917,7 +1929,7 @@ func scmPRToGraphQLPR(scmPR *scm.PullRequest, scmRepo *scm.Repository) *PullRequ
19171929
HeadRefOID: githubql.String(scmPR.Head.Sha),
19181930
Mergeable: mergeable,
19191931
Repository: scmRepoToGraphQLRepo(scmRepo),
1920-
Labels: labels,
1932+
Labels: qlLabels,
19211933
Body: githubql.String(scmPR.Body),
19221934
Title: githubql.String(scmPR.Title),
19231935
UpdatedAt: githubql.DateTime{Time: scmPR.Updated},

pkg/keeper/status.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,7 @@ func requirementDiff(pr *PullRequest, q *keeper.Query, cc contextChecker) (strin
161161
// Weight incorrect labels and statues with low (normal) diff values.
162162
var missingLabels []string
163163
for _, l1 := range q.Labels {
164-
var found bool
165-
for _, l2 := range pr.Labels.Nodes {
166-
if strings.EqualFold(string(l2.Name), string(l1)) {
167-
found = true
168-
break
169-
}
170-
}
171-
if !found {
164+
if !hasLabel(pr, l1) {
172165
missingLabels = append(missingLabels, l1)
173166
}
174167
}
@@ -185,11 +178,8 @@ func requirementDiff(pr *PullRequest, q *keeper.Query, cc contextChecker) (strin
185178

186179
var presentLabels []string
187180
for _, l1 := range q.MissingLabels {
188-
for _, l2 := range pr.Labels.Nodes {
189-
if string(l2.Name) == l1 {
190-
presentLabels = append(presentLabels, l1)
191-
break
192-
}
181+
if hasLabel(pr, l1) {
182+
presentLabels = append(presentLabels, l1)
193183
}
194184
}
195185
diff += len(presentLabels)
@@ -229,6 +219,15 @@ func requirementDiff(pr *PullRequest, q *keeper.Query, cc contextChecker) (strin
229219
return desc, diff
230220
}
231221

222+
func hasLabel(pr *PullRequest, l1 string) bool {
223+
for _, l2 := range pr.Labels.Nodes {
224+
if strings.EqualFold(string(l2.Name), l1) {
225+
return true
226+
}
227+
}
228+
return false
229+
}
230+
232231
// Returns expected status state and description.
233232
// If a PR is not mergeable, we have to select a KeeperQuery to compare it against
234233
// in order to generate a diff for the status description. We choose the query

pkg/labels/labels.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
CpApproved = "cherry-pick-approved"
2727
CpUnapproved = "do-not-merge/cherry-pick-not-approved"
2828
GoodFirstIssue = "good first issue"
29+
HasConflicts = "has conflicts"
2930
Help = "help wanted"
3031
Hold = "do-not-merge/hold"
3132
InvalidOwners = "do-not-merge/invalid-owners-file"

0 commit comments

Comments
 (0)