Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/actions/setup-go/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Setup Go
description: Configure git auth and install Go

inputs:
gh-token:
description: GitHub token for private module access
required: true
cache:
description: Whether to enable Go module caching
required: false
default: "true"

runs:
using: composite
steps:
- name: Setup git
shell: bash
run: git config --global url."https://${{ inputs.gh-token }}@github.com/".insteadOf "https://github.com/"

- uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: ${{ inputs.cache }}
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI

on:
push:
branches: [main]
pull_request:

env:
GOPRIVATE: github.com/teamwork/*
LANG: en_US.UTF-8
GO_VERSION: "1.26"

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-go
with:
gh-token: ${{ secrets.GH_TOKEN }}
cache: "false"

- uses: golangci/golangci-lint-action@v9
with:
version: latest

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: ./.github/actions/setup-go
with:
gh-token: ${{ secrets.GH_TOKEN }}

- name: Run tests
run: go test -race -count=1 ./...
16 changes: 12 additions & 4 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ func Call[T any, R any, L any](ctx context.Context, service Service[T, R, L], ac
log.Fatal(err)
}

enc.Encode(item)
if err := enc.Encode(item); err != nil {
log.Fatal(err)
}

case "list":
items, err := service.List(ctx, nil)
if err != nil {
log.Fatal(err)
}

enc.Encode(items)
if err := enc.Encode(items); err != nil {
log.Fatal(err)
}

case "create":
item := createItem()
Expand All @@ -50,7 +54,9 @@ func Call[T any, R any, L any](ctx context.Context, service Service[T, R, L], ac
return
}

enc.Encode(created)
if err := enc.Encode(created); err != nil {
log.Fatal(err)
}

case "update":
if id == 0 {
Expand All @@ -62,7 +68,9 @@ func Call[T any, R any, L any](ctx context.Context, service Service[T, R, L], ac
log.Print(err)
return
}
enc.Encode(updated)
if err := enc.Encode(updated); err != nil {
log.Fatal(err)
}

default:
log.Fatalf("Unsupported action: %s", action)
Expand Down
6 changes: 4 additions & 2 deletions client/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func (s *FileService) Upload(ctx context.Context, file *models.FileResponse, f [
return fmt.Errorf("copy file data: %w", err)
}

writer.Close()
if err := writer.Close(); err != nil {
return fmt.Errorf("close multipart writer: %w", err)
}

uploadURL := ""
if file.URL != nil {
Expand All @@ -110,7 +112,7 @@ func (s *FileService) Upload(ctx context.Context, file *models.FileResponse, f [
if err != nil {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
Expand Down
38 changes: 38 additions & 0 deletions client/helpdocarticles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package client

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/sonh/qs"
"github.com/teamwork/desksdkgo/models"
)

// HelpDocArticleService handles help doc article-related operations
type HelpDocArticleService struct {
*Service[models.HelpDocArticleResponse, models.HelpDocArticlesResponse]
client *Client
}

// NewHelpDocArticleService creates a new help doc article service
func NewHelpDocArticleService(client *Client) *HelpDocArticleService {
return &HelpDocArticleService{
Service: NewService[models.HelpDocArticleResponse, models.HelpDocArticlesResponse](client, NewDefaultPathHandler("helpdocssites/helpdocarticles")),
client: client,
}
}

Expand All @@ -38,3 +44,35 @@ func (s *HelpDocArticleService) Create(ctx context.Context, article *models.Help
func (s *HelpDocArticleService) Update(ctx context.Context, id int, article *models.HelpDocArticleResponse) (*models.HelpDocArticleResponse, error) {
return s.Service.Update(ctx, id, article)
}

// Search searches for help doc articles based on filter parameters
func (s *HelpDocArticleService) Search(ctx context.Context, filter *models.SearchHelpdocsFilter) (*models.HelpDocArticlesResponse, error) {
encoder := qs.NewEncoder()
values, err := encoder.Values(filter)
if err != nil {
return nil, err
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet,
fmt.Sprintf("%s/search/helpdocs.json?%s", s.client.baseURL, values.Encode()), nil)
if err != nil {
return nil, err
}

resp, err := s.client.doRequest(ctx, req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var resources models.HelpDocArticlesResponse
if err := json.NewDecoder(resp.Body).Decode(&resources); err != nil {
return nil, err
}

return &resources, nil
}
2 changes: 1 addition & 1 deletion client/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *MessageService) CreateForTicket(ctx context.Context, ticketID int, mess
if err != nil {
return nil, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
b, err := io.ReadAll(resp.Body)
Expand Down
8 changes: 4 additions & 4 deletions client/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *Service[T, L]) Get(ctx context.Context, id int, params url.Values) (*T,
s.logError("request failed", slog.Any("error", err), slog.String("method", http.MethodGet), slog.String("url", req.URL.String()))
return nil, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -100,7 +100,7 @@ func (s *Service[T, L]) List(ctx context.Context, params url.Values) (*L, error)
s.logError("request failed", slog.Any("error", err), slog.String("method", http.MethodGet), slog.String("url", req.URL.String()))
return nil, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -146,7 +146,7 @@ func (s *Service[T, L]) Create(ctx context.Context, resource *T) (*T, error) {
s.logError("request failed", slog.Any("error", err), slog.String("method", http.MethodPost), slog.String("url", req.URL.String()))
return nil, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
b, err := io.ReadAll(resp.Body)
Expand Down Expand Up @@ -209,7 +209,7 @@ func (s *Service[T, L]) Update(ctx context.Context, id int, resource *T) (*T, er
s.logError("request failed", slog.Any("error", err), slog.String("method", method), slog.String("url", req.URL.String()))
return nil, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
Expand Down
2 changes: 1 addition & 1 deletion client/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *TicketService) Search(ctx context.Context, filter *models.SearchTickets
if err != nil {
return nil, err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/teamwork/desksdkgo

go 1.24.2
go 1.26

require (
github.com/brianvoe/gofakeit/v7 v7.2.1
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func generateData(
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(resp)
if err := enc.Encode(resp); err != nil {
log.Fatalf("Failed to encode response: %v", err)
}
return
}
api.Call(ctx, c.Tickets, action, id, func() *models.TicketResponse {
Expand Down Expand Up @@ -328,7 +330,9 @@ func generateData(
log.Fatalf("Failed to upload file: %v", err)
}

enc.Encode(resp)
if err := enc.Encode(resp); err != nil {
log.Fatalf("Failed to encode response: %v", err)
}
case "spamlists":
api.Call(ctx, c.Spamlists, action, id, func() *models.SpamlistResponse {
resp := &models.SpamlistResponse{Spamlist: models.Spamlist{
Expand Down
10 changes: 10 additions & 0 deletions models/helpdocarticle.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package models

type SearchHelpdocsFilter struct {
Search string `qs:"search,omitempty"`
SiteID int64 `qs:"siteId,omitempty"`
Status string `qs:"status,omitempty"`
CategoryID int64 `qs:"categoryId,omitempty"`
Page int `qs:"page,omitempty"`
PageSize int `qs:"pageSize,omitempty"`
Includes []string `qs:"includes,omitempty"`
}

type HelpDocArticle struct {
BaseEntity
Helpdocsite EntityRef `json:"helpdocsite"`
Expand Down
Loading