From ee3f364d193ab47ed3a6bd4867d97f624cdd3b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Svantesson?= Date: Thu, 22 Sep 2022 13:13:39 +0200 Subject: [PATCH] fix: Reuse pull request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixing check that pr is not made from fork Update title and body of reused PR Added debug and trace output Set reuse pr filter from labels to set on new PR Remove unused filter field Number Signed-off-by: MÃ¥rten Svantesson --- pkg/environments/gitops.go | 33 +++++++++++++++++++-------------- pkg/environments/pr.go | 7 ++++++- pkg/environments/types.go | 2 +- pkg/promote/pr.go | 11 +++++++++++ pkg/promote/promote.go | 6 +----- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/pkg/environments/gitops.go b/pkg/environments/gitops.go index 8689ab7..01568cf 100644 --- a/pkg/environments/gitops.go +++ b/pkg/environments/gitops.go @@ -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 ( @@ -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 { @@ -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 diff --git a/pkg/environments/pr.go b/pkg/environments/pr.go index 9ca4c8c..b822a92 100644 --- a/pkg/environments/pr.go +++ b/pkg/environments/pr.go @@ -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, diff --git a/pkg/environments/types.go b/pkg/environments/types.go index bd11bf2..48e0da6 100644 --- a/pkg/environments/types.go +++ b/pkg/environments/types.go @@ -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 } diff --git a/pkg/promote/pr.go b/pkg/promote/pr.go index 5f2c79b..50729a5 100644 --- a/pkg/promote/pr.go +++ b/pkg/promote/pr.go @@ -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" @@ -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, diff --git a/pkg/promote/promote.go b/pkg/promote/promote.go index 8f821ab..fbca649 100644 --- a/pkg/promote/promote.go +++ b/pkg/promote/promote.go @@ -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 {