Skip to content

Commit

Permalink
feat: add changelog to PR body
Browse files Browse the repository at this point in the history
simplify code by using []string instead of []scm.Label within detail
add app name first in changelog
  • Loading branch information
msvticket committed Nov 21, 2022
1 parent 3d7f768 commit c5bf4c1
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 113 deletions.
63 changes: 32 additions & 31 deletions docs/cmd/promote.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,37 @@ For more documentation see: https://jenkins-x.io/docs/getting-started/promotion/
### Options

```
--alias string The optional alias used in the 'requirements.yaml' file
--all Promote to all automatic and manual environments in order using a draft PR for manual promotion environments
--all-auto Promote to all automatic environments in order
-a, --app string The Application to promote
--app-git-url string The Git URL of the application being promoted. Only required if using file or kpt rules
--auto-merge If enabled add the 'updatebot' label to tell lighthouse to eagerly merge. Usually the Pull Request pipeline will add this label during the Pull Request pipeline after any extra generation/commits have been done and the PR is valid
-b, --batch-mode Enables batch mode which avoids prompting for user input
--build string The Build number which is used to update the PipelineActivity. If not specified its defaulted from the '$BUILD_NUMBER' environment variable
--default-app-namespace string The default namespace for promoting to remote clusters for the first
-e, --env string The Environment to promote to
-f, --filter string The search filter to find charts to promote
--git-token string Git token used to clone the development environment. If not specified its loaded from the git credentials file
--git-user string Git username used to clone the development environment. If not specified its loaded from the git credentials file
-r, --helm-repo-name string The name of the helm repository that contains the app (default "releases")
-u, --helm-repo-url string The Helm Repository URL to use for the App
-h, --help help for promote
--ignore-local-file Ignores the local file system when deducing the Git repository
-n, --namespace string The Namespace to promote to
--no-helm-update Allows the 'helm repo update' command if you are sure your local helm cache is up to date with the version you wish to promote
--no-merge Disables automatic merge of promote Pull Requests
--no-poll Disables polling for Pull Request or Pipeline status
--no-pr-group Disables grouping Auto promotions to different Environments in the same git repository within a single Pull Request which causes them to use separate Pull Requests
--no-wait Disables waiting for completing promotion after the Pull request is merged
--pipeline string The Pipeline string in the form 'folderName/repoName/branch' which is used to update the PipelineActivity. If not specified its defaulted from the '$BUILD_NUMBER' environment variable
--promotion-environments stringArray The environments considered for promotion
--pull-request-poll-time string Poll time when waiting for a Pull Request to merge (default "20s")
--release string The name of the helm release
-t, --timeout string The timeout to wait for the promotion to succeed in the underlying Environment. The command fails if the timeout is exceeded or the promotion does not complete (default "1h")
-v, --version string The Version to promote. If no version is specified it defaults to $VERSION which is usually populated in a pipeline. If no value can be found you will be prompted to pick the version
--version-file string the file to load the version from if not specified directly or via a $VERSION environment variable. Defaults to VERSION in the current dir
-c, --add-changelog string a file to take a changelog from to add to the pullr equest body. Typically a file generated by jx changelog.
--alias string The optional alias used in the 'requirements.yaml' file
--all Promote to all automatic and manual environments in order using a draft PR for manual promotion environments. Implies batch mode.
--all-auto Promote to all automatic environments in order
-a, --app string The Application to promote
--app-git-url string The Git URL of the application being promoted. Only required if using file or kpt rules
--auto-merge If enabled add the 'updatebot' label to tell lighthouse to eagerly merge. Usually the Pull Request pipeline will add this label during the Pull Request pipeline after any extra generation/commits have been done and the PR is valid
-b, --batch-mode Enables batch mode which avoids prompting for user input
--build string The Build number which is used to update the PipelineActivity. If not specified its defaulted from the '$BUILD_NUMBER' environment variable
--changelog-separator string the separator to use between commit message and changelog in the pull request body. Default to ----- or if set the CHANGELOG_SEPARATOR environment variable
-e, --env stringArray The environment(s) to promote to
-f, --filter string The search filter to find charts to promote
--git-token string Git token used to clone the development environment. If not specified its loaded from the git credentials file
--git-user string Git username used to clone the development environment. If not specified its loaded from the git credentials file
-r, --helm-repo-name string The name of the helm repository that contains the app (default "releases")
-u, --helm-repo-url string The Helm Repository URL to use for the App
-h, --help help for promote
--ignore-local-file Ignores the local file system when deducing the Git repository
--interactive Enables interactive mode
-n, --namespace string The Namespace to promote to
--no-helm-update Allows the 'helm repo update' command if you are sure your local helm cache is up to date with the version you wish to promote
--no-merge Disables automatic merge of promote Pull Requests
--no-poll Disables polling for Pull Request or Pipeline status
--no-pr-group Disables grouping Auto promotions to different Environments in the same git repository within a single Pull Request which causes them to use separate Pull Requests
--no-wait Disables waiting for completing promotion after the Pull request is merged
--pipeline string The Pipeline string in the form 'folderName/repoName/branch' which is used to update the PipelineActivity. If not specified its defaulted from the '$BUILD_NUMBER' environment variable
--pull-request-poll-time string Poll time when waiting for a Pull Request to merge (default "20s")
--release string The name of the helm release
-t, --timeout string The timeout to wait for the promotion to succeed in the underlying Environment. The command fails if the timeout is exceeded or the promotion does not complete (default "1h")
-v, --version string The Version to promote. If no version is specified it defaults to $VERSION which is usually populated in a pipeline. If no value can be found you will be prompted to pick the version
--version-file string the file to load the version from if not specified directly or via a $VERSION environment variable. Defaults to VERSION in the current dir
```

