-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add organisation level import [GROOT-1495] (#2824)
* feat: Add organisation level import [GROOT-1495] * clean up, extract taxonomy import * add broader, related, concept schemes * docs: tidy and improve accuracy of testing notes * docs: clarify * refactor: fix test execution inconsistency * refactor: clarify testing docs * chore: [GROOT-1495] update management sdk version * chore: [GROOT-1495] update typings to match new management sdk version * test: outlined scenarios * test: help message * test: help msg when incorrect args * chore: [GROOT-1495] taxonomy import unit test * test: wip - creating concepts * chore: [GROOT-1495] fix org import unit tests * fix: fix integration tests * test: reduce number of expected concept patch requests * feat: add update integration test * refactor: remove comments * chore: remove unintended change to 'contentful' command --------- Co-authored-by: Adrian L Thomas <[email protected]> Co-authored-by: LiamStokingerContentful <[email protected]>
- Loading branch information
1 parent
7c3335e
commit a829969
Showing
20 changed files
with
20,106 additions
and
11,220 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,3 +194,6 @@ contentful-import-error-log-*.json | |
reports | ||
|
||
data/* | ||
|
||
# integration test output | ||
bin/output.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
import Listr from 'listr' | ||
import { noop } from 'lodash' | ||
import type { Argv } from 'yargs' | ||
import { handleAsyncError as handle } from '../../utils/async' | ||
import { createPlainClient } from '../../utils/contentful-clients' | ||
import { getHeadersFromOption } from '../../utils/headers' | ||
import { TaxonomyJson } from './taxonomy/taxonomy' | ||
import PQueue from 'p-queue' | ||
import { | ||
displayErrorLog, | ||
setupLogging, | ||
writeErrorLogFile | ||
} from 'contentful-batch-libs/dist/logging' | ||
import taxonomyImport from './taxonomy/taxonomy-import' | ||
import { PlainClientAPI } from 'contentful-management' | ||
import { readContentFile } from './utils/read-content-file' | ||
|
||
module.exports.command = 'import' | ||
|
||
module.exports.desc = 'import organization level entities' | ||
|
||
module.exports.builder = (yargs: Argv) => { | ||
return yargs | ||
.usage('Usage: contentful organization import') | ||
.option('management-token', { | ||
alias: 'mt', | ||
describe: 'Contentful management API token', | ||
type: 'string' | ||
}) | ||
.option('organization-id', { | ||
alias: 'oid', | ||
describe: 'ID of Organization with source data', | ||
type: 'string', | ||
demandOption: true | ||
}) | ||
.option('header', { | ||
alias: 'H', | ||
type: 'string', | ||
describe: 'Pass an additional HTTP Header' | ||
}) | ||
.option('content-file', { | ||
alias: 'f', | ||
type: 'string', | ||
describe: 'Content file with entities that need to be imported', | ||
demandOption: true | ||
}) | ||
.option('silent', { | ||
alias: 's', | ||
type: 'boolean', | ||
describe: 'Suppress any log output', | ||
default: false | ||
}) | ||
.option('error-log-file', { | ||
describe: 'Full path to the error log file', | ||
type: 'string' | ||
}) | ||
} | ||
|
||
export interface OrgImportParams { | ||
context: { managementToken: string } | ||
header?: string | ||
organizationId: string | ||
contentFile: string | ||
silent?: boolean | ||
errorLogFile?: string | ||
} | ||
|
||
export interface OrgImportContext { | ||
data: { | ||
taxonomy?: TaxonomyJson['taxonomy'] | ||
} | ||
requestQueue: PQueue | ||
cmaClient: PlainClientAPI | ||
} | ||
|
||
const ONE_SECOND = 1000 | ||
|
||
interface ErrorMessage { | ||
ts: string | ||
level: 'error' | ||
error: Error | ||
} | ||
|
||
async function importCommand(params: OrgImportParams) { | ||
const { context, header, organizationId, contentFile, silent, errorLogFile } = | ||
params | ||
const { managementToken } = context | ||
|
||
const cmaClient = await createPlainClient({ | ||
accessToken: managementToken, | ||
feature: 'org-import', | ||
headers: getHeadersFromOption(header), | ||
throttle: 8, | ||
logHandler: noop | ||
}) | ||
|
||
const importContext: OrgImportContext = { | ||
data: {}, | ||
requestQueue: new PQueue({ | ||
concurrency: 1, | ||
interval: ONE_SECOND, | ||
intervalCap: 1, | ||
carryoverConcurrencyCount: true | ||
}), | ||
cmaClient | ||
} | ||
|
||
const log: ErrorMessage[] = [] | ||
setupLogging(log) | ||
|
||
const tasks = new Listr( | ||
[ | ||
{ | ||
title: 'Import organization level entities', | ||
task: async () => { | ||
return new Listr([ | ||
{ | ||
title: 'Read content file', | ||
task: async ctx => { | ||
const data = await readContentFile(contentFile) | ||
|
||
if (data.taxonomy) { | ||
ctx.data.taxonomy = data.taxonomy | ||
} | ||
} | ||
}, | ||
{ | ||
title: 'Import taxonomy', | ||
task: async ctx => { | ||
return taxonomyImport(params, ctx) | ||
} | ||
} | ||
]) | ||
} | ||
} | ||
], | ||
{ renderer: silent ? 'silent' : 'default' } | ||
) | ||
|
||
await tasks | ||
.run(importContext) | ||
.catch(err => { | ||
importContext.requestQueue.clear() | ||
|
||
log.push({ | ||
ts: new Date().toISOString(), | ||
level: 'error', | ||
error: err as Error | ||
}) | ||
}) | ||
.then(() => { | ||
if (log.length > 0) { | ||
displayErrorLog(log) | ||
const errorLogFilePath = | ||
errorLogFile || | ||
`${Date.now()}-${organizationId}-import-org-error-log.json` | ||
writeErrorLogFile(errorLogFilePath, log) | ||
} | ||
}) | ||
} | ||
|
||
module.exports.organizationImport = importCommand | ||
|
||
module.exports.handler = handle(importCommand) | ||
|
||
export default importCommand |
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
8 changes: 6 additions & 2 deletions
8
...organization_cmds/utils/concept-scheme.ts → ...anization_cmds/taxonomy/concept-scheme.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
8 changes: 6 additions & 2 deletions
8
lib/cmds/organization_cmds/utils/concept.ts → ...mds/organization_cmds/taxonomy/concept.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
Oops, something went wrong.