Page MenuHomePhabricator

create_repo.php

Authored By
avivey
Mar 30 2016, 11:41 PM
Size
6 KB
Referenced Files
None
Subscribers
None

create_repo.php

#!/usr/bin/env php
<?php
$root = dirname(dirname(dirname(__FILE__)));
$phab = dirname($root).'/phabricator';
require_once $phab.'/scripts/__init_script__.php';
$args = new PhutilArgumentParser($argv);
$args->setTagline(pht('Create repositories'));
$args->setSynopsis(<<<EOSYNOPSIS
**create_repo** [__options__]
Creates a new Hosted repository.
EOSYNOPSIS
);
$args->parseStandardArguments();
$args_spec = array(
array(
'name' => 'admin',
'param' => 'user',
'help' => 'Username act as.',
),
array(
'name' => 'name',
'param' => 'name',
'help' => 'Repository Long, human readable, nice name. Optional.',
),
array(
'name' => 'shortname',
'param' => 'shortname',
'help' => 'Short name. Will be used as the "clone as" name.',
),
array(
'name' => 'policy',
'param' => 'policy',
'help' => 'PHID tp use as view policy. May be a USER, PROJ, or PLCY. '.
'Defaults to "all users".',
),
array(
'name' => 'callsign',
'param' => 'callsign',
'help' => 'Callsign to use (Default no callsign)',
),
array(
'name' => 'projects',
'param' => 'projects',
'help' => 'List of project tags to add (optional, comma-sperated).',
),
array(
'name' => 'description',
'param' => 'description',
'help' => 'Description to give repo (optional).',
),
);
$args->parse($args_spec);
$viewer = id(new PhabricatorUser())->loadOneWhere(
'username = %s',
$args->getArg('admin'));
if (!$viewer) {
throw new Exception(
pht(
'Admin user must be the username of a valid Phabricator account, used '.
'to create the repository.'));
}
$policy_id = $args->getArg('policy');
$projects = explode(',', $args->getArg('projects'));
$staging_repo = 'rSTAGE';
$object_names = array(
$policy_id,
$staging_repo,
);
$object_names = array_merge($object_names, $projects);
$object_query = id(new PhabricatorObjectQuery())
->setViewer($viewer)
->withNames($object_names);
$object_query->execute();
$named_objects = $object_query->getNamedResults();
$project_phids = array();
foreach ($projects as $proj) {
$p = idx($named_objects, $proj);
if ($p) {
$project_phids[] = $p->getPHID();
} else {
throw new Exception('Unknown project "'.$proj.'": '.
'projects list should be comma-separated.');
}
}
if ($policy_id) {
$view_policy = idx($named_objects, $policy_id);
if (!$view_policy) {
throw new Exception('unknown view policy '.$policy_id);
}
$allowed = array(
'PhabricatorUser' => true,
'PhabricatorProject' => true,
'PhabricatorPolicy' => true,
);
if (!idx($allowed, get_class($view_policy))) {
throw new Exception('Not a policy: '.$policy_id);
}
$view_policy = $view_policy->getPHID();
} else {
$view_policy = null;
}
$description = $args->getArg('description');
$callsign = $args->getArg('callsign');
$short_name = $args->getArg('shortname');
if (!$short_name) {
throw Exception('Required shortname missing');
}
$long_name = $args->getArg('name');
if (!strlen($long_name)) {
$long_name = $short_name;
}
$staging_repo = $named_objects[$staging_repo];
$staging_repo_uri = $staging_repo->getPublicCloneURI();
$template = id(new PhabricatorRepositoryTransaction());
$type_name = PhabricatorRepositoryTransaction::TYPE_NAME;
$type_vcs = PhabricatorRepositoryTransaction::TYPE_VCS;
$type_activate = PhabricatorRepositoryTransaction::TYPE_ACTIVATE;
$type_hosting = PhabricatorRepositoryTransaction::TYPE_HOSTING;
$type_http = PhabricatorRepositoryTransaction::TYPE_PROTOCOL_HTTP;
$type_ssh = PhabricatorRepositoryTransaction::TYPE_PROTOCOL_SSH;
$type_view = PhabricatorTransactions::TYPE_VIEW_POLICY;
$type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY;
$type_space = PhabricatorTransactions::TYPE_SPACE;
$type_push = PhabricatorRepositoryTransaction::TYPE_PUSH_POLICY;
$type_service = PhabricatorRepositoryTransaction::TYPE_SERVICE;
$type_track = PhabricatorRepositoryTransaction::TYPE_TRACK_ONLY;
$type_autoclose = PhabricatorRepositoryTransaction::TYPE_AUTOCLOSE_ONLY;
$type_staging = PhabricatorRepositoryTransaction::TYPE_STAGING_URI;
$type_desc = PhabricatorRepositoryTransaction::TYPE_DESCRIPTION;
$type_edge = PhabricatorTransactions::TYPE_EDGE;
$type_slug = PhabricatorRepositoryTransaction::TYPE_SLUG;
$type_callsign = PhabricatorRepositoryTransaction::TYPE_CALLSIGN;
$xactions = array();
$repository = PhabricatorRepository::initializeNewRepository($viewer);
$xactions[] = id(clone $template)
->setTransactionType($type_name)
->setNewValue($long_name);
$xactions[] = id(clone $template)
->setTransactionType($type_vcs)
->setNewValue('git');
if ($description) {
$xactions[] = id(clone $template)
->setTransactionType($type_desc)
->setNewValue($description);
}
$xactions[] = id(clone $template)
->setTransactionType($type_slug)
->setNewValue($short_name);
if ($callsign) {
$xactions[] = id(clone $template)
->setTransactionType($type_callsign)
->setNewValue($callsign);
}
$xactions[] = id(clone $template)
->setTransactionType($type_edge)
->setMetadataValue(
'edge:type',
PhabricatorProjectObjectHasProjectEdgeType::EDGECONST)
->setNewValue(
array(
'=' => array_fuse($project_phids),
));
$xactions[] = id(clone $template)
->setTransactionType($type_view)
->setNewValue($view_policy);
$xactions[] = id(clone $template)
->setTransactionType($type_autoclose)
->setNewValue(array('master'));
$xactions[] = id(clone $template)
->setTransactionType($type_activate)
->setNewValue(true);
$xactions[] = id(clone $template)
->setTransactionType($type_staging)
->setNewValue($staging_repo_uri);
if (PhabricatorEnv::getEnvConfig('diffusion.ssh-user')) {
$ssh_mode = PhabricatorRepository::SERVE_READWRITE;
} else {
$ssh_mode = PhabricatorRepository::SERVE_OFF;
}
$xactions[] = id(clone $template)
->setTransactionType($type_ssh)
->setNewValue($ssh_mode);
$xactions[] = id(clone $template)
->setTransactionType($type_hosting)
->setNewValue(true);
id(new PhabricatorRepositoryEditor())
->setContinueOnNoEffect(true)
->setContentSource(PhabricatorContentSource::newConsoleSource())
->setActor($viewer)
->applyTransactions($repository, $xactions);
$repo_uri = $repository->getURI();
echo 'Created new repo: '.$repo_uri."\n";

File Metadata

Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
92600
Default Alt Text
create_repo.php (6 KB)

Event Timeline