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

Add publish flag for rollback command #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
feature: add publish flag for rollback command
shchypylov committed Jul 7, 2023
commit 94787afd1df5e98dfb3d4fcbdea23d9bd378f248
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -323,6 +323,10 @@ $ storyblok rollback-migration --space 1234 --component Product --field title
* `space`: the space you get from the space settings area
* `component`: component name. It needs to be a valid component
* `field`: name of field
* `publish` (optional): publish the content when update
* `all`: publish all stories, even if they have not yet been published
* `published`: only publish stories that already are published and don't have unpublished changes
* `published-with-changes`: publish stories that are published and have unpublished changes

### spaces

14 changes: 13 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
@@ -418,23 +418,35 @@ program
.description('Rollback-migration a migration file')
.requiredOption('-c, --component <COMPONENT_NAME>', 'Name of the component')
.requiredOption('-f, --field <FIELD_NAME>', 'Name of the component field')
.option('--publish <PUBLISH_OPTION>', 'Publish the content. It can be: all, published or published-with-changes')
.action(async (options) => {
const field = options.field || ''
const component = options.component || ''
const publish = options.publish || null
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(1)
}

const publishOptionsAvailable = [
'all',
'published',
'published-with-changes'
]
if (publish && !publishOptionsAvailable.includes(publish)) {
console.log(chalk.red('X') + ' Please provide a correct publish option: all, published, or published-with-changes')
process.exit(1)
}

try {
if (!api.isAuthorized()) {
await api.processLogin()
}

api.setSpaceId(space)

await tasks.rollbackMigration(api, field, component)
await tasks.rollbackMigration(api, field, component, { publish })
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred when run rollback-migration: ' + e.message)
process.exit(1)
48 changes: 45 additions & 3 deletions src/tasks/migrations/rollback.js
Original file line number Diff line number Diff line change
@@ -6,15 +6,42 @@ const {
urlTofRollbackMigrationFile
} = require('./utils')

/**
* @method isStoryPublishedWithoutChanges
* @param {Object} story
* @return {Boolean}
*/
const isStoryPublishedWithoutChanges = story => {
return story.published && !story.unpublished_changes
}

/**
* @method isStoryWithUnpublishedChanges
* @param {Object} story
* @return {Boolean}
*/
const isStoryWithUnpublishedChanges = story => {
return story.published && story.unpublished_changes
}
/**
* @typedef {'all'|'published'|'published-with-changes'} PublishOptions
*
* @typedef {Object} RunRollbackOptions
* @property {PublishOptions} publish
* /

Copy link
Member

Choose a reason for hiding this comment

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

These functions already exist in the run.js file, how about extracting them from there to a constants file and using them in both places?

Copy link
Author

Choose a reason for hiding this comment

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

Done, moved them to utils.js file and reused them in run.js and rollback.js files.

/**
* @method rollbackMigration
* @param {Object} api api instance
* @param {String} component name of the component to rollback
* @param {String} field name of the field to rollback
* @param {RunRollbackOptions} options disable execution
* @return {Promise}
*/

const rollbackMigration = async (api, field, component) => {
const rollbackMigration = async (api, field, component, options = {}) => {
const publish = options.publish || null

if (!fs.existsSync(MIGRATIONS_ROLLBACK_DIRECTORY)) {
console.log(`
${chalk.red('X')} To execute the rollback-migration command you need to have changed some component with the migrations commands.`
@@ -47,10 +74,25 @@ const rollbackMigration = async (api, field, component) => {
console.log(
`${chalk.blue('-')} Restoring data from "${chalk.blue(story.full_slug)}" ...`
)
await api.put(`stories/${story.id}`, {
const storyData = await api.getSingleStory(story.id)
const payload = {
story: { content: story.content },
force_update: '1'
})
}

if (publish === 'published' && isStoryPublishedWithoutChanges(storyData)) {
payload.publish = '1'
}

if (publish === 'published-with-changes' && isStoryWithUnpublishedChanges(story)) {
payload.publish = '1'
}

if (publish === 'all') {
payload.publish = '1'
}

await api.put(`stories/${story.id}`, payload)
console.log(
` ${chalk.blue('-')} ${story.full_slug} data has been restored!`
)
2 changes: 1 addition & 1 deletion src/utils/last-step.js
Original file line number Diff line number Diff line change
@@ -49,7 +49,7 @@ const lastStep = answers => {
console.log(chalk.green('✓') + ' - The github repository ' + gitRepo + ' will be cloned now...')

ghdownload(gitRepo, outputDir, async (err) => {
if(err) {
if (err) {
if (err.code === 'ENOTEMPTY') {
console.log(chalk.red(' Oh Snap! It seems that you already have a project with the name: ' + name))
reject(new Error('This repository already has been cloned'))