Skip to content

Commit

Permalink
Print user-friendly HTML URL of created bug or user story
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKienzler committed Sep 16, 2023
1 parent e06f036 commit d3543e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cmd/template/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,14 @@ func ApplyFlow(ctx context.Context, service *templates.Service, templateName str
log.Fatalf("Could not create from template '%s': %s", templateName, err)
}

coloredItemPrinter(template.Type, template.Title, createdItemHint)
createdItemHintWithURL := createdItemHint

url, err := service.WorkitemsService.GetWorkItemHTMLRef(userStory)
if err == nil && url != nil {
createdItemHintWithURL += fmt.Sprintf(" at %s", *url)
}

coloredItemPrinter(template.Type, template.Title, createdItemHintWithURL)

for i := range template.Tasks {
task := template.Tasks[i]
Expand Down
29 changes: 29 additions & 0 deletions pkg/workitems/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
ErrOffsetTooFarInFuture = errors.New("offset points to a non-existent iteration in the future")
ErrOffsetTooFarInPast = errors.New("offset points to a non-existent iteration in the past")
ErrTaskWithoutParent = errors.New("cannot create task underneath work item without parent")
ErrCouldNotAssertLinks = errors.New("could not assert the expected type from the workItems' Links field")
)

// addOp is a shortcut variable for the Add operation.
Expand Down Expand Up @@ -159,6 +160,34 @@ func (s *Service) CreateTaskUnderneath(ctx context.Context, title, description s
})
}

// GetWorkItemHTMLRef returns the URL pointing to the Azure DevOps link that
// shows the HTML view of the passed work item. That's the URL the user would
// want to visit in a browser. Returns an error if the necessary type assertion
// on the Links field fails or the expected map key isn't present. Returns nil
// in dry-run mode.
func (s *Service) GetWorkItemHTMLRef(workItem *workitemtracking.WorkItem) (*string, error) {
if s.DryRun {
return nil, nil
}

links, ok := workItem.Links.(map[string]interface{})
if !ok {
return nil, ErrCouldNotAssertLinks
}

html, ok := links["html"].(map[string]interface{})
if !ok {
return nil, ErrCouldNotAssertLinks
}

href, ok := html["href"].(string)
if !ok {
return nil, ErrCouldNotAssertLinks
}

return &href, nil
}

func (s *Service) buildBasicWorkItemJSONPatchDocument(title, description string, templateType config.TemplateType) []webapi.JsonPatchOperation {
fieldPathForDescription := ""
switch templateType {
Expand Down

0 comments on commit d3543e3

Please sign in to comment.