###### Auto generated by spf13/cobra on 13-Apr-2021
###### Auto generated by spf13/cobra on 21-Nov-2022
14 changes: 1 addition & 13 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,6 @@ For example if the ‘config-root” directory contains a Config Sync gi
<code>config-root/namespaces/myapps</code> folder. If so set the path to <code>config-root/namespaces/myapps</code></p>
</td>
</tr>
<tr>
<td>
<code>namespace</code></br>
<em>
string
</em>
</td>
<td>
<p>Namespace specifies the namespace to deploy applications if using kpt. If specified this value will be used instead
of the Environment.Spec.Namespace in the Environment CRD</p>
</td>
</tr>
</tbody>
</table>
<h3 id="promote.jenkins-x.io/v1alpha1.LineMatcher">LineMatcher
Expand Down Expand Up @@ -469,5 +457,5 @@ KptRule
<hr/>
<p><em>
Generated with <code>gen-crd-api-reference-docs</code>
on git commit <code>88df823</code>.
on git commit <code>bd71f32</code>.
</em></p>
22 changes: 13 additions & 9 deletions docs/man/man1/promote.1
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ For more documentation see:


.SH OPTIONS
.PP
\fB\-c\fP, \fB\-\-add\-changelog\fP=""
a file to take a changelog from to add to the pullr equest body. Typically a file generated by jx changelog.

.PP
\fB\-\-alias\fP=""
The optional alias used in the 'requirements.yaml' file

.PP
\fB\-\-all\fP[=false]
Promote to all automatic and manual environments in order using a draft PR for manual promotion environments
Promote to all automatic and manual environments in order using a draft PR for manual promotion environments. Implies batch mode.

.PP
\fB\-\-all\-auto\fP[=false]
Expand All @@ -56,12 +60,12 @@ For more documentation see:
The Build number which is used to update the PipelineActivity. If not specified its defaulted from the '$BUILD\_NUMBER' environment variable

.PP
\fB\-\-default\-app\-namespace\fP=""
The default namespace for promoting to remote clusters for the first
\fB\-\-changelog\-separator\fP=""
the separator to use between commit message and changelog in the pull request body. Default to \-\-\-\-\- or if set the CHANGELOG\_SEPARATOR environment variable

.PP
\fB\-e\fP, \fB\-\-env\fP=""
The Environment to promote to
\fB\-e\fP, \fB\-\-env\fP=[]
The environment(s) to promote to

.PP
\fB\-f\fP, \fB\-\-filter\fP=""
Expand Down Expand Up @@ -91,6 +95,10 @@ For more documentation see:
\fB\-\-ignore\-local\-file\fP[=false]
Ignores the local file system when deducing the Git repository

