mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-21 13:00:56 +01:00
Adding conduit API method to find owner for the affected file
Summary: Adding method that given a path will go up the folder hierarchy until it finds the owning package and return owners for that package. Task ID: #403724 Test Plan: Tried the new API call through console on various path combinations Reviewed By: epriestley Reviewers: epriestley, dpepper, tuomaspelkonen CC: epriestley, Leon Revert Plan: n/a Tags: bootcamp, Push Efficiency - begin *PUBLIC* platform impact section - Bugzilla: # - end platform impact - Differential Revision: 126
This commit is contained in:
parent
6baeda8aad
commit
bff6aef87a
5 changed files with 179 additions and 19 deletions
|
@ -89,6 +89,7 @@ phutil_register_library_map(array(
|
|||
'ConduitAPI_differential_updaterevision_Method' => 'applications/conduit/method/differential/updaterevision',
|
||||
'ConduitAPI_diffusion_getcommits_Method' => 'applications/conduit/method/diffusion/getcommits',
|
||||
'ConduitAPI_file_upload_Method' => 'applications/conduit/method/file/upload',
|
||||
'ConduitAPI_path_getowners_Method' => 'applications/conduit/method/path/getowners',
|
||||
'ConduitAPI_user_find_Method' => 'applications/conduit/method/user/find',
|
||||
'ConduitAPI_user_whoami_Method' => 'applications/conduit/method/user/whoami',
|
||||
'ConduitException' => 'applications/conduit/protocol/exception',
|
||||
|
@ -537,6 +538,7 @@ phutil_register_library_map(array(
|
|||
'ConduitAPI_differential_updaterevision_Method' => 'ConduitAPIMethod',
|
||||
'ConduitAPI_diffusion_getcommits_Method' => 'ConduitAPIMethod',
|
||||
'ConduitAPI_file_upload_Method' => 'ConduitAPIMethod',
|
||||
'ConduitAPI_path_getowners_Method' => 'ConduitAPIMethod',
|
||||
'ConduitAPI_user_find_Method' => 'ConduitAPIMethod',
|
||||
'ConduitAPI_user_whoami_Method' => 'ConduitAPIMethod',
|
||||
'DarkConsoleConfigPlugin' => 'DarkConsolePlugin',
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
class ConduitAPI_path_getowners_Method extends ConduitAPIMethod {
|
||||
|
||||
public function getMethodDescription() {
|
||||
return "Find owners package given its name";
|
||||
}
|
||||
|
||||
public function defineParamTypes() {
|
||||
return array(
|
||||
'repositoryCallsign' => 'required nonempty string',
|
||||
'path' => 'required nonempty string'
|
||||
);
|
||||
}
|
||||
|
||||
public function defineReturnType() {
|
||||
return 'array of packages containing phid, primary_owner (phid=>username),'.
|
||||
'owners(array of phid=>username)';
|
||||
}
|
||||
|
||||
public function defineErrorTypes() {
|
||||
return array(
|
||||
'ERR_REP_NOT_FOUND' => 'The repository callsign is not recognized',
|
||||
'ERR_PATH_NOT_FOUND' => 'The specified path is not known to any package',
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(ConduitAPIRequest $request) {
|
||||
|
||||
$repository = id(new PhabricatorRepository())->loadOneWhere('callsign = %s',
|
||||
$request->getValue('repositoryCallsign'));
|
||||
|
||||
if (empty($repository)) {
|
||||
throw new ConduitException('ERR_REP_NOT_FOUND');
|
||||
}
|
||||
|
||||
$packages = PhabricatorOwnersPackage::loadOwningPackages(
|
||||
$repository, $request->getValue('path'));
|
||||
if (empty($packages)) {
|
||||
throw new ConduitException('ERR_PATH_NOT_FOUND');
|
||||
}
|
||||
|
||||
$owner = new PhabricatorOwnersOwner();
|
||||
$user = new PhabricatorUser();
|
||||
$result = array();
|
||||
foreach ($packages as $package) {
|
||||
$p_result = array();
|
||||
$p_result['phid'] = $package->getID();
|
||||
$primary_owner_phid = $package->getPrimaryOwnerPHID();
|
||||
if (!empty($primary_owner_phid)) {
|
||||
$p_user = $user->loadOneWhere('phid = %s',
|
||||
$primary_owner_phid);
|
||||
$p_result['primaryOwner'] = $p_user->getPhid();
|
||||
}
|
||||
|
||||
$p_owners = $owner->loadAllForPackages(array($package));
|
||||
$p_users = $user->loadAllWhere('phid IN (%Ls)',
|
||||
mpull($p_owners, 'getUserPHID'));
|
||||
|
||||
$p_result['owners'] = array_values(mpull($p_users, 'getPhid'));
|
||||
|
||||
$result[] = $p_result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
19
src/applications/conduit/method/path/getowners/__init__.php
Normal file
19
src/applications/conduit/method/path/getowners/__init__.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is automatically generated. Lint this module to rebuild it.
|
||||
* @generated
|
||||
*/
|
||||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'applications/conduit/method/base');
|
||||
phutil_require_module('phabricator', 'applications/conduit/protocol/exception');
|
||||
phutil_require_module('phabricator', 'applications/owners/storage/owner');
|
||||
phutil_require_module('phabricator', 'applications/owners/storage/package');
|
||||
phutil_require_module('phabricator', 'applications/people/storage/user');
|
||||
phutil_require_module('phabricator', 'applications/repository/storage/repository');
|
||||
|
||||
phutil_require_module('phutil', 'utils');
|
||||
|
||||
|
||||
phutil_require_source('ConduitAPI_path_getowners_Method.php');
|
|
@ -79,29 +79,71 @@ class PhabricatorOwnersPackage extends PhabricatorOwnersDAO {
|
|||
);
|
||||
|
||||
foreach ($paths as $path) {
|
||||
$trailing_slash = preg_match('@/$@', $path) ? '/' : '';
|
||||
$path = trim($path, '/');
|
||||
$parts = explode('/', $path);
|
||||
while (count($parts)) {
|
||||
$fragments['/'.implode('/', $parts).$trailing_slash] = true;
|
||||
$trailing_slash = '/';
|
||||
array_pop($parts);
|
||||
$fragments += self::splitPath($path);
|
||||
}
|
||||
|
||||
return self::loadPackagesForPaths($repository, array_keys($fragments));
|
||||
}
|
||||
|
||||
public static function loadOwningPackages($repository, $path) {
|
||||
if (empty($path)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$fragments = self::splitPath($path);
|
||||
return self::loadPackagesForPaths($repository, array_keys($fragments), 1);
|
||||
}
|
||||
|
||||
private static function loadPackagesForPaths(
|
||||
PhabricatorRepository $repository,
|
||||
array $paths,
|
||||
$limit = 0) {
|
||||
$package = new PhabricatorOwnersPackage();
|
||||
$path = new PhabricatorOwnersPath();
|
||||
$conn = $package->establishConnection('r');
|
||||
|
||||
$repository_clause = qsprintf($conn, 'AND p.repositoryPHID = %s',
|
||||
$repository->getPHID());
|
||||
|
||||
$limit_clause = '';
|
||||
if (!empty($limit)) {
|
||||
$limit_clause = qsprintf($conn, 'LIMIT %d', $limit);
|
||||
}
|
||||
|
||||
$data = queryfx_all(
|
||||
$conn,
|
||||
'SELECT pkg.id FROM %T pkg JOIN %T p ON p.packageID = pkg.id
|
||||
WHERE p.path IN (%Ls) %Q ORDER BY LENGTH(p.path) DESC %Q',
|
||||
$package->getTableName(),
|
||||
$path->getTableName(),
|
||||
$paths,
|
||||
$repository_clause,
|
||||
$limit_clause);
|
||||
|
||||
$ids = ipull($data, 'id');
|
||||
|
||||
if (empty($ids)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$order = array();
|
||||
foreach ($ids as $id) {
|
||||
if (empty($order[$id])) {
|
||||
$order[$id] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$package = new PhabricatorOwnersPackage();
|
||||
$path = new PhabricatorOwnersPath();
|
||||
$data = queryfx_all(
|
||||
$package->establishConnection('r'),
|
||||
'SELECT pkg.* FROM %T pkg JOIN %T p ON p.packageID = pkg.id
|
||||
WHERE p.repositoryPHID = %s
|
||||
AND p.path IN (%Ls)',
|
||||
$package->getTableName(),
|
||||
$path->getTableName(),
|
||||
$repository->getPHID(),
|
||||
array_keys($fragments));
|
||||
$packages = $package->loadAllWhere('id in (%Ld)', array_keys($order));
|
||||
|
||||
return $package->loadAllFromArray($data);
|
||||
$result = array();
|
||||
// Reorder packages according to specificity.
|
||||
foreach ($packages as $package) {
|
||||
$result[$package->getID()] = $package;
|
||||
}
|
||||
|
||||
$result = array_select_keys($result, array_keys($order));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function save() {
|
||||
|
@ -167,4 +209,16 @@ class PhabricatorOwnersPackage extends PhabricatorOwnersDAO {
|
|||
return parent::delete();
|
||||
}
|
||||
|
||||
private static function splitPath($path) {
|
||||
$result = array();
|
||||
$trailing_slash = preg_match('@/$@', $path) ? '/' : '';
|
||||
$path = trim($path, '/');
|
||||
$parts = explode('/', $path);
|
||||
while (count($parts)) {
|
||||
$result['/'.implode('/', $parts).$trailing_slash] = true;
|
||||
$trailing_slash = '/';
|
||||
array_pop($parts);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ phutil_require_module('phabricator', 'applications/owners/storage/base');
|
|||
phutil_require_module('phabricator', 'applications/owners/storage/owner');
|
||||
phutil_require_module('phabricator', 'applications/owners/storage/path');
|
||||
phutil_require_module('phabricator', 'applications/phid/storage/phid');
|
||||
phutil_require_module('phabricator', 'storage/qsprintf');
|
||||
phutil_require_module('phabricator', 'storage/queryfx');
|
||||
|
||||
phutil_require_module('phutil', 'utils');
|
||||
|
|
Loading…
Reference in a new issue