Skip to content

Commit

Permalink
Merge pull request #4 from marcelblijleven/feature/general-improvements
Browse files Browse the repository at this point in the history
Feature/general improvements
  • Loading branch information
marcelblijleven authored Jan 14, 2022
2 parents d08545d + c157c7f commit 5be3bba
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 122 deletions.
78 changes: 51 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Helper tool to interact with Jira from CI/CD scripts. Its main purpose is to create and assign version
based on GitHub releases to Jira tickets.

You can provide comma separated issue numbers or a body of text which contains issue numbers and the jira-helper
will automatically assign the version to those issues in Jira.

## GitHub actions example
The following can be used to trigger a release (fixVersion) in Jira whenever a GitHub release is created. It will use the body of the
release and search for any issue numbers in it and automatically assign the newly created release to them.
Expand Down Expand Up @@ -33,32 +36,44 @@ Usage:
jira-helper [command]
Available Commands:
assignVersion Assigns a version to all provided issues in the release body
completion Generate the autocompletion script for the specified shell
createRelease Create a fix version in Jira
help Help about any command
assignRelease Assigns a version to all provided issues in the release body
completion Generate the autocompletion script for the specified shell
createAndAssign Creates a fix version in Jira and assigns it to the issues
createRelease Create a fix version in Jira
help Help about any command
Flags:
-h, --help help for jira-helper
-h, --help help for jira-helper
-s, --host string Host of the Jira API. If the host URL contains a scheme (e.g. https), you must include it
-p, --project string Project key of the Jira project, e.g. MB
-t, --token string Token used to authenticate against the Jira API
-u, --user string User (email) for authenticating against the Jira API
-v, --version string Name of the version
```

### Assign version
### Assign release
Assigns a version to all provided issues. The issue numbers are retrieved from
the provided release body.

```
Usage:
jira-helper assignVersion [flags]
jira-helper assignRelease [flags]
Aliases:
assignRelease, assignVersion
Flags:
-h, --help help for assignVersion
-s, --host string host of the Jira API
-p, --project string Abbreviation of the Jira project, e.g. MB
-b, --releaseBody string The body of the Github release
-t, --token string Token used to authenticate against the Jira API
-u, --user string user used for authenticating against the Jira API
-v, --version string Version name
-h, --help help for assignRelease
-i, --issues strings The issues you want to assign to release to, can be a single issue or comma separated
-b, --releaseBody string The body of text which contains Jira issues, e.g. a GitHub release body
Global Flags:
-s, --host string Host of the Jira API. If the host URL contains a scheme (e.g. https), you must include it
-p, --project string Project key of the Jira project, e.g. MB
-t, --token string Token used to authenticate against the Jira API
-u, --user string User (email) for authenticating against the Jira API
-v, --version string Name of the version
```

### Create release
Expand All @@ -69,15 +84,20 @@ today.

```
Usage:
jira-helper createRelease [flags]
jira-helper createRelease [flags]
Aliases:
createRelease, createVersion
Flags:
-h, --help help for createRelease
-s, --host string host of the Jira API
-p, --project string Abbreviation of the Jira project, e.g. MB
-t, --token string Token used to authenticate against the Jira API
-u, --user string user used for authenticating against the Jira API
-v, --version string Version name
-h, --help help for createRelease
Global Flags:
-s, --host string Host of the Jira API. If the host URL contains a scheme (e.g. https), you must include it
-p, --project string Project key of the Jira project, e.g. MB
-t, --token string Token used to authenticate against the Jira API
-u, --user string User (email) for authenticating against the Jira API
-v, --version string Name of the version
```

Expand All @@ -93,10 +113,14 @@ jira-helper createAndAssign [flags]
Flags:
-h, --help help for createAndAssign
-s, --host string host of the Jira API
-p, --project string Abbreviation of the Jira project, e.g. MB
-b, --releaseBody string The body of the Github release
-t, --token string Token used to authenticate against the Jira API
-u, --user string user used for authenticating against the Jira API
-v, --version string Version name
-i, --issues strings The issues you want to assign to release to, can be a single issue or comma separated
-b, --releaseBody string The body of text which contains Jira issues, e.g. a GitHub release body
Global Flags:
-s, --host string Host of the Jira API. If the host URL contains a scheme (e.g. https), you must include it
-p, --project string Project key of the Jira project, e.g. MB
-t, --token string Token used to authenticate against the Jira API
-u, --user string User (email) for authenticating against the Jira API
-v, --version string Name of the version
```
38 changes: 15 additions & 23 deletions cmd/assignVersion.go → cmd/assignRelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,36 @@ limitations under the License.
package cmd

