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: adopt new batch-lib version [NONE] #950

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 7 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import VerboseRenderer from 'listr-verbose-renderer'
import { startCase } from 'lodash'
import PQueue from 'p-queue'

import { displayErrorLog, setupLogging, writeErrorLogFile } from 'contentful-batch-libs/dist/logging'
import { displayErrorLog, setupLogging, writeErrorLogFile } from 'contentful-batch-libs'
import { wrapTask } from 'contentful-batch-libs/dist/listr'

import initClient from './tasks/init-client'
Expand All @@ -16,7 +16,8 @@ import pushToSpace from './tasks/push-to-space/push-to-space'
import transformSpace from './transform/transform-space'
import { assertDefaultLocale, assertPayload } from './utils/validations'
import parseOptions from './parseOptions'
import { ContentfulMultiError, LogItem } from './utils/errors'
import { ContentfulMultiError } from './utils/errors'
import { isDisplayLog, isErrorLog } from './utils/logging'

const ONE_SECOND = 1000

Expand Down Expand Up @@ -59,7 +60,7 @@ type RunContentfulImportParams = {
}

async function runContentfulImport (params: RunContentfulImportParams) {
const log: LogItem[] = []
const log = setupLogging()
const options = await parseOptions(params)
const listrOptions = createListrOptions(options)
const requestQueue = new PQueue({
Expand All @@ -69,7 +70,6 @@ async function runContentfulImport (params: RunContentfulImportParams) {
})

// Setup custom log listener to store log messages for later
setupLogging(log)

const infoTable = new Table()

Expand Down Expand Up @@ -100,7 +100,6 @@ async function runContentfulImport (params: RunContentfulImportParams) {
assertPayload(options.content)
}
},

{
title: 'Initialize client',
task: wrapTask(async (ctx) => {
Expand Down Expand Up @@ -128,8 +127,7 @@ async function runContentfulImport (params: RunContentfulImportParams) {
{
title: 'Apply transformations to source data',
task: wrapTask(async (ctx) => {
const transformedSourceData = transformSpace(ctx.sourceDataUntransformed, ctx.destinationData)
ctx.sourceData = transformedSourceData
ctx.sourceData = transformSpace(ctx.sourceDataUntransformed, ctx.destinationData)
})
},
{
Expand Down Expand Up @@ -193,8 +191,8 @@ async function runContentfulImport (params: RunContentfulImportParams) {
})
})
.then((data) => {
const errorLog = log.filter((logMessage) => logMessage.level !== 'info' && logMessage.level !== 'warning')
const displayLog = log.filter((logMessage) => logMessage.level !== 'info')
const errorLog = log.filter(isErrorLog)
const displayLog = log.filter(isDisplayLog)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log only accepts messages with levels error or warn, I think this filter does nothing?

displayErrorLog(displayLog)

if (errorLog.length) {
Expand Down
3 changes: 1 addition & 2 deletions lib/parseOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import format from 'date-fns/format'

import { version } from '../package.json'
import { getHeadersConfig } from './utils/headers'
import { proxyStringToObject, agentFromProxy } from 'contentful-batch-libs/dist/proxy'
import addSequenceHeader from 'contentful-batch-libs/dist/add-sequence-header'
import { addSequenceHeader, proxyStringToObject, agentFromProxy } from 'contentful-batch-libs'
import { parseChunked } from '@discoveryjs/json-ext'

const SUPPORTED_ENTITY_TYPES = [
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/get-destination-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Promise from 'bluebird'

import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { logEmitter } from 'contentful-batch-libs'
import type { AssetProps, ContentTypeProps, EntryProps, LocaleProps, TagProps, WebhookProps } from 'contentful-management'
import { OriginalSourceData } from '../types'
import PQueue from 'p-queue'
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/init-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createClient } from 'contentful-management'

import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { logEmitter } from 'contentful-batch-libs'

function logHandler (level, data) {
logEmitter.emit(level, data)
Expand Down
13 changes: 6 additions & 7 deletions lib/tasks/push-to-space/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import fs from 'fs'
import { join } from 'path'
import { promisify } from 'util'

import getEntityName from 'contentful-batch-libs/dist/get-entity-name'
import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { ContentfulAssetError, ContentfulEntityError } from '../../utils/errors'
import { getEntityName, logEmitter } from 'contentful-batch-libs'

const stat = promisify(fs.stat)

Expand Down Expand Up @@ -51,11 +50,11 @@ async function lastResult (promises: Promise<any>[]) {
}

type ProcessAssetsParams = {
assets: any[];
timeout?: number;
retryLimit?: number;
requestQueue: any;
locales?: string[];
assets: any[];
timeout?: number;
retryLimit?: number;
requestQueue: any;
locales?: string[];
};

export async function processAssets ({
Expand Down
6 changes: 2 additions & 4 deletions lib/tasks/push-to-space/creation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { find } from 'lodash/collection'
import { assign, get, omitBy, omit } from 'lodash/object'
import { find, assign, get, omitBy, omit } from 'lodash'

import getEntityName from 'contentful-batch-libs/dist/get-entity-name'
import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { getEntityName, logEmitter } from 'contentful-batch-libs'
import { ContentfulEntityError } from '../../utils/errors'
import { TransformedSourceData, TransformedSourceDataUnion } from '../../types'
import PQueue from 'p-queue'
Expand Down
3 changes: 1 addition & 2 deletions lib/tasks/push-to-space/publishing.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import getEntityName from 'contentful-batch-libs/dist/get-entity-name'
import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { logEmitter, getEntityName } from 'contentful-batch-libs'
import { ContentfulEntityError } from '../../utils/errors'
import { ResourcesUnion } from '../../types'
import PQueue from 'p-queue'
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/push-to-space/push-to-space.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Listr from 'listr'
import verboseRenderer from 'listr-verbose-renderer'

import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { logEmitter } from 'contentful-batch-libs'
import { wrapTask } from 'contentful-batch-libs/dist/listr'

import * as assets from './assets'
Expand Down
7 changes: 3 additions & 4 deletions lib/transform/transform-space.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omit, defaults } from 'lodash/object'
import { defaults, omit } from 'lodash'

import * as defaultTransformers from './transformers'
import sortEntries from '../utils/sort-entries'
Expand All @@ -22,15 +22,14 @@ export default function (
sourceData.locales = sortLocales(sourceData.locales)
const tagsEnabled = !!destinationData.tags

return entities.reduce((transformedSpaceData, type) => {
return <TransformedSourceData>entities.reduce((transformedSpaceData, type) => {
// tags don't contain links to other entities, don't need to be sorted
const sortedEntities = (type === 'tags') ? sourceData[type] : sortEntries(sourceData[type])

const transformedEntities = sortedEntities.map((entity) => ({
transformedSpaceData[type] = sortedEntities?.map((entity) => ({
original: entity,
transformed: transformers[type](entity, destinationData[type], tagsEnabled)
}))
transformedSpaceData[type] = transformedEntities
return transformedSpaceData
}, baseSpaceData)
}
11 changes: 11 additions & 0 deletions lib/utils/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LogMessage } from 'contentful-batch-libs'

type ErrorMessage = Extract<LogMessage, { level: 'error' }>

export function isErrorLog (log: LogMessage): log is ErrorMessage {
return log.level !== 'info' && log.level !== 'warning'
}

export function isDisplayLog (log: LogMessage): boolean {
return log.level !== 'info'
}
14 changes: 6 additions & 8 deletions lib/utils/sort-entries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { some, filter, map } from 'lodash/collection'
import * as _o from 'lodash/object'
import { flatten } from 'lodash/array'
import { some, filter, map, flatten, values, get, has } from 'lodash'

/**
* Given a list of entries, this function reorders them so that entries which
Expand Down Expand Up @@ -34,7 +32,7 @@ function getLinkedEntries (entries) {
const entryIndex = entries.indexOf(entry)

const rawLinks = map(entry.fields, (field) => {
field = _o.values(field)[0]
field = values(field)[0]
if (isEntryLink(field)) {
return getFieldEntriesIndex(field, entries)
} else if (isEntityArray(field) && isEntryLink(field[0])) {
Expand All @@ -50,17 +48,17 @@ function getLinkedEntries (entries) {
}

function getFieldEntriesIndex (field, entries) {
const id = _o.get(field, 'sys.id')
const id = get(field, 'sys.id')
return entries.findIndex((entry) => entry.sys.id === id)
}

function isEntryLink (item) {
return _o.get(item, 'sys.type') === 'Entry' ||
_o.get(item, 'sys.linkType') === 'Entry'
return get(item, 'sys.type') === 'Entry' ||
get(item, 'sys.linkType') === 'Entry'
}

function isEntityArray (item) {
return Array.isArray(item) && item.length > 0 && _o.has(item[0], 'sys')
return Array.isArray(item) && item.length > 0 && has(item[0], 'sys')
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/validations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { payloadSchema } from './schema'
import getEntityName from 'contentful-batch-libs/dist/get-entity-name'
import { getEntityName } from 'contentful-batch-libs'

const attachEntityName = (details, payload) => {
details.map((detail) => {
Expand Down
23 changes: 7 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@discoveryjs/json-ext": "^0.5.7",
"bluebird": "^3.7.2",
"cli-table3": "^0.6.0",
"contentful-batch-libs": "^9.6.0",
"contentful-batch-libs": "^10.1.1",
"contentful-management": "^11.5.0",
"date-fns": "^2.30.0",
"eslint": "^8.53.0",
Expand Down
15 changes: 5 additions & 10 deletions test/unit/tasks/init-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import initClient from '../../../lib/tasks/init-client'

import contentfulManagement from 'contentful-management'
import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { logEmitter } from 'contentful-batch-libs'

jest.mock('contentful-management', () => {
return {
createClient: jest.fn(() => 'cmaClient')
}
})

jest.mock('contentful-batch-libs/dist/logging', () => {
return {
logEmitter: {
emit: jest.fn()
}
}
})
jest.spyOn(logEmitter, 'emit').mockImplementation(jest.fn())
const mockedLogEmitter = logEmitter as jest.Mocked<typeof logEmitter>

test('does create clients and passes custom logHandler', () => {
const opts = {
Expand Down Expand Up @@ -53,6 +48,6 @@ test('does create clients and passes custom logHandler', () => {
// Call passed log handler
(contentfulManagement.createClient as jest.Mock).mock.calls[0][0].logHandler('level', 'logMessage')

expect(logEmitter.emit.mock.calls[0][0]).toBe('level')
expect(logEmitter.emit.mock.calls[0][1]).toBe('logMessage')
expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('level')
expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe('logMessage')
})
27 changes: 12 additions & 15 deletions test/unit/tasks/push/assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ import {
getAssetStreamForURL
} from '../../../../lib/tasks/push-to-space/assets'

import { logEmitter } from 'contentful-batch-libs/dist/logging'
import { logEmitter } from 'contentful-batch-libs'
import { MockedFs } from '../../../types'

jest.mock('contentful-batch-libs/dist/logging', () => ({
logEmitter: {
emit: jest.fn()
}
}))
jest.spyOn(logEmitter, 'emit').mockImplementation(jest.fn())
const mockedLogEmitter = logEmitter as jest.Mocked<typeof logEmitter>

jest.mock('fs')

Expand All @@ -31,7 +28,7 @@ beforeEach(() => {
interval: 1000,
intervalCap: 1000
})
logEmitter.emit.mockClear();
mockedLogEmitter.emit.mockClear();
(fs as unknown as MockedFs).__setMockFiles(assetPaths)
})

Expand Down Expand Up @@ -65,7 +62,7 @@ test('Process assets', async () => {
expect(processStub.mock.calls[1][0]).toBe('en-GB')
expect(processStub.mock.calls[2][0]).toBe('en-US')
expect(processStub.mock.calls[3][0]).toBe('en-GB')
expect(logEmitter.emit.mock.calls).toHaveLength(2)
expect(mockedLogEmitter.emit.mock.calls).toHaveLength(2)
})

test('Return most up to date processed asset version', async () => {
Expand Down Expand Up @@ -143,13 +140,13 @@ test('Process assets fails', async () => {
// We expect two calls for the first asset (one for each locale)
// and two for the second asset of which one fails
expect(processStub.mock.calls).toHaveLength(4)
expect(logEmitter.emit.mock.calls).toHaveLength(3)
expect(logEmitter.emit.mock.calls[0][0]).toBe('info')
expect(logEmitter.emit.mock.calls[0][1]).toBe('Processing Asset 123')
expect(logEmitter.emit.mock.calls[1][0]).toBe('info')
expect(logEmitter.emit.mock.calls[1][1]).toBe('Processing Asset 456')
expect(logEmitter.emit.mock.calls[2][0]).toBe('error')
expect(logEmitter.emit.mock.calls[2][1]).toBe(failedError)
expect(mockedLogEmitter.emit.mock.calls).toHaveLength(3)
expect(mockedLogEmitter.emit.mock.calls[0][0]).toBe('info')
expect(mockedLogEmitter.emit.mock.calls[0][1]).toBe('Processing Asset 123')
expect(mockedLogEmitter.emit.mock.calls[1][0]).toBe('info')
expect(mockedLogEmitter.emit.mock.calls[1][1]).toBe('Processing Asset 456')
expect(mockedLogEmitter.emit.mock.calls[2][0]).toBe('error')
expect(mockedLogEmitter.emit.mock.calls[2][1]).toBe(failedError)
})

test('Get asset stream for url: Throw error if filePath does not exist', async () => {
Expand Down
Loading