Skip to content
This repository was archived by the owner on Sep 23, 2023. It is now read-only.

Commit 4571d79

Browse files
committed
WIP Allow patches to be updated
Fixes #54
1 parent d724828 commit 4571d79

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

index.php

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

331331
$actions = [];
332332
if ( $canDelete ) {
333+
$actions[] = '<a href="update.php?wiki=' . $wiki . '">Update</a>';
333334
$actions[] = '<a href="delete.php?wiki=' . $wiki . '">Delete</a>';
334335
}
335336
if ( $canCreate ) {

new/applypatch.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ set -ex
33

44
cd $PATCHDEMO/wikis/$NAME/$REPO
55

6+
# Required when updating an existing wiki
7+
git reset --hard origin/master
8+
69
git fetch origin $REF
710

811
# Apply $HASH and its parent commits up to $BASE on top of current HEAD.

update.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
require_once "includes.php";
4+
5+
include "header.php";
6+
7+
ob_implicit_flush( true );
8+
9+
if ( $useOAuth && !$user ) {
10+
echo oauth_signin_prompt();
11+
die();
12+
}
13+
14+
$wiki = $_GET['wiki'];
15+
$wikiData = get_wiki_data( $wiki );
16+
17+
if ( !can_delete( $wikiData['creator'] ) ) {
18+
die( '<p>You are not allowed to update this wiki.</p>' );
19+
}
20+
21+
function abandon( string $errHtml ) {
22+
die( $errHtml );
23+
}
24+
25+
echo '<div class="consoleLog">';
26+
27+
$patchesApplied = [];
28+
$linkedTasks = [];
29+
$commands = [];
30+
31+
foreach ( $wikiData['patchList'] as $patch => $patchData ) {
32+
$r = $patchData['r'];
33+
$data = gerrit_query( "changes/?q=change:$r&o=LABELS&o=CURRENT_REVISION", true );
34+
35+
// get the info
36+
$repo = $data[0]['project'];
37+
$base = 'origin/' . $data[0]['branch'];
38+
$revision = $data[0]['current_revision'];
39+
$ref = $data[0]['revisions'][$revision]['ref'];
40+
$id = $data[0]['id'];
41+
42+
$repos = get_repo_data();
43+
if ( !isset( $repos[ $repo ] ) ) {
44+
$repo = htmlentities( $repo );
45+
abandon( "Repository <em>$repo</em> not supported" );
46+
}
47+
$path = $repos[ $repo ];
48+
49+
if (
50+
$config[ 'requireVerified' ] &&
51+
( $data[0]['labels']['Verified']['approved']['_account_id'] ?? null ) !== 75
52+
) {
53+
// The patch doesn't have V+2, check if the uploader is trusted
54+
$uploaderId = $data[0]['revisions'][$revision]['uploader']['_account_id'];
55+
$uploader = gerrit_query( 'accounts/' . $uploaderId, true );
56+
if ( !is_trusted_user( $uploader['email'] ) ) {
57+
abandon( "Patch must be approved (Verified+2) by jenkins-bot, or uploaded by a trusted user" );
58+
}
59+
}
60+
61+
$patchesApplied[] = $data[0]['_number'] . ',' . $data[0]['revisions'][$revision]['_number'];
62+
63+
$r = $patchData['r'];
64+
$pOld = (int)$patchData['p'];
65+
$pNew = $data[0]['revisions'][$revision]['_number'];
66+
if ( $pNew > $pOld ) {
67+
echo "<strong>Updating change $r from patchset $pOld to $pNew.</strong>";
68+
} else {
69+
echo "<strong>Change $r is already using the latest patchset ($pOld).</strong>";
70+
continue;
71+
}
72+
73+
$commands[] = [
74+
[
75+
'REPO' => $path,
76+
'REF' => $ref,
77+
'BASE' => $base,
78+
'HASH' => $revision,
79+
],
80+
__DIR__ . '/new/applypatch.sh'
81+
];
82+
83+
$relatedChanges = [];
84+
$relatedChanges[] = [ $data[0]['_number'], $data[0]['revisions'][$revision]['_number'] ];
85+
86+
// Look at all commits in this patch's tree for cross-repo dependencies to add
87+
$data = gerrit_query( "changes/$id/revisions/$revision/related", true );
88+
// Ancestor commits only, not descendants
89+
$foundCurr = false;
90+
foreach ( $data['changes'] as $change ) {
91+
if ( $foundCurr ) {
92+
// Querying by change number is allegedly deprecated, but the /related API doesn't return the 'id'
93+
$relatedChanges[] = [ $change['_change_number'], $change['_revision_number'] ];
94+
}
95+
$foundCurr = $foundCurr || $change['commit']['commit'] === $revision;
96+
}
97+
98+
foreach ( $relatedChanges as [ $c, $r ] ) {
99+
$data = gerrit_query( "changes/$c/revisions/$r/commit", true );
100+
101+
preg_match_all( '/^Depends-On: (.+)$/m', $data['message'], $m );
102+
foreach ( $m[1] as $changeid ) {
103+
if ( !in_array( $changeid, $patches, true ) ) {
104+
// The entry we add here will be processed by the topmost foreach
105+
$patches[] = $changeid;
106+
}
107+
}
108+
}
109+
}
110+
111+
$baseEnv = [
112+
'PATCHDEMO' => __DIR__,
113+
'NAME' => $wiki,
114+
];
115+
116+
if ( !count( $commands ) ) {
117+
abandon( 'No patches to update.' );
118+
}
119+
120+
$error = shell_echo( __DIR__ . '/new/unlink.sh', $baseEnv );
121+
if ( $error ) {
122+
abandon( "Could not un-duplicate wiki." );
123+
}
124+
125+
foreach ( $commands as $i => $command ) {
126+
$error = shell_echo( $command[1], $baseEnv + $command[0] );
127+
if ( $error ) {
128+
abandon( "Could not apply patch {$patchesApplied[$i]}" );
129+
}
130+
}
131+
132+
// Update DB record with patches applied
133+
wiki_add_patches( $wiki, $patchesApplied );
134+
135+
echo "Done!";
136+
137+
echo '</div>';

0 commit comments

Comments
 (0)