This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
/
ShipItUserFilters.php
103 lines (98 loc) · 2.9 KB
/
ShipItUserFilters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?hh // strict
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
namespace Facebook\ShipIt;
class ShipItUserFilters {
/** Rewrite authors that match a certain pattern.
*
* @param $pattern a regular expression defining a 'user' named capture
*/
public static function rewriteAuthorWithUserPattern(
ShipItChangeset $changeset,
classname<ShipItUserInfo> $user_info,
string $pattern,
): ShipItChangeset {
$matches = [];
if (
preg_match(
$pattern,
$changeset->getAuthor(),
&$matches,
)
&& array_key_exists('user', $matches)
) {
$author = \HH\Asio\join(
$user_info::getDestinationAuthorFromLocalUser(
$matches['user']
)
);
if ($author !== null) {
return $changeset->withAuthor($author);
}
}
return $changeset;
}
/** Rewrite author fields created by git-svn or HgSubversion.
*
* Original author: foobar@uuid
* New author: Foo Bar <foobar@example.com>
*/
public static function rewriteSVNAuthor(
ShipItChangeset $changeset,
classname<ShipItUserInfo> $user_info,
): ShipItChangeset {
return self::rewriteAuthorWithUserPattern(
$changeset,
$user_info,
'/^(?<user>.*)@[a-f0-9-]{36}$/',
);
}
public static function rewriteMentions(
ShipItChangeset $changeset,
classname<ShipItUserInfo> $user_info,
): ShipItChangeset {
return ShipItMentions::rewriteMentions(
$changeset,
function(string $mention): string use ($user_info) {
$mention = substr($mention, 1); // chop off leading @
$new = \HH\Asio\join(
$user_info::getDestinationUserFromLocalUser($mention)
);
return '@'.($new ?? $mention);
},
);
}
/** Replace the author with a specially-formatted part of the commit
* message.
*
* Useful for dealing with pull requests if there are restrictions on who
* is a valid author for your internal repository.
*
* @param $pattern regexp pattern defining an 'author' capture group
*/
public static function rewriteAuthorFromMessagePattern(
ShipItChangeset $changeset,
string $pattern,
): ShipItChangeset {
$matches = [];
if (preg_match($pattern, $changeset->getMessage(), &$matches)) {
return $changeset->withAuthor($matches['author']);
}
return $changeset;
}
/** Convenience wrapper for the above for 'GitHub Author: ' lines */
public static function rewriteAuthorFromGitHubAuthorLine(
ShipItChangeset $changeset,
): ShipItChangeset {
return self::rewriteAuthorFromMessagePattern(
$changeset,
'/(^|\n)GitHub Author: (?<author>.*?)(\n|$)/si',
);
}
}