Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: activity name labels #888

Merged
merged 2 commits into from
Aug 20, 2024
Merged
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
91 changes: 63 additions & 28 deletions backend/api/getPriorityEntriesHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package api

import (
"encoding/json"
"fmt"
"urdr-api/internal/redmine"

"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -59,29 +61,6 @@ func getPriorityEntriesHandler(c *fiber.Ctx) error {
return err
}

// Now fetch the activities from Redmine and fill out the
// activity names.
c.Response().Reset()
if err := getProjectActivitiesHandler(c); err != nil {
// There was some error in the handler.
return err
} else if c.Response().StatusCode() != fiber.StatusOK {
// There was some error sent to us from Redmine.
return nil
}

activitiesResponse := struct {
Activities []struct {
Id int `json:"id"`
Name string `json:"name"`
} `json:"time_entry_activities"`
}{}

if err := json.Unmarshal(c.Response().Body(), &activitiesResponse); err != nil {
c.Response().Reset()
return c.SendStatus(fiber.StatusUnprocessableEntity)
}

var priorityEntries []PriorityEntry

for i, dbPriorityEntry := range dbPriorityEntries {
Expand All @@ -91,15 +70,71 @@ func getPriorityEntriesHandler(c *fiber.Ctx) error {
CustomName: dbPriorityEntry.Name,
IsHidden: dbPriorityEntry.IsHidden,
}
for _, activity := range activitiesResponse.Activities {
if activity.Id == priorityEntry.Activity.Id {
priorityEntry.Activity.Name = activity.Name
break
}
projectId, err := getProjectIdForIssue(c, priorityEntry.Issue.Id)
if err != nil {
return err
}

activityName, err := getActivityNameForIssue(c, priorityEntry.Issue.Id, projectId, priorityEntry.Activity.Id)
if err != nil {
return err
}
priorityEntry.Activity.Name = activityName

priorityEntries = append(priorityEntries, priorityEntry)
}

return c.JSON(priorityEntries)
}

func getProjectIdForIssue(c *fiber.Ctx, issueId int) (int, error) {
c.Response().Reset()
c.Request().URI().SetQueryString(
fmt.Sprintf("issue_id=%d", issueId))

if err := getIssuesHandler(c); err != nil {
} else if c.Response().StatusCode() != fiber.StatusOK {
return 0, nil
}
issuesResponse := struct {
Issues []Issue `json:"issues"`
}{}
if err := json.Unmarshal(c.Response().Body(), &issuesResponse); err != nil {
c.Response().Reset()
return 0, err
}

projectId := issuesResponse.Issues[0].Project.Id
return projectId, nil

}

func getActivityNameForIssue(c *fiber.Ctx, issueId int, projectId int, activityId int) (string, error) {
c.Response().Reset()
c.Context().URI().SetQueryString(fmt.Sprintf("project_id=%d&issue_id=%d", projectId, issueId))
if err := getProjectActivitiesHandler(c); err != nil {
log.Errorf("Error getProjectActivitiesHandler %v", err)
return "", err
} else if c.Response().StatusCode() != fiber.StatusOK {
log.Errorf("Error StatCode %v", err)
return "", err
}

projectEntryResponse := redmine.Project{}

if err := json.Unmarshal(c.Response().Body(), &projectEntryResponse); err != nil {
c.Response().Reset()
log.Errorf("Error Unmarshal projectEntryResponse %v", err)
return "", err
}

activityName := ""
for _, activity := range projectEntryResponse.TimeEntryActivities {
if activity.Id == activityId {
activityName = activity.Name
break
}
}

return activityName, nil
}
Loading