-
Notifications
You must be signed in to change notification settings - Fork 61
feat(exports): add server-side XLSX issue exports with export history #292
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
Merged
+508
−28
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/middleware" | ||
| "github.com/Devlaner/devlane/api/internal/service" | ||
| "github.com/gin-gonic/gin" | ||
| "github.com/google/uuid" | ||
| ) | ||
|
|
||
| const xlsxContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | ||
|
|
||
| // ExportHandler serves server-side issue exports. | ||
| type ExportHandler struct { | ||
| Export *service.ExportService | ||
| } | ||
|
|
||
| // CreateExport generates an .xlsx of the selected projects' issues and streams | ||
| // it back, recording the request in the export history. | ||
| // POST /api/workspaces/:slug/exports/ | ||
| func (h *ExportHandler) CreateExport(c *gin.Context) { | ||
| user := middleware.GetUser(c) | ||
| if user == nil { | ||
| c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | ||
| return | ||
| } | ||
| slug := c.Param("slug") | ||
| var body struct { | ||
| ProjectIDs []string `json:"project_ids"` | ||
| Name string `json:"name"` | ||
| } | ||
| if err := c.ShouldBindJSON(&body); err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request", "detail": err.Error()}) | ||
| return | ||
| } | ||
| projectIDs := make([]uuid.UUID, 0, len(body.ProjectIDs)) | ||
| for _, s := range body.ProjectIDs { | ||
| id, err := uuid.Parse(s) | ||
| if err != nil { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID: " + s}) | ||
| return | ||
| } | ||
| projectIDs = append(projectIDs, id) | ||
| } | ||
| filename, data, err := h.Export.ExportIssues(c.Request.Context(), slug, user.ID, projectIDs, body.Name) | ||
| if err != nil { | ||
| switch err { | ||
| case service.ErrWorkspaceNotFound, service.ErrProjectNotFound: | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "Not found"}) | ||
| case service.ErrWorkspaceForbidden: | ||
| c.JSON(http.StatusForbidden, gin.H{"error": "Not a member of this workspace"}) | ||
| case service.ErrNoProjectsSelected: | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "Select at least one project to export"}) | ||
| default: | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate export"}) | ||
| } | ||
| return | ||
| } | ||
| c.Header("Content-Disposition", `attachment; filename="`+filename+`"`) | ||
| c.Data(http.StatusOK, xlsxContentType, data) | ||
| } | ||
|
|
||
| // ListExports returns the workspace's export history. | ||
| // GET /api/workspaces/:slug/exports/ | ||
| func (h *ExportHandler) ListExports(c *gin.Context) { | ||
| user := middleware.GetUser(c) | ||
| if user == nil { | ||
| c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | ||
| return | ||
| } | ||
| slug := c.Param("slug") | ||
| list, err := h.Export.ListHistory(c.Request.Context(), slug, user.ID) | ||
| if err != nil { | ||
| switch err { | ||
| case service.ErrWorkspaceNotFound: | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "Workspace not found"}) | ||
| case service.ErrWorkspaceForbidden: | ||
| c.JSON(http.StatusForbidden, gin.H{"error": "Not a member of this workspace"}) | ||
| default: | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list exports"}) | ||
| } | ||
| return | ||
| } | ||
| c.JSON(http.StatusOK, gin.H{"exports": list}) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package handler_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/testutil" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/xuri/excelize/v2" | ||
| ) | ||
|
|
||
| // A workspace member can export a project's issues as a real .xlsx workbook that | ||
| // parses back and contains the issues, the request is recorded in the export | ||
| // history, and non-members / empty selections are refused. Covers #198. | ||
| func TestExport_IssuesXLSXAndHistory(t *testing.T) { | ||
| ts := testutil.NewTestServer(t) | ||
| w := testutil.SeedWorld(t, ts.DB) | ||
| url := "/api/workspaces/" + w.Workspace.Slug + "/exports/" | ||
|
|
||
| i1 := testutil.CreateIssue(t, ts.DB, w.Project.ID, w.Workspace.ID, w.User.ID) | ||
| testutil.CreateIssue(t, ts.DB, w.Project.ID, w.Workspace.ID, w.User.ID) | ||
|
|
||
| rr := ts.POST(url, map[string]any{"project_ids": []string{w.Project.ID.String()}}, w.Session) | ||
| require.Equal(t, http.StatusOK, rr.Code, "body=%s", rr.Body.String()) | ||
| require.Contains(t, rr.Header().Get("Content-Type"), "spreadsheetml.sheet") | ||
|
|
||
| body := rr.Body.Bytes() | ||
| require.True(t, len(body) > 4 && body[0] == 'P' && body[1] == 'K', "response should be an xlsx (zip) file") | ||
|
|
||
| // It opens as a real workbook and contains the exported issue. | ||
| f, err := excelize.OpenReader(bytes.NewReader(body)) | ||
| require.NoError(t, err) | ||
| rows, err := f.GetRows("Issues") | ||
| require.NoError(t, err) | ||
| require.GreaterOrEqual(t, len(rows), 3, "header row + two issues") | ||
| require.Equal(t, "Title", rows[0][2]) | ||
| found := false | ||
| for _, r := range rows[1:] { | ||
| if len(r) > 2 && r[2] == i1.Name { | ||
| found = true | ||
| } | ||
| } | ||
| require.True(t, found, "exported sheet should contain the issue title") | ||
|
|
||
| // The request is recorded in the export history. | ||
| lr := ts.GET(url, w.Session) | ||
| require.Equal(t, http.StatusOK, lr.Code) | ||
| var hist struct { | ||
| Exports []struct { | ||
| Provider string `json:"provider"` | ||
| Status string `json:"status"` | ||
| } `json:"exports"` | ||
| } | ||
| require.NoError(t, json.Unmarshal(lr.Body.Bytes(), &hist)) | ||
| require.Len(t, hist.Exports, 1) | ||
| require.Equal(t, "xlsx", hist.Exports[0].Provider) | ||
| require.Equal(t, "completed", hist.Exports[0].Status) | ||
|
|
||
| // A non-member is refused. | ||
| outsider := testutil.CreateUser(t, ts.DB) | ||
| outsiderSession := testutil.LoginAs(t, ts.DB, outsider) | ||
| require.Equal(t, http.StatusForbidden, | ||
| ts.POST(url, map[string]any{"project_ids": []string{w.Project.ID.String()}}, outsiderSession).Code) | ||
|
|
||
| // An empty selection is a 400. | ||
| require.Equal(t, http.StatusBadRequest, | ||
| ts.POST(url, map[string]any{"project_ids": []string{}}, w.Session).Code) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package model | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // Exporter matches the exporters table: one row per issue-export request, kept | ||
| // as the export history. project_ids are recorded inside Filters so we avoid the | ||
| // UUID[] column's array-driver quirks. | ||
| type Exporter struct { | ||
| ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"` | ||
| Name string `gorm:"type:varchar(255)" json:"name,omitempty"` | ||
| Type string `gorm:"type:varchar(50);default:issue_exports" json:"type"` | ||
| Provider string `gorm:"type:varchar(50);not null" json:"provider"` | ||
| Status string `gorm:"type:varchar(50);not null;default:queued" json:"status"` | ||
| Reason string `gorm:"type:text;default:''" json:"reason,omitempty"` | ||
| Filters JSONMap `gorm:"type:jsonb;serializer:json" json:"filters,omitempty"` | ||
| CreatedAt time.Time `json:"created_at"` | ||
| UpdatedAt time.Time `json:"updated_at"` | ||
| WorkspaceID uuid.UUID `gorm:"type:uuid;not null" json:"workspace_id"` | ||
| InitiatedByID uuid.UUID `gorm:"type:uuid;not null" json:"initiated_by_id"` | ||
| } | ||
|
|
||
| func (Exporter) TableName() string { return "exporters" } | ||
|
|
||
| func (e *Exporter) BeforeCreate(tx *gorm.DB) error { | ||
| if e.ID == uuid.Nil { | ||
| e.ID = uuid.New() | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.