Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
adding functionality to assign a user to a story; also functionality …
Browse files Browse the repository at this point in the history
…for getting URLs to entities (#26)
  • Loading branch information
Luke Reed authored Dec 9, 2020
1 parent aed8570 commit 5167020
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func init() {
// Client is the API client for Targetprocess. Create this using NewClient.
// This can also be constructed manually but it isn't recommended.
type Client struct {
// account is a place to hold the account name for this instance
account string

// baseURL is the base URL for v1 API requests.
baseURL *url.URL

Expand Down Expand Up @@ -95,6 +98,7 @@ func NewClient(account, token string) (*Client, error) {
return nil, err
}
return &Client{
account: account,
baseURL: baseURL,
baseURLReadOnly: baseURLReadOnly,
Client: c,
Expand Down
31 changes: 26 additions & 5 deletions general.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@

package targetprocess

import (
"fmt"
)

// DateTime currently does nothing special but will represent the TP DateTime objects
type DateTime string

// AssignedUser is used in UserStories and potentially other places. Returns a list of user assignments.
// Assignments is a generic entity that lists assignments
type Assignments struct {
Items []Assignment `json:",omitempty"`
}

// Assignment is a generic entity that lists a single assignment
type Assignment struct {
ID int32 `json:"Id,omitempty"`
ResourceType string `json:",omitempty"`
GeneralUser *User `json:",omitempty"`
}

// AssignedUser is used in UserStories and potentially other places. Returns a list of user assignments
type AssignedUser struct {
Items []UserAssignment
Items []UserAssignment `json:",omitempty"`
}

// UserAssignment has its own unique Id and also includes a reference to a user, which also has an Id
type UserAssignment struct {
User
}

// TeamAssignment has it's own unique Id and also includes a reference to the team, which also has an Id
Expand All @@ -30,7 +51,7 @@ type TeamAssignment struct {
Team *Team `json:",omitempty"`
}

// UserAssignment has its own unique Id and also includes a reference to a user, which also has an Id
type UserAssignment struct {
User
// GenerateURL takes an account name and entityID and returns a URL that should work in a browser
func GenerateURL(account string, entityID int32) string {
return fmt.Sprintf("https://{%s}.tpondemand.com/entity/%d/RestUI/board.aspx", account, entityID)
}
41 changes: 31 additions & 10 deletions userstory.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type UserStory struct {
TimeRemain float32 `json:",omitempty"`
LastStateChangeDate DateTime `json:",omitempty"`
InitialEstimate float32 `json:",omitempty"`
Assignments *Assignments `json:",omitempty"`
ResponsibleTeam *TeamAssignment `json:",omitempty"`
Team *Team `json:",omitempty"`
EntityState *EntityState `json:",omitempty"`
Expand Down Expand Up @@ -112,6 +113,18 @@ func (us *UserStory) SetFeature(feature string) error {
return nil
}

// SetAssignedUserID assigns the UserStory to a User based on their ID number
func (us *UserStory) SetAssignedUserID(userID int32) {
u := User{
ID: userID,
}
au := Assignment{
GeneralUser: &u,
}
assignments := Assignments{Items: []Assignment{au}}
us.Assignments = &assignments
}

// GetUserStories will return all user stories
//
// Use with caution if you have a lot and are not setting the MaxPerPage to a high number
Expand Down Expand Up @@ -142,25 +155,27 @@ func (c *Client) GetUserStories(page bool, filters ...QueryFilter) ([]UserStory,
}

// Create takes a UserStory struct and crafts a POST to make it so in TP
// it returns the ID of the UserStory created
func (us UserStory) Create() (int32, error) {
// it returns the ID of the UserStory created as well as a link to the entity
// on the Target Process frontend
func (us UserStory) Create() (int32, string, error) {
client := us.client
resp := &struct {
ID int32 `json:"Id"`
}{}
body, err := json.Marshal(us)
if err != nil {
return 0, errors.Wrap(err, fmt.Sprintf("error marshaling POST body for UserStory %s", us.Name))
return 0, "", errors.Wrap(err, fmt.Sprintf("error marshaling POST body for UserStory %s", us.Name))
}

client.debugLog(fmt.Sprintf("Attempting to POST UserStory: %+v", us))
err = client.Post(resp, "UserStory", nil, body)
if err != nil {
return 0, errors.Wrap(err, fmt.Sprintf("error POSTing UserStory %s", us.Name))
return 0, "", errors.Wrap(err, fmt.Sprintf("error POSTing UserStory %s", us.Name))
}
client.debugLog("[targetprocess] Successfully POSTed UserStory")
client.debugLog(fmt.Sprintf("[targetprocess] UserStory created. ID: %d", resp.ID))
return resp.ID, nil
link := GenerateURL(client.account, resp.ID)
return resp.ID, link, nil
}

// NewUserStoryList returns a UserStoryList from a list of user stories.
Expand All @@ -173,24 +188,30 @@ func (c *Client) NewUserStoryList(list []UserStory) *UserStoryList {
}

// Create posts a list of user stories to create them
func (usl UserStoryList) Create() ([]int32, error) {
// returns a list of entity IDs along with a list of links to them
func (usl UserStoryList) Create() ([]int32, []string, error) {
client := usl.client
resp := &UserStoryResponse{}
body, err := json.Marshal(usl.Stories)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error marshaling POST body for UserStoryList %v", usl))
return nil, nil, errors.Wrap(err, fmt.Sprintf("error marshaling POST body for UserStoryList %v", usl))
}
client.debugLog(fmt.Sprintf("[targetprocess] Attempting to POST UserStory: %+v", usl))
err = client.Post(resp, "UserStories/bulk", nil, body)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("error POSTing UserStoryList %v", usl))
return nil, nil, errors.Wrap(err, fmt.Sprintf("error POSTing UserStoryList %v", usl))
}
client.debugLog("[targetprocess] Successfully POSTed UserStoryList")

var ret []int32
var (
ret []int32
links []string
)

for _, story := range resp.Items {
ret = append(ret, story.ID)
links = append(links, GenerateURL(client.account, story.ID))
}
client.debugLog(fmt.Sprintf("[targetprocess] User stories created with IDs: %v", ret))
return ret, nil
return ret, links, nil
}

0 comments on commit 5167020

Please sign in to comment.