Skip to content

Commit

Permalink
Generialisations
Browse files Browse the repository at this point in the history
  * Added interface TaigaBaseObject
  * Added SetFilePath to Attachment struct
  * Simplified the CreateAttachment implementations
  • Loading branch information
theriverman committed May 18, 2020
1 parent 2033530 commit 51fe438
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 42 deletions.
23 changes: 23 additions & 0 deletions common.interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package taigo

// TaigaBaseObject represents the following Taiga object types:
/*
* Epic
* User Story
* Task
* Issue
These Taiga objects have the following must-have fields in common:
* ID
* Ref
* Version
* Subject
* Project
*/
type TaigaBaseObject interface {
GetID() int
GetRef() int
GetVersion() int
GetSubject() string
GetProject() int
}
5 changes: 5 additions & 0 deletions common.models.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ type Attachment struct {
filePath string // For package-internal use only
}

// SetFilePath takes the path to the file be uploaded
func (a *Attachment) SetFilePath(FilePath string) {
a.filePath = FilePath
}

// attachmentsQueryParams is a helper to transfer and render ObjectID and Project ID as URL query parameters
type attachmentsQueryParams struct {
ObjectID int `url:"object_id,omitempty"`
Expand Down
15 changes: 3 additions & 12 deletions epics.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,9 @@ func (s *EpicService) CreateRelatedUserStory(epicRelatedUSDetail *EpicRelatedUse

// CreateAttachment creates a new Epic attachment => https://taigaio.github.io/taiga-doc/dist/api.html#epics-create-attachment
//
// Mandatory parameters: `ObjectID`; `ProjectID`;
// **Note**: Set ObjectID to epic.ID
func (s *EpicService) CreateAttachment(attachment *Attachment, epic *Epic, filePath string) (*Attachment, error) {
url := s.client.APIURL + endpointEpics + "/attachments"
attachment.filePath = filePath
attachment.ObjectID = epic.ID
if attachment.Project == 0 && epic.Project > 0 {
attachment.Project = epic.Project
} else {
return nil, fmt.Errorf("Project.ID could not be fetched from any possible sources")
}
return newfileUploadRequest(s.client, url, attachment)
func (s *EpicService) CreateAttachment(attachment *Attachment, task *Task) (*Attachment, error) {
url := s.client.APIURL + endpointTasks + "/attachments"
return newfileUploadRequest(s.client, url, attachment, task)
}

// Clone takes an *Epic struct with loaded properties and duplicates it
Expand Down
26 changes: 26 additions & 0 deletions epics.models.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func genericToEpics(anyEpicObjectSlice interface{}) []Epic {

// Epic represents the mandatory fields of an Epic only
type Epic struct {
TaigaBaseObject
ID int `json:"id,omitempty"`
Ref int `json:"ref,omitempty"`
Version int `json:"version,omitempty"`
Expand All @@ -39,6 +40,31 @@ type Epic struct {
EpicDetailLIST *EpicDetailLIST
}

// GetID returns the ID
func (tgObj *Epic) GetID() int {
return tgObj.ID
}

// GetRef returns the Ref
func (tgObj *Epic) GetRef() int {
return tgObj.Ref
}

// GetVersion return the version
func (tgObj *Epic) GetVersion() int {
return tgObj.Version
}

// GetSubject returns the subject
func (tgObj *Epic) GetSubject() string {
return tgObj.Subject
}

// GetProject returns the project ID
func (tgObj *Epic) GetProject() int {
return tgObj.Project
}

// EpicDetailLIST -> Epic detail (LIST)
// https://taigaio.github.io/taiga-doc/dist/api.html#object-epic-detail-list
type EpicDetailLIST []struct {
Expand Down
13 changes: 3 additions & 10 deletions issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,7 @@ func (s *IssueService) List(queryParams *IssueQueryParams) ([]Issue, error) {
}

// CreateAttachment creates a new Issue attachment => https://taigaio.github.io/taiga-doc/dist/api.html#issues-create-attachment
func (s *IssueService) CreateAttachment(attachment *Attachment, issue *Issue, filePath string) (*Attachment, error) {
url := s.client.APIURL + endpointIssues + "/attachments"
attachment.filePath = filePath
attachment.ObjectID = issue.ID
if attachment.Project == 0 && issue.Project > 0 {
attachment.Project = issue.Project
} else {
return nil, fmt.Errorf("Project.ID could not be fetched from any possible sources")
}
return newfileUploadRequest(s.client, url, attachment)
func (s *IssueService) CreateAttachment(attachment *Attachment, task *Task) (*Attachment, error) {
url := s.client.APIURL + endpointTasks + "/attachments"
return newfileUploadRequest(s.client, url, attachment, task)
}
26 changes: 26 additions & 0 deletions issues.models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func genericToIssues(anyIssueObjectSlice interface{}) []Issue {

// Issue represents the mandatory fields of an Issue only
type Issue struct {
TaigaBaseObject
ID int `json:"id,omitempty"`
Ref int `json:"ref,omitempty"`
Version int `json:"version,omitempty"`
Expand All @@ -38,6 +39,31 @@ type Issue struct {
IssueDetailLIST *IssueDetailLIST
}

// GetID returns the ID
func (tgObj *Issue) GetID() int {
return tgObj.ID
}

// GetRef returns the Ref
func (tgObj *Issue) GetRef() int {
return tgObj.Ref
}

// GetVersion return the version
func (tgObj *Issue) GetVersion() int {
return tgObj.Version
}

// GetSubject returns the subject
func (tgObj *Issue) GetSubject() string {
return tgObj.Subject
}

// GetProject returns the project ID
func (tgObj *Issue) GetProject() int {
return tgObj.Project
}

// IssueDetailLIST -> Issue detail (LIST)
//
// https://taigaio.github.io/taiga-doc/dist/api.html#object-issue-detail-list
Expand Down
6 changes: 5 additions & 1 deletion requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ func evaluateResponseAndStatusCode() {
*/

// NOTE: responseBody must always be a pointer otherwise we lose the response data!
func newfileUploadRequest(c *Client, url string, attachment *Attachment) (*Attachment, error) {
func newfileUploadRequest(c *Client, url string, attachment *Attachment, tgBaseObj TaigaBaseObject) (*Attachment, error) {
// Map Object details into *Attachment
attachment.ObjectID = tgBaseObj.GetID()
attachment.Project = tgBaseObj.GetProject()

// Open file
f, err := os.Open(attachment.filePath)
if err != nil {
Expand Down
11 changes: 2 additions & 9 deletions tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,7 @@ func (s *TaskService) ListAttachments(task interface{}) (*[]Attachment, error) {
}

// CreateAttachment creates a new Task attachment => https://taigaio.github.io/taiga-doc/dist/api.html#tasks-create-attachment
func (s *TaskService) CreateAttachment(attachment *Attachment, task *Task, filePath string) (*Attachment, error) {
func (s *TaskService) CreateAttachment(attachment *Attachment, task *Task) (*Attachment, error) {
url := s.client.APIURL + endpointTasks + "/attachments"
attachment.filePath = filePath
attachment.ObjectID = task.ID
if attachment.Project == 0 && task.Project > 0 {
attachment.Project = task.Project
} else {
return nil, fmt.Errorf("Project.ID could not be fetched from any possible sources")
}
return newfileUploadRequest(s.client, url, attachment)
return newfileUploadRequest(s.client, url, attachment, task)
}
26 changes: 26 additions & 0 deletions tasks.models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func genericToTasks(anyTaskObjectSlice interface{}) []Task {

// Task represents a subset of (TaskDetail, TaskDetailGET, TaskDetailLIST)
type Task struct {
TaigaBaseObject
ID int
AssignedTo int `json:"assigned_to,omitempty"`
BlockedNote string `json:"blocked_note,omitempty"`
Expand All @@ -40,6 +41,31 @@ type Task struct {
TaskDetailLIST *TaskDetailLIST
}

// GetID returns the ID
func (tgObj *Task) GetID() int {
return tgObj.ID
}

// GetRef returns the Ref
func (tgObj *Task) GetRef() int {
return tgObj.Ref
}

// GetVersion return the version
func (tgObj *Task) GetVersion() int {
return tgObj.Version
}

// GetSubject returns the subject
func (tgObj *Task) GetSubject() string {
return tgObj.Subject
}

// GetProject returns the project ID
func (tgObj *Task) GetProject() int {
return tgObj.Project
}

// TaskDetailLIST => https://taigaio.github.io/taiga-doc/dist/api.html#object-task-detail-list
type TaskDetailLIST []struct {
AssignedTo int `json:"assigned_to,omitempty"`
Expand Down
13 changes: 3 additions & 10 deletions user_stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,9 @@ func (s *UserStoryService) Delete(userStoryID int) error {
}

// CreateAttachment creates a new UserStory attachment => https://taigaio.github.io/taiga-doc/dist/api.html#user-stories-create-attachment
func (s *UserStoryService) CreateAttachment(attachment *Attachment, userStory *UserStory, filePath string) (*Attachment, error) {
url := s.client.APIURL + endpointUserStories + "/attachments"
attachment.filePath = filePath
attachment.ObjectID = userStory.ID
if attachment.Project == 0 && userStory.Project > 0 {
attachment.Project = userStory.Project
} else {
return nil, fmt.Errorf("Project.ID could not be fetched from any possible sources")
}
return newfileUploadRequest(s.client, url, attachment)
func (s *UserStoryService) CreateAttachment(attachment *Attachment, task *Task) (*Attachment, error) {
url := s.client.APIURL + endpointTasks + "/attachments"
return newfileUploadRequest(s.client, url, attachment, task)
}

/*
Expand Down
26 changes: 26 additions & 0 deletions user_stories.models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func genericToUserStories(anyUsObjectSlice interface{}) []UserStory {

// UserStory represents a subset of (UserStoryDetail, UserStoryDetailGET, UserStoryDetailLIST) structs for creating new objects
type UserStory struct {
TaigaBaseObject
ID int `json:"id,omitempty"`
Ref int `json:"ref,omitempty"`
Version int `json:"version,omitempty"`
Expand All @@ -41,6 +42,31 @@ type UserStory struct {
UserStoryDetailLIST *UserStoryDetailLIST
}

// GetID returns the ID
func (tgObj *UserStory) GetID() int {
return tgObj.ID
}

// GetRef returns the Ref
func (tgObj *UserStory) GetRef() int {
return tgObj.Ref
}

// GetVersion return the version
func (tgObj *UserStory) GetVersion() int {
return tgObj.Version
}

// GetSubject returns the subject
func (tgObj *UserStory) GetSubject() string {
return tgObj.Subject
}

// GetProject returns the project ID
func (tgObj *UserStory) GetProject() int {
return tgObj.Project
}

// UserStoryDetailLIST => https://taigaio.github.io/taiga-doc/dist/api.html#object-userstory-detail-list
type UserStoryDetailLIST []struct {
AssignedTo int `json:"assigned_to,omitempty"`
Expand Down
16 changes: 16 additions & 0 deletions wiki.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package taigo

var wikiURI = "/wiki"

// WikiService is a handle to actions related to Tasks
//
// https://taigaio.github.io/taiga-doc/dist/api.html#tasks
type WikiService struct {
client *Client
}

// CreateAttachment creates a new Wiki attachment -> https://taigaio.github.io/taiga-doc/dist/api.html#wiki-create-attachment
func (s *WikiService) CreateAttachment(attachment *Attachment, wikiPage *WikiPage) (*Attachment, error) {
url := s.client.APIURL + endpointTasks + "/attachments"
return newfileUploadRequest(s.client, url, attachment, wikiPage)
}
42 changes: 42 additions & 0 deletions wiki.models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package taigo

import "time"

// WikiPage -> https://taigaio.github.io/taiga-doc/dist/api.html#object-wiki-detail
type WikiPage struct {
TaigaBaseObject
Content string `json:"content"`
CreatedDate time.Time `json:"created_date"`
Editions int `json:"editions"`
HTML string `json:"html"`
ID int `json:"id"`
IsWatcher bool `json:"is_watcher"`
LastModifier int `json:"last_modifier"`
ModifiedDate time.Time `json:"modified_date"`
Owner int `json:"owner"`
Project int `json:"project"`
ProjectExtraInfo struct {
ID int `json:"id"`
LogoSmallURL string `json:"logo_small_url"`
Name string `json:"name"`
Slug string `json:"slug"`
} `json:"project_extra_info"`
Slug string `json:"slug"`
TotalWatchers int `json:"total_watchers"`
Version int `json:"version"`
}

// GetID returns the ID
func (tgObj *WikiPage) GetID() int {
return tgObj.ID
}

// GetVersion return the version
func (tgObj *WikiPage) GetVersion() int {
return tgObj.Version
}

// GetProject returns the project ID
func (tgObj *WikiPage) GetProject() int {
return tgObj.Project
}

0 comments on commit 51fe438

Please sign in to comment.