Skip to content

Commit

Permalink
feat: support full bulk ticket import
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyh committed Apr 22, 2021
1 parent 3701905 commit e65f9a8
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Go-Zendesk

[![CircleCI](https://circleci.com/gh/MEDIGO/go-zendesk.svg?style=shield)](https://circleci.com/gh/MEDIGO/go-zendesk)
[![GoDoc](http://godoc.org/github.com/MEDIGO/go-zendesk/zendesk?status.png)](http://godoc.org/github.com/MEDIGO/go-zendesk/zendesk)
[![GoDoc](http://godoc.org/github.com/flume/go-zendesk/zendesk?status.png)](http://godoc.org/github.com/flume/go-zendesk/zendesk)

Go-Zendesk is a [Zendesk Core API](https://developer.zendesk.com/rest_api/docs/core/introduction) client library for Go.

This library is used internally at MEDIGO and the API resources are being implemented as needed.
This library is used internally at [~~MEDIGO~~](https://github.com/MEDIGO/go-zendesk) Flume and the API resources are being implemented as needed.

**It's work in progress. Use with caution.**

Expand All @@ -17,7 +16,7 @@ package main
import (
"log"

"github.com/MEDIGO/go-zendesk/zendesk"
"github.com/flume/go-zendesk/zendesk"
)

func main() {
Expand All @@ -33,7 +32,7 @@ func main() {
}
```

Find the complete API on https://godoc.org/github.com/MEDIGO/go-zendesk/zendesk#NewClient
Find the complete API on https://godoc.org/github.com/flume/go-zendesk/zendesk#NewClient


## Development
Expand Down Expand Up @@ -66,5 +65,5 @@ Please note that integration tests will create and alter entities in the configu
You most likely want to run them against a [Zendesk Sandbox](https://support.zendesk.com/hc/en-us/articles/203661826-Testing-changes-in-your-sandbox-Enterprise-) instance.

## Copyright and license

Copyright © 2020-2021 Flume Health, Inc. go-zendesk is licensed under the MIT License. See LICENSE for the full license text.
Copyright © 2017 MEDIGO GmbH. go-zendesk is licensed under the MIT License. See LICENSE for the full license text.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/MEDIGO/go-zendesk
module github.com/flume/go-zendesk

go 1.14

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
Expand Down
41 changes: 35 additions & 6 deletions zendesk/ticket.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zendesk

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -52,7 +53,7 @@ type Ticket struct {
RemoveTags []string `json:"remove_tags,omitempty"`
}

type BulkImportTicket struct {
type ImportTicket struct {
AssigneeID *int64 `json:"assignee_id,omitempty"`
Comments []TicketComment `json:"comments,omitempty"`
CollaboratorIDs []int `json:"collaborator_ids,omitempty"`
Expand Down Expand Up @@ -106,17 +107,45 @@ type Requester struct {
Email *string `json:"email,omitempty"`
}

type BulkPayload struct {
Ticket *BulkImportTicket `json:"ticket,omitempty"`
type ImportTicketPayload struct {
Ticket *ImportTicket `json:"ticket,omitempty"`
}

func (c *client) BulkImportTicket(bulk *BulkImportTicket) (*BulkImportTicket, error) {
in := &BulkPayload{Ticket: bulk}
out := new(BulkPayload)
func (c *client) ImportTicket(bulk *ImportTicket) (*ImportTicket, error) {
in := &ImportTicketPayload{Ticket: bulk}
out := new(ImportTicketPayload)
err := c.post("/api/v2/imports/tickets.json", in, out)
return out.Ticket, err
}

type BulkImportTicketPayload struct {
Tickets []ImportTicket `json:"tickets"`
}

func (c *client) BulkImportTickets(tickets []ImportTicket) ([]JobStatus, error) {
floor := 0
var out []JobStatus
var errs []error
for floor+100 <= len(tickets) {
payload := BulkImportTicketPayload{Tickets: tickets[floor : floor+100]}
status := new(JobStatus)
err := c.post("/api/v2/imports/tickets/create_many.json", &payload, status)
if err != nil {
errs = append(errs, err)
} else {
out = append(out, *status)
}
floor += 100
}

errStrOut := ""
for _, err := range errs {
errStrOut = fmt.Sprintf("%s | %s", errStrOut, err.Error())
}

return out, errors.New(errStrOut)
}

func (c *client) ShowTicket(id int64) (*Ticket, error) {
out := new(APIPayload)
err := c.get(fmt.Sprintf("/api/v2/tickets/%d.json", id), out)
Expand Down
1 change: 1 addition & 0 deletions zendesk/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (c *client) ShowManyUsers(ids []int64) ([]User, error) {
err := c.get(fmt.Sprintf("/api/v2/users/show_many.json?ids=%s", strings.Join(sids, ",")), out)
return out.Users, err
}

// ShowManyUsersByExternalIDs accepts a comma-separated list of external ids.
//
// Zendesk Core API docs: https://developer.zendesk.com/rest_api/docs/support/users#show-many-users
Expand Down
3 changes: 2 additions & 1 deletion zendesk/zendesk.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type Client interface {
CreateOrganizationMembership(*OrganizationMembership) (*OrganizationMembership, error)
CreateOrUpdateOrganization(*Organization) (*Organization, error)
CreateOrUpdateUser(*User) (*User, error)
BulkImportTicket(bulk *BulkImportTicket) (*BulkImportTicket, error)
ImportTicket(ticket *ImportTicket) (*ImportTicket, error)
BulkImportTickets(tickets []ImportTicket) ([]JobStatus, error)
CreateTicket(*Ticket) (*Ticket, error)
CreateUser(*User) (*User, error)
CreateGroup(*Group) (*Group, error)
Expand Down
2 changes: 1 addition & 1 deletion zendesk/zendesk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"net/http/httptest"
"testing"

"github.com/MEDIGO/go-zendesk/zendesk"
"github.com/flume/go-zendesk/zendesk"
"github.com/stretchr/testify/require"
)

Expand Down

0 comments on commit e65f9a8

Please # to comment.