-
Notifications
You must be signed in to change notification settings - Fork 784
/
step_get_version_changeset.go
129 lines (116 loc) · 4.67 KB
/
step_get_version_changeset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package cmd
import (
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/version"
"github.com/spf13/cobra"
"path/filepath"
"strings"
)
const (
defaultRepoName = "origin"
defaultStableVersionBranch = "master"
)
// StepGetVersionChangeSetOptions contains the command line flags
type StepGetVersionChangeSetOptions struct {
StepOptions
VersionsDir string
VersionsRepository string
TestingBranch string
StableBranch string
PR string
}
var (
// StepGetVersionChangeSetLong command long description
StepGetVersionChangeSetLong = templates.LongDesc(`
This pipeline step generates environment variables from the differences of versions between jenkins-x-version branches
`)
// StepGetVersionChangeSetExample command example
StepGetVersionChangeSetExample = templates.Examples(`
# This pipeline step generates environment variables from the differences of versions between jenkins-x-version PR21 and the master branch
jx step get version-changeset --pr 21
# This pipeline step generates environment variables from the differences of versions between jenkins-x-version PR21 and a branch called stuff
jx step get version-changeset --stable-branch stuff --pr 21
`)
)
// NewCmdStepGetVersionChangeSet create the 'step git envs' command
func NewCmdStepGetVersionChangeSet(commonOpts *CommonOptions) *cobra.Command {
options := StepGetVersionChangeSetOptions{
StepOptions: StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "version-changeset",
Short: "Creates environment variables from the differences of versions between jenkins-x-version branches",
Long: StepGetVersionChangeSetLong,
Example: StepGetVersionChangeSetExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.VersionsRepository, "repo", "r", DefaultVersionsURL, "Jenkins X versions Git repo")
cmd.Flags().StringVarP(&options.StableBranch, "stable-branch", "", defaultStableVersionBranch, "the versions git repository branch to compare against")
cmd.Flags().StringVarP(&options.TestingBranch, "testing-branch", "", defaultStableVersionBranch, "the versions git repository branch to clone")
cmd.Flags().StringVarP(&options.VersionsDir, "versions-dir", "", "", "the directory containing the versions repo")
cmd.Flags().StringVarP(&options.PR, "pr", "", "", "the PR in the versions repository top clone")
return cmd
}
// Run implements the command
func (o *StepGetVersionChangeSetOptions) Run() error {
if o.VersionsDir == "" {
versionDir, err := o.cloneJXVersionsRepo(o.VersionsRepository)
if err != nil {
return err
}
o.VersionsDir = versionDir
}
if o.PR != "" {
o.TestingBranch = o.PR
o.Git().FetchBranch(o.VersionsDir, defaultRepoName, "pull/"+o.PR+"/head:"+o.PR)
}
if o.StableBranch != defaultStableVersionBranch {
o.Git().FetchBranch(o.VersionsDir, defaultRepoName, o.StableBranch+":"+o.StableBranch)
}
o.Git().Checkout(o.VersionsDir, o.TestingBranch)
bulkChange, _ := o.Git().ListChangedFilesFromBranch(o.VersionsDir, o.StableBranch)
changeSets := strings.Split(bulkChange, "\n")
appUpdatedVersions := make([]string, 0)
appPreviousVersions := make([]string, 0)
for _, line := range changeSets {
output := strings.Split(line, "\t")
if len(output) == 2 {
modifier := output[0]
if modifier == "A" || modifier == "M" {
file := output[1]
if strings.HasSuffix(file, ".yml") {
stableVersion, err := version.LoadStableVersionFile(filepath.Join(o.VersionsDir, file))
if err == nil {
if modifier == "M" {
fileData, _ := o.Git().LoadFileFromBranch(o.VersionsDir, o.StableBranch, file)
oldStableVersion, _ := version.LoadStableVersionFromData([]byte(fileData))
oldApp := formatVersion(file, oldStableVersion)
appPreviousVersions = append(appPreviousVersions, oldApp)
}
changedApp := formatVersion(file, stableVersion)
appUpdatedVersions = append(appUpdatedVersions, changedApp)
}
}
}
}
}
updateEnv := strings.Join(appUpdatedVersions, ",")
previousEnv := strings.Join(appPreviousVersions, ",")
log.Infof("JX_CHANGED_VERSIONS=\"%s\"\n", updateEnv)
log.Infof("JX_STABLE_VERSIONS=\"%s\"\n", previousEnv)
return nil
}
func formatVersion(fileName string, stableVersion *version.StableVersion) string {
formattedVersion := strings.Replace(fileName, string(version.KindChart)+"/", string(version.KindChart)+":", 1)
formattedVersion = strings.Replace(formattedVersion, ".yml", "", 1)
formattedVersion = formattedVersion + ":" + stableVersion.Version
return formattedVersion
}