Skip to content

Commit

Permalink
[GH-14] Return ticketNumber once a ticket has been created.
Browse files Browse the repository at this point in the history
  • Loading branch information
030 committed Nov 5, 2021
1 parent 7d00704 commit 9a4b152
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.7.0] - 2021-11-05

### Added

- Return ticketNumber once a ticket has been created.

## [0.6.1] - 2021-11-04

### Fixed
Expand Down Expand Up @@ -58,7 +64,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Issue JQL queries.
- Describe in README how to use the jops library.

[Unreleased]: https://github.com/030/jops/compare/0.6.1...HEAD
[Unreleased]: https://github.com/030/jops/compare/0.7.0...HEAD
[0.7.0]: https://github.com/030/jops/compare/0.6.1...0.7.0
[0.6.1]: https://github.com/030/jops/compare/0.6.0...0.6.1
[0.6.0]: https://github.com/030/jops/compare/0.5.0...0.6.0
[0.5.0]: https://github.com/030/jops/compare/0.4.0...0.5.0
Expand Down
3 changes: 2 additions & 1 deletion cmd/jops/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var createCmd = &cobra.Command{
fmt.Println("create called")

j := create.Jira{User: user, Pass: pass, FQDN: fqdn, Priority: priority, Project: project, Summary: summary, Description: description, Labels: labels}
if err := j.Create(); err != nil {
_, err := j.Create()
if err != nil {
log.Fatal(err)
}
},
Expand Down
37 changes: 23 additions & 14 deletions internal/pkg/httprequest/httprequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"

log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)

type Jira struct {
Expand All @@ -31,34 +32,42 @@ func (j *Jira) Construct() (*http.Request, error) {
return req, nil
}

func (j *Jira) Initiate(req *http.Request) error {
func (j *Jira) Initiate(req *http.Request) (string, error) {
req.SetBasicAuth(j.User, j.Pass)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
return "", err
}
defer resp.Body.Close()
log.Info(resp.StatusCode)
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err

var ticketNumber = ""
if resp.StatusCode == http.StatusCreated {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
bodyString := string(bodyBytes)
log.Info(bodyString)

value := gjson.Get(bodyString, "key")
ticketNumber = value.String()
}
bodyString := string(bodyBytes)
log.Info(bodyString)
return nil

return ticketNumber, nil
}

func (j *Jira) ConstructAndInitiate() error {
func (j *Jira) ConstructAndInitiate() (string, error) {
h, err := j.Construct()
if err != nil {
return err
return "", err
}

if err := j.Initiate(h); err != nil {
return err
ticketNumber, err := j.Initiate(h)
if err != nil {
return "", err
}
return nil
return ticketNumber, nil
}
20 changes: 15 additions & 5 deletions pkg/jira/v2/create/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package create

import "github.com/030/jops/internal/pkg/httprequest"
import (
"fmt"

func (j *Jira) Create() error {
"github.com/030/jops/internal/pkg/httprequest"
log "github.com/sirupsen/logrus"
)

func (j *Jira) Create() (string, error) {
fields := Fields{
Description: j.Description,
Summary: j.Summary,
Expand All @@ -16,9 +21,14 @@ func (j *Jira) Create() error {
}

htj := httprequest.Jira{APIVersion: "2", Data: data, Method: "POST", FQDN: j.FQDN, User: j.User, Pass: j.Pass}
if err := htj.ConstructAndInitiate(); err != nil {
return err
ticketNumber, err := htj.ConstructAndInitiate()
if err != nil {
return "", err
}
if ticketNumber == "" {
return "", fmt.Errorf("ticketNumber should not be empty")
}
log.Infof("Ticket: '%s' has been created", ticketNumber)

return nil
return ticketNumber, nil
}
3 changes: 2 additions & 1 deletion pkg/jira/v2/done/done.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func (j *Jira) Done() error {

uri := "/" + j.TicketNumber + "/transitions"
htj := httprequest.Jira{URI: uri, APIVersion: "2", Data: data, Method: "POST", FQDN: j.FQDN, User: j.User, Pass: j.Pass}
if err := htj.ConstructAndInitiate(); err != nil {
_, err = htj.ConstructAndInitiate()
if err != nil {
return err
}

Expand Down

0 comments on commit 9a4b152

Please sign in to comment.