Add get-jira-ticket command

This commit is contained in:
Miguel de la Cruz 2020-03-01 13:02:52 +01:00
parent 5d727b8995
commit 0ae0c6ffdf
4 changed files with 72 additions and 13 deletions

View file

@ -19,6 +19,7 @@ func StandaloneCmd() *cobra.Command {
cmd.AddCommand( cmd.AddCommand(
CreateJiraTicketStandaloneCmd(), CreateJiraTicketStandaloneCmd(),
GetJiraTicketStandaloneCmd(),
) )
return cmd return cmd
@ -49,6 +50,22 @@ func CreateJiraTicketStandaloneCmd() *cobra.Command {
return cmd return cmd
} }
func GetJiraTicketStandaloneCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get-jira-ticket",
Short: "Gets the ticket from jira",
Args: cobra.ExactArgs(1),
Run: getJiraTicketStandaloneCmdF,
}
cmd.Flags().String("username", "", "the jira username")
_ = cmd.MarkFlagRequired("username")
cmd.Flags().String("token", "", "the jira token")
_ = cmd.MarkFlagRequired("token")
return cmd
}
func getVarMap(vars []string) (map[string]string, error) { func getVarMap(vars []string) (map[string]string, error) {
varMap := map[string]string{} varMap := map[string]string{}
for _, v := range vars { for _, v := range vars {
@ -99,7 +116,7 @@ func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error {
jiraClient := jira.NewClient(username, token) jiraClient := jira.NewClient(username, token)
ticketKey, err := jiraClient.CreateTicket(epicId, team, summary, description) ticketKey, err := jiraClient.CreateIssue(epicId, team, summary, description)
if err != nil { if err != nil {
ErrorAndExit(cmd, err) ErrorAndExit(cmd, err)
} }
@ -107,3 +124,17 @@ func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error {
cmd.Printf("Ticket %s successfully created in JIRA", ticketKey) cmd.Printf("Ticket %s successfully created in JIRA", ticketKey)
return nil return nil
} }
func getJiraTicketStandaloneCmdF(cmd *cobra.Command, args []string) {
username, _ := cmd.Flags().GetString("username")
token, _ := cmd.Flags().GetString("token")
jiraClient := jira.NewClient(username, token)
issue, err := jiraClient.GetIssue(args[0])
if err != nil {
ErrorAndExit(cmd, err)
}
fmt.Printf("Key: %s\nStatus: %s\n", issue.Key, issue.Fields.Status.Name)
}

View file

@ -7,6 +7,6 @@ import (
) )
func ErrorAndExit(cmd *cobra.Command, err error) { func ErrorAndExit(cmd *cobra.Command, err error) {
cmd.PrintErrln(err) cmd.PrintErrln("ERROR: " + err.Error())
os.Exit(1) os.Exit(1)
} }

View file

@ -6,7 +6,7 @@ import (
) )
func Do(method, username, token, url string, body []byte) (*http.Response, error) { func Do(method, username, token, url string, body []byte) (*http.Response, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -2,7 +2,7 @@ package jira
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io"
"git.ctrlz.es/mgdelacroix/campaigner/http" "git.ctrlz.es/mgdelacroix/campaigner/http"
) )
@ -13,19 +13,37 @@ type JiraClient struct {
Url string Url string
} }
type JiraIssueFieldsStatus struct {
Name string `json:"name"`
}
type JiraIssueFields struct {
Status JiraIssueFieldsStatus `json:"status"`
}
type JiraIssue struct { type JiraIssue struct {
Key string `json:"key"` Key string `json:"key"`
Fields JiraIssueFields `json:"fields"`
}
func IssueFromJson(body io.Reader) (*JiraIssue, error) {
var issue JiraIssue
if err := json.NewDecoder(body).Decode(&issue); err != nil {
return nil, err
}
return &issue, nil
} }
func NewClient(username, token string) *JiraClient { func NewClient(username, token string) *JiraClient {
return &JiraClient{ return &JiraClient{
Username: username, Username: username,
Token: token, Token: token,
Url: "https://mattermost.atlassian.net/rest/api/2", Url: "https://mattermost.atlassian.net/rest/api/2/",
} }
} }
func (c *JiraClient) CreateTicket(epicId, team, summary, description string) (string, error) { func (c *JiraClient) CreateIssue(epicId, team, summary, description string) (string, error) {
data := map[string]interface{}{ data := map[string]interface{}{
"fields": map[string]interface{}{ "fields": map[string]interface{}{
"project": map[string]interface{}{"key": "MM"}, "project": map[string]interface{}{"key": "MM"},
@ -42,21 +60,31 @@ func (c *JiraClient) CreateTicket(epicId, team, summary, description string) (st
return "", err return "", err
} }
res, err := http.DoPost(c.Username, c.Token, c.Url+"/issue/", body) res, err := http.DoPost(c.Username, c.Token, c.Url+"issue/", body)
if err != nil { if err != nil {
return "", err return "", err
} }
defer res.Body.Close() defer res.Body.Close()
respBody, err := ioutil.ReadAll(res.Body) issue, err := IssueFromJson(res.Body)
if err != nil { if err != nil {
return "", err return "", err
} }
var issue JiraIssue
if err := json.Unmarshal(respBody, &issue); err != nil {
return "", err
}
return issue.Key, nil return issue.Key, nil
} }
func (c *JiraClient) GetIssue(issueNo string) (*JiraIssue, error) {
res, err := http.DoGet(c.Username, c.Token, c.Url+"issue/"+issueNo)
if err != nil {
return nil, err
}
defer res.Body.Close()
issue, err := IssueFromJson(res.Body)
if err != nil {
return nil, err
}
return issue, nil
}