diff --git a/cmd/plugin/github-repo-scaffolding-golang/main.go b/cmd/plugin/github-repo-scaffolding-golang/main.go deleted file mode 100644 index 9590a76fb..000000000 --- a/cmd/plugin/github-repo-scaffolding-golang/main.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "log" - - "github.com/devstream-io/devstream/internal/pkg/plugin/reposcaffolding/github/golang" -) - -// NAME is the name of this DevStream plugin. -const NAME = "github-repo-scaffolding-golang" - -// Plugin is the type used by DevStream core. It's a string. -type Plugin string - -// Create implements the installation of the github-repo-scaffolding-golang. -func (p Plugin) Create(options map[string]interface{}) (map[string]interface{}, error) { - return golang.Create(options) -} - -// Update implements the reinstallation of the github-repo-scaffolding-golang. -func (p Plugin) Update(options map[string]interface{}) (map[string]interface{}, error) { - return golang.Update(options) -} - -// Read implements the healthy check of the github-repo-scaffolding-golang. -func (p Plugin) Read(options map[string]interface{}) (map[string]interface{}, error) { - return golang.Read(options) -} - -// Delete implements the uninstallation of the github-repo-scaffolding-golang. -func (p Plugin) Delete(options map[string]interface{}) (bool, error) { - return golang.Delete(options) -} - -// DevStreamPlugin is the exported variable used by the DevStream core. -var DevStreamPlugin Plugin - -func main() { - log.Printf("%T: %s is a plugin for DevStream. Use it with DevStream.\n", DevStreamPlugin, NAME) -} diff --git a/cmd/plugin/gitlab-repo-scaffolding-golang/main.go b/cmd/plugin/gitlab-repo-scaffolding-golang/main.go deleted file mode 100644 index b9c1ba76b..000000000 --- a/cmd/plugin/gitlab-repo-scaffolding-golang/main.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "log" - - "github.com/devstream-io/devstream/internal/pkg/plugin/reposcaffolding/gitlab/golang" -) - -// NAME is the name of this DevStream plugin. -const NAME = "gitlab-repo-scaffolding-golang" - -// Plugin is the type used by DevStream core. It's a string. -type Plugin string - -// Create implements the installation of the gitlab-repo-scaffolding-golang. -func (p Plugin) Create(options map[string]interface{}) (map[string]interface{}, error) { - return golang.Create(options) -} - -// Update implements the reinstallation of the gitlab-repo-scaffolding-golang. -func (p Plugin) Update(options map[string]interface{}) (map[string]interface{}, error) { - return golang.Update(options) -} - -// Read implements the healthy check of the gitlab-repo-scaffolding-golang. -func (p Plugin) Read(options map[string]interface{}) (map[string]interface{}, error) { - return golang.Read(options) -} - -// Delete implements the uninstallation of the gitlab-repo-scaffolding-golang. -func (p Plugin) Delete(options map[string]interface{}) (bool, error) { - return golang.Delete(options) -} - -// DevStreamPlugin is the exported variable used by the DevStream core. -var DevStreamPlugin Plugin - -func main() { - log.Printf("%T: %s is a plugin for DevStream. Use it with DevStream.\n", DevStreamPlugin, NAME) -} diff --git a/docs/best-practices/gitops.md b/docs/best-practices/gitops.md index c9e91b784..882a0b0bc 100644 --- a/docs/best-practices/gitops.md +++ b/docs/best-practices/gitops.md @@ -16,7 +16,7 @@ For Chinese readers, watch this one instead: ## Plugins needed -1. [github-repo-scaffolding-golang](../plugins/github-repo-scaffolding-golang.md) +1. [repo-scaffolding](../plugins/repo-scaffolding.md) 2. [jira-github](../plugins/jira-github-integ.md) 3. [githubactions-golang](../plugins/githubactions-golang.md) 4. [argocd](../plugins/argocd.md) @@ -24,9 +24,9 @@ For Chinese readers, watch this one instead: The dependencies of these plugins are(`a -> b` means for `a depends on b`): -- `jira-github` -> `github-repo-scaffolding-golang` -- `githubactions-golang` -> `github-repo-scaffolding-golang` -- `argocdapp` -> `argocd` and `github-repo-scaffolding-golang` +- `jira-github` -> `repo-scaffolding` +- `githubactions-golang` -> `repo-scaffolding` +- `argocdapp` -> `argocd`, `githubactions-golang` and `repo-scaffolding` Note: These dependencies are optional; you can use dependency to make sure a certain tool is installed before another. We should use dependency according to the actual usage situation. diff --git a/docs/best-practices/gitops.zh.md b/docs/best-practices/gitops.zh.md index 48215eb0f..402c62d78 100644 --- a/docs/best-practices/gitops.zh.md +++ b/docs/best-practices/gitops.zh.md @@ -8,7 +8,7 @@ ## 所需插件 -1. [github-repo-scaffolding-golang](../plugins/github-repo-scaffolding-golang.md) +1. [repo-scaffolding](../plugins/repo-scaffolding.md) 2. [jira-github](../plugins/jira-github-integ.md) 3. [githubactions-golang](../plugins/githubactions-golang.md) 4. [argocd](../plugins/argocd.md) @@ -16,9 +16,9 @@ 这些插件的依赖关系如下(`a -> b`意味着`a依赖b`): -- `jira-github` -> `github-repo-scaffolding-golang` -- `githubactions-golang` -> `github-repo-scaffolding-golang` -- `argocdapp` -> `argocd` 和 `github-repo-scaffolding-golang` +- `jira-github` -> `repo-scaffolding` +- `githubactions-golang` -> `repo-scaffolding` +- `argocdapp` -> `argocd`, `githubactions-golang` 和 `repo-scaffolding` **注意**:依赖并不是必须指定的,我们可以用依赖确保某个工具可以先于另外一个工具安装。我们应该根据实际的使用场景来使用`dependsOn`。 diff --git a/docs/core-concepts/config.md b/docs/core-concepts/config.md index 0a719fd2b..898fe2779 100644 --- a/docs/core-concepts/config.md +++ b/docs/core-concepts/config.md @@ -62,17 +62,24 @@ See the `tools.yaml` example down below: ```yaml tools: -- name: github-repo-scaffolding-golang - instanceID: default +- name: repo-scaffolding + instanceID: golang-github options: - owner: [[ githubUsername ]] - org: "" - repo: [[ repoName ]] - branch: [[ defaultBranch ]] - image_repo: [[ dockerhubUsername ]]/[[ repoName ]] + destination_repo: + owner: [[ githubUsername ]] + org: "" + repo: [[ repoName ]] + branch: [[ defaultBranch ]] + repo_type: github + vars: + ImageRepo: "[[ dockerhubUsername ]]/[[ repoName ]]" + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github - name: jira-github-integ instanceID: default - dependsOn: [ "github-repo-scaffolding-golang.default" ] + dependsOn: [ "repo-scaffolding.golang-github" ] options: owner: [[ githubUsername ]] repo: [[ repoName ]] diff --git a/docs/core-concepts/config.zh.md b/docs/core-concepts/config.zh.md index 57ba4d3f3..422ee25f9 100644 --- a/docs/core-concepts/config.zh.md +++ b/docs/core-concepts/config.zh.md @@ -59,17 +59,24 @@ dockerhubUsername: exploitht ```yaml tools: -- name: github-repo-scaffolding-golang - instanceID: default +- name: repo-scaffolding + instanceID: golang-github options: - owner: [[ githubUsername ]] - org: "" - repo: [[ repoName ]] - branch: [[ defaultBranch ]] - image_repo: [[ dockerhubUsername ]]/[[ repoName ]] + destination_repo: + owner: [[ githubUsername ]] + org: "" + repo: [[ repoName ]] + branch: [[ defaultBranch ]] + repo_type: github + vars: + ImageRepo: "[[ dockerhubUsername ]]/[[ repoName ]]" + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github - name: jira-github-integ instanceID: default - dependsOn: [ "github-repo-scaffolding-golang.default" ] + dependsOn: [ "repo-scaffolding.golang-github" ] options: owner: [[ githubUsername ]] repo: [[ repoName ]] diff --git a/docs/core-concepts/dependencies.md b/docs/core-concepts/dependencies.md index 1fd171244..c280a7878 100644 --- a/docs/core-concepts/dependencies.md +++ b/docs/core-concepts/dependencies.md @@ -14,23 +14,31 @@ Since `dependsOn` is a list, a tool can have multiple dependencies: dependsOn: [ "ToolName1.ToolInstanceID1", "ToolName2.ToolInstanceID2", "..." ] ``` -In the following tool file example, tool "github-repo-scaffolding-golang" (with instance id "default") will be installed before tool "githubactions-golang" (with instance id "default"): +In the following tool file example, tool "repo-scaffolding" (with instance id "golang-github") will be installed before tool "githubactions-golang" (with instance id "default"): ```yaml tools: -- name: github-repo-scaffolding-golang - instanceID: default +- name: repo-scaffolding + instanceID: golang-github options: - org: devstream-io - repo: dtm-e2e-go - branch: main - image_repo: dtme2etest/dtm-e2e-go + destination_repo: + owner: [[ githubUsername ]] + org: "" + repo: [[ repoName ]] + branch: [[ defaultBranch ]] + repo_type: github + vars: + ImageRepo: "[[ dockerhubUsername ]]/[[ repoName ]]" + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github - name: githubactions-golang instanceID: default - dependsOn: ["github-repo-scaffolding-golang.default"] + dependsOn: ["repo-scaffolding.golang-github"] options: - org: ${{github-repo-scaffolding-golang.default.outputs.org}} - repo: ${{github-repo-scaffolding-golang.default.outputs.repo}} + org: ${{repo-scaffolding.golang-github.outputs.org}} + repo: ${{repo-scaffolding.golang-github.outputs.repo}} language: name: go version: "1.18" diff --git a/docs/core-concepts/output.md b/docs/core-concepts/output.md index 4299334b9..cb591cf6c 100644 --- a/docs/core-concepts/output.md +++ b/docs/core-concepts/output.md @@ -47,13 +47,21 @@ Config: ```yaml --- tools: -- name: github-repo-scaffolding-golang +- name: repo-scaffolding instanceID: default options: - owner: IronCore864 - repo: golang-demo - branch: main - image_repo: ironcore864/golang-demo + destination_repo: + owner: IronCore864 + org: "" + repo: golang-demo + branch: main + repo_type: github + vars: + ImageRepo: "ironcore864/golang-demo" + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github - name: argocd instanceID: default options: @@ -70,7 +78,7 @@ tools: upgradeCRDs: true - name: argocdapp instanceID: default - dependsOn: [ "argocd.default", "github-repo-scaffolding-golang.default" ] + dependsOn: [ "argocd.default", "repo-scaffolding.golang-github" ] options: app: name: golang-demo @@ -81,9 +89,9 @@ tools: source: valuefile: values.yaml path: helm/golang-demo - repoURL: ${{ github-repo-scaffolding-golang.default.outputs.repoURL }} # pay attention here + repoURL: ${{ repo-scaffolding.golang-github.outputs.repoURL }} # pay attention here ``` In this example: -- The "default" instance of tool `argocdapp` depends on the "default" instance of tool `github-repo-scaffolding-golang` -- The "default" instance of tool `argocdapp` has an user option "options.source.repoURL", which uses the "default" instance of tool `github-repo-scaffolding-golang`'s output "repoURL" (`${{ github-repo-scaffolding-golang.default.outputs.repoURL }}`) +- The "default" instance of tool `argocdapp` depends on the "default" instance of tool `repo-scaffolding` +- The "default" instance of tool `argocdapp` has an user option "options.source.repoURL", which uses the "default" instance of tool `repo-scaffolding`'s output "repoURL" (`${{ repo-scaffolding.golang-github.outputs.repoURL }}`) diff --git a/docs/core-concepts/stateconfig.md b/docs/core-concepts/stateconfig.md index f9ecafaa6..42cb18a57 100644 --- a/docs/core-concepts/stateconfig.md +++ b/docs/core-concepts/stateconfig.md @@ -81,7 +81,7 @@ argocdDeployTimeout: 5m ```yaml tools: -- name: github-repo-scaffolding-golang +- name: repo-scaffolding instanceID: default options: owner: [[ githubUsername ]] @@ -112,16 +112,16 @@ tiexin@mbp ~/work/devstream-io/devstream $ ./dtm apply 2022-05-30 17:07:59 ℹ [INFO] Apply started. 2022-05-30 17:07:59 ℹ [INFO] Using dir <.devstream> to store plugins. 2022-05-30 17:07:59 ℹ [INFO] Using s3 backend. Bucket: devstream-test-remote-state, region: ap-southeast-1, key: devstream.state. -2022-05-30 17:08:00 ℹ [INFO] Tool (github-repo-scaffolding-golang/default) found in config but doesn't exist in the state, will be created. +2022-05-30 17:08:00 ℹ [INFO] Tool (repo-scaffolding/default) found in config but doesn't exist in the state, will be created. Continue? [y/n] Enter a value (Default is n): y 2022-05-30 17:08:08 ℹ [INFO] Start executing the plan. 2022-05-30 17:08:08 ℹ [INFO] Changes count: 1. 2022-05-30 17:08:08 ℹ [INFO] -------------------- [ Processing progress: 1/1. ] -------------------- -2022-05-30 17:08:08 ℹ [INFO] Processing: (github-repo-scaffolding-golang/default) -> Create ... +2022-05-30 17:08:08 ℹ [INFO] Processing: (repo-scaffolding/default) -> Create ... 2022-05-30 17:08:12 ℹ [INFO] The repo dtm-test-go has been created. -2022-05-30 17:08:29 ✔ [SUCCESS] Tool (github-repo-scaffolding-golang/default) Create done. +2022-05-30 17:08:29 ✔ [SUCCESS] Tool (repo-scaffolding/default) Create done. 2022-05-30 17:08:29 ℹ [INFO] -------------------- [ Processing done. ] -------------------- 2022-05-30 17:08:29 ✔ [SUCCESS] All plugins applied successfully. 2022-05-30 17:08:29 ✔ [SUCCESS] Apply finished. @@ -138,8 +138,8 @@ tiexin@mbp ~/work/devstream-io/devstream $ aws s3 cp s3://devstream-test-remote- And if we open the downloaded file, we will see something similar to the following: ```yaml -github-repo-scaffolding-golang_default: - name: github-repo-scaffolding-golang +repo-scaffolding_default: + name: repo-scaffolding instanceid: default dependson: [] options: diff --git a/docs/development/test.md b/docs/development/test.md index 81cfde5b3..f313ae4cb 100644 --- a/docs/development/test.md +++ b/docs/development/test.md @@ -23,7 +23,7 @@ The definition of the GitHub Action is [here](https://github.com/devstream-io/de We have a simple e2e test that tests the following plugins: -- `github-repo-scaffolding-golang` +- `repo-scaffolding` - `githubactions-golang` - `argocd` - `argocdapp` diff --git a/docs/development/test.zh.md b/docs/development/test.zh.md index 3119d42b5..1eaab7891 100644 --- a/docs/development/test.zh.md +++ b/docs/development/test.zh.md @@ -22,7 +22,7 @@ GitHub Action 工作流程的定义在[这里](https://github.com/devstream-io/d 目前,我们编写了针对以下插件的简单端到端测试: -- `github-repo-scaffolding-golang` +- `repo-scaffolding` - `githubactions-golang` - `argocd` - `argocdapp` diff --git a/docs/plugins/argocdapp.md b/docs/plugins/argocdapp.md index 934f28028..69039e957 100644 --- a/docs/plugins/argocdapp.md +++ b/docs/plugins/argocdapp.md @@ -17,30 +17,38 @@ This plugin creates an [ArgoCD Application](https://argo-cd.readthedocs.io/en/st --8<-- "argocdapp.yaml" ``` -## Use Together with the `github-repo-scaffolding-golang` Plugin +## Use Together with the `repo-scaffolding` Plugin -This plugin can be used together with the `github-repo-scaffolding-golang` plugin (see document [here](./github-repo-scaffolding-golang.md).) +This plugin can be used together with the `repo-scaffolding` plugin (see document [here](./repo-scaffolding.md).) -For example, you can first use `github-repo-scaffolding-golang` to bootstrap a Golang repo, then use this plugin to set up basic GitHub Actions CI workflows. In this scenario: +For example, you can first use `repo-scaffolding` to bootstrap a Golang repo, then use this plugin to set up basic GitHub Actions CI workflows. In this scenario: -- This plugin can specify `github-repo-scaffolding-golang` as a dependency, so that the dependency is first satisfied before executing this plugin. -- This plugin can refer to `github-repo-scaffolding-golang`'s output to reduce copy/paste human error. +- This plugin can specify `repo-scaffolding` as a dependency, so that the dependency is first satisfied before executing this plugin. +- This plugin can refer to `repo-scaffolding`'s output to reduce copy/paste human error. See the example below: ```yaml --- tools: -- name: go-webapp-repo - plugin: github-repo-scaffolding-golang +- name: repo-scaffolding + instanceID: golang-github options: - owner: IronCore864 - repo: go-webapp-devstream-demo - branch: main - image_repo: ironcore864/go-webapp-devstream-demo + destination_repo: + owner: [[ githubUsername ]] + org: "" + repo: [[ repoName ]] + branch: [[ defaultBranch ]] + repo_type: github + vars: + ImageRepo: "[[ dockerhubUsername ]]/[[ repoName ]]" + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github - name: go-webapp-argocd-deploy plugin: argocdapp - dependsOn: ["go-webapp-repo.github-repo-scaffolding-golang"] + dependsOn: ["repo-scaffolding.golang-github"] options: app: name: hello @@ -51,12 +59,12 @@ tools: source: valuefile: values.yaml path: charts/go-hello-http - repoURL: ${{go-webapp-repo.github-repo-scaffolding-golang.outputs.repoURL}} + repoURL: ${{repo-scaffolding.golang-github.outputs.repoURL}} ``` In the example above: -- We put `go-webapp-repo.github-repo-scaffolding-golang` as dependency by using the `dependsOn` keyword. -- We used `go-webapp-repo.github-repo-scaffolding-golang`'s output as input for the `githubactions-golang` plugin. +- We put `repo-scaffolding.golang-github` as dependency by using the `dependsOn` keyword. +- We used `repo-scaffolding.golang-github`'s output as input for the `githubactions-golang` plugin. Pay attention to the `${{ xxx }}` part in the example. `${{ TOOL_NAME.PLUGIN.outputs.var}}` is the syntax for using an output. diff --git a/docs/plugins/github-repo-scaffolding-golang.md b/docs/plugins/github-repo-scaffolding-golang.md deleted file mode 100644 index 6a8abcffc..000000000 --- a/docs/plugins/github-repo-scaffolding-golang.md +++ /dev/null @@ -1,80 +0,0 @@ -# github-repo-scaffolding-golang Plugin - -This plugin bootstraps a GitHub repo with scaffolding code for a Golang web application. - -_This plugin depends on the following environment variable:_ - -- GITHUB_TOKEN - -Set it before using this plugin. - -If you don't know how to create this token, check out: -- [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) - -*Tips:* - -*1. If you run `dtm delete`, the repo on GitHub will be completely removed.* - -*2. If the `Update` interface is called, the repo on github will be completely removed and recreated. However, given our current implementation, this interface shall not be called, as of in v0.2.0.* - -## Usage - -**Please note that the `owner` parameter is case-sensitive.** - -```yaml ---8<-- "github-repo-scaffolding-golang.yaml" -``` - -Replace the following from the config above: - -- `YOUR_GITHUB_USERNAME` -- `YOUR_ORGANIZATION_NAME` -- `YOUR_REPO_NAME` -- `YOUR_DOCKERHUB_USERNAME` -- `YOUR_DOCKERHUB_REPOSITORY` - -The "branch" in the example above is "main", but you can adjust accordingly. - -Currently, all the parameters in the example above are mandatory. - -## Outputs - -This plugin has three outputs: - -- `owner` -- `repo` -- `repoURL` (example: "https://github.com/IronCore864/test.git") - -If, for example, you want to use the outputs as inputs for another plugin, you can refer to the following example: - -```yaml ---- -tools: -- name: go-webapp-repo - plugin: github-repo-scaffolding-golang - options: - owner: YOUR_GITHUB_USERNAME - repo: YOUR_REPO_NAME - branch: main - image_repo: YOUR_DOCKERHUB_REPOSITORY -- name: golang-demo-actions - plugin: githubactions-golang - dependsOn: ["go-webapp-repo.github-repo-scaffolding-golang"] - options: - owner: ${{go-webapp-repo.github-repo-scaffolding-golang.outputs.owner}} - repo: ${{go-webapp-repo.github-repo-scaffolding-golang.outputs.repo}} - language: - name: go - version: "1.18" - branch: main - build: - enable: True - test: - enable: True - coverage: - enable: True - docker: - enable: False -``` - -Pay attention to the `${{ xxx }}` part in the example. `${{ TOOL_NAME.PLUGIN.outputs.var}}` is the syntax for using an output. diff --git a/docs/plugins/github-repo-scaffolding-golang.zh.md b/docs/plugins/github-repo-scaffolding-golang.zh.md deleted file mode 100644 index de13df617..000000000 --- a/docs/plugins/github-repo-scaffolding-golang.zh.md +++ /dev/null @@ -1,3 +0,0 @@ -# github-repo-scaffolding-golang 插件 - -todo diff --git a/docs/plugins/githubactions-golang.md b/docs/plugins/githubactions-golang.md index 497e2e14d..52643c385 100644 --- a/docs/plugins/githubactions-golang.md +++ b/docs/plugins/githubactions-golang.md @@ -25,22 +25,22 @@ _If Docker image build/push is enabled (see the example below), you also need to Some parameters are optional. See the default values and optional parameters in the example above. -## Use Together with the `github-repo-scaffolding-golang` Plugin +## Use Together with the `repo-scaffolding` Plugin -This plugin can be used together with the `github-repo-scaffolding-golang` plugin (see document [here](./github-repo-scaffolding-golang.md).) +This plugin can be used together with the `repo-scaffolding` plugin (see document [here](./repo-scaffolding.md).) -For example, you can first use `github-repo-scaffolding-golang` to bootstrap a Golang repo, then use this plugin to set up basic GitHub Actions CI workflows. In this scenario: +For example, you can first use `repo-scaffolding` to bootstrap a Golang repo, then use this plugin to set up basic GitHub Actions CI workflows. In this scenario: -- This plugin can specify `github-repo-scaffolding-golang` as a dependency, so that the dependency is first satisfied before executing this plugin. -- This plugin can refer to `github-repo-scaffolding-golang`'s output to reduce copy/paste human error. +- This plugin can specify `repo-scaffolding` as a dependency, so that the dependency is first satisfied before executing this plugin. +- This plugin can refer to `repo-scaffolding`'s output to reduce copy/paste human error. See the example below: ```yaml --- tools: -- name: github-repo-scaffolding-golang - instanceID: default +- name: repo-scaffolding + instanceID: golang-github options: owner: IronCore864 repo: go-webapp-devstream-demo @@ -48,10 +48,10 @@ tools: image_repo: ironcore864/go-webapp-devstream-demo - name: githubactions-golang instanceID: default - dependsOn: ["github-repo-scaffolding-golang.default"] + dependsOn: ["repo-scaffolding.golang-github"] options: - owner: ${{github-repo-scaffolding-golang.default.outputs.owner}} - repo: ${{github-repo-scaffolding-golang.default.outputs.repo}} + owner: ${{repo-scaffolding.golang-github.outputs.owner}} + repo: ${{repo-scaffolding.golang-github.outputs.repo}} language: name: go version: "1.18" @@ -67,12 +67,12 @@ tools: registry: type: dockerhub username: [[ dockerhubUsername ]] - repository: ${{github-repo-scaffolding-golang.default.outputs.repo}} + repository: ${{repo-scaffolding.golang-github.outputs.repo}} ``` In the example above: -- We put `ggithub-repo-scaffolding-golang.default` as dependency by using the `dependsOn` keyword. -- We used `github-repo-scaffolding-golang.default`'s output as input for the `githubactions-golang` plugin. +- We put `repo-scaffolding.golang-github` as dependency by using the `dependsOn` keyword. +- We used `repo-scaffolding.golang-github`'s output as input for the `githubactions-golang` plugin. Pay attention to the `${{ xxx }}` part in the example. `${{ TOOL_NAME.TOOL_INSTANCE_ID.outputs.var}}` is the syntax for using an output. diff --git a/docs/plugins/gitlab-repo-scaffolding-golang.md b/docs/plugins/gitlab-repo-scaffolding-golang.md deleted file mode 100644 index df6cf2582..000000000 --- a/docs/plugins/gitlab-repo-scaffolding-golang.md +++ /dev/null @@ -1,47 +0,0 @@ -# gitlab-repo-scaffolding-golang Plugin - -This plugin bootstraps a GitLab repo with scaffolding code for a Golang web application. - -_This plugin depends on the following environment variable:_ - -- GITLAB_TOKEN - -Set it before using this plugin. - -If you don't know how to create this token, check out: -- [GitLab Personal Access Tokens](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) - -*Tips:* - -*1. If you run `dtm delete`, the repo on GitLab will be completely removed.* - -*2. If the `Update` interface is called, the repo on GitLab will be completely removed and recreated. - -## Usage - -**Please note that the `owner` parameter is case-sensitive.** - -```yaml ---8<-- "gitlab-repo-scaffolding-golang.yaml" -``` - -Replace the following from the config above: - -- `YOUR_GITLAB_USERNAME` -- `YOUR_ORG_OR_GROUP_NAME` -- `YOUR_REPO_NAME` -- `YOUR_DOCKERHUB_USERNAME` -- `YOUR_DOCKERHUB_REPOSITORY` - -The "branch" in the example above is "main", but you can adjust accordingly. - -You have to specify either "owner" or "org". - -## Outputs - -This plugin has four outputs: - -- `org` -- `owner` -- `repo` -- `repoURL` (example: "https://gitlab.com/ironcore864/dtm-app-test.git") diff --git a/docs/plugins/gitlab-repo-scaffolding-golang.zh.md b/docs/plugins/gitlab-repo-scaffolding-golang.zh.md deleted file mode 100644 index a2f11fa06..000000000 --- a/docs/plugins/gitlab-repo-scaffolding-golang.zh.md +++ /dev/null @@ -1,3 +0,0 @@ -# gitlab-repo-scaffolding-golang 插件 - -todo diff --git a/docs/plugins/plugins-list.md b/docs/plugins/plugins-list.md index 7f110a468..b4342f472 100644 --- a/docs/plugins/plugins-list.md +++ b/docs/plugins/plugins-list.md @@ -5,8 +5,7 @@ | Issue Tracking | trello-github-integ | Trello/GitHub integration | [doc](trello-github-integ.md) | | Issue Tracking | trello | Trello | [doc](trello.md) | | Issue Tracking | jira-github-integ | Jira/GitHub integration | [doc](jira-github-integ.md) | -| Source Code Management | github-repo-scaffolding-golang | Go WebApp scaffolding | [doc](github-repo-scaffolding-golang.md) | -| Source Code Management | gitlab-repo-scaffolding-golang | Go WebApp scaffolding | [doc](gitlab-repo-scaffolding-golang.md) | +| Source Code Management | repo-scaffolding | App scaffolding | [doc](repo-scaffolding.md) | | CI | jenkins | Jenkins installation | [doc](jenkins.md) | | CI | githubactions-golang | GitHub Actions CI for Golang | [doc](githubactions-golang.md) | | CI | githubactions-python | GitHub Actions CI for Python | [doc](githubactions-python.md) | @@ -27,7 +26,7 @@ $ dtm list plugins argocd argocdapp devlake -github-repo-scaffolding-golang +repo-scaffolding githubactions-golang githubactions-nodejs githubactions-python diff --git a/docs/plugins/repo-scaffolding.md b/docs/plugins/repo-scaffolding.md index 76cf4b0c9..9129df8b1 100644 --- a/docs/plugins/repo-scaffolding.md +++ b/docs/plugins/repo-scaffolding.md @@ -4,19 +4,23 @@ This plugin bootstraps a GitHub or GitLab repo with scaffolding code for a web a ## Requirement -This plugin depends on the following environment variable: +This plugin need fllowing config base on your repo type: -- GITHUB_TOKEN +### GitHub -Set it before using this plugin. +- GITHUB_TOKEN: Set it before using this plugin.If you don't know how to create this token, check out [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token). -If you don't know how to create this token, check out: +### GitLab -- [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) +- GITLAB_TOKEN: Please set the environment variable before using the plugin. If you do not know how to create the token, Can view the document [Personal access tokens](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html). + +- `destination_repo.baseUrl`: If you are using a self-built GitLab repository, set this configuration to the URL of the self-built GitLab. + +- `destination_repo.visibility`: This configuration is used to set the permissions of the new repository. The options are `public`, `private`, and `internal`. *Tips:* -- If you run `dtm delete`, the repo on GitHub will be completely removed. +- If you run `dtm delete`, the repo will be completely removed. - If the `Update` interface is called, the repo will be completely removed and recreated. @@ -36,12 +40,13 @@ Replace the following from the config above: This configuration is used for the target repo, it includes the following config. -- `YOUR_GITHUB_USERNAME` -- `YOUR_GITHUB_ORGANIZATION_NAME` -- `YOUR_GITHUB_REPO_NAME` -- `YOUR_GITHUB_REPO_MAIN_BRANCH` +- `YOUR_DESTINATION_USERNAME` +- `YOUR_DESTINATION_ORGANIZATION_NAME` +- `YOUR_DESTINATION_REPO_NAME` +- `YOUR_DESTINATION_REPO_MAIN_BRANCH` +- `YOUR_DESTINATION_REPO_TYPE` -Currently, `owner`, `org`, and `repo` are mandatory, `branch` has the default value "main". +Currently, `owner`, `org`, and `repo` are mandatory, `branch` has the default value "main", `repo_type` support `gitlab` and `github` for now. ### source_repo @@ -49,12 +54,9 @@ This configuration is used for the source scaffolding repoI(only supports GitHub - `YOUR_TEMPLATE_REPO_ORG` - `YOUR_TEMPLATE_REPO_NAME` +- `YOUR_TEMPLATE_REPO_TYPE` -All the parameters in the example above are mandatory for now. - -### repo_type - -This configuration is used for destination_repo location, `gitlab` and `github` can be used for now. If you set it to "github", the generated repo will be pushed to GitHub. If you set it to "gitlab", the generated repo will be pushed to GitLab by your config. +All the parameters in the example above are mandatory for now, `repo_type` only support `github` for now. ### vars @@ -94,10 +96,11 @@ tools: org: "" repo: dtm-test-golang branch: main - repo_type: github + repo_type: github source_repo: org: devstream-io repo: dtm-scaffolding-golang + repo_type: github vars: ImageRepo: dtm-test/golang-repo ``` @@ -116,10 +119,13 @@ tools: org: "" repo: dtm-test-java branch: main - repo_type: github + baseUrl: 127.0.0.1:30001 + visibility: public + repo_type: gitlab source_repo: org: spring-guides repo: gs-spring-boot + repo_type: github ``` this config will create `dtm-test-java` repo for user test_owner in GitHub. diff --git a/docs/plugins/repo-scaffolding.zh.md b/docs/plugins/repo-scaffolding.zh.md index b8620a0f3..6e7c9e329 100644 --- a/docs/plugins/repo-scaffolding.zh.md +++ b/docs/plugins/repo-scaffolding.zh.md @@ -4,19 +4,23 @@ ## 运行需求 -这个插件运行需要设置以下环境变量: +这个插件基于你使用的代码仓库类型需要设置以下配置: -- GITHUB_TOKEN +### GitHub -在使用插件之前请先设置这个环境变量,如果你不知道如何获取这个 token,可以查看以下文档: +- GITHUB_TOKEN: 在使用插件之前请先设置这个环境变量,如果你不知道如何获取这个 token,可以查看文档 [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)。 -- [Creating a personal access token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) +### GitLab + +- GITLAB_TOKEN: 在使用插件之前请先设置这个环境变量,如果你不知道如何获取这个 token,可以查看文档 [Personal access tokens](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html)。 +- `destination_repo.baseUrl`:如果你使用的是自建的 GitLab 仓库,需要将该配置设置为自建 GItLab 的 URL 地址。 +- `destination_repo.visibility`:此配置用于设置新建仓库的权限,支持的选项有 `public`, `private` 和 `internal`。 *注意:* -- 如果你执行 `dtm delete` 命令,这个 GitHub 上的仓库将会被删除。 +- 如果你执行 `dtm delete` 命令,这个仓库将会被删除。 -- 如果你执行 `dtm update` 命令, 这个 GitHub 上的仓库将会被删除然后重新创建。 +- 如果你执行 `dtm update` 命令, 这个仓库将会被删除然后重新创建。 - 对于 `repo-scaffolding` 插件,目前只需要 token 有 `repo`, `delete_repo` 权限即可。 @@ -34,12 +38,13 @@ 这个是目标仓库的配置,包括以下几个配置项: -- `YOUR_GITHUB_USERNAME` -- `YOUR_GITHUB_ORGANIZATION_NAME` -- `YOUR_GITHUB_REPO_NAME` -- `YOUR_GITHUB_REPO_MAIN_BRANCH` +- `YOUR_DESTINATION_USERNAME` +- `YOUR_DESTINATION_ORGANIZATION_NAME` +- `YOUR_DESTINATION_REPO_NAME` +- `YOUR_DESTINATION_REPO_MAIN_BRANCH` +- `YOUR_DESTINATION_REPO_TYPE` -`owner`,`org` 和 `repo` 目前是必填的,`branch` 的默认值是 "main"。 +`owner`,`org` 和 `repo` 目前是必填的,`branch` 的默认值是 "main",`repo_type` 配置目前支持 `gitlab` 和 `github`。 ### source_repo @@ -47,12 +52,9 @@ - `YOUR_TEMPLATE_REPO_ORG` - `YOUR_TEMPLATE_REPO_NAME` +- `YOUR_TEMPLATE_REPO_TYPE` -目前这两个配置项都是必填的。 - -### repo_type - -这个配置用于设置推送的目标仓库类型,目前支持 `GitLab` 和 `github`。 +目前这两个配置项都是必填的,`repo_type` 配置目前只支持 `github`。 ### vars @@ -92,10 +94,11 @@ tools: org: "" repo: dtm-test-golang branch: main - repo_type: github + repo_type: github source_repo: org: devstream-io repo: dtm-scaffolding-golang + repo_type: github vars: ImageRepo: dtm-test/golang-repo ``` @@ -114,13 +117,16 @@ tools: org: "" repo: dtm-test-java branch: main - repo_type: github + baseUrl: 127.0.0.1:30001 + visibility: public + repo_type: gitlab source_repo: org: spring-guides repo: gs-spring-boot + repo_type: github ``` -这个配置会在 GitHub 为用户 test_owner 创建 `dtm-test-java` 仓库,使用的是 Spring 官方的 `spring-guides/gs-spring-boot` 仓库。 +这个配置会在 GitLab 为用户 test_owner 创建 `dtm-test-java` 仓库,使用的是 Spring 官方的 `spring-guides/gs-spring-boot` 仓库。 ## Outputs diff --git a/docs/quickstart.md b/docs/quickstart.md index f51dfdb02..1d8c92c13 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -70,19 +70,19 @@ and you should see similar output to the following: ``` 2022-06-30 11:21:48 ℹ [INFO] Got Backend from config: local 2022-06-30 11:21:48 ℹ [INFO] Using dir <.devstream> to store plugins. -2022-06-30 11:21:48 ℹ [INFO] Downloading: [github-repo-scaffolding-golang-darwin-arm64_0.7.0.so] ... +2022-06-30 11:21:48 ℹ [INFO] Downloading: [repo-scaffolding-darwin-arm64_0.7.0.so] ... 15.05 MiB / 15.05 MiB [================================] 100.00% 21.17 MiB/s 0s -2022-06-30 11:21:49 ✔ [SUCCESS] [github-repo-scaffolding-golang-darwin-arm64_0.7.0.so] download succeeded. -2022-06-30 11:21:49 ℹ [INFO] Downloading: [github-repo-scaffolding-golang-darwin-arm64_0.7.0.md5] ... +2022-06-30 11:21:49 ✔ [SUCCESS] [repo-scaffolding-darwin-arm64_0.7.0.so] download succeeded. +2022-06-30 11:21:49 ℹ [INFO] Downloading: [repo-scaffolding-darwin-arm64_0.7.0.md5] ... 33 B / 33 B [==========================================] 100.00% 35.29 KiB/s 0s -2022-06-30 11:21:49 ✔ [SUCCESS] [github-repo-scaffolding-golang-darwin-arm64_0.7.0.md5] download succeeded. -2022-06-30 11:21:49 ℹ [INFO] Plugin: github-repo-scaffolding-golang-darwin-arm64_0.7.0.so doesn't match with .md5 and will be downloaded. -2022-06-30 11:21:49 ℹ [INFO] Downloading: [github-repo-scaffolding-golang-darwin-arm64_0.7.0.so] ... +2022-06-30 11:21:49 ✔ [SUCCESS] [repo-scaffolding-darwin-arm64_0.7.0.md5] download succeeded. +2022-06-30 11:21:49 ℹ [INFO] Plugin: repo-scaffolding-darwin-arm64_0.7.0.so doesn't match with .md5 and will be downloaded. +2022-06-30 11:21:49 ℹ [INFO] Downloading: [repo-scaffolding-darwin-arm64_0.7.0.so] ... 15.05 MiB / 15.05 MiB [================================] 100.00% 31.25 MiB/s 0s -2022-06-30 11:21:50 ✔ [SUCCESS] [github-repo-scaffolding-golang-darwin-arm64_0.7.0.so] download succeeded. -2022-06-30 11:21:50 ℹ [INFO] Downloading: [github-repo-scaffolding-golang-darwin-arm64_0.7.0.md5] ... +2022-06-30 11:21:50 ✔ [SUCCESS] [repo-scaffolding-darwin-arm64_0.7.0.so] download succeeded. +2022-06-30 11:21:50 ℹ [INFO] Downloading: [repo-scaffolding-darwin-arm64_0.7.0.md5] ... 33 B / 33 B [==========================================] 100.00% 43.43 KiB/s 0s -2022-06-30 11:21:50 ✔ [SUCCESS] [github-repo-scaffolding-golang-darwin-arm64_0.7.0.md5] download succeeded. +2022-06-30 11:21:50 ✔ [SUCCESS] [repo-scaffolding-darwin-arm64_0.7.0.md5] download succeeded. 2022-06-30 11:21:50 ℹ [INFO] Downloading: [githubactions-golang-darwin-arm64_0.7.0.so] ... 17.49 MiB / 17.49 MiB [================================] 100.00% 31.18 MiB/s 0s 2022-06-30 11:21:51 ✔ [SUCCESS] [githubactions-golang-darwin-arm64_0.7.0.so] download succeeded. @@ -124,7 +124,7 @@ You should see similar output to the following 2022-06-30 11:25:47 ℹ [INFO] Got Backend from config: local 2022-06-30 11:25:47 ℹ [INFO] Using dir <.devstream> to store plugins. 2022-06-30 11:25:47 ℹ [INFO] Using local backend. State file: devstream.state. -2022-06-30 11:25:47 ℹ [INFO] Tool (github-repo-scaffolding-golang/default) found in config but doesn't exist in the state, will be created. +2022-06-30 11:25:47 ℹ [INFO] Tool (repo-scaffolding/default) found in config but doesn't exist in the state, will be created. 2022-06-30 11:25:47 ℹ [INFO] Tool (githubactions-golang/default) found in config but doesn't exist in the state, will be created. Continue? [y/n] Enter a value (Default is n): y @@ -132,9 +132,9 @@ Enter a value (Default is n): y 2022-06-30 11:26:20 ℹ [INFO] Start executing the plan. 2022-06-30 11:26:20 ℹ [INFO] Changes count: 2. 2022-06-30 11:26:20 ℹ [INFO] -------------------- [ Processing progress: 1/2. ] -------------------- -2022-06-30 11:26:20 ℹ [INFO] Processing: (github-repo-scaffolding-golang/default) -> Create ... +2022-06-30 11:26:20 ℹ [INFO] Processing: (repo-scaffolding/default) -> Create ... 2022-06-30 11:26:24 ℹ [INFO] The repo go-webapp-devstream-demo has been created. -2022-06-30 11:26:37 ✔ [SUCCESS] Tool (github-repo-scaffolding-golang/default) Create done. +2022-06-30 11:26:37 ✔ [SUCCESS] Tool (repo-scaffolding/default) Create done. 2022-06-30 11:26:37 ℹ [INFO] -------------------- [ Processing progress: 2/2. ] -------------------- 2022-06-30 11:26:37 ℹ [INFO] Processing: (githubactions-golang/default) -> Create ... 2022-06-30 11:26:38 ℹ [INFO] Creating GitHub Actions workflow pr-builder.yml ... @@ -173,7 +173,7 @@ input `y` the same just like you did in the previous steps, and you should see s 2022-06-30 11:31:01 ℹ [INFO] Using dir <.devstream> to store plugins. 2022-06-30 11:31:01 ℹ [INFO] Using local backend. State file: devstream.state. 2022-06-30 11:31:01 ℹ [INFO] Tool (githubactions-golang/default) will be deleted. -2022-06-30 11:31:01 ℹ [INFO] Tool (github-repo-scaffolding-golang/default) will be deleted. +2022-06-30 11:31:01 ℹ [INFO] Tool (repo-scaffolding/default) will be deleted. Continue? [y/n] Enter a value (Default is n): y @@ -188,10 +188,10 @@ Enter a value (Default is n): y 2022-06-30 11:31:06 ℹ [INFO] Prepare to delete 'githubactions-golang_default' from States. 2022-06-30 11:31:06 ✔ [SUCCESS] Tool (githubactions-golang/default) delete done. 2022-06-30 11:31:06 ℹ [INFO] -------------------- [ Processing progress: 2/2. ] -------------------- -2022-06-30 11:31:06 ℹ [INFO] Processing: (github-repo-scaffolding-golang/default) -> Delete ... +2022-06-30 11:31:06 ℹ [INFO] Processing: (repo-scaffolding/default) -> Delete ... 2022-06-30 11:31:06 ✔ [SUCCESS] GitHub repo go-webapp-devstream-demo removed. -2022-06-30 11:31:06 ℹ [INFO] Prepare to delete 'github-repo-scaffolding-golang_default' from States. -2022-06-30 11:31:06 ✔ [SUCCESS] Tool (github-repo-scaffolding-golang/default) delete done. +2022-06-30 11:31:06 ℹ [INFO] Prepare to delete 'repo-scaffolding_default' from States. +2022-06-30 11:31:06 ✔ [SUCCESS] Tool (repo-scaffolding/default) delete done. 2022-06-30 11:31:06 ℹ [INFO] -------------------- [ Processing done. ] -------------------- 2022-06-30 11:31:06 ✔ [SUCCESS] All plugins deleted successfully. 2022-06-30 11:31:06 ✔ [SUCCESS] Delete finished. diff --git a/examples/gitops.yaml b/examples/gitops.yaml index 10c9b9b3c..c61a5218e 100644 --- a/examples/gitops.yaml +++ b/examples/gitops.yaml @@ -22,17 +22,24 @@ argocdDeployTimeout: 10m --- # plugins config tools: - - name: github-repo-scaffolding-golang - instanceID: default + - name: repo-scaffolding + instanceID: golang-github options: - owner: [[ githubUsername ]] - org: "" - repo: [[ repoName ]] - branch: [[ defaultBranch ]] - image_repo: [[ dockerhubUsername ]]/[[ repoName ]] + destination_repo: + owner: [[ githubUsername ]] + org: "" + repo: [[ repoName ]] + branch: [[ defaultBranch ]] + repo_type: github + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github + vars: + ImageRepo: "[[ dockerhubUsername ]]/[[ repoName ]]" - name: jira-github-integ instanceID: default - dependsOn: [ "github-repo-scaffolding-golang.default" ] + dependsOn: [ "repo-scaffolding.golang-github" ] options: owner: [[ githubUsername ]] repo: [[ repoName ]] @@ -42,11 +49,11 @@ tools: branch: main - name: githubactions-golang instanceID: default - dependsOn: [ "github-repo-scaffolding-golang.default" ] + dependsOn: [ "repo-scaffolding.golang-github" ] options: - owner: ${{github-repo-scaffolding-golang.default.outputs.owner}} + owner: ${{repo-scaffolding.golang-github.outputs.owner}} org: "" - repo: ${{github-repo-scaffolding-golang.default.outputs.repo}} + repo: ${{repo-scaffolding.golang-github.outputs.repo}} language: name: go version: "1.18" @@ -66,7 +73,7 @@ tools: registry: type: dockerhub username: [[ dockerhubUsername ]] - repository: ${{github-repo-scaffolding-golang.default.outputs.repo}} + repository: ${{repo-scaffolding.golang-github.outputs.repo}} - name: argocd instanceID: default options: @@ -83,15 +90,15 @@ tools: upgradeCRDs: true - name: argocdapp instanceID: default - dependsOn: ["argocd.default", "github-repo-scaffolding-golang.default"] + dependsOn: ["argocd.default", "repo-scaffolding.golang-github"] options: app: - name: ${{github-repo-scaffolding-golang.default.outputs.repo}} + name: ${{repo-scaffolding.golang-github.outputs.repo}} namespace: [[ argocdNameSpace ]] destination: server: https://kubernetes.default.svc namespace: default source: valuefile: values.yaml - path: helm/${{github-repo-scaffolding-golang.default.outputs.repo}} - repoURL: ${{github-repo-scaffolding-golang.default.outputs.repoURL}} + path: helm/${{repo-scaffolding.golang-github.outputs.repo}} + repoURL: ${{repo-scaffolding.golang-github.outputs.repoURL}} diff --git a/examples/quickstart.yaml b/examples/quickstart.yaml index 2a3bc8b84..a8bb96759 100644 --- a/examples/quickstart.yaml +++ b/examples/quickstart.yaml @@ -10,16 +10,24 @@ state: # state config, backend can be local or s3 --- # plugins config tools: - - name: github-repo-scaffolding-golang - instanceID: default + - name: repo-scaffolding + instanceID: golang-github options: - owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE - repo: go-webapp-devstream-demo - branch: main - image_repo: YOUR_DOCKER_USERNAME/go-webapp-devstream-demo + destination_repo: + owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE + org: "" + repo: go-webapp-devstream-demo + branch: main + repo_type: github + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github + vars: + ImageRepo: YOUR_DOCKER_USERNAME/go-webapp-devstream-demo - name: githubactions-golang instanceID: default - dependsOn: ["github-repo-scaffolding-golang.default"] + dependsOn: ["repo-scaffolding.golang-github"] options: owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE repo: go-webapp-devstream-demo diff --git a/internal/pkg/configloader/config_test.go b/internal/pkg/configloader/config_test.go index 872361f13..57b54ebc9 100644 --- a/internal/pkg/configloader/config_test.go +++ b/internal/pkg/configloader/config_test.go @@ -18,7 +18,7 @@ var _ = Describe("Config", func() { BeforeEach(func() { validToolBytes = []byte(` tools: - - name: github-repo-scaffolding-golang + - name: repo-scaffolding instanceID: default options: owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE @@ -134,11 +134,11 @@ state: notValidContent := []byte(` --- tools: - - name: github-repo-scaffolding-golang + - name: repo-scaffolding instanceID: default --- tools: - - name: github-repo-scaffolding-golang + - name: repo-scaffolding instanceID: default`) _, _, _, err := SplitConfigFileBytes(notValidContent) Expect(err).Error().Should(HaveOccurred()) @@ -168,7 +168,7 @@ tools: It("should return false if config type not valid", func() { notValidType := []byte(` test: - - name: github-repo-scaffolding-golang + - name: repo-scaffolding instanceID: default`) isValid, err := checkConfigType(notValidType, "core") Expect(err).Error().ShouldNot(HaveOccurred()) diff --git a/internal/pkg/configloader/toolconfig_test.go b/internal/pkg/configloader/toolconfig_test.go index 3118cfe26..a4871a91a 100644 --- a/internal/pkg/configloader/toolconfig_test.go +++ b/internal/pkg/configloader/toolconfig_test.go @@ -46,7 +46,7 @@ var _ = Describe("Tool Config", func() { Context("NewToolWithToolConfigBytesAndVarsConfigBytes func", func() { toolConfigBytes := []byte(` tools: - - name: github-repo-scaffolding-golang + - name: repo-scaffolding instanceID: 0 options: owner: [[ owner ]] diff --git a/internal/pkg/develop/plugin/template/special_plugins.go b/internal/pkg/develop/plugin/template/special_plugins.go index df552feaa..e45af11cd 100644 --- a/internal/pkg/develop/plugin/template/special_plugins.go +++ b/internal/pkg/develop/plugin/template/special_plugins.go @@ -13,11 +13,9 @@ func NewSpecialPlugin(dirName string, packageName string) *SpecialPlugin { } var SpecialPluginNameMap = map[string]*SpecialPlugin{ - "gitlabci-golang": NewSpecialPlugin("gitlabci/golang", "gitlabci"), - "gitlabci-generic": NewSpecialPlugin("gitlabci/generic", "generic"), - "gitlab-repo-scaffolding-golang": NewSpecialPlugin("reposcaffolding/github/golang", "golang"), - "github-repo-scaffolding-golang": NewSpecialPlugin("reposcaffolding/gitlab/golang", "golang"), - "githubactions-golang": NewSpecialPlugin("githubactions/golang", "golang"), - "githubactions-nodejs": NewSpecialPlugin("githubactions/nodejs", "nodejs"), - "githubactions-python": NewSpecialPlugin("githubactions/python", "python"), + "gitlabci-golang": NewSpecialPlugin("gitlabci/golang", "gitlabci"), + "gitlabci-generic": NewSpecialPlugin("gitlabci/generic", "generic"), + "githubactions-golang": NewSpecialPlugin("githubactions/golang", "golang"), + "githubactions-nodejs": NewSpecialPlugin("githubactions/nodejs", "nodejs"), + "githubactions-python": NewSpecialPlugin("githubactions/python", "python"), } diff --git a/internal/pkg/plugin/common/reposcaffolding/download.go b/internal/pkg/plugin/common/reposcaffolding/download.go deleted file mode 100644 index a0c717406..000000000 --- a/internal/pkg/plugin/common/reposcaffolding/download.go +++ /dev/null @@ -1,22 +0,0 @@ -package reposcaffolding - -import "github.com/devstream-io/devstream/pkg/util/github" - -func download(org, repo, workpath string) error { - ghOption := &github.Option{ - Org: org, - Repo: repo, - NeedAuth: false, - WorkPath: workpath, - } - ghClient, err := github.NewClient(ghOption) - if err != nil { - return err - } - - if err = ghClient.DownloadLatestCodeAsZipFile(); err != nil { - return err - } - - return nil -} diff --git a/internal/pkg/plugin/common/reposcaffolding/helper.go b/internal/pkg/plugin/common/reposcaffolding/helper.go deleted file mode 100644 index ae8b9ad54..000000000 --- a/internal/pkg/plugin/common/reposcaffolding/helper.go +++ /dev/null @@ -1,139 +0,0 @@ -package reposcaffolding - -import ( - "fmt" - "io/fs" - "io/ioutil" - "os" - "path/filepath" - "regexp" - "strings" - "text/template" - - "github.com/devstream-io/devstream/pkg/util/github" - "github.com/devstream-io/devstream/pkg/util/log" -) - -func walkLocalRepoPath(workpath string, opts *Options, ghClient *github.Client) error { - repoPath := filepath.Join(workpath, fmt.Sprintf("%s-%s", TemplateRepo, MainBranch)) - appName := opts.Repo - outputDir := fmt.Sprintf("%s/%s", workpath, opts.Repo) - if err := os.MkdirAll(outputDir, os.ModePerm); err != nil { - return err - } - - if err := filepath.Walk(repoPath, func(path string, info fs.FileInfo, err error) error { - if err != nil { - log.Debugf("Walk error: %s.", err) - return err - } - - if info.IsDir() { - log.Debugf("Walk: found dir: %s.", path) - oldDir, err := replaceAppNameInPathStr(path, appName) - if err != nil { - return err - } - oldDirSections := strings.Split(oldDir, "/") - newDir := fmt.Sprintf("%s/%s", outputDir, strings.Join(oldDirSections[2:], "/")) - if err := os.MkdirAll(newDir, os.ModePerm); err != nil { - return err - } - log.Debugf("Walk: new output dir created: %s.", newDir) - return nil - } - - if strings.Contains(path, ".git/") { - log.Debugf("Walk: ignore file %s.", "./git/xxx") - return nil - } - - if strings.HasSuffix(path, "README.md") { - log.Debugf("Walk: ignore file %s.", "README.md") - return nil - } - - log.Debugf("Walk: found file: %s.", path) - - outputPath := fmt.Sprintf("%s/%s", outputDir, strings.Join(strings.Split(path, "/")[2:], "/")) - if outputPath, err = replaceAppNameInPathStr(outputPath, appName); err != nil { - return err - } - outputPath = strings.TrimSuffix(outputPath, ".tpl") - - if strings.Contains(path, "tpl") { - err = render(path, outputPath, opts) - if err != nil { - return err - } - } - - return nil - }); err != nil { - return err - } - - return nil -} - -func render(filePath, output string, opts *Options) error { - var owner = opts.Owner - if opts.Org != "" { - owner = opts.Org - } - - config := Config{ - AppName: opts.Repo, - ImageRepo: opts.ImageRepo, - Repo: Repo{ - Name: opts.Repo, - Owner: owner, - }, - } - - log.Debugf("Render filePath: %s.", filePath) - log.Debugf("Render config: %v.", config) - log.Debugf("Render output: %s.", output) - - textBytes, err := ioutil.ReadFile(filePath) - if err != nil { - return err - } - textStr := string(textBytes) - - tpl := template.New("github-repo-scaffolding-golang").Delims("[[", "]]") - parsed, err := tpl.Parse(textStr) - if err != nil { - log.Debugf("Template parse file failed: %s.", err) - return err - } - - f, err := os.Create(output) - if err != nil { - log.Error("create file: ", err) - return err - } - defer f.Close() - if err = parsed.Execute(f, config); err != nil { - log.Debugf("Template execution failed: %s.", err) - return err - } - - return nil -} - -func replaceAppNameInPathStr(filePath, appName string) (string, error) { - if !strings.Contains(filePath, AppNamePlaceHolder) { - return filePath, nil - } - - reg, err := regexp.Compile(AppNamePlaceHolder) - if err != nil { - return "", err - } - newFilePath := reg.ReplaceAllString(filePath, appName) - - log.Debugf("Replace file path place holder. Before: %s, after: %s.", filePath, newFilePath) - - return newFilePath, nil -} diff --git a/internal/pkg/plugin/common/reposcaffolding/options.go b/internal/pkg/plugin/common/reposcaffolding/options.go deleted file mode 100644 index 78a331b9e..000000000 --- a/internal/pkg/plugin/common/reposcaffolding/options.go +++ /dev/null @@ -1,34 +0,0 @@ -package reposcaffolding - -import ( - "fmt" - "path/filepath" - - "github.com/devstream-io/devstream/pkg/util/validator" -) - -type Options struct { - Owner string `validate:"required_without=Org"` - Org string `validate:"required_without=Owner"` - Repo string `validate:"required"` - Branch string `validate:"required"` - PathWithNamespace string - BaseURL string `validate:"omitempty,url"` - ImageRepo string `mapstructure:"image_repo"` - Visibility string `validate:"omitempty,oneof=public private internal"` -} - -// Validate validates the options provided by the core. -func Validate(opts *Options) []error { - retErrors := make([]error, 0) - retErrors = append(retErrors, validator.Struct(opts)...) - // set PathWithNamespace for GitLab. GitHub won't need to use this - // opts.PathWithNamespace = fmt.Sprintf("%s/%s", opts.Owner, opts.Repo) - if opts.Org != "" { - opts.PathWithNamespace = filepath.Clean(fmt.Sprintf("%s/%s", opts.Org, opts.Repo)) - } else { - opts.PathWithNamespace = filepath.Clean(fmt.Sprintf("%s/%s", opts.Owner, opts.Repo)) - } - - return retErrors -} diff --git a/internal/pkg/plugin/common/reposcaffolding/reposcaffolding.go b/internal/pkg/plugin/common/reposcaffolding/reposcaffolding.go deleted file mode 100644 index e14702560..000000000 --- a/internal/pkg/plugin/common/reposcaffolding/reposcaffolding.go +++ /dev/null @@ -1,56 +0,0 @@ -package reposcaffolding - -import ( - "path/filepath" - - "github.com/devstream-io/devstream/pkg/util/github" - "github.com/devstream-io/devstream/pkg/util/log" - "github.com/devstream-io/devstream/pkg/util/zip" -) - -const ( - TemplateRepo = "dtm-scaffolding-golang" - TemplateOrg = "devstream-io" - TransitBranch = "init-with-devstream" - MainBranch = "main" - AppNamePlaceHolder = "_app_name_" -) - -type Config struct { - AppName string - ImageRepo string - Repo Repo -} - -type Repo struct { - Name string - Owner string -} - -func CreateAndRenderLocalRepo(workpath string, opts *Options) error { - ghOptions := &github.Option{ - Owner: opts.Owner, - Org: opts.Org, - Repo: opts.Repo, - NeedAuth: true, - } - ghClient, err := github.NewClient(ghOptions) - if err != nil { - return err - } - - if err := download(TemplateOrg, TemplateRepo, workpath); err != nil { - return err - } - - if err := zip.UnZip(filepath.Join(workpath, github.DefaultLatestCodeZipfileName), workpath); err != nil { - return err - } - - if retErr := walkLocalRepoPath(workpath, opts, ghClient); retErr != nil { - log.Debugf("Failed to walk local repo-path: %s.", retErr) - return retErr - } - - return nil -} diff --git a/internal/pkg/plugin/reposcaffolding/delete.go b/internal/pkg/plugin/reposcaffolding/delete.go index 5265cded8..7a00e45da 100644 --- a/internal/pkg/plugin/reposcaffolding/delete.go +++ b/internal/pkg/plugin/reposcaffolding/delete.go @@ -10,6 +10,7 @@ func Delete(options map[string]interface{}) (bool, error) { runner := &plugininstaller.Runner{ PreExecuteOperations: []plugininstaller.MutableOperation{ reposcaffolding.Validate, + reposcaffolding.SetDefaultTemplateRepo, }, ExecuteOperations: []plugininstaller.BaseOperation{ reposcaffolding.DeleteRepo, diff --git a/internal/pkg/plugin/reposcaffolding/github/golang/create.go b/internal/pkg/plugin/reposcaffolding/github/golang/create.go deleted file mode 100644 index 527af6f36..000000000 --- a/internal/pkg/plugin/reposcaffolding/github/golang/create.go +++ /dev/null @@ -1,47 +0,0 @@ -package golang - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/log" -) - -// Create installs github-repo-scaffolding-golang with provided options. -func Create(options map[string]interface{}) (map[string]interface{}, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return nil, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return nil, fmt.Errorf("options are illegal") - } - - return install(&opts) -} -func install(opts *rs.Options) (map[string]interface{}, error) { - defer func() { - if err := os.RemoveAll(DefaultWorkPath); err != nil { - log.Errorf("Failed to clear workpath %s: %s.", DefaultWorkPath, err) - } - }() - - err := rs.CreateAndRenderLocalRepo(DefaultWorkPath, opts) - if err != nil { - return nil, err - } - - if err := pushToRemote(filepath.Join(DefaultWorkPath, opts.Repo), opts); err != nil { - return nil, err - } - - return buildState(opts), nil -} diff --git a/internal/pkg/plugin/reposcaffolding/github/golang/delete.go b/internal/pkg/plugin/reposcaffolding/github/golang/delete.go deleted file mode 100644 index 28f57bbc4..000000000 --- a/internal/pkg/plugin/reposcaffolding/github/golang/delete.go +++ /dev/null @@ -1,46 +0,0 @@ -package golang - -import ( - "fmt" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/github" - "github.com/devstream-io/devstream/pkg/util/log" -) - -// Delete uninstalls github-repo-scaffolding-golang with provided options. -func Delete(options map[string]interface{}) (bool, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return false, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return false, fmt.Errorf("opts are illegal") - } - - return uninstall(&opts) -} - -func uninstall(opts *rs.Options) (bool, error) { - ghOptions := &github.Option{ - Owner: opts.Owner, - Org: opts.Org, - Repo: opts.Repo, - NeedAuth: true, - } - - ghClient, err := github.NewClient(ghOptions) - if err != nil { - return false, err - } - if err := ghClient.DeleteRepo(); err != nil { - return false, err - } - return true, nil -} diff --git a/internal/pkg/plugin/reposcaffolding/github/golang/golang.go b/internal/pkg/plugin/reposcaffolding/github/golang/golang.go deleted file mode 100644 index 4f968573c..000000000 --- a/internal/pkg/plugin/reposcaffolding/github/golang/golang.go +++ /dev/null @@ -1,158 +0,0 @@ -package golang - -import ( - "fmt" - "io/fs" - "io/ioutil" - "path/filepath" - "strings" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/github" - "github.com/devstream-io/devstream/pkg/util/log" -) - -const ( - DefaultWorkPath = ".github-repo-scaffolding-golang" - TransitBranch = "init-with-devstream" - DefaultMainBranch = "main" -) - -type Config struct { - AppName string - ImageRepo string - Repo Repo -} - -type Repo struct { - Name string - Owner string -} - -func pushToRemote(repoPath string, opts *rs.Options) error { - ghOptions := &github.Option{ - Owner: opts.Owner, - Org: opts.Org, - Repo: opts.Repo, - NeedAuth: true, - } - ghClient, err := github.NewClient(ghOptions) - if err != nil { - return err - } - - mainBranch := getMainBranchName(opts) - - var retErr error - // It's ok to give the opts.Org to CreateRepo() when create a repository for a authenticated user. - if err := ghClient.CreateRepo(opts.Org, mainBranch); err != nil { - log.Errorf("Failed to create repo: %s.", err) - return err - } - log.Infof("The repo %s has been created.", opts.Repo) - - err = ghClient.CreateFile([]byte(" "), ".gitignore", mainBranch) - if err != nil { - log.Debugf("Failed to add the first file: %s.", err) - return err - } - log.Debugf("Added the .gitignore file.") - - defer func() { - if retErr == nil { - return - } - // need to clean the repo created when retErr != nil - if err := ghClient.DeleteRepo(); err != nil { - log.Errorf("Failed to delete the repo %s: %s.", opts.Repo, err) - } - }() - - err = ghClient.NewBranch(mainBranch, TransitBranch) - if err != nil { - log.Debugf("Failed to create transit branch: %s", err) - return err - } - - if retErr = walkLocalRepoPath(repoPath, opts, ghClient); retErr != nil { - log.Debugf("Failed to walk local repo-path: %s.", retErr) - return retErr - } - - if retErr = mergeCommits(ghClient, mainBranch); retErr != nil { - log.Debugf("Failed to merge commits: %s.", retErr) - return retErr - } - - err = ghClient.DeleteBranch(TransitBranch) - if err != nil { - log.Debugf("Failed to delete transit branch: %s", err) - return err - } - - return nil -} - -func walkLocalRepoPath(repoPath string, opts *rs.Options, ghClient *github.Client) error { - if err := filepath.Walk(repoPath, func(path string, info fs.FileInfo, err error) error { - if err != nil { - log.Debugf("Walk error: %s.", err) - return err - } - - if info.IsDir() { - log.Debugf("Found dir: %s.", path) - return nil - } - - log.Debugf("Found file: %s.", path) - - content, err := ioutil.ReadFile(path) - if err != nil { - return err - } - - githubPath := strings.Join(strings.Split(path, "/")[2:], "/") - return ghClient.CreateFile(content, githubPath, TransitBranch) - }); err != nil { - return err - } - - return nil -} - -func mergeCommits(ghClient *github.Client, mainBranch string) error { - number, err := ghClient.NewPullRequest(TransitBranch, mainBranch) - if err != nil { - return err - } - - return ghClient.MergePullRequest(number, github.MergeMethodSquash) -} - -func getMainBranchName(opts *rs.Options) string { - if opts.Branch == "" { - return DefaultMainBranch - } - return opts.Branch -} - -func buildState(opts *rs.Options) map[string]interface{} { - res := make(map[string]interface{}) - res["owner"] = opts.Owner - res["org"] = opts.Org - res["repoName"] = opts.Repo - - outputs := make(map[string]interface{}) - outputs["owner"] = opts.Owner - outputs["org"] = opts.Org - outputs["repo"] = opts.Repo - if opts.Owner != "" { - outputs["repoURL"] = fmt.Sprintf("https://github.com/%s/%s.git", opts.Owner, opts.Repo) - } else { - outputs["repoURL"] = fmt.Sprintf("https://github.com/%s/%s.git", opts.Org, opts.Repo) - } - res["outputs"] = outputs - - return res -} diff --git a/internal/pkg/plugin/reposcaffolding/github/golang/read.go b/internal/pkg/plugin/reposcaffolding/github/golang/read.go deleted file mode 100644 index ee3008475..000000000 --- a/internal/pkg/plugin/reposcaffolding/github/golang/read.go +++ /dev/null @@ -1,73 +0,0 @@ -package golang - -import ( - "fmt" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/github" - "github.com/devstream-io/devstream/pkg/util/log" -) - -// Read check the health for github-repo-scaffolding-golang with provided param. -func Read(options map[string]interface{}) (map[string]interface{}, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return nil, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return nil, fmt.Errorf("opts are illegal") - } - - return buildReadState(&opts) -} - -func buildReadState(opts *rs.Options) (map[string]interface{}, error) { - ghOptions := &github.Option{ - Owner: opts.Owner, - Org: opts.Org, - Repo: opts.Repo, - NeedAuth: true, - } - - ghClient, err := github.NewClient(ghOptions) - if err != nil { - return nil, err - } - - repo, err := ghClient.GetRepoDescription() - if err != nil { - return nil, err - } - if repo == nil { - return nil, nil - } - - res := make(map[string]interface{}) - res["owner"] = opts.Owner - res["org"] = opts.Org - res["repoName"] = *repo.Name - - outputs := make(map[string]interface{}) - - if opts.Owner == "" { - outputs["owner"] = opts.Owner - } else { - outputs["owner"] = *repo.Owner.Login - } - if opts.Org == "" { - outputs["org"] = opts.Org - } else { - outputs["org"] = *repo.Organization.Login - } - outputs["repo"] = opts.Repo - outputs["repoURL"] = *repo.CloneURL - res["outputs"] = outputs - - return res, nil -} diff --git a/internal/pkg/plugin/reposcaffolding/github/golang/update.go b/internal/pkg/plugin/reposcaffolding/github/golang/update.go deleted file mode 100644 index 3cd185f87..000000000 --- a/internal/pkg/plugin/reposcaffolding/github/golang/update.go +++ /dev/null @@ -1,32 +0,0 @@ -package golang - -import ( - "fmt" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/log" -) - -// Update re-installs github-repo-scaffolding-golang with provided options. -func Update(options map[string]interface{}) (map[string]interface{}, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return nil, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return nil, fmt.Errorf("options are illegal") - } - - _, err := uninstall(&opts) - if err != nil { - return nil, err - } - - return install(&opts) -} diff --git a/internal/pkg/plugin/reposcaffolding/gitlab/golang/create.go b/internal/pkg/plugin/reposcaffolding/gitlab/golang/create.go deleted file mode 100644 index 3e5c7dae2..000000000 --- a/internal/pkg/plugin/reposcaffolding/gitlab/golang/create.go +++ /dev/null @@ -1,48 +0,0 @@ -package golang - -import ( - "fmt" - "os" - "path/filepath" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/log" -) - -// Create installs gitlab-repo-scaffolding-golang with provided options. -func Create(options map[string]interface{}) (map[string]interface{}, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return nil, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return nil, fmt.Errorf("options are illegal") - } - - return install(&opts) -} - -func install(opts *rs.Options) (map[string]interface{}, error) { - defer func() { - if err := os.RemoveAll(DefaultWorkPath); err != nil { - log.Errorf("Failed to clear workpath %s: %s.", DefaultWorkPath, err) - } - }() - - err := rs.CreateAndRenderLocalRepo(DefaultWorkPath, opts) - if err != nil { - return nil, err - } - - if err := pushToRemote(filepath.Join(DefaultWorkPath, opts.Repo), opts); err != nil { - return nil, err - } - - return buildState(opts), nil -} diff --git a/internal/pkg/plugin/reposcaffolding/gitlab/golang/delete.go b/internal/pkg/plugin/reposcaffolding/gitlab/golang/delete.go deleted file mode 100644 index a81b7fd4c..000000000 --- a/internal/pkg/plugin/reposcaffolding/gitlab/golang/delete.go +++ /dev/null @@ -1,41 +0,0 @@ -package golang - -import ( - "fmt" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/gitlab" - "github.com/devstream-io/devstream/pkg/util/log" -) - -func Delete(options map[string]interface{}) (bool, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return false, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return false, fmt.Errorf("opts are illegal") - } - - return uninstall(&opts) -} - -func uninstall(opts *rs.Options) (bool, error) { - c, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL)) - if err != nil { - return false, err - } - - if err := c.DeleteProject(opts.PathWithNamespace); err != nil { - log.Errorf("Failed to create repo: %s.", err) - return false, err - } - - return true, nil -} diff --git a/internal/pkg/plugin/reposcaffolding/gitlab/golang/golang.go b/internal/pkg/plugin/reposcaffolding/gitlab/golang/golang.go deleted file mode 100644 index 0e152d001..000000000 --- a/internal/pkg/plugin/reposcaffolding/gitlab/golang/golang.go +++ /dev/null @@ -1,140 +0,0 @@ -package golang - -import ( - "fmt" - "io/fs" - "io/ioutil" - "path/filepath" - "strings" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/gitlab" - "github.com/devstream-io/devstream/pkg/util/log" -) - -const ( - DefaultWorkPath = ".gitlab-repo-scaffolding-golang" - TransitBranch = "init-with-devstream" - DefaultMainBranch = "main" - DefaultCommitMessage = "initialized by DevStream" -) - -type Config struct { - AppName string - ImageRepo string - Repo Repo -} - -type Repo struct { - Name string - Owner string -} - -func pushToRemote(repoPath string, opts *rs.Options) error { - // create a GitLab client - c, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL)) - if err != nil { - return err - } - - var retErr error - createProjOpts := &gitlab.CreateProjectOptions{ - Name: opts.Repo, - Branch: opts.Branch, - Namespace: opts.Org, - Visibility: opts.Visibility, - } - - // create the project - if err := c.CreateProject(createProjOpts); err != nil { - log.Errorf("Failed to create repo: %s.", err) - return err - } - - log.Infof("The repo %s has been created.", opts.Repo) - - defer func() { - if retErr == nil { - return - } - log.Errorf("Failed to push to remote: %s.", retErr) - // need to clean the repo created when retErr != nil - if err := c.DeleteProject(opts.PathWithNamespace); err != nil { - log.Errorf("Failed to delete the repo %s: %s.", opts.PathWithNamespace, err) - } - }() - - if retErr = walkLocalRepoPath(repoPath, opts, c); retErr != nil { - log.Debugf("Failed to walk local repo-path: %s.", retErr) - return retErr - } - - return nil -} - -func walkLocalRepoPath(repoPath string, opts *rs.Options, c *gitlab.Client) error { - mainBranch := getMainBranchName(opts) - - var files = make(map[string][]byte) - - if err := filepath.Walk(repoPath, func(path string, info fs.FileInfo, err error) error { - if err != nil { - log.Debugf("Walk error: %s.", err) - return err - } - - if info.IsDir() { - log.Debugf("Found dir: %s.", path) - return nil - } - - log.Debugf("Found file: %s.", path) - - content, err := ioutil.ReadFile(path) - if err != nil { - return err - } - gitlabPath := strings.Join(strings.Split(path, "/")[2:], "/") - files[gitlabPath] = content - return nil - }); err != nil { - return err - } - - return c.CommitMultipleFiles(opts.PathWithNamespace, mainBranch, DefaultCommitMessage, files) -} - -func getMainBranchName(opts *rs.Options) string { - if opts.Branch == "" { - return DefaultMainBranch - } - return opts.Branch -} - -func buildState(opts *rs.Options) map[string]interface{} { - res := make(map[string]interface{}) - res["owner"] = opts.Owner - res["org"] = opts.Org - res["repoName"] = opts.Repo - - outputs := make(map[string]interface{}) - outputs["owner"] = opts.Owner - outputs["org"] = opts.Org - outputs["repo"] = opts.Repo - - var gitlabURL string - if opts.BaseURL != "" { - gitlabURL = opts.BaseURL - } else { - gitlabURL = gitlab.DefaultGitlabHost - } - - if opts.Org != "" { - outputs["repoURL"] = fmt.Sprintf("%s/%s/%s.git", gitlabURL, opts.Org, opts.Repo) - } else { - outputs["repoURL"] = fmt.Sprintf("%s/%s/%s.git", gitlabURL, opts.Owner, opts.Repo) - } - res["outputs"] = outputs - - return res -} diff --git a/internal/pkg/plugin/reposcaffolding/gitlab/golang/read.go b/internal/pkg/plugin/reposcaffolding/gitlab/golang/read.go deleted file mode 100644 index 52875c414..000000000 --- a/internal/pkg/plugin/reposcaffolding/gitlab/golang/read.go +++ /dev/null @@ -1,66 +0,0 @@ -package golang - -import ( - "fmt" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/gitlab" - "github.com/devstream-io/devstream/pkg/util/log" -) - -func Read(options map[string]interface{}) (map[string]interface{}, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return nil, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return nil, fmt.Errorf("opts are illegal") - } - - return buildReadState(&opts) -} - -func buildReadState(opts *rs.Options) (map[string]interface{}, error) { - c, err := gitlab.NewClient(gitlab.WithBaseURL(opts.BaseURL)) - if err != nil { - return nil, err - } - - project, err := c.DescribeProject(opts.PathWithNamespace) - if err != nil { - return nil, err - } - if project == nil { - return nil, nil - } - - res := make(map[string]interface{}) - outputs := make(map[string]interface{}) - - log.Debugf("GitLab Project is: %#v\n", project) - - if project.Owner != nil { - log.Debugf("GitLab project owner is: %#v.\n", project.Owner) - res["owner"] = project.Owner.Username - res["org"] = project.Owner.Organization - outputs["owner"] = project.Owner.Username - outputs["org"] = project.Owner.Organization - } else { - res["owner"] = opts.Owner - res["org"] = opts.Org - outputs["owner"] = opts.Owner - outputs["org"] = opts.Org - } - res["repoName"] = project.Name - outputs["repo"] = project.Name - outputs["repoURL"] = project.HTTPURLToRepo - res["outputs"] = outputs - - return res, nil -} diff --git a/internal/pkg/plugin/reposcaffolding/gitlab/golang/update.go b/internal/pkg/plugin/reposcaffolding/gitlab/golang/update.go deleted file mode 100644 index a4d1b8211..000000000 --- a/internal/pkg/plugin/reposcaffolding/gitlab/golang/update.go +++ /dev/null @@ -1,32 +0,0 @@ -package golang - -import ( - "fmt" - - "github.com/mitchellh/mapstructure" - - rs "github.com/devstream-io/devstream/internal/pkg/plugin/common/reposcaffolding" - "github.com/devstream-io/devstream/pkg/util/log" -) - -// Update re-installs gitlab-repo-scaffolding-golang with provided options. -func Update(options map[string]interface{}) (map[string]interface{}, error) { - var opts rs.Options - if err := mapstructure.Decode(options, &opts); err != nil { - return nil, err - } - - if errs := rs.Validate(&opts); len(errs) != 0 { - for _, e := range errs { - log.Errorf("Options error: %s.", e) - } - return nil, fmt.Errorf("options are illegal") - } - - _, err := uninstall(&opts) - if err != nil { - return nil, err - } - - return install(&opts) -} diff --git a/internal/pkg/plugin/reposcaffolding/read.go b/internal/pkg/plugin/reposcaffolding/read.go index 79089829b..8cf3cd2c9 100644 --- a/internal/pkg/plugin/reposcaffolding/read.go +++ b/internal/pkg/plugin/reposcaffolding/read.go @@ -10,6 +10,7 @@ func Read(options map[string]interface{}) (map[string]interface{}, error) { runner := &plugininstaller.Runner{ PreExecuteOperations: []plugininstaller.MutableOperation{ reposcaffolding.Validate, + reposcaffolding.SetDefaultTemplateRepo, }, GetStatusOperation: reposcaffolding.GetDynamicState, } diff --git a/internal/pkg/plugininstaller/reposcaffolding/dstrepo.go b/internal/pkg/plugininstaller/reposcaffolding/dstrepo.go index 5d20f7bf8..01f786e54 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/dstrepo.go +++ b/internal/pkg/plugininstaller/reposcaffolding/dstrepo.go @@ -8,6 +8,7 @@ import ( "github.com/devstream-io/devstream/internal/pkg/plugininstaller/util" "github.com/devstream-io/devstream/pkg/util/github" + "github.com/devstream-io/devstream/pkg/util/gitlab" "github.com/devstream-io/devstream/pkg/util/log" "github.com/devstream-io/devstream/pkg/util/template" ) @@ -19,7 +20,8 @@ type DstRepo struct { Repo string `validate:"required"` Branch string `validate:"required"` PathWithNamespace string - // TODO(steinliber): used for gitlab + RepoType string `validate:"oneof=gitlab github" mapstructure:"repo_type"` + // This is config for gitlab BaseURL string `validate:"omitempty,url"` Visibility string `validate:"omitempty,oneof=public private internal"` } @@ -115,16 +117,15 @@ func (d *DstRepo) createGithubClient(needAuth bool) (*github.Client, error) { return ghClient, nil } -// TODO(steinliber): add gitlab support, temporary comment code -// func (d *DstRepo) createGitlabClient() (*gitlab.Client, error) { -// return gitlab.NewClient(gitlab.WithBaseURL(d.BaseURL)) -// } - -// func (d *DstRepo) creategitlabOpts() *gitlab.CreateProjectOptions { -// return &gitlab.CreateProjectOptions{ -// Name: d.Repo, -// Branch: d.Branch, -// Namespace: d.Org, -// Visibility: d.Visibility, -// } -// } +func (d *DstRepo) createGitlabClient() (*gitlab.Client, error) { + return gitlab.NewClient(gitlab.WithBaseURL(d.BaseURL)) +} + +func (d *DstRepo) buildgitlabOpts() *gitlab.CreateProjectOptions { + return &gitlab.CreateProjectOptions{ + Name: d.Repo, + Branch: d.Branch, + Namespace: d.Org, + Visibility: d.Visibility, + } +} diff --git a/internal/pkg/plugininstaller/reposcaffolding/installer.go b/internal/pkg/plugininstaller/reposcaffolding/installer.go index 9bf8e2079..f91aa7d7f 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/installer.go +++ b/internal/pkg/plugininstaller/reposcaffolding/installer.go @@ -33,15 +33,16 @@ func InstallRepo(options plugininstaller.RawOptions) error { return err } + dstRepo := opts.DestinationRepo // 2. Push local repo to remote repoLoc := filepath.Join(dirName, opts.DestinationRepo.Repo) - switch opts.RepoType { + switch dstRepo.RepoType { case "github": err = opts.PushToRemoteGithub(repoLoc) case "gitlab": err = opts.PushToRemoteGitlab(repoLoc) default: - err = fmt.Errorf("scaffolding not support repo destination: %s", opts.RepoType) + err = fmt.Errorf("scaffolding not support repo destination: %s", dstRepo.RepoType) } if err != nil { return err @@ -51,18 +52,28 @@ func InstallRepo(options plugininstaller.RawOptions) error { // DeleteRepo will delete repo by options func DeleteRepo(options plugininstaller.RawOptions) error { + var err error opts, err := NewOptions(options) if err != nil { return err } - // 1. create ghClient - dstRepo := &opts.DestinationRepo - ghClient, err := dstRepo.createGithubClient(true) - if err != nil { - return err + dstRepo := opts.DestinationRepo + switch dstRepo.RepoType { + case "github": + // 1. create ghClient + ghClient, err := opts.DestinationRepo.createGithubClient(true) + if err != nil { + return err + } + // 2. delete github repo + return ghClient.DeleteRepo() + case "gitlab": + gLclient, err := dstRepo.createGitlabClient() + if err != nil { + return err + } + return gLclient.DeleteProject(dstRepo.PathWithNamespace) } - - // 2. delete github repo - return ghClient.DeleteRepo() + return fmt.Errorf("scaffolding not support repo destination: %s", dstRepo.RepoType) } diff --git a/internal/pkg/plugininstaller/reposcaffolding/option.go b/internal/pkg/plugininstaller/reposcaffolding/option.go index 894716616..d64e228ee 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/option.go +++ b/internal/pkg/plugininstaller/reposcaffolding/option.go @@ -10,12 +10,12 @@ import ( const ( transitBranch = "init-with-devstream" appNamePlaceHolder = "_app_name_" + defaultCommitMsg = "init with devstream" ) type Options struct { - RepoType string `validate:"oneof=gitlab github" mapstructure:"repo_type"` - SourceRepo SrcRepo `validate:"required" mapstructure:"source_repo"` - DestinationRepo DstRepo `validate:"required" mapstructure:"destination_repo"` + SourceRepo *SrcRepo `validate:"required" mapstructure:"source_repo"` + DestinationRepo *DstRepo `validate:"required" mapstructure:"destination_repo"` Vars map[string]interface{} } @@ -51,13 +51,45 @@ func (opts *Options) CreateAndRenderLocalRepo(workpath string) error { // PushToRemoteGitLab push local repo to remote gitlab repo func (opts *Options) PushToRemoteGitlab(repoPath string) error { - // TODO: add gitlab push func + dstRepo := opts.DestinationRepo + // 1. init gitlab client + c, err := dstRepo.createGitlabClient() + if err != nil { + log.Debugf("Gitlab push: init gitlab client failed %s", err) + return err + } + + // 2. create the project + if err := c.CreateProject(dstRepo.buildgitlabOpts()); err != nil { + log.Errorf("Failed to create repo: %s.", err) + return err + } + + // if encounter error, delete repo + var needRollBack bool + defer func() { + if !needRollBack { + return + } + // need to clean the repo created when retErr != nil + if err := c.DeleteProject(dstRepo.PathWithNamespace); err != nil { + log.Errorf("Failed to delete the repo %s: %s.", dstRepo.PathWithNamespace, err) + } + }() + + needRollBack, err = c.PushLocalPathToBranch( + repoPath, dstRepo.Branch, dstRepo.PathWithNamespace, defaultCommitMsg, + ) + if err != nil { + log.Errorf("Failed to push to remote: %s.", err) + return err + } return nil } // PushToRemoteGithub push local repo to remote github repo func (opts *Options) PushToRemoteGithub(repoPath string) error { - dstRepo := &opts.DestinationRepo + dstRepo := opts.DestinationRepo // 1. init github client ghClient, err := dstRepo.createGithubClient(true) if err != nil { diff --git a/internal/pkg/plugininstaller/reposcaffolding/srcrepo.go b/internal/pkg/plugininstaller/reposcaffolding/srcrepo.go index fb25ab0f5..778a756d9 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/srcrepo.go +++ b/internal/pkg/plugininstaller/reposcaffolding/srcrepo.go @@ -13,8 +13,9 @@ var srcDefaultBranch = "main" // SrcRepo describe how to get scaffolding repo type SrcRepo struct { - Repo string `validate:"required"` - Org string `validate:"required"` + Repo string `validate:"required"` + Org string `validate:"required"` + RepoType string `validate:"oneof=github" mapstructure:"repo_type"` } func (t *SrcRepo) DownloadRepo(workpath string) error { diff --git a/internal/pkg/plugininstaller/reposcaffolding/state.go b/internal/pkg/plugininstaller/reposcaffolding/state.go index 61b81c7f9..f63b59cd0 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/state.go +++ b/internal/pkg/plugininstaller/reposcaffolding/state.go @@ -4,6 +4,8 @@ import ( "fmt" "github.com/devstream-io/devstream/internal/pkg/plugininstaller" + "github.com/devstream-io/devstream/pkg/util/gitlab" + "github.com/devstream-io/devstream/pkg/util/log" ) func GetStaticState(options plugininstaller.RawOptions) (map[string]interface{}, error) { @@ -22,10 +24,26 @@ func GetStaticState(options plugininstaller.RawOptions) (map[string]interface{}, outputs["owner"] = dstRepo.Owner outputs["org"] = dstRepo.Org outputs["repo"] = dstRepo.Repo - if dstRepo.Owner != "" { - outputs["repoURL"] = fmt.Sprintf("https://github.com/%s/%s.git", dstRepo.Owner, dstRepo.Repo) - } else { - outputs["repoURL"] = fmt.Sprintf("https://github.com/%s/%s.git", dstRepo.Org, dstRepo.Repo) + + switch dstRepo.RepoType { + case "github": + if dstRepo.Owner != "" { + outputs["repoURL"] = fmt.Sprintf("https://github.com/%s/%s.git", dstRepo.Owner, dstRepo.Repo) + } else { + outputs["repoURL"] = fmt.Sprintf("https://github.com/%s/%s.git", dstRepo.Org, dstRepo.Repo) + } + case "gitlab": + var gitlabURL string + if dstRepo.BaseURL != "" { + gitlabURL = dstRepo.BaseURL + } else { + gitlabURL = gitlab.DefaultGitlabHost + } + if dstRepo.Org != "" { + outputs["repoURL"] = fmt.Sprintf("%s/%s/%s.git", gitlabURL, dstRepo.Org, dstRepo.Repo) + } else { + outputs["repoURL"] = fmt.Sprintf("%s/%s/%s.git", gitlabURL, dstRepo.Owner, dstRepo.Repo) + } } res["outputs"] = outputs return res, nil @@ -36,14 +54,14 @@ func GetDynamicState(options plugininstaller.RawOptions) (map[string]interface{} if err != nil { return nil, err } - switch opts.RepoType { + dstRepo := opts.DestinationRepo + switch dstRepo.RepoType { case "github": - return getGithubStatus(&opts.DestinationRepo) + return getGithubStatus(dstRepo) case "gitlab": - // TODO: add gitlab status logic - return nil, nil + return getGitlabStatus(dstRepo) default: - return nil, fmt.Errorf("read state not support repo type: %s", opts.RepoType) + return nil, fmt.Errorf("read state not support repo type: %s", dstRepo.RepoType) } } @@ -86,3 +104,42 @@ func getGithubStatus(dstRepo *DstRepo) (map[string]interface{}, error) { return res, nil } + +func getGitlabStatus(dstRepo *DstRepo) (map[string]interface{}, error) { + c, err := dstRepo.createGitlabClient() + if err != nil { + return nil, err + } + + project, err := c.DescribeProject(dstRepo.PathWithNamespace) + if err != nil { + return nil, err + } + if project == nil { + return nil, nil + } + + res := make(map[string]interface{}) + outputs := make(map[string]interface{}) + + log.Debugf("GitLab Project is: %#v\n", project) + + if project.Owner != nil { + log.Debugf("GitLab project owner is: %#v.\n", project.Owner) + res["owner"] = project.Owner.Username + res["org"] = project.Owner.Organization + outputs["owner"] = project.Owner.Username + outputs["org"] = project.Owner.Organization + } else { + res["owner"] = dstRepo.Owner + res["org"] = dstRepo.Org + outputs["owner"] = dstRepo.Owner + outputs["org"] = dstRepo.Org + } + res["repoName"] = project.Name + outputs["repo"] = project.Name + outputs["repoURL"] = project.HTTPURLToRepo + res["outputs"] = outputs + + return res, nil +} diff --git a/internal/pkg/plugininstaller/reposcaffolding/validate.go b/internal/pkg/plugininstaller/reposcaffolding/validate.go index 88538599f..85b0239f1 100644 --- a/internal/pkg/plugininstaller/reposcaffolding/validate.go +++ b/internal/pkg/plugininstaller/reposcaffolding/validate.go @@ -28,7 +28,7 @@ func SetDefaultTemplateRepo(options plugininstaller.RawOptions) (plugininstaller return nil, err } // set dstRepo default value - dstRepo := &opts.DestinationRepo + dstRepo := opts.DestinationRepo // set PathWithNamespace for GitLab. GitHub won't need to use this // opts.PathWithNamespace = fmt.Sprintf("%s/%s", opts.Owner, opts.Repo) if dstRepo.Org != "" { diff --git a/internal/pkg/show/config/default.yaml b/internal/pkg/show/config/default.yaml index 70b5d4f1f..3e9d0489d 100644 --- a/internal/pkg/show/config/default.yaml +++ b/internal/pkg/show/config/default.yaml @@ -11,12 +11,19 @@ state: # tools.yaml sample: tools: - - name: github-repo-scaffolding-golang + - name: repo-scaffolding instanceID: default options: - owner: [ [ githubUsername ] ] + destination_repo: + owner: [[ githubUsername ]] org: "" - repo: [ [ repoName ] ] + repo: [[ repo ]] + branch: main + repo_type: github + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github # variables.yaml sample: githubUsername: daniel-hutao diff --git a/internal/pkg/show/config/embed_gen.go b/internal/pkg/show/config/embed_gen.go index 43de54b07..aa9a8e1b8 100644 --- a/internal/pkg/show/config/embed_gen.go +++ b/internal/pkg/show/config/embed_gen.go @@ -21,9 +21,6 @@ var ( //go:embed plugins/devlake.yaml DevlakeDefaultConfig string - //go:embed plugins/github-repo-scaffolding-golang.yaml - GithubRepoScaffoldingGolangDefaultConfig string - //go:embed plugins/githubactions-golang.yaml GithubactionsGolangDefaultConfig string @@ -36,9 +33,6 @@ var ( //go:embed plugins/gitlab-ce-docker.yaml GitlabCeDockerDefaultConfig string - //go:embed plugins/gitlab-repo-scaffolding-golang.yaml - GitlabRepoScaffoldingGolangDefaultConfig string - //go:embed plugins/gitlabci-generic.yaml GitlabciGenericDefaultConfig string @@ -86,31 +80,29 @@ var ( ) var pluginDefaultConfigs = map[string]string{ - "argocd": ArgocdDefaultConfig, - "argocdapp": ArgocdappDefaultConfig, - "artifactory": ArtifactoryDefaultConfig, - "devlake": DevlakeDefaultConfig, - "github-repo-scaffolding-golang": GithubRepoScaffoldingGolangDefaultConfig, - "githubactions-golang": GithubactionsGolangDefaultConfig, - "githubactions-nodejs": GithubactionsNodejsDefaultConfig, - "githubactions-python": GithubactionsPythonDefaultConfig, - "gitlab-ce-docker": GitlabCeDockerDefaultConfig, - "gitlab-repo-scaffolding-golang": GitlabRepoScaffoldingGolangDefaultConfig, - "gitlabci-generic": GitlabciGenericDefaultConfig, - "gitlabci-golang": GitlabciGolangDefaultConfig, - "harbor": HarborDefaultConfig, - "hashicorp-vault": HashicorpVaultDefaultConfig, - "helm-generic": HelmGenericDefaultConfig, - "jenkins-pipeline-kubernetes": JenkinsPipelineKubernetesDefaultConfig, - "jenkins": JenkinsDefaultConfig, - "jira-github-integ": JiraGithubIntegDefaultConfig, - "kube-prometheus": KubePrometheusDefaultConfig, - "openldap": OpenldapDefaultConfig, - "repo-scaffolding": RepoScaffoldingDefaultConfig, - "tekton": TektonDefaultConfig, - "trello-github-integ": TrelloGithubIntegDefaultConfig, - "trello": TrelloDefaultConfig, - "zentao": ZentaoDefaultConfig, + "argocd": ArgocdDefaultConfig, + "argocdapp": ArgocdappDefaultConfig, + "artifactory": ArtifactoryDefaultConfig, + "devlake": DevlakeDefaultConfig, + "githubactions-golang": GithubactionsGolangDefaultConfig, + "githubactions-nodejs": GithubactionsNodejsDefaultConfig, + "githubactions-python": GithubactionsPythonDefaultConfig, + "gitlab-ce-docker": GitlabCeDockerDefaultConfig, + "gitlabci-generic": GitlabciGenericDefaultConfig, + "gitlabci-golang": GitlabciGolangDefaultConfig, + "harbor": HarborDefaultConfig, + "hashicorp-vault": HashicorpVaultDefaultConfig, + "helm-generic": HelmGenericDefaultConfig, + "jenkins-pipeline-kubernetes": JenkinsPipelineKubernetesDefaultConfig, + "jenkins": JenkinsDefaultConfig, + "jira-github-integ": JiraGithubIntegDefaultConfig, + "kube-prometheus": KubePrometheusDefaultConfig, + "openldap": OpenldapDefaultConfig, + "repo-scaffolding": RepoScaffoldingDefaultConfig, + "tekton": TektonDefaultConfig, + "trello-github-integ": TrelloGithubIntegDefaultConfig, + "trello": TrelloDefaultConfig, + "zentao": ZentaoDefaultConfig, } //go:embed quickstart.yaml diff --git a/internal/pkg/show/config/gitops.yaml b/internal/pkg/show/config/gitops.yaml index 10c9b9b3c..bc84a4f51 100644 --- a/internal/pkg/show/config/gitops.yaml +++ b/internal/pkg/show/config/gitops.yaml @@ -22,17 +22,24 @@ argocdDeployTimeout: 10m --- # plugins config tools: - - name: github-repo-scaffolding-golang - instanceID: default + - name: repo-scaffolding + instanceID: golang-github options: - owner: [[ githubUsername ]] - org: "" - repo: [[ repoName ]] - branch: [[ defaultBranch ]] - image_repo: [[ dockerhubUsername ]]/[[ repoName ]] + destination_repo: + owner: [[ githubUsername ]] + org: "" + repo: [[ repoName ]] + branch: [[ defaultBranch ]] + repo_type: github + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github + vars: + ImageRepo: [[ dockerhubUsername ]]/[[ repoName ]] - name: jira-github-integ instanceID: default - dependsOn: [ "github-repo-scaffolding-golang.default" ] + dependsOn: [ "repo-scaffolding.golang-github" ] options: owner: [[ githubUsername ]] repo: [[ repoName ]] @@ -42,11 +49,11 @@ tools: branch: main - name: githubactions-golang instanceID: default - dependsOn: [ "github-repo-scaffolding-golang.default" ] + dependsOn: [ "repo-scaffolding.golang-github" ] options: - owner: ${{github-repo-scaffolding-golang.default.outputs.owner}} + owner: ${{repo-scaffolding.golang-github.outputs.owner}} org: "" - repo: ${{github-repo-scaffolding-golang.default.outputs.repo}} + repo: ${{repo-scaffolding.golang-github.outputs.repo}} language: name: go version: "1.18" @@ -66,7 +73,7 @@ tools: registry: type: dockerhub username: [[ dockerhubUsername ]] - repository: ${{github-repo-scaffolding-golang.default.outputs.repo}} + repository: ${{repo-scaffolding.golang-github.outputs.repo}} - name: argocd instanceID: default options: @@ -83,15 +90,15 @@ tools: upgradeCRDs: true - name: argocdapp instanceID: default - dependsOn: ["argocd.default", "github-repo-scaffolding-golang.default"] + dependsOn: ["argocd.default", "repo-scaffolding.golang-github"] options: app: - name: ${{github-repo-scaffolding-golang.default.outputs.repo}} + name: ${{repo-scaffolding.golang-github.outputs.repo}} namespace: [[ argocdNameSpace ]] destination: server: https://kubernetes.default.svc namespace: default source: valuefile: values.yaml - path: helm/${{github-repo-scaffolding-golang.default.outputs.repo}} - repoURL: ${{github-repo-scaffolding-golang.default.outputs.repoURL}} + path: helm/${{repo-scaffolding.golang-github.outputs.repo}} + repoURL: ${{repo-scaffolding.golang-github.outputs.repoURL}} diff --git a/internal/pkg/show/config/plugins/github-repo-scaffolding-golang.yaml b/internal/pkg/show/config/plugins/github-repo-scaffolding-golang.yaml deleted file mode 100644 index 7dadb9a35..000000000 --- a/internal/pkg/show/config/plugins/github-repo-scaffolding-golang.yaml +++ /dev/null @@ -1,19 +0,0 @@ -tools: - # name of the tool - - name: github-repo-scaffolding-golang - # id of the tool instance - instanceID: default - # format: name.instanceID; If specified, dtm will make sure the dependency is applied first before handling this tool. - dependsOn: [ ] - # options for the plugin - options: - # the repo's owner. It should be case-sensitive here; strictly use your GitHub user name; please change the value below. - owner: YOUR_GITHUB_USERNAME - # the repo's org. If you set this property, then the new repo will be created under the org you're given, and the "owner" setting above will be ignored. - org: YOUR_ORGANIZATION_NAME - # the repo which you'd like to create; please change the value below. - repo: YOUR_REPO_NAME - # the branch of the repo you'd like to hold the code - branch: main - # the image repo you'd like to push the container image; please change the value below. - image_repo: YOUR_DOCKERHUB_USERNAME/YOUR_DOCKERHUB_REPOSITORY diff --git a/internal/pkg/show/config/plugins/gitlab-repo-scaffolding-golang.yaml b/internal/pkg/show/config/plugins/gitlab-repo-scaffolding-golang.yaml deleted file mode 100644 index b5c82d42e..000000000 --- a/internal/pkg/show/config/plugins/gitlab-repo-scaffolding-golang.yaml +++ /dev/null @@ -1,19 +0,0 @@ -tools: - # name of the tool - - name: gitlab-repo-scaffolding-golang - # id of the tool instance - instanceID: default - # format: name.instanceID; If specified, dtm will make sure the dependency is applied first before handling this tool. - dependsOn: [ ] - # options for the plugin - options: - # the repo's owner. It should be case-sensitive here; strictly use your GitLab user name; please change the value below. - owner: YOUR_GITLAB_USERNAME - # the repo's org. If you set this property, then the new repo will be created under the org you're given, and the "owner" setting above will be ignored. - org: YOUR_ORGANIZATION_NAME - # the repo which you'd like to create; please change the value below. - repo: YOUR_REPO_NAME - # the branch of the repo you'd like to hold the code - branch: main - # the image repo you'd like to push the container image; please change the value below. - image_repo: YOUR_DOCKERHUB_USERNAME/YOUR_DOCKERHUB_REPOSITORY diff --git a/internal/pkg/show/config/plugins/repo-scaffolding.yaml b/internal/pkg/show/config/plugins/repo-scaffolding.yaml index 6f7e71ef8..dbf3bd955 100644 --- a/internal/pkg/show/config/plugins/repo-scaffolding.yaml +++ b/internal/pkg/show/config/plugins/repo-scaffolding.yaml @@ -9,16 +9,18 @@ tools: options: # desination_repo is the config for desination repo destination_repo: - owner: YOUR_GITHUB_USERNAME - org: YOUR_GITHUB_ORGANIZATION_NAME - repo: YOUR_GITHUB_REPO_NAME - branch: YOUR_GITHUB_REPO_MAIN_BRANCH - # repo_type is the type of repo to push, support github|gitlab - repo_type: YOUR_REPO_TYPE + owner: YOUR_DESTINATION_USERNAME + org: YOUR_DESTINATION_ORGANIZATION_NAME + repo: YOUR_DESTINATION_REPO_NAME + branch: YOUR_DESTINATION_REPO_MAIN_BRANCH + # repo_type is the type of repo to push, support github|gitlab + repo_type: YOUR_DESTINATION_REPO_TYPE # source_repo is the template repo location, support github only source_repo: org: YOUR_TEMPLATE_REPO_ORG repo: YOUR_TEMPLATE_REPO_NAME + # repo_type is the type of repo to get template, support github only + repo_type: YOUR_TEMPLATE_REPO_TYPE # this is used for template render vars: ImageRepo: YOUR_DOCKERHUB_REPOSITORY diff --git a/internal/pkg/show/config/quickstart.yaml b/internal/pkg/show/config/quickstart.yaml index 2a3bc8b84..a8bb96759 100644 --- a/internal/pkg/show/config/quickstart.yaml +++ b/internal/pkg/show/config/quickstart.yaml @@ -10,16 +10,24 @@ state: # state config, backend can be local or s3 --- # plugins config tools: - - name: github-repo-scaffolding-golang - instanceID: default + - name: repo-scaffolding + instanceID: golang-github options: - owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE - repo: go-webapp-devstream-demo - branch: main - image_repo: YOUR_DOCKER_USERNAME/go-webapp-devstream-demo + destination_repo: + owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE + org: "" + repo: go-webapp-devstream-demo + branch: main + repo_type: github + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github + vars: + ImageRepo: YOUR_DOCKER_USERNAME/go-webapp-devstream-demo - name: githubactions-golang instanceID: default - dependsOn: ["github-repo-scaffolding-golang.default"] + dependsOn: ["repo-scaffolding.golang-github"] options: owner: YOUR_GITHUB_USERNAME_CASE_SENSITIVE repo: go-webapp-devstream-demo diff --git a/pkg/util/gitlab/commits.go b/pkg/util/gitlab/commits.go index 8b17061d3..174d45057 100644 --- a/pkg/util/gitlab/commits.go +++ b/pkg/util/gitlab/commits.go @@ -72,7 +72,7 @@ func (c *Client) UpdateSingleFile(project, branch, commitMessage, filename, cont } func (c *Client) CommitMultipleFiles(project, branch, commitMessage string, files map[string][]byte) error { - var commitActionsOptions = make([]*gitlab.CommitActionOptions, len(files)) + var commitActionsOptions = make([]*gitlab.CommitActionOptions, 0, len(files)) for fileName, content := range files { commitActionsOptions = append(commitActionsOptions, &gitlab.CommitActionOptions{ diff --git a/pkg/util/gitlab/file.go b/pkg/util/gitlab/file.go index 05f7fd758..e52cb88c9 100644 --- a/pkg/util/gitlab/file.go +++ b/pkg/util/gitlab/file.go @@ -4,14 +4,14 @@ import ( "io/fs" "io/ioutil" "path/filepath" - "strings" "github.com/devstream-io/devstream/pkg/util/log" ) -func (c *Client) PushLocalPath(repoPath, branch, pathWithNamespace, commitMsg string) error { +func (c *Client) PushLocalPathToBranch(repoPath, branch, pathWithNamespace, commitMsg string) (bool, error) { var files = make(map[string][]byte) + // 1. walk through files if err := filepath.Walk(repoPath, func(path string, info fs.FileInfo, err error) error { if err != nil { log.Debugf("Walk error: %s.", err) @@ -29,11 +29,19 @@ func (c *Client) PushLocalPath(repoPath, branch, pathWithNamespace, commitMsg st if err != nil { return err } - repoPath := strings.Join(strings.Split(path, "/")[2:], "/") - files[repoPath] = content + + gitlabPath, _ := filepath.Rel(repoPath, path) + files[gitlabPath] = content return nil }); err != nil { - return err + return false, err + } + + //2. push repo to gitlab + err := c.CommitMultipleFiles(pathWithNamespace, branch, commitMsg, files) + needRollBack := false + if err != nil { + needRollBack = true } - return c.CommitMultipleFiles(pathWithNamespace, branch, commitMsg, files) + return needRollBack, err } diff --git a/pkg/util/gitlab/project.go b/pkg/util/gitlab/project.go index ea81b9810..33d1ce3eb 100644 --- a/pkg/util/gitlab/project.go +++ b/pkg/util/gitlab/project.go @@ -80,6 +80,7 @@ func (c *Client) DescribeProject(project string) (*gitlab.Project, error) { res, _, err := c.Projects.GetProject(project, p) if err != nil { + log.Debugf("gitlab project: get [%s] info error %s", project, err) return nil, err } diff --git a/staging/dtm-repo-scaffolding-golang/README.md b/staging/dtm-repo-scaffolding-golang/README.md index 949cfdc50..8579a2334 100644 --- a/staging/dtm-repo-scaffolding-golang/README.md +++ b/staging/dtm-repo-scaffolding-golang/README.md @@ -1,6 +1,6 @@ # dtm-scaffolding-golang -This repo contains templates used by DevStream plugin "github-repo-scaffolding-golang" (thereafter: the plugin). +This repo contains templates used by DevStream plugin "repo-scaffolding" (thereafter: the plugin). This repo isn't intended to be used directly without DevStream. It should only be consumed by the plugin automatically. diff --git a/test/e2e/yaml/e2e-test-local.yaml b/test/e2e/yaml/e2e-test-local.yaml index b7b6a8925..c7835d017 100644 --- a/test/e2e/yaml/e2e-test-local.yaml +++ b/test/e2e/yaml/e2e-test-local.yaml @@ -14,19 +14,27 @@ argocdNameSpace: argocd-e2e-test argocdDeployTimeout: 10m --- tools: -- name: github-repo-scaffolding-golang - instanceID: default +- name: repo-scaffolding + instanceID: golang-github options: - owner: [[ githubUsername ]] - repo: [[ repoName ]] - branch: [[ defaultBranch ]] - image_repo: "[[ dockerRegistryUserName ]]/[[ repoName ]]" + destination_repo: + owner: [[ githubUsername ]] + org: "" + repo: [[ repoName ]] + branch: [[ defaultBranch ]] + repo_type: github + vars: + ImageRepo: "[[ dockerhubUsername ]]/[[ repoName ]]" + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github - name: githubactions-golang instanceID: default - dependsOn: ["github-repo-scaffolding-golang.default"] + dependsOn: ["repo-scaffolding.golang-github"] options: - owner: ${{github-repo-scaffolding-golang.default.outputs.owner}} - repo: ${{github-repo-scaffolding-golang.default.outputs.repo}} + owner: ${{repo-scaffolding.golang-github.outputs.owner}} + repo: ${{repo-scaffolding.golang-github.outputs.repo}} language: name: go version: "1.17" @@ -58,15 +66,15 @@ tools: upgradeCRDs: true - name: argocdapp instanceID: default - dependsOn: ["argocd.default", "github-repo-scaffolding-golang.default"] + dependsOn: ["argocd.default", "repo-scaffolding.golang-github"] options: app: - name: ${{github-repo-scaffolding-golang.default.outputs.repo}} + name: ${{repo-scaffolding.golang-github.outputs.repo}} namespace: [[ argocdNameSpace ]] destination: server: https://kubernetes.default.svc namespace: default source: valuefile: values.yaml - path: helm/${{github-repo-scaffolding-golang.default.outputs.repo}} - repoURL: ${{github-repo-scaffolding-golang.default.outputs.repoURL}} + path: helm/${{repo-scaffolding.golang-github.outputs.repo}} + repoURL: ${{repo-scaffolding.golang-github.outputs.repoURL}} diff --git a/test/e2e/yaml/e2e-tools.yaml b/test/e2e/yaml/e2e-tools.yaml index 6d6c4e577..db88163ec 100644 --- a/test/e2e/yaml/e2e-tools.yaml +++ b/test/e2e/yaml/e2e-tools.yaml @@ -1,17 +1,24 @@ tools: - - name: github-repo-scaffolding-golang - instanceID: default + - name: repo-scaffolding + instanceID: golang-github options: - org: [[ githubOrganization ]] - repo: [[ repoName ]] - branch: [[ defaultBranch ]] - image_repo: [[ dockerRegistryUserName ]]/[[ repoName ]] + destination_repo: + org: [ [ githubOrganization ] ] + repo: [ [ repoName ] ] + branch: [ [ defaultBranch ] ] + repo_type: github + vars: + ImageRepo: "[[ dockerRegistryUserName ]]/[[ repoName ]]" + source_repo: + org: devstream-io + repo: dtm-scaffolding-golang + repo_type: github - name: githubactions-golang instanceID: default - dependsOn: ["github-repo-scaffolding-golang.default"] + dependsOn: ["repo-scaffolding.golang-github"] options: - org: ${{github-repo-scaffolding-golang.default.outputs.org}} - repo: ${{github-repo-scaffolding-golang.default.outputs.repo}} + org: ${{repo-scaffolding.golang-github.outputs.org}} + repo: ${{repo-scaffolding.golang-github.outputs.repo}} language: name: go version: "1.18" @@ -43,15 +50,15 @@ tools: upgradeCRDs: true - name: argocdapp instanceID: default - dependsOn: ["argocd.default", "github-repo-scaffolding-golang.default"] + dependsOn: ["argocd.default", "repo-scaffolding.golang-github"] options: app: - name: ${{github-repo-scaffolding-golang.default.outputs.repo}} + name: ${{repo-scaffolding.golang-github.outputs.repo}} namespace: [[ argocdNameSpace ]] destination: server: https://kubernetes.default.svc namespace: default source: valuefile: values.yaml - path: helm/${{github-repo-scaffolding-golang.default.outputs.repo}} - repoURL: ${{github-repo-scaffolding-golang.default.outputs.repoURL}} + path: helm/${{repo-scaffolding.golang-github.outputs.repo}} + repoURL: ${{repo-scaffolding.golang-github.outputs.repoURL}}