Page MenuHomePhabricator
Paste P1836

bulk_move_workboard.php
ActivePublic

Authored by joshuaspence on Aug 2 2015, 9:54 PM.
Tags
None
Referenced Files
F685109: bulk_move_workboard.php
Aug 2 2015, 9:54 PM
Subscribers
None
<?php
// Depends on https://secure.phabricator.com/D13718
require_once __DIR__.'/scripts/__init_script__.php';
$args = new PhutilArgumentParser($argv);
$args->setSynopsis(<<<EOSYNOPSIS
**bulk_move_workboard.php** [__options__]
Bulk moves resolved tasks on a workboard. This script will select //all//
resolved belonging to a specified project and will move all resolved tasks to
the specified column on the project's workboard.
EOSYNOPSIS
);
$args->parseStandardArguments();
$args->parse(
array(
array(
'name' => 'user',
'param' => 'username',
'help' => pht(
'The acting user. Transactions will be generated on '.
'behalf of this user.'),
),
array(
'name' => 'project',
'param' => 'slug',
'help' => pht('The project slug.'),
),
array(
'name' => 'column',
'param' => 'name',
'help' => pht(
'The destination column. All matching tasks will be moved to '.
'this column on the project workboard.'),
),
array(
'name' => 'dry-run',
'help' => pht(
'If this flag is specified, the script will not perform any '.
'writes but rather will output the intended operations to the '.
'console for verification.'),
),
));
$username = $args->getArg('user');
if (!$username) {
echo pht('You must specify an acting user.')."\n";
exit(1);
}
$viewer = id(new PhabricatorPeopleQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withUsernames(array($args->getArg('user')))
->executeOne();
if (!$viewer) {
throw new Exception(pht("User '%s' not found!", $username));
}
$project_slug = $args->getArg('project');
if (!$project_slug) {
echo pht('You must specify a project.')."\n";
exit(1);
}
$project = id(new PhabricatorProjectQuery())
->setViewer($viewer)
->withSlugs(array($project_slug))
->executeOne();
if (!$project) {
throw new Exception(pht("Project '#%s' not found!", $project_slug));
}
$column_name = $args->getArg('column');
if (!$column_name) {
echo pht('You must specify a column.')."\n";
exit(1);
}
$column = id(new PhabricatorProjectColumnQuery())
->setViewer($viewer)
->withProjectPHIDs(array($project->getPHID()))
->withNames(array($column_name))
->executeOne();
if (!$column) {
throw new Exception(pht("Column '%s' not found!", $column_name));
}
$task_phids = PhabricatorEdgeQuery::loadDestinationPHIDs(
$project->getPHID(),
PhabricatorProjectProjectHasObjectEdgeType::EDGECONST);
$tasks = id(new ManiphestTaskQuery())
->setViewer($viewer)
->withPHIDs($task_phids)
->withStatus(ManiphestTaskQuery::STATUS_RESOLVED)
->execute();
$console = PhutilConsole::getConsole();
$editor = id(new ManiphestTransactionEditor())
->setActor($viewer)
->setContentSource(
PhabricatorContentSource::newForSource(
PhabricatorContentSource::SOURCE_UNKNOWN,
array()));
foreach ($tasks as $task) {
$position = id(new PhabricatorProjectColumnPositionQuery())
->setViewer($viewer)
->withBoardPHIDs(array($column->getProjectPHID()))
->withObjectPHIDs(array($task->getPHID()))
->executeOne();
if (!$position) {
throw new Exception(
pht('Position not found for task %s!', $task->getPHID()));
}
if ($position->getColumnPHID() == $column->getPHID()) {
$console->writeOut(
pht(
"Task %s (%s) does not need to be moved.\n",
$task->getPHID(),
$task->getMonogram()));
continue;
}
if ($args->getArg('dry-run')) {
$console->writeOut(
pht(
"Task %s (%s) would be moved from %s to %s.\n",
$task->getPHID(),
$task->getMonogram(),
$position->getColumnPHID(),
$column->getPHID()));
continue;
}
$xactions = array();
$xactions[] = id(new ManiphestTransaction())
->setTransactionType(ManiphestTransaction::TYPE_PROJECT_COLUMN)
->setOldValue(
array(
'columnPHIDs' => array($position->getColumnPHID()),
'projectPHID' => $position->getBoardPHID(),
))
->setNewValue(
array(
'columnPHIDs' => array($column->getPHID()),
'projectPHID' => $column->getProjectPHID(),
));
$editor->applyTransactions($task, $xactions);
$console->writeOut(
pht(
"Task %s (%s) moved from %s to %s.\n",
$task->getPHID(),
$task->getMonogram(),
$position->getColumnPHID(),
$column->getPHID()));
}
$console->writeOut(pht('Done!')."\n");

Event Timeline

joshuaspence changed the title of this paste from untitled to bulk_move_workboard.php.
joshuaspence updated the paste's language from autodetect to autodetect.