.PP
\fB\-\-interactive\fP[=false]
Enables interactive mode

.PP
\fB\-n\fP, \fB\-\-namespace\fP=""
The Namespace to promote to
Expand Down Expand Up @@ -119,10 +127,6 @@ For more documentation see:
\fB\-\-pipeline\fP=""
The Pipeline string in the form 'folderName/repoName/branch' which is used to update the PipelineActivity. If not specified its defaulted from the '$BUILD\_NUMBER' environment variable

.PP
\fB\-\-promotion\-environments\fP=[]
The environments considered for promotion

.PP
\fB\-\-pull\-request\-poll\-time\fP="20s"
Poll time when waiting for a Pull Request to merge
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/cpuguy83/go-md2man v1.0.10
github.com/davecgh/go-spew v1.1.1
github.com/jenkins-x-plugins/jx-gitops v0.7.6
github.com/jenkins-x/go-scm v1.11.29
github.com/jenkins-x/go-scm v1.12.0
github.com/jenkins-x/jx-api/v4 v4.6.2
github.com/jenkins-x/jx-helpers/v3 v3.4.2
github.com/jenkins-x/jx-logging/v3 v3.0.10
Expand Down Expand Up @@ -52,7 +52,7 @@ require (
github.com/alecthomas/participle v0.4.2-0.20191220090139-9fbceec1d131 // indirect
github.com/aws/aws-sdk-go v1.40.28 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bluekeyes/go-gitdiff v0.6.1 // indirect
github.com/bluekeyes/go-gitdiff v0.7.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
Expand Down Expand Up @@ -149,7 +149,6 @@ require (
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.19.0 // indirect
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqO
github.com/blang/semver v3.1.0+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/bluekeyes/go-gitdiff v0.6.1 h1:VPpQZanEVwFid0IoAIyiJLD+SMWM/AXZXYFc+3E9CS4=
github.com/bluekeyes/go-gitdiff v0.6.1/go.mod h1:QpfYYO1E0fTVHVZAZKiRjtSGY9823iCdvGXBcEzHGbM=
github.com/bluekeyes/go-gitdiff v0.7.0 h1:w4SrRFcufU0/tEpWx3VurDBAnWfpxsmwS7yWr14meQk=
github.com/bluekeyes/go-gitdiff v0.7.0/go.mod h1:QpfYYO1E0fTVHVZAZKiRjtSGY9823iCdvGXBcEzHGbM=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
Expand Down Expand Up @@ -818,8 +818,8 @@ github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uc
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag=
github.com/jenkins-x-plugins/jx-gitops v0.7.6 h1:53ZapHjOS5QlBB2+7+wvwcHCg9EGyTZvc3/OErtKFzI=
github.com/jenkins-x-plugins/jx-gitops v0.7.6/go.mod h1:IQurqapdxQ3oaLBqo25gH9nahOWPC9krPwQrk6BqiOY=
github.com/jenkins-x/go-scm v1.11.29 h1:SXpqD+ZTs9yMH9FHfX3ysXOq6RaKO9vVdwaqDoqMJNA=
github.com/jenkins-x/go-scm v1.11.29/go.mod h1:s5AZIMeNzvDHRnOG1nhD/OLZZ/LOwHTGLXehCsjwGuo=
github.com/jenkins-x/go-scm v1.12.0 h1:YngmCaqyQp+pKxw6BECeb0Jz7p6DLRdHzVIKGrD3QWg=
github.com/jenkins-x/go-scm v1.12.0/go.mod h1:JyL6p5dNNeJZt41yfysDTBe0Q51cLa5MVK0du7JnXt0=
github.com/jenkins-x/jx-api/v4 v4.6.2 h1:RuS5pyPUhVe19CpUStA+1Bqmv1B3kLFyng8Wd6KYOxs=
github.com/jenkins-x/jx-api/v4 v4.6.2/go.mod h1:OhbFyIS+3OqAQwyCEQMQoTILCREyv0uQl2Ggq0Im2/A=
github.com/jenkins-x/jx-helpers/v3 v3.4.2 h1:dAEV58U4v416pukmxN1ZU12yX58Q70uoND3GVqiBo3U=
Expand Down Expand Up @@ -1438,8 +1438,6 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.0/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
21 changes: 10 additions & 11 deletions pkg/environments/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package environments

import (
"context"
"io/ioutil"
"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/maps"
"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/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
Expand All @@ -30,7 +29,7 @@ const (
// the message as the body for both the commit and the pull request,
// and the pullRequestInfo for any existing PR that exists to modify the environment that we want to merge these
// changes into.
func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, pullRequestDetails *scm.PullRequest, autoMerge bool) (*scm.PullRequest, error) {
func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, labels []string, autoMerge bool) (*scm.PullRequest, error) {
scmClient, repoFullName, err := o.GetScmClient(gitURL, o.GitKind)
if err != nil {
return nil, errors.Wrapf(err, "failed to create ScmClient")
Expand All @@ -45,7 +44,7 @@ func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, pullRequest
}

if prDir == "" {
tempDir, err := ioutil.TempDir("", "create-pr")
tempDir, err := os.MkdirTemp("", "create-pr")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,18 +107,18 @@ func (o *EnvironmentPullRequestOptions) Create(gitURL, prDir string, pullRequest
}

o.Labels = nil

// lets merge any labels together...
labelsSet := make(map[string]string)
if autoMerge {
o.Labels = append(o.Labels, LabelUpdatebot)
labelsSet[LabelUpdatebot] = ""
}
for _, l := range pullRequestDetails.Labels {
if l != nil {
label := l.Name
if label != "" && stringhelpers.StringArrayIndex(o.Labels, label) < 0 {
o.Labels = append(o.Labels, label)
}
for _, l := range labels {
if l != "" {
labelsSet[l] = ""
}
}
o.Labels = maps.MapKeys(labelsSet)

latestSha, err := gitclient.GetLatestCommitSha(o.Gitter, dir)
if err != nil {
Expand Down
14 changes: 9 additions & 5 deletions pkg/environments/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func (o *EnvironmentPullRequestOptions) CreatePullRequest(scmClient *scm.Client,
log.Logger().Infof("no changes detected so not creating a Pull Request on %s", termcolor.ColorInfo(gitURL))
return nil, nil
}
changelogPrefix := fmt.Sprintf("\n# %s\n", o.Application)
existingChangelog := false

if existingPR != nil {
if existingPR.Source == "" {
Expand All @@ -55,6 +57,11 @@ func (o *EnvironmentPullRequestOptions) CreatePullRequest(scmClient *scm.Client,
o.BranchName = existingPR.Source
log.Logger().Infof("replacing commits of existing Pull Request %s", existingPR.Link)
}
parts := strings.SplitN(existingPR.Body, o.ChangelogSeparator, 2)
if len(parts) == 2 {
changelogPrefix = parts[1]
existingChangelog = true
}
}
baseBranch := o.BaseBranchName

Expand Down Expand Up @@ -108,12 +115,9 @@ func (o *EnvironmentPullRequestOptions) CreatePullRequest(scmClient *scm.Client,

commitTitle := strings.TrimSpace(o.CommitTitle)
commitBody := o.CommitMessage
if commitBody == "" {
commitBody = o.CommitMessageSuffix
} else {
commitBody += "\n" + o.CommitMessageSuffix
if existingChangelog || o.CommitChangelog != "" {
commitBody += "\n" + o.ChangelogSeparator + changelogPrefix + "\n" + o.CommitChangelog
}

commitMessage := fmt.Sprintf("%s\n\n%s", commitTitle, commitBody)
_, err = gitclient.AddAndCommitFiles(gitter, dir, strings.TrimSpace(commitMessage))
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion pkg/environments/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ type EnvironmentPullRequestOptions struct {
BranchName string
CommitTitle string
CommitMessage string
CommitMessageSuffix string
CommitChangelog string
ChangelogSeparator string
Namespace string
JXClient versioned.Interface
ScmClient *scm.Client
Expand All @@ -58,6 +59,7 @@ type EnvironmentPullRequestOptions struct {
Fork bool
ReusePullRequest bool
SparseCheckoutPatterns []string
Application string
}

// A PullRequestFilter defines a filter for finding pull requests
Expand Down
Loading

0 comments on commit c5bf4c1

Please sign in to comment.