Skip to content

Commit d282549

Browse files
author
Jason Ge
committed
Add commits to the Phabricator search index
Summary: create the indexer for commit Test Plan: run the reindex_one_commit.php against one existing commit and it is returned in search result on the webpage; run parse_one_commit against another commit; modified reindex_everything.php to let it only parse one commit after loading all commits. Reviewed By: epriestley Reviewers: epriestley, aran CC: aran, jungejason, epriestley, debow Differential Revision: 490
1 parent 405b05a commit d282549

File tree

11 files changed

+170
-7
lines changed

11 files changed

+170
-7
lines changed

scripts/search/index_one_commit.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/*
5+
* Copyright 2011 Facebook, Inc.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
$root = dirname(dirname(dirname(__FILE__)));
21+
require_once $root.'/scripts/__init_script__.php';
22+
require_once $root.'/scripts/__init_env__.php';
23+
24+
if (empty($argv[1])) {
25+
echo "usage: index_one_commit.php <commit_name>\n";
26+
die(1);
27+
}
28+
29+
$commit = isset($argv[1]) ? $argv[1] : null;
30+
if (!$commit) {
31+
throw new Exception("Provide a commit to index!");
32+
}
33+
$matches = null;
34+
if (!preg_match('/r([A-Z]+)([a-z0-9]+)/', $commit, $matches)) {
35+
throw new Exception("Can't parse commit identifier!");
36+
}
37+
$repo = id(new PhabricatorRepository())->loadOneWhere(
38+
'callsign = %s',
39+
$matches[1]);
40+
if (!$repo) {
41+
throw new Exception("Unknown repository!");
42+
}
43+
44+
$commit = id(new PhabricatorRepositoryCommit())->loadOneWhere(
45+
'repositoryID = %d AND commitIdentifier = %s',
46+
$repo->getID(),
47+
$matches[2]);
48+
if (!$commit) {
49+
throw new Exception('Unknown commit.');
50+
}
51+
52+
PhabricatorSearchCommitIndexer::indexCommit($commit);
53+
echo "Done.\n";

scripts/search/reindex_everything.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424
// TODO: Get rid of this script eventually, once this stuff is better-formalized
2525
// in Timeline consumers.
2626

27-
phutil_require_module('phutil', 'symbols');
28-
PhutilSymbolLoader::loadClass('DifferentialRevision');
29-
PhutilSymbolLoader::loadClass('PhabricatorSearchDifferentialIndexer');
30-
PhutilSymbolLoader::loadClass('ManiphestTask');
31-
PhutilSymbolLoader::loadClass('PhabricatorSearchManiphestIndexer');
32-
3327
echo "Loading revisions...\n";
3428
$revs = id(new DifferentialRevision())->loadAll();
3529
$count = count($revs);
@@ -40,6 +34,16 @@
4034
}
4135
echo "\n";
4236

37+
echo "Loading commits...\n";
38+
$commits = id(new PhabricatorRepositoryCommit())->loadAll();
39+
$count = count($commits);
40+
echo "Reindexing {$count} commits";
41+
foreach ($commits as $commit) {
42+
PhabricatorSearchCommitIndexer::indexCommit($commit);
43+
echo '.';
44+
}
45+
echo "\n";
46+
4347
echo "Loading tasks...\n";
4448
$tasks = id(new ManiphestTask())->loadAll();
4549
$count = count($tasks);

src/__phutil_library_map__.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@
479479
'PhabricatorSearchAbstractDocument' => 'applications/search/index/abstractdocument',
480480
'PhabricatorSearchAttachController' => 'applications/search/controller/attach',
481481
'PhabricatorSearchBaseController' => 'applications/search/controller/base',
482+
'PhabricatorSearchCommitIndexer' => 'applications/search/index/indexer/repository',
482483
'PhabricatorSearchController' => 'applications/search/controller/search',
483484
'PhabricatorSearchDAO' => 'applications/search/storage/base',
484485
'PhabricatorSearchDifferentialIndexer' => 'applications/search/index/indexer/differential',
@@ -943,6 +944,7 @@
943944
'PhabricatorRepositorySvnCommitMessageParserWorker' => 'PhabricatorRepositoryCommitMessageParserWorker',
944945
'PhabricatorSearchAttachController' => 'PhabricatorSearchController',
945946
'PhabricatorSearchBaseController' => 'PhabricatorController',
947+
'PhabricatorSearchCommitIndexer' => 'PhabricatorSearchDocumentIndexer',
946948
'PhabricatorSearchController' => 'PhabricatorSearchBaseController',
947949
'PhabricatorSearchDAO' => 'PhabricatorLiskDAO',
948950
'PhabricatorSearchDifferentialIndexer' => 'PhabricatorSearchDocumentIndexer',

src/applications/repository/storage/commitdata/PhabricatorRepositoryCommitData.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
class PhabricatorRepositoryCommitData extends PhabricatorRepositoryDAO {
2020

21+
const SUMMARY_MAX_LENGTH = 100;
22+
2123
protected $commitID;
2224
protected $authorName;
2325
protected $commitMessage;
@@ -33,7 +35,12 @@ public function getConfiguration() {
3335
}
3436

3537
public function getSummary() {
36-
return substr($this->getCommitMessage(), 0, 80);
38+
$message = $this->getCommitMessage();
39+
$lines = explode("\n", $message);
40+
$summary = head($lines);
41+
$summary = substr($summary, 0, self::SUMMARY_MAX_LENGTH);
42+
43+
return $summary;
3744
}
3845

3946
public function getCommitDetail($key, $default = null) {

src/applications/repository/worker/commitchangeparser/base/PhabricatorRepositoryCommitChangeParserWorker.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ private function lookupPaths(array $paths) {
7373

7474
protected function finishParse() {
7575
$commit = $this->commit;
76+
PhabricatorSearchCommitIndexer::indexCommit($commit);
77+
7678
if ($this->shouldQueueFollowupTasks()) {
7779
$task = new PhabricatorWorkerTask();
7880
$task->setTaskClass('PhabricatorRepositoryCommitHeraldWorker');

src/applications/repository/worker/commitchangeparser/base/__init__.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
phutil_require_module('phabricator', 'applications/repository/storage/repository');
1010
phutil_require_module('phabricator', 'applications/repository/worker/base');
11+
phutil_require_module('phabricator', 'applications/search/index/indexer/repository');
1112
phutil_require_module('phabricator', 'infrastructure/daemon/workers/storage/task');
1213
phutil_require_module('phabricator', 'storage/qsprintf');
1314
phutil_require_module('phabricator', 'storage/queryfx');

src/applications/search/constants/relationship/PhabricatorSearchRelationship.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class PhabricatorSearchRelationship {
2424
const RELATIONSHIP_COMMENTER = 'comm';
2525
const RELATIONSHIP_OWNER = 'ownr';
2626
const RELATIONSHIP_PROJECT = 'proj';
27+
const RELATIONSHIP_REPOSITORY = 'repo';
2728

2829
const RELATIONSHIP_OPEN = 'open';
2930
const RELATIONSHIP_TOUCH = 'poke';

src/applications/search/controller/search/PhabricatorSearchController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function processRequest() {
7070
$options = array(
7171
'' => 'All Documents',
7272
PhabricatorPHIDConstants::PHID_TYPE_DREV => 'Differential Revisions',
73+
PhabricatorPHIDConstants::PHID_TYPE_CMIT => 'Repository Commits',
7374
PhabricatorPHIDConstants::PHID_TYPE_TASK => 'Maniphest Tasks',
7475
) + $more;
7576

src/applications/search/execute/mysql/PhabricatorSearchMySQLExecutor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ public function executeSearch(PhabricatorSearchQuery $query) {
165165
'project',
166166
PhabricatorSearchRelationship::RELATIONSHIP_PROJECT);
167167

168+
$join[] = $this->joinRelationship(
169+
$conn_r,
170+
$query,
171+
'repository',
172+
PhabricatorSearchRelationship::RELATIONSHIP_REPOSITORY);
173+
168174
/*
169175
$join[] = $this->joinRelationship(
170176
$conn_r,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2011 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
class PhabricatorSearchCommitIndexer
20+
extends PhabricatorSearchDocumentIndexer {
21+
22+
public static function indexCommit(PhabricatorRepositoryCommit $commit) {
23+
$commit_data = id(new PhabricatorRepositoryCommitData())->loadOneWhere(
24+
'commitID = %d',
25+
$commit->getID());
26+
$date_created = $commit->getEpoch();
27+
$commit_message = $commit_data->getCommitMessage();
28+
$author_phid = $commit_data->getCommitDetail('authorPHID');
29+
30+
$repository = id(new PhabricatorRepository())->loadOneWhere(
31+
'id = %d',
32+
$commit->getRepositoryID());
33+
34+
$title = 'r'.$repository->getCallsign().$commit->getCommitIdentifier().
35+
" ".$commit_data->getSummary();
36+
37+
$doc = new PhabricatorSearchAbstractDocument();
38+
$doc->setPHID($commit->getPHID());
39+
$doc->setDocumentType(PhabricatorPHIDConstants::PHID_TYPE_CMIT);
40+
$doc->setDocumentCreated($date_created);
41+
$doc->setDocumentModified($date_created);
42+
$doc->setDocumentTitle($title);
43+
44+
$doc->addField(
45+
PhabricatorSearchField::FIELD_BODY,
46+
$commit_message);
47+
48+
if ($author_phid) {
49+
$doc->addRelationship(
50+
PhabricatorSearchRelationship::RELATIONSHIP_AUTHOR,
51+
$author_phid,
52+
PhabricatorPHIDConstants::PHID_TYPE_USER,
53+
$date_created);
54+
}
55+
56+
$doc->addRelationship(
57+
PhabricatorSearchRelationship::RELATIONSHIP_REPOSITORY,
58+
$repository->getPHID(),
59+
PhabricatorPHIDConstants::PHID_TYPE_REPO,
60+
$date_created);
61+
62+
PhabricatorSearchDocument::reindexAbstractDocument($doc);
63+
}
64+
}
65+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phabricator', 'applications/phid/constants');
10+
phutil_require_module('phabricator', 'applications/repository/storage/commitdata');
11+
phutil_require_module('phabricator', 'applications/repository/storage/repository');
12+
phutil_require_module('phabricator', 'applications/search/constants/field');
13+
phutil_require_module('phabricator', 'applications/search/constants/relationship');
14+
phutil_require_module('phabricator', 'applications/search/index/abstractdocument');
15+
phutil_require_module('phabricator', 'applications/search/index/indexer/base');
16+
phutil_require_module('phabricator', 'applications/search/storage/document/document');
17+
18+
phutil_require_module('phutil', 'utils');
19+
20+
21+
phutil_require_source('PhabricatorSearchCommitIndexer.php');

0 commit comments

Comments
 (0)