From 0ae0c6ffdf0fc0718738a5b206e1c2fdcbf3222f Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sun, 1 Mar 2020 13:02:52 +0100 Subject: [PATCH] Add get-jira-ticket command --- cmd/standalone.go | 33 +++++++++++++++++++++++++++++++- cmd/utils.go | 2 +- http/http.go | 2 +- jira/jira.go | 48 +++++++++++++++++++++++++++++++++++++---------- 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/cmd/standalone.go b/cmd/standalone.go index 91517e1..2d26131 100644 --- a/cmd/standalone.go +++ b/cmd/standalone.go @@ -19,6 +19,7 @@ func StandaloneCmd() *cobra.Command { cmd.AddCommand( CreateJiraTicketStandaloneCmd(), + GetJiraTicketStandaloneCmd(), ) return cmd @@ -49,6 +50,22 @@ func CreateJiraTicketStandaloneCmd() *cobra.Command { 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) { varMap := map[string]string{} for _, v := range vars { @@ -99,7 +116,7 @@ func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error { jiraClient := jira.NewClient(username, token) - ticketKey, err := jiraClient.CreateTicket(epicId, team, summary, description) + ticketKey, err := jiraClient.CreateIssue(epicId, team, summary, description) if err != nil { ErrorAndExit(cmd, err) } @@ -107,3 +124,17 @@ func createJiraTicketStandaloneCmdF(cmd *cobra.Command, _ []string) error { cmd.Printf("Ticket %s successfully created in JIRA", ticketKey) 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) +} diff --git a/cmd/utils.go b/cmd/utils.go index 40ca1ca..ea255e4 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -7,6 +7,6 @@ import ( ) func ErrorAndExit(cmd *cobra.Command, err error) { - cmd.PrintErrln(err) + cmd.PrintErrln("ERROR: " + err.Error()) os.Exit(1) } diff --git a/http/http.go b/http/http.go index 633fb2d..289652b 100644 --- a/http/http.go +++ b/http/http.go @@ -6,7 +6,7 @@ import ( ) 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 { return nil, err } diff --git a/jira/jira.go b/jira/jira.go index 73c9360..fbb5431 100644 --- a/jira/jira.go +++ b/jira/jira.go @@ -2,7 +2,7 @@ package jira import ( "encoding/json" - "io/ioutil" + "io" "git.ctrlz.es/mgdelacroix/campaigner/http" ) @@ -13,19 +13,37 @@ type JiraClient struct { Url string } +type JiraIssueFieldsStatus struct { + Name string `json:"name"` +} + +type JiraIssueFields struct { + Status JiraIssueFieldsStatus `json:"status"` +} + type JiraIssue struct { 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 { return &JiraClient{ Username: username, 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{}{ "fields": map[string]interface{}{ "project": map[string]interface{}{"key": "MM"}, @@ -42,21 +60,31 @@ func (c *JiraClient) CreateTicket(epicId, team, summary, description string) (st 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 { return "", err } defer res.Body.Close() - respBody, err := ioutil.ReadAll(res.Body) + issue, err := IssueFromJson(res.Body) if err != nil { return "", err } - var issue JiraIssue - if err := json.Unmarshal(respBody, &issue); err != nil { - return "", err - } - 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 +}