This repository was archived by the owner on Sep 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathupdate.php
More file actions
205 lines (165 loc) · 5.61 KB
/
update.php
File metadata and controls
205 lines (165 loc) · 5.61 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
use Symfony\Component\Yaml\Yaml;
require_once "includes.php";
include "header.php";
ob_implicit_flush( true );
if ( $useOAuth && !$user ) {
echo oauth_signin_prompt();
die();
}
$wiki = $_GET['wiki'];
$wikiData = get_wiki_data( $wiki );
if ( !can_delete( $wikiData['creator'] ) ) {
die( '<p>You are not allowed to update this wiki.</p>' );
}
function abandon( string $errHtml ) {
die( $errHtml );
}
echo '<p>Updating wiki <a class="wiki" href="wikis/' . $wiki . '/w" title="' . $wiki . '">' . $wiki . '</a>.</p>';
echo '<div class="consoleLog">';
ob_flush();
$baseEnv = [
'PATCHDEMO' => __DIR__,
'NAME' => $wiki,
];
$patchesApplied = [];
$patchesToUpdate = [];
$linkedTasks = [];
$commands = [];
$usedRepos = [];
foreach ( $wikiData['patchList'] as $patch => $patchData ) {
$r = $patchData['r'];
$data = gerrit_query( "changes/?q=change:$r&o=LABELS&o=CURRENT_REVISION", true );
// get the info
$repo = $data[0]['project'];
$base = 'origin/' . $data[0]['branch'];
$revision = $data[0]['current_revision'];
$ref = $data[0]['revisions'][$revision]['ref'];
$id = $data[0]['id'];
$repos = get_repo_data( 'w-updating/' );
if ( !isset( $repos[ $repo ] ) ) {
$repo = htmlentities( $repo );
abandon( "Repository <em>$repo</em> not supported" );
}
$path = $repos[ $repo ];
$usedRepos[] = $repo;
if (
$config[ 'requireVerified' ] &&
( $data[0]['labels']['Verified']['approved']['_account_id'] ?? null ) !== 75
) {
// The patch doesn't have V+2, check if the uploader is trusted
$uploaderId = $data[0]['revisions'][$revision]['uploader']['_account_id'];
$uploader = gerrit_query( 'accounts/' . $uploaderId, true );
if ( !is_trusted_user( $uploader['email'] ) ) {
abandon( "Patch must be approved (Verified+2) by jenkins-bot, or uploaded by a trusted user" );
}
}
$patch = $data[0]['_number'] . ',' . $data[0]['revisions'][$revision]['_number'];
$patchesApplied[] = $patch;
$r = $patchData['r'];
$pOld = (int)$patchData['p'];
$pNew = $data[0]['revisions'][$revision]['_number'];
if ( $pNew > $pOld ) {
echo "<strong>Updating change $r from patchset $pOld to $pNew.</strong>";
} else {
echo "<strong>Change $r is already using the latest patchset ($pOld).</strong>";
continue;
}
$patchesToUpdate[] = $patch;
$commands[] = [
[
'REPO' => $path,
'REF' => $ref,
'BASE' => $base,
'HASH' => $revision,
],
__DIR__ . '/new/applypatch.sh'
];
$relatedChanges = [];
$relatedChanges[] = [ $data[0]['_number'], $data[0]['revisions'][$revision]['_number'] ];
// Look at all commits in this patch's tree for cross-repo dependencies to add
$data = gerrit_query( "changes/$id/revisions/$revision/related", true );
// Ancestor commits only, not descendants
$foundCurr = false;
foreach ( $data['changes'] as $change ) {
if ( $foundCurr ) {
// Querying by change number is allegedly deprecated, but the /related API doesn't return the 'id'
$relatedChanges[] = [ $change['_change_number'], $change['_revision_number'] ];
}
$foundCurr = $foundCurr || $change['commit']['commit'] === $revision;
}
foreach ( $relatedChanges as [ $c, $r ] ) {
$data = gerrit_query( "changes/$c/revisions/$r/commit", true );
preg_match_all( '/^Depends-On: (.+)$/m', $data['message'], $m );
foreach ( $m[1] as $changeid ) {
if ( !in_array( $changeid, $patches, true ) ) {
// The entry we add here will be processed by the topmost foreach
$patches[] = $changeid;
}
}
}
ob_flush();
}
$usedRepos = array_unique( $usedRepos );
if ( !count( $commands ) ) {
abandon( 'No patches to update.' );
}
$error = shell_echo( __DIR__ . '/new/unlinkbefore.sh', $baseEnv );
if ( $error ) {
abandon( "Could not copy wiki files to update." );
}
// The working directory is now w-updating/, which is ignored by the rdfind cron job (deduplicate.sh)
// This means $wgScriptPath is incorrect, so don't try to run any MW scripts.
foreach ( $commands as $i => $command ) {
$error = shell_echo( $command[1], $baseEnv + $command[0] );
if ( $error ) {
abandon( "Could not apply patch {$patchesToUpdate[$i]}" );
}
ob_flush();
}
$composerInstallRepos = Yaml::parse( file_get_contents( __DIR__ . '/repository-lists/composerinstall.yaml' ) );
foreach ( $usedRepos as $repo ) {
if ( in_array( $repo, $composerInstallRepos, true ) ) {
$error = shell_echo( __DIR__ . '/new/composerinstall.sh',
$baseEnv + [
// Variable used by composer itself, not our script
'COMPOSER_HOME' => __DIR__ . '/composer',
'REPO_TARGET' => $repos[$repo],
]
);
if ( $error ) {
abandon( "Could not fetch dependencies for <em>$repo</em>" );
}
ob_flush();
}
}
$error = shell_echo( __DIR__ . '/new/unlinkafter.sh', $baseEnv );
if ( $error ) {
abandon( "Could not overwrite wiki files after updating." );
}
// The working directory is back to w/. Scripts beyond this point should not modify the filesystem.
$mainPage = "\n\nThis wiki was updated on ~~~~~ with the following newer patches:";
foreach ( $patchesToUpdate as $patch ) {
preg_match( '`([0-9]+),([0-9]+)`', $patch, $matches );
list( $t, $r, $p ) = $matches;
$data = gerrit_query( "changes/$r/revisions/$p/commit", true );
if ( $data ) {
$t = $t . ': ' . $data[ 'subject' ];
get_linked_tasks( $data['message'], $linkedTasks );
}
$t = htmlentities( $t );
$mainPage .= "\n:* [{$config['gerritUrl']}/r/c/$r/$p <nowiki>$t</nowiki>]";
}
$error = shell_echo( __DIR__ . '/new/postupdate.sh',
$baseEnv + [
'MAINPAGE' => $mainPage,
]
);
if ( $error ) {
abandon( "Could not update wiki content" );
}
// Update DB record with _all_ patches applied (include those which weren't updated)
wiki_add_patches( $wiki, $patchesApplied );
wiki_update_timestamp( $wiki );
echo "Done!";
echo '</div>';