diff --git a/cmd/jt/main.go b/cmd/jt/main.go index 59def34..882b11c 100644 --- a/cmd/jt/main.go +++ b/cmd/jt/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "net/url" "os" @@ -9,6 +10,10 @@ import ( "github.com/leosunmo/jt" ) +var ( + msg = flag.String("m", "", "Issue Description") +) + func main() { if err := run(); err != nil { fmt.Printf("%s\n", err) @@ -17,8 +22,17 @@ func main() { } func run() error { + // Parse flags + flag.Parse() + + var desc string + // Check if msg is set + if *msg != "" { + desc = *msg + } + // Read the issue summary from the command line arguments. - summary := strings.Join(os.Args[1:], " ") + summary := strings.Join(flag.Args(), " ") if summary == "" { return fmt.Errorf("please provide a summary for the issue") } @@ -49,7 +63,7 @@ func run() error { } c := jt.NewJiraClient(jc) - key, err := c.NewJIRATicket(summary) + key, err := c.NewJIRATicket(summary, desc) if err != nil { return fmt.Errorf("failed to create ticket: %s\n", err) } diff --git a/jira.go b/jira.go index f82deaa..a5b5195 100644 --- a/jira.go +++ b/jira.go @@ -84,6 +84,7 @@ type Content struct { Content []ContentBlock `json:"content,omitempty"` } type Description struct { + Version int `json:"version,omitempty"` Type string `json:"type,omitempty"` Content []Content `json:"content,omitempty"` } @@ -106,7 +107,7 @@ type CreatedIssueResponse struct { // NewJIRATicket creates a new JIRA ticket using the JIRA REST API v3. // The function returns the key of the created ticket and an error if the ticket could not be created. // https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-post -func (jc JiraClient) NewJIRATicket(summary string) (string, error) { +func (jc JiraClient) NewJIRATicket(summary string, desc string) (string, error) { // Build the body of the request using a CreateIssueRequest reqBody := CreateIssueRequest{} @@ -119,6 +120,7 @@ func (jc JiraClient) NewJIRATicket(summary string) (string, error) { } reqBody.Fields.Summary = summary + reqBody.Fields.Description = setDescription(desc) jsonBody, err := json.MarshalIndent(reqBody, "", " ") if err != nil { @@ -161,3 +163,22 @@ func (jc JiraClient) NewJIRATicket(summary string) (string, error) { return createResponse.Key, nil } + +func setDescription(msg string) *Description { + desc := Description{ + Type: "doc", + Version: 1, + Content: []Content{ + { + Type: "paragraph", + Content: []ContentBlock{ + { + Type: "text", + Text: msg, + }, + }, + }, + }, + } + return &desc +}