github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/command/jenkins.go (about) 1 // Copyright 2022 Harness, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package command 16 17 import ( 18 "context" 19 "flag" 20 "io/ioutil" 21 "log" 22 "os" 23 24 "github.com/drone/go-convert/convert/harness/downgrader" 25 "github.com/drone/go-convert/convert/jenkins" 26 27 "github.com/google/subcommands" 28 ) 29 30 type Jenkins struct { 31 token string 32 attempts int 33 name string 34 proj string 35 org string 36 repoName string 37 repoConn string 38 kubeName string 39 kubeConn string 40 dockerConn string 41 format string 42 43 downgrade bool 44 beforeAfter bool 45 debug bool 46 } 47 48 func (*Jenkins) Name() string { return "jenkins" } 49 func (*Jenkins) Synopsis() string { return "converts a jenkins pipeline" } 50 func (*Jenkins) Usage() string { 51 return `jenkins [-token] [-downgrade] [Jenkinsfile] 52 ` 53 } 54 55 func (c *Jenkins) SetFlags(f *flag.FlagSet) { 56 f.StringVar(&c.token, "token", "", "chat gpt token") 57 f.IntVar(&c.attempts, "attempts", 1, "chat gtp generation attempts") 58 f.BoolVar(&c.downgrade, "downgrade", false, "downgrade to the legacy yaml format") 59 f.BoolVar(&c.beforeAfter, "before-after", false, "print the befor and after") 60 f.StringVar(&c.format, "format", "github", "configure the intermediate yaml format") 61 f.BoolVar(&c.debug, "debug", false, "enable message debugging") 62 63 f.StringVar(&c.org, "org", "default", "harness organization") 64 f.StringVar(&c.proj, "project", "default", "harness project") 65 f.StringVar(&c.name, "pipeline", "default", "harness pipeline name") 66 f.StringVar(&c.repoConn, "repo-connector", "", "repository connector") 67 f.StringVar(&c.repoName, "repo-name", "", "repository name") 68 f.StringVar(&c.kubeConn, "kube-connector", "", "kubernetes connector") 69 f.StringVar(&c.kubeName, "kube-namespace", "", "kubernets namespace") 70 f.StringVar(&c.dockerConn, "docker-connector", "", "dockerhub connector") 71 } 72 73 func (c *Jenkins) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { 74 path := f.Arg(0) 75 76 // use the token from environment variable 77 if token := os.Getenv("TOKEN"); token != "" { 78 c.token = token 79 } 80 81 // if the user does not specify the path as 82 // a command line arg, assume the default path. 83 if path == "" { 84 path = "Jenkinsfile" 85 } 86 87 // open the jenkins yaml 88 before, err := ioutil.ReadFile(path) 89 if err != nil { 90 log.Println(err) 91 return subcommands.ExitFailure 92 } 93 94 opts := []jenkins.Option{ 95 jenkins.WithDockerhub(c.dockerConn), 96 jenkins.WithKubernetes(c.kubeName, c.kubeConn), 97 jenkins.WithToken(c.token), 98 jenkins.WithFormatString(c.format), 99 } 100 101 if c.debug { 102 opts = append(opts, jenkins.WithDebug()) 103 } 104 105 // convert the pipeline yaml from the jenkins 106 // format to the harness yaml format. 107 after, err := jenkins.New(opts...).ConvertBytes(before) 108 if err != nil { 109 log.Println(err) 110 return subcommands.ExitFailure 111 } 112 113 // downgrade from the v1 harness yaml format 114 // to the v0 harness yaml format. 115 if c.downgrade { 116 // downgrade to the v0 yaml 117 d := downgrader.New( 118 downgrader.WithCodebase(c.repoName, c.repoConn), 119 downgrader.WithDockerhub(c.dockerConn), 120 downgrader.WithKubernetes(c.kubeName, c.kubeConn), 121 downgrader.WithName(c.name), 122 downgrader.WithOrganization(c.org), 123 downgrader.WithProject(c.proj), 124 ) 125 after, err = d.Downgrade(after) 126 if err != nil { 127 log.Println(err) 128 return subcommands.ExitFailure 129 } 130 } 131 132 if c.beforeAfter { 133 os.Stdout.WriteString("---\n") 134 os.Stdout.Write(before) 135 os.Stdout.WriteString("\n---\n") 136 } 137 138 os.Stdout.Write(after) 139 140 return subcommands.ExitSuccess 141 }