Plug framework export

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-09-04 10:34:21 +02:00
committed by Sacha Al Himdani
parent d47cac911d
commit 9a3c4d3965
9 changed files with 206 additions and 32 deletions

View File

@@ -27,6 +27,7 @@ import { useOrganizationId } from "/hooks/useOrganizationId";
import type { FrameworkGraphNodeQuery } from "/hooks/graph/__generated__/FrameworkGraphNodeQuery.graphql"; import type { FrameworkGraphNodeQuery } from "/hooks/graph/__generated__/FrameworkGraphNodeQuery.graphql";
import type { FrameworkDetailPageFragment$key } from "./__generated__/FrameworkDetailPageFragment.graphql"; import type { FrameworkDetailPageFragment$key } from "./__generated__/FrameworkDetailPageFragment.graphql";
import type { FrameworkDetailPageGenerateFrameworkStateOfApplicabilityMutation } from "./__generated__/FrameworkDetailPageGenerateFrameworkStateOfApplicabilityMutation.graphql"; import type { FrameworkDetailPageGenerateFrameworkStateOfApplicabilityMutation } from "./__generated__/FrameworkDetailPageGenerateFrameworkStateOfApplicabilityMutation.graphql";
import type { FrameworkDetailPageExportFrameworkMutation } from "./__generated__/FrameworkDetailPageExportFrameworkMutation.graphql";
import { FrameworkFormDialog } from "./dialogs/FrameworkFormDialog"; import { FrameworkFormDialog } from "./dialogs/FrameworkFormDialog";
import { FrameworkControlDialog } from "./dialogs/FrameworkControlDialog"; import { FrameworkControlDialog } from "./dialogs/FrameworkControlDialog";
import { useMutationWithToasts } from "/hooks/useMutationWithToasts"; import { useMutationWithToasts } from "/hooks/useMutationWithToasts";
@@ -66,6 +67,18 @@ const generateFrameworkStateOfApplicabilityMutation = graphql`
} }
`; `;
const exportFrameworkMutation = graphql`
mutation FrameworkDetailPageExportFrameworkMutation(
$frameworkId: ID!
) {
exportFramework(
input: { frameworkId: $frameworkId }
) {
exportJobId
}
}
`;
type Props = { type Props = {
queryRef: PreloadedQuery<FrameworkGraphNodeQuery>; queryRef: PreloadedQuery<FrameworkGraphNodeQuery>;
}; };
@@ -100,6 +113,15 @@ export default function FrameworkDetailPage(props: Props) {
} }
); );
const [exportFramework] =
useMutationWithToasts<FrameworkDetailPageExportFrameworkMutation>(
exportFrameworkMutation,
{
errorMessage: "Failed to export framework",
successMessage: "Framework export started successfully",
}
);
usePageTitle(`${framework.name} | ${selectedControl?.sectionTitle}`); usePageTitle(`${framework.name} | ${selectedControl?.sectionTitle}`);
const onDelete = () => { const onDelete = () => {
deleteFramework({ deleteFramework({
@@ -156,6 +178,16 @@ export default function FrameworkDetailPage(props: Props) {
> >
{__("Download SOA")} {__("Download SOA")}
</DropdownItem> </DropdownItem>
<DropdownItem
variant="primary"
onClick={() => {
exportFramework({
variables: { frameworkId: framework.id },
});
}}
>
{__("Export Framework")}
</DropdownItem>
<DropdownItem icon={IconTrashCan} variant="danger" onClick={onDelete}> <DropdownItem icon={IconTrashCan} variant="danger" onClick={onDelete}>
{__("Delete")} {__("Delete")}
</DropdownItem> </DropdownItem>

View File

@@ -0,0 +1,95 @@
/**
* @generated SignedSource<<05766c7969f5e78f91ca5121d2c30db8>>
* @lightSyntaxTransform
* @nogrep
*/
/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
import { ConcreteRequest } from 'relay-runtime';
export type FrameworkDetailPageExportFrameworkMutation$variables = {
frameworkId: string;
};
export type FrameworkDetailPageExportFrameworkMutation$data = {
readonly exportFramework: {
readonly exportJobId: string;
};
};
export type FrameworkDetailPageExportFrameworkMutation = {
response: FrameworkDetailPageExportFrameworkMutation$data;
variables: FrameworkDetailPageExportFrameworkMutation$variables;
};
const node: ConcreteRequest = (function(){
var v0 = [
{
"defaultValue": null,
"kind": "LocalArgument",
"name": "frameworkId"
}
],
v1 = [
{
"alias": null,
"args": [
{
"fields": [
{
"kind": "Variable",
"name": "frameworkId",
"variableName": "frameworkId"
}
],
"kind": "ObjectValue",
"name": "input"
}
],
"concreteType": "ExportFrameworkPayload",
"kind": "LinkedField",
"name": "exportFramework",
"plural": false,
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "exportJobId",
"storageKey": null
}
],
"storageKey": null
}
];
return {
"fragment": {
"argumentDefinitions": (v0/*: any*/),
"kind": "Fragment",
"metadata": null,
"name": "FrameworkDetailPageExportFrameworkMutation",
"selections": (v1/*: any*/),
"type": "Mutation",
"abstractKey": null
},
"kind": "Request",
"operation": {
"argumentDefinitions": (v0/*: any*/),
"kind": "Operation",
"name": "FrameworkDetailPageExportFrameworkMutation",
"selections": (v1/*: any*/)
},
"params": {
"cacheID": "d6fdd2bb2dacfc88248ccfc6f671eb4a",
"id": null,
"metadata": {},
"name": "FrameworkDetailPageExportFrameworkMutation",
"operationKind": "mutation",
"text": "mutation FrameworkDetailPageExportFrameworkMutation(\n $frameworkId: ID!\n) {\n exportFramework(input: {frameworkId: $frameworkId}) {\n exportJobId\n }\n}\n"
}
};
})();
(node as any).hash = "3866a75fb0aeaaa6cdf97934508116c1";
export default node;

View File

@@ -65,15 +65,13 @@ INSERT INTO framework_exports (
tenant_id, tenant_id,
framework_id, framework_id,
status, status,
created_at, created_at
expires_at
) VALUES ( ) VALUES (
@id, @id,
@tenant_id, @tenant_id,
@framework_id, @framework_id,
@status, @status,
@created_at, @created_at
@expires_at
)` )`
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
@@ -130,6 +128,7 @@ SELECT
id, id,
framework_id, framework_id,
status, status,
file_id,
created_at, created_at,
started_at, started_at,
completed_at completed_at

View File

@@ -249,8 +249,6 @@ WHERE %s
` `
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment()) q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
fmt.Printf("\n%s\n", q)
args := pgx.NamedArgs{"control_id": controlID} args := pgx.NamedArgs{"control_id": controlID}
maps.Copy(args, scope.SQLArguments()) maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments()) maps.Copy(args, filter.SQLArguments())

View File

@@ -1,6 +1,6 @@
CREATE TYPE framework_export_status AS ENUM ( CREATE TYPE framework_export_status AS ENUM (
'pending', 'pending',
'processing', 'processing',
'completed', 'completed',
'failed' 'failed'
); );
@@ -16,5 +16,14 @@ CREATE TABLE framework_exports (
completed_at TIMESTAMP WITH TIME ZONE completed_at TIMESTAMP WITH TIME ZONE
); );
ALTER TABLE framework_exports ADD CONSTRAINT framework_exports_framework_id_fkey
FOREIGN KEY (framework_id)
REFERENCES frameworks(id)
ON UPDATE CASCADE
ON DELETE CASCADE;
ALTER TABLE framework_exports ADD CONSTRAINT framework_exports_file_id_fkey
FOREIGN KEY (file_id)
REFERENCES files(id)
ON UPDATE CASCADE
ON DELETE SET NULL;

View File

@@ -71,8 +71,10 @@ type (
func (s FrameworkService) RequestExport( func (s FrameworkService) RequestExport(
ctx context.Context, ctx context.Context,
frameworkID gid.GID, frameworkID gid.GID,
) error { ) (error, *coredata.FrameworkExport) {
return s.svc.pg.WithTx(ctx, func(conn pg.Conn) error { frameworkExport := &coredata.FrameworkExport{}
err := s.svc.pg.WithTx(ctx, func(conn pg.Conn) error {
framework := &coredata.Framework{} framework := &coredata.Framework{}
if err := framework.LoadByID(ctx, conn, s.svc.scope, frameworkID); err != nil { if err := framework.LoadByID(ctx, conn, s.svc.scope, frameworkID); err != nil {
return fmt.Errorf("cannot load framework: %w", err) return fmt.Errorf("cannot load framework: %w", err)
@@ -80,7 +82,7 @@ func (s FrameworkService) RequestExport(
now := time.Now() now := time.Now()
frameworkExport := &coredata.FrameworkExport{ frameworkExport = &coredata.FrameworkExport{
ID: gid.New(framework.ID.TenantID(), coredata.FrameworkExportEntityType), ID: gid.New(framework.ID.TenantID(), coredata.FrameworkExportEntityType),
FrameworkID: frameworkID, FrameworkID: frameworkID,
Status: coredata.FrameworkExportStatusPending, Status: coredata.FrameworkExportStatusPending,
@@ -93,6 +95,12 @@ func (s FrameworkService) RequestExport(
return nil return nil
}) })
if err != nil {
return err, nil
}
return nil, frameworkExport
} }
func (s FrameworkService) Export( func (s FrameworkService) Export(

View File

@@ -201,6 +201,13 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
} }
scope := coredata.NewScope(fe.ID.TenantID()) scope := coredata.NewScope(fe.ID.TenantID())
fe.Status = coredata.FrameworkExportStatusProcessing
fe.StartedAt = ref.Ref(time.Now())
if err := fe.Update(ctx, tx, scope); err != nil {
return fmt.Errorf("cannot update framework export: %w", err)
}
framework := &coredata.Framework{} framework := &coredata.Framework{}
if err := framework.LoadByID(ctx, tx, scope, fe.FrameworkID); err != nil { if err := framework.LoadByID(ctx, tx, scope, fe.FrameworkID); err != nil {
fe.Status = coredata.FrameworkExportStatusFailed fe.Status = coredata.FrameworkExportStatusFailed
@@ -209,9 +216,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err) return fmt.Errorf("cannot update framework export: %w", err)
} }
// s.logger.Error(ctx, "cannot load framework", "error", err) return fmt.Errorf("cannot load framework: %w", err)
return nil
} }
tenantService := s.WithTenant(fe.ID.TenantID()) tenantService := s.WithTenant(fe.ID.TenantID())
@@ -225,9 +230,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err) return fmt.Errorf("cannot update framework export: %w", err)
} }
// s.logger.Error(ctx, "cannot create temp file", "error", err) return fmt.Errorf("cannot create temp file: %w", err)
return nil
} }
defer tempFile.Close() defer tempFile.Close()
defer os.Remove(tempFile.Name()) defer os.Remove(tempFile.Name())
@@ -240,8 +243,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err) return fmt.Errorf("cannot update framework export: %w", err)
} }
// s.logger.Error(ctx, "cannot export framework", "error", err) return fmt.Errorf("cannot export framework: %w", err)
return nil
} }
uuid, err := uuid.NewV4() uuid, err := uuid.NewV4()
@@ -252,8 +254,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err) return fmt.Errorf("cannot update framework export: %w", err)
} }
// s.logger.Error(ctx, "cannot generate UUID", "error", err) return fmt.Errorf("cannot update framework export: %w", err)
return nil
} }
if _, err := tempFile.Seek(0, 0); err != nil { if _, err := tempFile.Seek(0, 0); err != nil {
@@ -263,8 +264,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err) return fmt.Errorf("cannot update framework export: %w", err)
} }
// s.logger.Error(ctx, "cannot seek temp file", "error", err) return fmt.Errorf("cannot seek temp file: %w", err)
return nil
} }
fileInfo, err := tempFile.Stat() fileInfo, err := tempFile.Stat()
@@ -275,8 +275,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err) return fmt.Errorf("cannot update framework export: %w", err)
} }
// s.logger.Error(ctx, "cannot get temp file info", "error", err) return fmt.Errorf("cannot stat temp file: %w", err)
return nil
} }
_, err = s.s3.PutObject( _, err = s.s3.PutObject(
@@ -300,8 +299,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err) return fmt.Errorf("cannot update framework export: %w", err)
} }
// s.logger.Error(ctx, "cannot upload file to S3", "error", err) return fmt.Errorf("cannot upload file to S3: %w", err)
return nil
} }
now := time.Now() now := time.Now()
@@ -312,6 +310,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
MimeType: "application/zip", MimeType: "application/zip",
FileName: fmt.Sprintf("%s Archive %s.zip", framework.Name, time.Now().Format("2006-01-02")), FileName: fmt.Sprintf("%s Archive %s.zip", framework.Name, time.Now().Format("2006-01-02")),
FileKey: uuid.String(), FileKey: uuid.String(),
FileSize: int(fileInfo.Size()),
CreatedAt: now, CreatedAt: now,
UpdatedAt: now, UpdatedAt: now,
} }

View File

@@ -330,9 +330,19 @@ func (impl *Implm) Run(
} }
}() }()
frameworkExporterCtx, stopFrameworkExporter := context.WithCancel(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
if err := impl.runFrameworkExporter(frameworkExporterCtx, proboService, l.Named("framework-exporter")); err != nil {
cancel(fmt.Errorf("framework exporter crashed: %w", err))
}
}()
<-ctx.Done() <-ctx.Done()
stopMailer() stopMailer()
stopFrameworkExporter()
stopApiServer() stopApiServer()
wg.Wait() wg.Wait()
@@ -342,6 +352,26 @@ func (impl *Implm) Run(
return context.Cause(ctx) return context.Cause(ctx)
} }
func (impl *Implm) runFrameworkExporter(
ctx context.Context,
proboService *probo.Service,
l *log.Logger,
) error {
LOOP:
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(30 * time.Second):
if err := proboService.ExportFrameworkJob(ctx); err != nil {
if !errors.Is(err, coredata.ErrNoFrameworkExportAvailable) {
l.ErrorCtx(ctx, "cannot process framework export", log.Error(err))
}
}
goto LOOP
}
}
func (impl *Implm) runApiServer( func (impl *Implm) runApiServer(
ctx context.Context, ctx context.Context,
l *log.Logger, l *log.Logger,

View File

@@ -1680,7 +1680,15 @@ func (r *mutationResolver) GenerateFrameworkStateOfApplicability(ctx context.Con
// ExportFramework is the resolver for the exportFramework field. // ExportFramework is the resolver for the exportFramework field.
func (r *mutationResolver) ExportFramework(ctx context.Context, input types.ExportFrameworkInput) (*types.ExportFrameworkPayload, error) { func (r *mutationResolver) ExportFramework(ctx context.Context, input types.ExportFrameworkInput) (*types.ExportFrameworkPayload, error) {
panic(fmt.Errorf("not implemented: ExportFramework - exportFramework")) prb := r.ProboService(ctx, input.FrameworkID.TenantID())
err, exportJobID := prb.Frameworks.RequestExport(ctx, input.FrameworkID)
if err != nil {
return nil, fmt.Errorf("cannot export framework: %w", err)
}
return &types.ExportFrameworkPayload{
ExportJobID: exportJobID.ID,
}, nil
} }
// CreateControl is the resolver for the createControl field. // CreateControl is the resolver for the createControl field.
@@ -3963,10 +3971,6 @@ func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error
return types.NewVendor(vendor), nil return types.NewVendor(vendor), nil
case coredata.FrameworkEntityType: case coredata.FrameworkEntityType:
if err := prb.Frameworks.Export(ctx, id, nil); err != nil {
panic(fmt.Errorf("cannot export organization frameworks: %w", err))
}
framework, err := prb.Frameworks.Get(ctx, id) framework, err := prb.Frameworks.Get(ctx, id)
if err != nil { if err != nil {
panic(fmt.Errorf("cannot get framework: %w", err)) panic(fmt.Errorf("cannot get framework: %w", err))