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: csv upload should not save file #322

Merged
merged 2 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ codegenerators:
- any-glob-to-any-file: internal/ent/templates/**
coreclient:
- changed-files:
- any-glob-to-any-file: pkg/openalneclient/**
- any-glob-to-any-file: nternal/graphapi/query/**
- any-glob-to-any-file: pkg/openlaneclient/**
- any-glob-to-any-file: internal/graphapi/query/**
config:
- changed-files:
- any-glob-to-any-file: config/**
Expand Down
25 changes: 25 additions & 0 deletions internal/graphapi/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package graphapi_test

import (
"context"
"os"
"testing"

"github.com/99designs/gqlgen/graphql"
"github.com/brianvoe/gofakeit/v7"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -344,6 +346,29 @@ func (suite *GraphTestSuite) TestMutationCreateOrganization() {
}
}

func (suite *GraphTestSuite) TestMutationBulkCSVUploadOrganization() {
t := suite.T()

fileName := "testdata/uploads/orgs.csv"

input, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, os.ModePerm)
require.NoError(t, err)

file := graphql.Upload{
File: input,
Filename: fileName,
ContentType: "text/csv",
}

resp, err := suite.client.api.CreateBulkCSVOrganization(testUser1.UserCtx, file)

require.NoError(t, err)
require.NotNil(t, resp)

// make sure the orgs were created
assert.Len(t, resp.CreateBulkCSVOrganization.Organizations, 2)
}

func (suite *GraphTestSuite) TestMutationUpdateOrganization() {
t := suite.T()

Expand Down
3 changes: 3 additions & 0 deletions internal/graphapi/testdata/uploads/orgs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Name
Kroll Dogs Ratings Agency
Thomson Cats
13 changes: 13 additions & 0 deletions internal/middleware/objects/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package objects
import (
"context"
"path/filepath"
"slices"
"time"

"github.com/rs/zerolog/log"
Expand All @@ -13,12 +14,24 @@ import (
"github.com/theopenlane/core/pkg/objects"
)

var (
// keys that should be skipped when uploading files
// these keys are used for bulk uploads
skipKeys = []string{"input"}
)

// Upload handles the file Upload process per key in the multipart form and returns the uploaded files
// in addition to uploading the files to the storage, it also creates the file in the database
func Upload(ctx context.Context, u *objects.Objects, files []objects.FileUpload) ([]objects.File, error) {
uploadedFiles := make([]objects.File, 0, len(files))

for _, f := range files {
if slices.Contains(skipKeys, f.Key) {
// skip the input key
log.Debug().Str("file", f.Filename).Msg("skipping input key, this is for bulk upload")
continue
}

// create the file in the database
entFile, err := createFile(ctx, u, f)
if err != nil {
Expand Down
Loading