forked from n8n-io/n8n
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): Use DTOs for source control push/pull requests (n8n-i…
…o#12470) Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com>
- Loading branch information
Showing
29 changed files
with
305 additions
and
234 deletions.
There are no files selected for viewing
This file contains 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
38 changes: 38 additions & 0 deletions
38
...ages/@n8n/api-types/src/dto/source-control/__tests__/pull-work-folder-request.dto.test.ts
This file contains 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,38 @@ | ||
import { PullWorkFolderRequestDto } from '../pull-work-folder-request.dto'; | ||
|
||
describe('PullWorkFolderRequestDto', () => { | ||
describe('Valid requests', () => { | ||
test.each([ | ||
{ | ||
name: 'with force', | ||
request: { force: true }, | ||
}, | ||
{ | ||
name: 'without force', | ||
request: {}, | ||
}, | ||
])('should validate $name', ({ request }) => { | ||
const result = PullWorkFolderRequestDto.safeParse(request); | ||
expect(result.success).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Invalid requests', () => { | ||
test.each([ | ||
{ | ||
name: 'invalid force type', | ||
request: { | ||
force: 'true', // Should be boolean | ||
}, | ||
expectedErrorPath: ['force'], | ||
}, | ||
])('should fail validation for $name', ({ request, expectedErrorPath }) => { | ||
const result = PullWorkFolderRequestDto.safeParse(request); | ||
expect(result.success).toBe(false); | ||
|
||
if (expectedErrorPath) { | ||
expect(result.error?.issues[0].path).toEqual(expectedErrorPath); | ||
} | ||
}); | ||
}); | ||
}); |
112 changes: 112 additions & 0 deletions
112
...ages/@n8n/api-types/src/dto/source-control/__tests__/push-work-folder-request.dto.test.ts
This file contains 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,112 @@ | ||
import { PushWorkFolderRequestDto } from '../push-work-folder-request.dto'; | ||
|
||
describe('PushWorkFolderRequestDto', () => { | ||
describe('Valid requests', () => { | ||
test.each([ | ||
{ | ||
name: 'complete valid push request with all fields', | ||
request: { | ||
force: true, | ||
fileNames: [ | ||
{ | ||
file: 'file1.json', | ||
id: '1', | ||
name: 'File 1', | ||
type: 'workflow', | ||
status: 'modified', | ||
location: 'local', | ||
conflict: false, | ||
updatedAt: '2023-10-01T12:00:00Z', | ||
pushed: true, | ||
}, | ||
], | ||
message: 'Initial commit', | ||
}, | ||
}, | ||
{ | ||
name: 'push request with only required fields', | ||
request: { | ||
fileNames: [ | ||
{ | ||
file: 'file2.json', | ||
id: '2', | ||
name: 'File 2', | ||
type: 'credential', | ||
status: 'new', | ||
location: 'remote', | ||
conflict: true, | ||
updatedAt: '2023-10-02T12:00:00Z', | ||
}, | ||
], | ||
}, | ||
}, | ||
])('should validate $name', ({ request }) => { | ||
const result = PushWorkFolderRequestDto.safeParse(request); | ||
expect(result.success).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('Invalid requests', () => { | ||
test.each([ | ||
{ | ||
name: 'missing required fileNames field', | ||
request: { | ||
force: true, | ||
message: 'Initial commit', | ||
}, | ||
expectedErrorPath: ['fileNames'], | ||
}, | ||
{ | ||
name: 'invalid fileNames type', | ||
request: { | ||
fileNames: 'not-an-array', // Should be an array | ||
}, | ||
expectedErrorPath: ['fileNames'], | ||
}, | ||
{ | ||
name: 'invalid fileNames array element', | ||
request: { | ||
fileNames: [ | ||
{ | ||
file: 'file4.json', | ||
id: '4', | ||
name: 'File 4', | ||
type: 'invalid-type', // Invalid type | ||
status: 'modified', | ||
location: 'local', | ||
conflict: false, | ||
updatedAt: '2023-10-04T12:00:00Z', | ||
}, | ||
], | ||
}, | ||
expectedErrorPath: ['fileNames', 0, 'type'], | ||
}, | ||
{ | ||
name: 'invalid force type', | ||
request: { | ||
force: 'true', // Should be boolean | ||
fileNames: [ | ||
{ | ||
file: 'file5.json', | ||
id: '5', | ||
name: 'File 5', | ||
type: 'workflow', | ||
status: 'modified', | ||
location: 'local', | ||
conflict: false, | ||
updatedAt: '2023-10-05T12:00:00Z', | ||
}, | ||
], | ||
}, | ||
expectedErrorPath: ['force'], | ||
}, | ||
])('should fail validation for $name', ({ request, expectedErrorPath }) => { | ||
const result = PushWorkFolderRequestDto.safeParse(request); | ||
expect(result.success).toBe(false); | ||
|
||
if (expectedErrorPath) { | ||
expect(result.error?.issues[0].path).toEqual(expectedErrorPath); | ||
} | ||
}); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/@n8n/api-types/src/dto/source-control/pull-work-folder-request.dto.ts
This file contains 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,6 @@ | ||
import { z } from 'zod'; | ||
import { Z } from 'zod-class'; | ||
|
||
export class PullWorkFolderRequestDto extends Z.class({ | ||
force: z.boolean().optional(), | ||
}) {} |
10 changes: 10 additions & 0 deletions
10
packages/@n8n/api-types/src/dto/source-control/push-work-folder-request.dto.ts
This file contains 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,10 @@ | ||
import { z } from 'zod'; | ||
import { Z } from 'zod-class'; | ||
|
||
import { SourceControlledFileSchema } from '../../schemas/source-controlled-file.schema'; | ||
|
||
export class PushWorkFolderRequestDto extends Z.class({ | ||
force: z.boolean().optional(), | ||
commitMessage: z.string().optional(), | ||
fileNames: z.array(SourceControlledFileSchema), | ||
}) {} |
This file contains 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
34 changes: 34 additions & 0 deletions
34
packages/@n8n/api-types/src/schemas/source-controlled-file.schema.ts
This file contains 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 @@ | ||
import { z } from 'zod'; | ||
|
||
const FileTypeSchema = z.enum(['credential', 'workflow', 'tags', 'variables', 'file']); | ||
export const SOURCE_CONTROL_FILE_TYPE = FileTypeSchema.Values; | ||
|
||
const FileStatusSchema = z.enum([ | ||
'new', | ||
'modified', | ||
'deleted', | ||
'created', | ||
'renamed', | ||
'conflicted', | ||
'ignored', | ||
'staged', | ||
'unknown', | ||
]); | ||
export const SOURCE_CONTROL_FILE_STATUS = FileStatusSchema.Values; | ||
|
||
const FileLocationSchema = z.enum(['local', 'remote']); | ||
export const SOURCE_CONTROL_FILE_LOCATION = FileLocationSchema.Values; | ||
|
||
export const SourceControlledFileSchema = z.object({ | ||
file: z.string(), | ||
id: z.string(), | ||
name: z.string(), | ||
type: FileTypeSchema, | ||
status: FileStatusSchema, | ||
location: FileLocationSchema, | ||
conflict: z.boolean(), | ||
updatedAt: z.string(), | ||
pushed: z.boolean().optional(), | ||
}); | ||
|
||
export type SourceControlledFile = z.infer<typeof SourceControlledFileSchema>; |
This file contains 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 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 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 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 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 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.