import (
"errors"
"jira-helper/pkg"
"net/http"
"time"

"github.com/spf13/cobra"
)

// assignVersionCmd represents the assignVersion command
var assignVersionCmd = &cobra.Command{
Use: "assignVersion",
// assignReleaseCmd represents the assignVersion command
var assignReleaseCmd = &cobra.Command{
Use: "assignRelease",
Short: "Assigns a version to all provided issues in the release body",
Long: `Assigns a version to all provided issues. The issue numbers are retrieved from
the provided release body.`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
if body == "" && (issues == nil || len(issues) == 0) {
cobra.CheckErr(errors.New("no issues provided. Provide issue through the issues and/or releaseBody flags"))
}

httpClient := http.DefaultClient
httpClient.Timeout = time.Second * 15
client, err := pkg.NewJiraClient(host, user, token, httpClient)

if err != nil {
return err
}

err = pkg.AssignVersions(body, version, client)

if err != nil {
return err
}

return nil
cobra.CheckErr(err)
cobra.CheckErr(pkg.AssignVersions(body, version, client, issues...))
},
}

func init() {
rootCmd.AddCommand(assignVersionCmd)
assignVersionCmd.Flags().StringVarP(&user, "user", "u", "", "user used for authenticating against the Jira API")
assignVersionCmd.Flags().StringVarP(&host, "host", "s", "", "host of the Jira API")
assignVersionCmd.Flags().StringVarP(&project, "project", "p", "", "Abbreviation of the Jira project, e.g. GGWM")
assignVersionCmd.Flags().StringVarP(&token, "token", "t", "", "Token used to authenticate against the Jira API")
assignVersionCmd.Flags().StringVarP(&version, "version", "v", "", "Version name")
assignVersionCmd.Flags().StringVarP(&body, "releaseBody", "b", "", "The body of the Github release")
rootCmd.AddCommand(assignReleaseCmd)
assignReleaseCmd.Aliases = []string{"assignVersion"}
assignReleaseCmd.Flags().StringVarP(&body, bodyFlagName, bodyShorthand, "", bodyUsage)
assignReleaseCmd.Flags().StringSliceVarP(&issues, issuesFlagName, issuesShorthand, []string{}, issuesUsage)
}
36 changes: 11 additions & 25 deletions cmd/createAndAssign.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"errors"
"jira-helper/pkg"
"net/http"
"time"
Expand All @@ -32,37 +33,22 @@ var createAndAssignCmd = &cobra.Command{
The release state of the fix version will be set to "released" and the day will be set to
today.`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
if body == "" && (issues == nil || len(issues) == 0) {
cobra.CheckErr(errors.New("no issues provided. Provide issue through the issues and/or releaseBody flags"))
}

httpClient := http.DefaultClient
httpClient.Timeout = time.Second * 15
client, err := pkg.NewJiraClient(host, user, token, httpClient)

if err != nil {
return err
}

err = client.CreateFixVersion(version, project)

if err != nil {
return err
}

err = pkg.AssignVersions(body, version, client)

if err != nil {
return err
}

return nil
cobra.CheckErr(err)
cobra.CheckErr(client.CreateFixVersion(version, project))
cobra.CheckErr(pkg.AssignVersions(body, version, client, issues...))
},
}

func init() {
rootCmd.AddCommand(createAndAssignCmd)
createAndAssignCmd.Flags().StringVarP(&user, "user", "u", "", "user used for authenticating against the Jira API")
createAndAssignCmd.Flags().StringVarP(&host, "host", "s", "", "host of the Jira API")
createAndAssignCmd.Flags().StringVarP(&project, "project", "p", "", "Abbreviation of the Jira project, e.g. GGWM")
createAndAssignCmd.Flags().StringVarP(&token, "token", "t", "", "Token used to authenticate against the Jira API")
createAndAssignCmd.Flags().StringVarP(&version, "version", "v", "", "Version name")
createAndAssignCmd.Flags().StringVarP(&body, "releaseBody", "b", "", "The body of the Github release")
createAndAssignCmd.Flags().StringVarP(&body, bodyFlagName, bodyShorthand, "", bodyUsage)
createAndAssignCmd.Flags().StringSliceVarP(&issues, issuesFlagName, issuesShorthand, []string{}, issuesUsage)
}
22 changes: 4 additions & 18 deletions cmd/createRelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,16 @@ var createReleaseCmd = &cobra.Command{
The release state of the fix version will be set to "released" and the day will be set to
today.`,
RunE: func(cmd *cobra.Command, args []string) error {
Run: func(cmd *cobra.Command, args []string) {
httpClient := http.DefaultClient
httpClient.Timeout = time.Second * 15
client, err := pkg.NewJiraClient(host, user, token, httpClient)

if err != nil {
return err
}

err = client.CreateFixVersion(version, project)

if err != nil {
return err
}

return nil
cobra.CheckErr(err)
cobra.CheckErr(client.CreateFixVersion(version, project))
},
}

func init() {
rootCmd.AddCommand(createReleaseCmd)
createReleaseCmd.Flags().StringVarP(&user, "user", "u", "", "user used for authenticating against the Jira API")
createReleaseCmd.Flags().StringVarP(&host, "host", "s", "", "host of the Jira API")
createReleaseCmd.Flags().StringVarP(&project, "project", "p", "", "Abbreviation of the Jira project, e.g. GGWM")
createReleaseCmd.Flags().StringVarP(&token, "token", "t", "", "Token used to authenticate against the Jira API")
createReleaseCmd.Flags().StringVarP(&version, "version", "v", "", "Version name")
createReleaseCmd.Aliases = []string{"createVersion"}
}
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ func Execute() {

func init() {
cobra.OnInitialize()
rootCmd.PersistentFlags().StringVarP(&user, userFlagName, userShorthand, "", userUsage)
rootCmd.PersistentFlags().StringVarP(&host, hostFlagName, hostShorthand, "", hostUsage)
rootCmd.PersistentFlags().StringVarP(&project, projectFlagName, projectShorthand, "", projectUsage)
rootCmd.PersistentFlags().StringVarP(&token, tokenFlagName, tokenShorthand, "", tokenUsage)
rootCmd.PersistentFlags().StringVarP(&version, versionFlagName, versionShorthand, "", versionUsage)

cobra.CheckErr(rootCmd.MarkPersistentFlagRequired(userFlagName))
cobra.CheckErr(rootCmd.MarkPersistentFlagRequired(hostFlagName))
cobra.CheckErr(rootCmd.MarkPersistentFlagRequired(projectFlagName))
cobra.CheckErr(rootCmd.MarkPersistentFlagRequired(tokenFlagName))
cobra.CheckErr(rootCmd.MarkPersistentFlagRequired(versionFlagName))
}
31 changes: 31 additions & 0 deletions cmd/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,35 @@ var (
project string
version string
body string
issues []string
)

const (
userFlagName = "user"
userShorthand = "u"
userUsage = "User (email) for authenticating against the Jira API"

hostFlagName = "host"
hostShorthand = "s"
hostUsage = "Host of the Jira API. If the host URL contains a scheme (e.g. https), you must include it"

tokenFlagName = "token"
tokenShorthand = "t"
tokenUsage = "Token used to authenticate against the Jira API"

projectFlagName = "project"
projectShorthand = "p"
projectUsage = "Project key of the Jira project, e.g. MB"

versionFlagName = "version"
versionShorthand = "v"
versionUsage = "Name of the version"

bodyFlagName = "releaseBody"
bodyShorthand = "b"
bodyUsage = "The body of text which contains Jira issues, e.g. a GitHub release body"

issuesFlagName = "issues"
issuesShorthand = "i"
issuesUsage = "The issues you want to assign to release to, can be a single issue or comma separated"
)
Loading

0 comments on commit 5be3bba

Please sign in to comment.