diff --git a/.github/workflows/pull-request-clean.yaml b/.github/workflows/pull-request-clean.yaml new file mode 100644 index 0000000000000000000000000000000000000000..d0c8afa1a6c93b093623e6bd1e17b4397c8b9932 --- /dev/null +++ b/.github/workflows/pull-request-clean.yaml @@ -0,0 +1,15 @@ +name: Pull Request Clean + +on: + pull_request: + types: [closed] + +jobs: + build: + name: Build + runs-on: ubuntu-18.04 + steps: + - name: Build + run: | + pr=$(echo $GITHUB_REF | sed 's/refs\/pull\///g' | sed 's/\/merge//g') + curl -ulinuxsuren:${{ secrets.BINTRAY_TOKEN }} -X DELETE https://api.bintray.com/packages/jenkins-zh/generic/jenkins-cli/versions/pr-$pr diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 0a08c8ece31ff9c7ac4416af47a04a6aac4ab05a..e267ee2ef503bf6aec4628140c505487199dde2f 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -29,12 +29,7 @@ jobs: - name: Build run: | export PATH=$PATH:${PWD}/bin:$GOPATH/bin:/home/runner/go/bin - make clean go-bindata-download-linux tools init build-all - - - name: Test - run: | - export PATH=$PATH:${PWD}/bin:$GOPATH/bin:/home/runner/go/bin - make test + make clean go-bindata-download-linux tools init release # - name: SonarCloud Scan # uses: LinuxSuRen/sonarcloud-github-action@master @@ -42,6 +37,22 @@ jobs: # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + - name: Upload PR to Bintray + run: | + pr=$(echo $GITHUB_REF | sed 's/refs\/pull\///g' | sed 's/\/merge//g') + echo "start to upload generic packages for pr-$pr" + curl -ulinuxsuren:${{ secrets.BINTRAY_TOKEN }} -X DELETE https://api.bintray.com/packages/jenkins-zh/generic/jenkins-cli/versions/pr-$pr + curl -T release/jcli-darwin-amd64.tar.gz -ulinuxsuren:${{ secrets.BINTRAY_TOKEN }} \ + -H "X-Bintray-Package:jenkins-cli" -H "X-Bintray-Version:pr-$pr" \ + https://api.bintray.com/content/jenkins-zh/generic/jenkins-cli/pr-$pr/jcli-darwin-amd64.tar.gz + curl -T release/jcli-linux-amd64.tar.gz -ulinuxsuren:${{ secrets.BINTRAY_TOKEN }} \ + -H "X-Bintray-Package:jenkins-cli" -H "X-Bintray-Version:pr-$pr" \ + https://api.bintray.com/content/jenkins-zh/generic/jenkins-cli/pr-$pr/jcli-linux-amd64.tar.gz + curl -T release/jcli-windows-386.tar.gz -ulinuxsuren:${{ secrets.BINTRAY_TOKEN }} \ + -H "X-Bintray-Package:jenkins-cli" -H "X-Bintray-Version:pr-$pr" \ + https://api.bintray.com/content/jenkins-zh/generic/jenkins-cli/pr-$pr/jcli-windows-386.tar.gz + curl -ulinuxsuren:${{ secrets.BINTRAY_TOKEN }} -X POST https://api.bintray.com/content/jenkins-zh/generic/jenkins-cli/pr-$pr/publish + - name: Upload darwin artifact uses: actions/upload-artifact@v1.0.0 with: @@ -59,3 +70,8 @@ jobs: with: name: windows path: bin/windows/jcli.exe + + - name: Test + run: | + export PATH=$PATH:${PWD}/bin:$GOPATH/bin:/home/runner/go/bin + make test diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c37d25ac105f260be776784ad29e7609535a10ca..a0aef6809ed3242617a4d48f3ac35f23da8c2a43 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -23,13 +23,22 @@ Useful links: * [Jenkins Remote Access API](https://wiki.jenkins.io/display/JENKINS/Remote+access+API) * [Jenkins REST API overview](https://www.youtube.com/watch?v=D93t1jElt4Q) by [Cliffano Subagio](https://github.com/cliffano) -* ## Testing We use a BDD Testing Framework to test our project. Please make sure you're familiar with [ginkgo](https://github.com/onsi/ginkgo) before you get start to contribute. +### Test By Manual + +Unit testing can help us a lot, but doing the manual test is still necessary. I highly suggest that you test it under +a totally fresh environment. Here is list of free resources that you can use: + +| Provider | Link | +|---|---| +| Aliyun | [https://api.aliyun.com/#/cli](https://api.aliyun.com/#/cli) | +| Google Could | [https://ssh.cloud.google.com/cloudshell/environment/view](https://ssh.cloud.google.com/cloudshell/environment/view) | + ## Pull Requests Before you get started, please fork this project into your GitHub account. Then diff --git a/app/cmd/center_download.go b/app/cmd/center_download.go index db1b9b5f6f9736133b60b898ab7b381cd5fdc758..dc1cb2a5fba89b7d74b3a3242806555b157d4f6f 100644 --- a/app/cmd/center_download.go +++ b/app/cmd/center_download.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/mitchellh/go-homedir" "go.uber.org/zap" "net/http" "os" @@ -22,6 +23,8 @@ type CenterDownloadOption struct { Output string ShowProgress bool + Formula string + RoundTripper http.RoundTripper } @@ -37,7 +40,7 @@ func init() { i18n.T("The mirror site of Jenkins")) centerDownloadCmd.Flags().BoolVarP(¢erDownloadOption.ShowProgress, "progress", "p", true, i18n.T("If you want to show the download progress")) - centerDownloadCmd.Flags().StringVarP(¢erDownloadOption.Output, "output", "o", "jenkins.war", + centerDownloadCmd.Flags().StringVarP(¢erDownloadOption.Output, "output", "o", "", i18n.T("The file of output")) } @@ -48,6 +51,19 @@ var centerDownloadCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, _ []string) error { return centerDownloadOption.DownloadJenkins() }, + PreRunE: func(cmd *cobra.Command, args []string) (err error) { + if centerDownloadOption.Output != "" { + return + } + + var userHome string + if userHome, err = homedir.Dir(); err != nil { + return + } + + centerDownloadOption.Output = fmt.Sprintf("%s/.jenkins-cli/cache/%s/jenkins.war", userHome, centerDownloadOption.Version) + return + }, } // DownloadJenkins download the Jenkins @@ -68,6 +84,7 @@ func (c *CenterDownloadOption) DownloadJenkins() (err error) { JenkinsCore: client.JenkinsCore{ RoundTripper: c.RoundTripper, }, + Formula: c.Formula, LTS: c.LTS, Version: c.Version, Output: c.Output, diff --git a/app/cmd/center_start.go b/app/cmd/center_start.go index a294887d603bf36fe4c7f5822981b8b04f665aa2..d29b34f41b8c66898fecda8b3c5ff2ddf8c8b8a4 100644 --- a/app/cmd/center_start.go +++ b/app/cmd/center_start.go @@ -37,6 +37,7 @@ type CenterStartOption struct { Download bool Version string LTS bool + Formula string DryRun bool } @@ -64,6 +65,8 @@ func init() { i18n.T("The of version of jenkins.war")) centerStartCmd.Flags().BoolVarP(¢erStartOption.LTS, "lts", "", true, i18n.T("If you want to download Jenkins as LTS")) + centerStartCmd.Flags().StringVarP(¢erStartOption.Formula, "formula", "", "", + i18n.T("The formula of jenkins.war, only support zh currently")) centerStartCmd.Flags().BoolVarP(¢erStartOption.HTTPSEnable, "https-enable", "", false, i18n.T("If you want to enable https")) @@ -99,6 +102,7 @@ var centerStartCmd = &cobra.Command{ if _, fileErr := os.Stat(jenkinsWar); fileErr != nil { download := &CenterDownloadOption{ Mirror: "default", + Formula: centerStartOption.Formula, LTS: centerStartOption.LTS, Output: jenkinsWar, ShowProgress: true, diff --git a/app/cmd/config.go b/app/cmd/config.go index e1a8022cccd9676dcf1dc26a47d577c6ec3e5b3a..7690f0d6505729473e0c2278565b948536358e31 100644 --- a/app/cmd/config.go +++ b/app/cmd/config.go @@ -158,10 +158,8 @@ func findSuiteByName(name string) (suite *PluginSuite) { } func loadDefaultConfig() (err error) { - var userHome string - userHome, err = homedir.Dir() - if err == nil { - configPath := fmt.Sprintf("%s/.jenkins-cli.yaml", userHome) + var configPath string + if configPath, err = getDefaultConfigPath(); err == nil { if _, err = os.Stat(configPath); err == nil { err = loadConfig(configPath) } @@ -169,6 +167,15 @@ func loadDefaultConfig() (err error) { return } +func getDefaultConfigPath() (configPath string, err error) { + var userHome string + userHome, err = homedir.Dir() + if err == nil { + configPath = fmt.Sprintf("%s/.jenkins-cli.yaml", userHome) + } + return +} + func loadConfig(path string) (err error) { configOptions.ConfigFileLocation = path diff --git a/app/cmd/config_generate.go b/app/cmd/config_generate.go index f4414ffb0aab36ab5e8cd34826bee37676e370f7..efc67ca6316886d06d4a77d4c039b9f32fd8cdd3 100644 --- a/app/cmd/config_generate.go +++ b/app/cmd/config_generate.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "github.com/mitchellh/go-homedir" + "go.uber.org/zap" "gopkg.in/yaml.v2" "io/ioutil" "os" @@ -59,13 +60,21 @@ var configGenerateCmd = &cobra.Command{ // InteractiveWithConfig be friendly for a newer func (o *ConfigGenerateOption) InteractiveWithConfig(cmd *cobra.Command, data []byte) (err error) { configPath := configOptions.ConfigFileLocation - _, err = os.Stat(configPath) + if configPath == "" { + configPath, err = getDefaultConfigPath() + } + + if err == nil { + _, err = os.Stat(configPath) + } + if err != nil && os.IsNotExist(err) { confirm := o.Confirm("Cannot found your config file, do you want to edit it?") if confirm { var content string content, err = o.Editor(string(data), "Edit your config file") if err == nil { + logger.Debug("write generated config file", zap.String("path", configPath)) err = ioutil.WriteFile(configPath, []byte(content), 0644) } } diff --git a/app/cmd/root.go b/app/cmd/root.go index 65589b4ca6dde6c08923c7ae22c2091941bfe9a5..f08343336a7aea291b38aa49464fc3eeb79899a0 100644 --- a/app/cmd/root.go +++ b/app/cmd/root.go @@ -46,10 +46,10 @@ var healthCheckRegister = &health.CheckRegister{ var rootCmd = &cobra.Command{ Use: "jcli", - Short: i18n.T("jcli is a tool which could help you with your multiple Jenkins"), - Long: `jcli is Jenkins CLI which could help with your multiple Jenkins, -Manage your Jenkins and your pipelines -More information could found at https://jenkins-zh.cn`, + Short: i18n.T("Jenkins CLI written by golang which could help you with your multiple Jenkins"), + Long: `Jenkins CLI written by golang which could help you with your multiple Jenkins, + +We'd love to hear your feedback at https://github.com/jenkins-zh/jenkins-cli/issues`, PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) { if logger, err = util.InitLogger(rootOptions.LoggerLevel); err == nil { client.SetLogger(logger) @@ -91,9 +91,10 @@ More information could found at https://jenkins-zh.cn`, func needReadConfig(cmd *cobra.Command) bool { ignoreConfigLoad := []string{ "config.generate", - "center.start", + //"center.start", // relay on the config when find a mirror "cwp", "version", + "completion", } configPath := getCmdPath(cmd) diff --git a/client/updateCenter.go b/client/updateCenter.go index e8aab34a51585576d22c5eadb79c00674337f7cd..05fd84a64cd9932cc9bf0e8cd98dfbeee98731b5 100644 --- a/client/updateCenter.go +++ b/client/updateCenter.go @@ -18,6 +18,8 @@ type UpdateCenterManager struct { Version string Output string + Formula string + ShowProgress bool } @@ -132,7 +134,9 @@ func (u *UpdateCenterManager) GetJenkinsWarURL() (warURL string) { version = "latest" } - if u.LTS { + if u.Formula == "zh" { + warURL = fmt.Sprintf("https://dl.bintray.com/jenkins-zh/generic/jenkins/%s/jenkins-zh.war", version) + } else if u.LTS { warURL = fmt.Sprintf("%s/war-stable/%s/jenkins.war", strings.TrimRight(u.MirrorSite, "/"), version) } else { warURL = fmt.Sprintf("%s/war/%s/jenkins.war", strings.TrimRight(u.MirrorSite, "/"), version)