Skip to content

Commit

Permalink
Merge pull request #407 from jenkins-x-plugins/debugreusepr
Browse files Browse the repository at this point in the history
fix: Reuse pull request
  • Loading branch information
jenkins-x-bot authored Sep 26, 2022
2 parents 2be9265 + ee3f364 commit f39f260
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
33 changes: 19 additions & 14 deletions pkg/environments/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"os"
"sort"

"github.com/davecgh/go-spew/spew"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/pkg/errors"

"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -174,15 +175,22 @@ func (o *EnvironmentPullRequestOptions) FindExistingPullRequest(scmClient *scm.C
return nil, nil
}
ctx := context.Background()
labels := o.PullRequestFilter.Labels
filterLabels := o.PullRequestFilter.Labels
log.Logger().Debugf("Trying to find open PRs in %s with labels %v", repoFullName, filterLabels)
prs, _, err := scmClient.PullRequests.List(ctx, repoFullName, &scm.PullRequestListOptions{
Size: 100,
Open: true,
Labels: labels,
Labels: filterLabels,
})
if scmhelpers.IsScmNotFound(err) || len(prs) == 0 {
return nil, nil
}
if err != nil {
return nil, errors.Wrapf(err, "Error listing PRs")
}
if log.Logger().Logger.IsLevelEnabled(logrus.TraceLevel) {
log.Logger().Tracef("Found PRs: %s", spew.Sdump(prs))
}

// sort in descending order of PR numbers (assumes PRs numbers increment!)
sort.Slice(prs, func(i, j int) bool {
Expand All @@ -191,22 +199,19 @@ func (o *EnvironmentPullRequestOptions) FindExistingPullRequest(scmClient *scm.C
return pi.Number > pj.Number
})

// lets find the latest PR which is not closed
// let's find the latest PR which is not closed
Prs:
for i := range prs {
pr := prs[i]
if pr.Closed || pr.Merged || pr.Source != repoFullName {
if pr.Closed || pr.Merged || pr.Base.Repo.FullName != repoFullName {
continue
}
found := false
for _, label := range pr.Labels {
if stringhelpers.StringArrayIndex(labels, label.Name) >= 0 {
found = true
break
for _, label := range filterLabels {
if !scmhelpers.ContainsLabel(pr.Labels, label) {
continue Prs
}
}
if !found {
continue
}
log.Logger().Debugf("Found matching pr: %s", spew.Sdump(pr))
return pr, nil
}
return nil, nil
Expand Down
7 changes: 6 additions & 1 deletion pkg/environments/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ func (o *EnvironmentPullRequestOptions) CreatePullRequest(scmClient *scm.Client,
head := headPrefix + o.BranchName

if existingPR != nil {
return o.addLabelsToPullRequest(ctx, scmClient, repoFullName, existingPR)
prInput := &scm.PullRequestInput{
Title: commitTitle,
Body: commitBody,
}
existingPR, _, err = scmClient.PullRequests.Update(ctx, repoFullName, existingPR.Number, prInput)
return existingPR, errors.Wrapf(err, "failed to update PullRequest %+v with %+v", existingPR, prInput)
}
pri := &scm.PullRequestInput{
Title: commitTitle,
Expand Down
2 changes: 1 addition & 1 deletion pkg/environments/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ type EnvironmentPullRequestOptions struct {
BatchMode bool
UseGitHubOAuth bool
Fork bool
ReusePullRequest bool
SparseCheckoutPatterns []string
}

// A PullRequestFilter defines a filter for finding pull requests
type PullRequestFilter struct {
Labels []string
Number *int
}
11 changes: 11 additions & 0 deletions pkg/promote/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package promote
import (
"fmt"

"github.com/jenkins-x-plugins/jx-promote/pkg/environments"
"github.com/jenkins-x/jx-helpers/v3/pkg/requirements"

jxcore "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
Expand Down Expand Up @@ -40,6 +41,16 @@ func (o *Options) PromoteViaPullRequest(envs []*jxcore.EnvironmentConfig, releas
Description: releaseInfo.FullAppName,
})

if o.ReusePullRequest && o.PullRequestFilter == nil {
var filterLabels []string
for i := range labels {
filterLabels = append(filterLabels, labels[i].Name)
}
o.PullRequestFilter = &environments.PullRequestFilter{Labels: filterLabels}
// Clearing so that it can be set for the correct environment on next call
defer func() { o.PullRequestFilter = nil }()
}

comment := fmt.Sprintf("chore: promote %s to version %s", app, versionName) + "\n\nthis commit will trigger a pipeline to [generate the actual kubernetes resources to perform the promotion](https://jenkins-x.io/docs/v3/about/how-it-works/#promotion) which will create a second commit on this Pull Request before it can merge"
details := scm.PullRequest{
Source: source,
Expand Down
6 changes: 1 addition & 5 deletions pkg/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,7 @@ func (o *Options) Promote(envs []*jxcore.EnvironmentConfig, warnIfAuto, noPoll b
if !envIsPermanent(env) {
return nil, errors.Errorf("cannot promote to Environment which is not a permanent Environment")
}
if env.ReusePullRequest {
o.PullRequestFilter = &environments.PullRequestFilter{Labels: []string{"dependency/" + fullAppName}}
} else {
o.PullRequestFilter = nil
}
o.ReusePullRequest = env.ReusePullRequest

sourceURL := requirements.EnvironmentGitURL(o.DevEnvContext.Requirements, env.Key)
if sourceURL == "" && !env.RemoteCluster && o.DevEnvContext.DevEnv != nil {
Expand Down

0 comments on commit f39f260

Please sign in to comment.