forked from devstream-io/devstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jira-github-integ plugin (devstream-io#256)
- Loading branch information
Showing
15 changed files
with
487 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/merico-dev/stream/internal/pkg/plugin/jiragithub" | ||
"github.com/merico-dev/stream/pkg/util/log" | ||
) | ||
|
||
// NAME is the name of this DevStream plugin. | ||
const NAME = "jiragithub" | ||
|
||
// Plugin is the type used by DevStream core. It's a string. | ||
type Plugin string | ||
|
||
// Create implements the installation of some jira-github-integ workflows. | ||
func (p Plugin) Create(options map[string]interface{}) (map[string]interface{}, error) { | ||
return jiragithub.Create(options) | ||
} | ||
|
||
// Update implements the installation of some jira-github-integ workflows. | ||
func (p Plugin) Update(options map[string]interface{}) (map[string]interface{}, error) { | ||
return jiragithub.Update(options) | ||
} | ||
|
||
// Read implements the healthy check of jira-github-integ workflows. | ||
func (p Plugin) Read(options map[string]interface{}) (map[string]interface{}, error) { | ||
return jiragithub.Read(options) | ||
} | ||
|
||
// Delete implements the installation of some jira-github-integ workflows. | ||
func (p Plugin) Delete(options map[string]interface{}) (bool, error) { | ||
return jiragithub.Delete(options) | ||
} | ||
|
||
// DevStreamPlugin is the exported variable used by the DevStream core. | ||
var DevStreamPlugin Plugin | ||
|
||
func main() { | ||
log.Infof("%T: %s is a plugin for DevStream. Use it with DevStream.\n", NAME, DevStreamPlugin) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
## 1 `jira-github-integ` Plugin | ||
|
||
This plugin integrates Jira with your GitHub repo. | ||
|
||
## 2 Usage: | ||
|
||
_Please confirm the preconditions:_ | ||
|
||
- Jira language must be English | ||
- There should be an existing Jira project | ||
|
||
_This plugin depends on the following two environment variables:_ | ||
|
||
- JIRA_API_TOKEN | ||
- GITHUB_TOKEN | ||
|
||
Set the values accordingly before using this plugin. | ||
|
||
|
||
To create a Jira API token, see [here](https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/). | ||
|
||
```yaml | ||
tools: | ||
- name: jira-github-integ-default | ||
# plugin profile | ||
plugin: | ||
# kind of this plugin | ||
kind: jira-github-integ | ||
# version of the plugin | ||
version: 0.3.0 | ||
# options for the plugin | ||
# checkout the version from the GitHub releases | ||
options: | ||
# the repo's owner | ||
owner: lfbdev | ||
# the repo where you'd like to setup GitHub Actions | ||
repo: opendeveloper | ||
# "base url: https://id.atlassian.net" | ||
jiraBaseUrl: https://merico.atlassian.net | ||
# "need real user email in cloud Jira" | ||
jiraUserEmail: [email protected] | ||
# "get it from project url, like 'HEAP' from https://merico.atlassian.net/jira/software/projects/HEAP/pages" | ||
jiraProjectKey: HEAP | ||
# main branch of the repo (to which branch the plugin will submit the workflows) | ||
branch: master | ||
``` | ||
Currently, all the parameters in the example above are mandatory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,17 @@ tools: | |
kanbanBoardName: kanban-name | ||
# main branch of the repo (to which branch the plugin will submit the workflows) | ||
branch: main | ||
- name: jira-github-integ-golang-demo | ||
plugin: | ||
kind: jira-github-integ | ||
version: 0.2.0 | ||
options: | ||
owner: ironcore864 | ||
repo: golang-demo | ||
jiraBaseUrl: https://merico.atlassian.net # "base url: https://id.atlassian.net" | ||
jiraUserEmail: [email protected] # "need real user email in cloud Jira" | ||
jiraProjectKey: PROJECT # "get it from project url, like 'DTM' from https://merico.atlassian.net/jira/software/projects/DTM/pages" | ||
branch: main | ||
- name: dev | ||
plugin: | ||
# name of the plugin | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package jiragithub | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/merico-dev/stream/pkg/util/github" | ||
) | ||
|
||
// Create sets up jira-github-integ workflows. | ||
func Create(options map[string]interface{}) (map[string]interface{}, error) { | ||
opt, err := parseAndValidateOptions(options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ghOptions := &github.Option{ | ||
Owner: opt.Owner, | ||
Repo: opt.Repo, | ||
NeedAuth: true, | ||
} | ||
ghClient, err := github.NewClient(ghOptions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
content, err := renderTemplate(workflow, opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
workflow.WorkflowContent = content | ||
|
||
if err := ghClient.AddWorkflow(workflow, opt.Branch); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := setRepoSecrets(ghClient); err != nil { | ||
return nil, err | ||
} | ||
|
||
return BuildState(opt.Owner, opt.Repo), nil | ||
} | ||
|
||
func BuildState(owner, repo string) map[string]interface{} { | ||
res := make(map[string]interface{}) | ||
res["workflowDir"] = fmt.Sprintf("/repos/%s/%s/contents/.github/workflows", owner, repo) | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package jiragithub | ||
|
||
import ( | ||
"github.com/merico-dev/stream/pkg/util/github" | ||
) | ||
|
||
// Delete remove jira-github-integ workflows. | ||
func Delete(options map[string]interface{}) (bool, error) { | ||
opt, err := parseAndValidateOptions(options) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
ghOptions := &github.Option{ | ||
Owner: opt.Owner, | ||
Repo: opt.Repo, | ||
NeedAuth: true, | ||
} | ||
ghClient, err := github.NewClient(ghOptions) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if err := ghClient.DeleteWorkflow(workflow, opt.Branch); err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package jiragithub | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/merico-dev/stream/pkg/util/github" | ||
"github.com/merico-dev/stream/pkg/util/log" | ||
) | ||
|
||
func parseAndValidateOptions(options map[string]interface{}) (*Options, error) { | ||
var opt Options | ||
err := mapstructure.Decode(options, &opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if errs := validate(&opt); len(errs) != 0 { | ||
for _, e := range errs { | ||
log.Errorf("Param error: %s.", e) | ||
} | ||
return nil, fmt.Errorf("incorrect params") | ||
} | ||
|
||
return &opt, nil | ||
} | ||
|
||
func setRepoSecrets(gitHubClient *github.Client) error { | ||
|
||
// JIRA_API_TOKEN, how to get it: "https://help.siteimprove.com/support/solutions/articles/80000448174-how-to-create-an-api-token-from-your-atlassian-account" | ||
if err := gitHubClient.AddRepoSecret("JIRA_API_TOKEN", viper.GetString("jira_api_token")); err != nil { | ||
return err | ||
} | ||
|
||
if err := gitHubClient.AddRepoSecret("GH_TOKEN", viper.GetString("github_token")); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package jiragithub | ||
|
||
// Options is the struct for configurations of the jiragithub plugin. | ||
type Options struct { | ||
Owner string | ||
Repo string | ||
JiraBaseUrl string | ||
JiraUserEmail string | ||
JiraProjectKey string | ||
Branch string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package jiragithub | ||
|
||
import ( | ||
"github.com/merico-dev/stream/pkg/util/github" | ||
) | ||
|
||
// Read get jira-github-integ workflows. | ||
func Read(options map[string]interface{}) (map[string]interface{}, error) { | ||
opt, err := parseAndValidateOptions(options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ghOptions := &github.Option{ | ||
Owner: opt.Owner, | ||
Repo: opt.Repo, | ||
NeedAuth: true, | ||
} | ||
ghClient, err := github.NewClient(ghOptions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
path, err := ghClient.GetWorkflowPath() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return BuildReadState(path), nil | ||
} | ||
|
||
func BuildReadState(path string) map[string]interface{} { | ||
res := make(map[string]interface{}) | ||
res["workflowDir"] = path | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package jiragithub | ||
|
||
import ( | ||
"bytes" | ||
"html/template" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
|
||
github "github.com/merico-dev/stream/pkg/util/github" | ||
) | ||
|
||
func renderTemplate(workflow *github.Workflow, options *Options) (string, error) { | ||
var opts Options | ||
err := mapstructure.Decode(options, &opts) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
//if use default {{.}}, it will confict (github actions vars also use them) | ||
t, err := template.New("jiragithub").Delims("[[", "]]").Parse(workflow.WorkflowContent) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var buff bytes.Buffer | ||
err = t.Execute(&buff, opts) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return buff.String(), nil | ||
} |
Oops, something went wrong.