-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_update_release_github.go
136 lines (120 loc) · 3.55 KB
/
step_update_release_github.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
130
131
132
133
134
135
136
package release
import (
"os"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/gits"
"github.com/pkg/errors"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/spf13/cobra"
)
var (
updateReleaseGithubLong = templates.LongDesc(`
updates the status of a release to be either a prerelease or a release
`)
updateReleaseGitHubExample = templates.Examples(`
jx step update release github -o jenkins-x -r jx -v 1.2.3 -p=false
`)
)
// StepUpdateReleaseGitHubOptions contains the command line flags
type StepUpdateReleaseGitHubOptions struct {
StepUpdateReleaseOptions
PreRelease bool
State StepUpdateReleaseStatusState
}
// StepUpdateReleaseStatusState contains the state information
type StepUpdateReleaseStatusState struct {
GitInfo *gits.GitRepository
GitProvider gits.GitProvider
Release *v1.Release
}
// NewCmdStepUpdateReleaseGitHub Creates a new Command object
func NewCmdStepUpdateReleaseGitHub(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepUpdateReleaseGitHubOptions{
StepUpdateReleaseOptions: StepUpdateReleaseOptions{
StepUpdateOptions: step.StepUpdateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
},
}
cmd := &cobra.Command{
Use: "github",
Short: "Updates a release to either a prerelease or a release",
Long: updateReleaseGithubLong,
Example: updateReleaseGitHubExample,
Aliases: []string{"gh"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
AddStepUpdateReleaseFlags(cmd, &options.StepUpdateReleaseOptions)
cmd.Flags().BoolVarP(&options.PreRelease, "prerelease", "p", false, "The release state of that version release of the repository")
return cmd
}
// ValidateGitHubOptions validates the common options for brew pr steps
func (o *StepUpdateReleaseGitHubOptions) ValidateGitHubOptions() error {
if err := o.ValidateOptions(); err != nil {
return errors.WithStack(err)
}
return nil
}
// Run implements this command
func (o *StepUpdateReleaseGitHubOptions) Run() error {
if err := o.ValidateGitHubOptions(); err != nil {
return errors.WithStack(err)
}
releaseInfo := &gits.GitRelease{
PreRelease: o.PreRelease,
}
dir, err := os.Getwd()
if err != nil {
return err
}
authConfigSvc, err := o.GitAuthConfigService()
if err != nil {
return err
}
gitDir, gitConfDir, err := o.Git().FindGitConfigDir(dir)
if err != nil {
return err
}
if gitDir == "" || gitConfDir == "" {
log.Logger().Warnf("No git directory could be found from dir %s", dir)
return nil
}
gitURL, err := o.Git().DiscoverUpstreamGitURL(gitConfDir)
if err != nil {
return err
}
gitInfo, err := gits.ParseGitURL(gitURL)
if err != nil {
return err
}
o.State.GitInfo = gitInfo
gitKind, err := o.GitServerKind(gitInfo)
if err != nil {
return err
}
ghOwner, err := o.GetGitHubAppOwner(gitInfo)
if err != nil {
return err
}
gitProvider, err := o.State.GitInfo.CreateProvider(o.InCluster(), authConfigSvc, gitKind, ghOwner, o.Git(), false, o.GetIOFileHandles())
if err != nil {
return errors.Wrap(err, "Could not create GitProvider, unable to update the release state %s")
}
o.State.GitProvider = gitProvider
err = gitProvider.UpdateReleaseStatus(o.Owner, o.Repository, o.Version, releaseInfo)
if err != nil {
return err
}
return nil
}