Skip to content

Commit

Permalink
feat: fetch activities from /projects
Browse files Browse the repository at this point in the history
  • Loading branch information
jonandernovella committed Sep 8, 2023
1 parent 55106ae commit 39366d2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
2 changes: 1 addition & 1 deletion backend/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Setup() *fiber.App {

app.Get("/api/issues", getIssuesHandler)

app.Get("/api/activities", getActivitiesHandler)
app.Get("/api/activities", getProjectActivitiesHandler)

app.Get("/api/priority_entries", getPriorityEntriesHandler)

Expand Down
42 changes: 14 additions & 28 deletions backend/api/getActivitiesHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@ package api
import (
"encoding/json"
"fmt"
"log"
"sort"
"strconv"
"urdr-api/internal/config"
"urdr-api/internal/redmine"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/proxy"
)

type ProjectsResponse struct {
TimeEntryActivities []struct {
Id int `json:"id"`
Name string `json:"name"`
} `json:"time_entry_activities"`
}

// getActivitiesHandler godoc
// @Summary Get a list of activities from the Redmine projects endpoint
// @Accept json
Expand All @@ -28,7 +21,7 @@ type ProjectsResponse struct {
// @Router /api/activities [get]
// @Param project_id query string false "Project ID" default(0)
// @Param issue_id query string false "Issue ID" default(0)
func getActivitiesHandler(c *fiber.Ctx) error {
func getProjectActivitiesHandler(c *fiber.Ctx) error {
redmineProjectId, err := strconv.Atoi(c.Query("project_id", "0"))
if err != nil {
return c.SendStatus(fiber.StatusInternalServerError)
Expand All @@ -42,13 +35,12 @@ func getActivitiesHandler(c *fiber.Ctx) error {
return err
}

log.Println("redmineProjectId:", redmineProjectId)

var redmineURL string

// If we don't have a real project ID, return an empty list of activities.
if redmineProjectId == 0 {
redmineURL = fmt.Sprintf("%s/enumerations/time_entry_activities.json",
config.Config.Redmine.URL)
emptyListResponse := redmine.ProjectEntry{}
return c.JSON(emptyListResponse)
} else {
redmineURL = fmt.Sprintf("%s/projects/%d.json?include=time_entry_activities",
config.Config.Redmine.URL, redmineProjectId)
Expand All @@ -61,33 +53,27 @@ func getActivitiesHandler(c *fiber.Ctx) error {
return nil
}

activitiesResponse := ProjectsResponse{}
activitiesResponse := redmine.ProjectEntry{}

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

// Sort the activities list alphabetically on the name.
sort.Slice(activitiesResponse.TimeEntryActivities, func(i, j int) bool {
return activitiesResponse.TimeEntryActivities[i].Name <
activitiesResponse.TimeEntryActivities[j].Name
sort.Slice(activitiesResponse.Project.TimeEntryActivities, func(i, j int) bool {
return activitiesResponse.Project.TimeEntryActivities[i].Name <
activitiesResponse.Project.TimeEntryActivities[j].Name
})

// Bypass filtering if we don't have a real issue ID.
if redmineIssueId == 0 {
// Return all activities.
return c.JSON(activitiesResponse)
}

filteredActivities := ProjectsResponse{}
filteredActivities := redmine.ProjectEntry{}

for _, activity := range activitiesResponse.TimeEntryActivities {
for _, activity := range activitiesResponse.Project.TimeEntryActivities {
if db.IsValidEntry(redmineIssueId, activity.Id) {
filteredActivities.TimeEntryActivities =
append(filteredActivities.TimeEntryActivities, activity)
filteredActivities.Project.TimeEntryActivities =
append(filteredActivities.Project.TimeEntryActivities, activity)
}
}

return c.JSON(filteredActivities)
return c.JSON(filteredActivities.Project)
}
2 changes: 1 addition & 1 deletion backend/api/getPriorityEntriesHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func getPriorityEntriesHandler(c *fiber.Ctx) error {
// Now fetch the activities from Redmine and fill out the
// activity names.
c.Response().Reset()
if err := getActivitiesHandler(c); err != nil {
if err := getProjectActivitiesHandler(c); err != nil {
// There was some error in the handler.
return err
} else if c.Response().StatusCode() != fiber.StatusOK {
Expand Down
33 changes: 21 additions & 12 deletions backend/api/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,19 @@ func Test_Handlers(t *testing.T) {

createdEntry, _ := json.Marshal(entryResult)
fetchedEntries, _ := json.Marshal(entriesResult)

entryActs := redmine.TimeEntryActivitiesResult{
TimeEntryActivities: []redmine.TimeEntryActivity{
{
Id: 1,
Name: "Test activity",
IsDefault: true,
projectEntry := redmine.ProjectEntry{
Project: redmine.Project{
TimeEntryActivities: []redmine.TimeEntryActivity{
{
Id: 1,
Name: "test activity",
IsDefault: true,
},
},
},
}

entryActsResponse, _ := json.Marshal(entryActs)
projectActivities, _ := json.Marshal(projectEntry)

issueAct := []api.PriorityEntry{
{
Expand Down Expand Up @@ -180,7 +181,7 @@ func Test_Handlers(t *testing.T) {
case "/issues.json":
_, err = w.Write(issuesResponse)
case "/projects/1.json":
_, err = w.Write(entryActsResponse)
_, err = w.Write(projectActivities)
default:
log.Debugf("%s.\n", endpoint)
_, err = w.Write(nil)
Expand Down Expand Up @@ -315,27 +316,35 @@ func Test_Handlers(t *testing.T) {
{
name: "Entry activities",
method: "GET",
endpoint: "/api/activities",
endpoint: "/api/activities?project_id=1&issue_id=1",
testRedmine: fakeRedmine,
useSessionHeader: true,
statusCode: fiber.StatusOK,
},
{
name: "Entry activities 401",
method: "GET",
endpoint: "/api/activities",
endpoint: "/api/activities?project_id=1&issue_id=1",
testRedmine: fakeRedmine,
useSessionHeader: false,
statusCode: fiber.StatusUnauthorized,
},
{
name: "Entry activities 422",
method: "GET",
endpoint: "/api/activities",
endpoint: "/api/activities?project_id=1&issue_id=1",
testRedmine: badRedmine,
useSessionHeader: true,
statusCode: fiber.StatusUnprocessableEntity,
},
{
name: "Entry activities no params",
method: "GET",
endpoint: "/api/activities",
testRedmine: badRedmine,
useSessionHeader: true,
statusCode: fiber.StatusOK,
},
{
name: "priority_entries POST",
method: "POST",
Expand Down
7 changes: 7 additions & 0 deletions backend/internal/redmine/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,10 @@ type Group struct {
Id int `json:"id"`
Name string `json:"name"`
}
type Project struct {
TimeEntryActivities []TimeEntryActivity `json:"time_entry_activities"`
}

type ProjectEntry struct {
Project Project `json:"project"`
}

0 comments on commit 39366d2

Please sign in to comment.