Skip to content

Commit

Permalink
Merge pull request #888 from NBISweden/fix/labels-issues
Browse files Browse the repository at this point in the history
fix: activity name labels
  • Loading branch information
KattisLej authored Aug 20, 2024
2 parents 736f976 + 128cfbe commit d71ea15
Showing 1 changed file with 63 additions and 28 deletions.
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
}

0 comments on commit d71ea15

Please sign in to comment.