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

Commit

Permalink
Allow patches to be updated
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
edg2s committed Feb 28, 2022
1 parent d724828 commit d0a598a
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@

$actions = [];
if ( $canDelete ) {
$actions[] = '<a href="update.php?wiki=' . $wiki . '">Update</a>';
$actions[] = '<a href="delete.php?wiki=' . $wiki . '">Delete</a>';
}
if ( $canCreate ) {
Expand Down
3 changes: 3 additions & 0 deletions new/applypatch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ set -ex

cd $PATCHDEMO/wikis/$NAME/$REPO

# Required when updating an existing wiki
git reset --hard origin/master

git fetch origin $REF

# Apply $HASH and its parent commits up to $BASE on top of current HEAD.
Expand Down
183 changes: 183 additions & 0 deletions update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?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 '<div class="consoleLog">';

$patchesApplied = [];
$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();
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" );
}
}

$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;
}

$patchesApplied[] = $data[0]['_number'] . ',' . $data[0]['revisions'][$revision]['_number'];

$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;
}
}
}
}
$usedRepos = array_unique( $usedRepos );

$baseEnv = [
'PATCHDEMO' => __DIR__,
'NAME' => $wiki,
];

if ( !count( $commands ) ) {
abandon( 'No patches to update.' );
}

$error = shell_echo( __DIR__ . '/new/unlink.sh', $baseEnv );
if ( $error ) {
abandon( "Could not un-duplicate wiki." );
}

foreach ( $commands as $i => $command ) {
$error = shell_echo( $command[1], $baseEnv + $command[0] );
if ( $error ) {
abandon( "Could not apply patch {$patchesApplied[$i]}" );
}
}

$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>" );
}
}
}

$mainPage = "\n\nThis wiki was updated on ~~~~~ with the following newer patches:";
foreach ( $patchesApplied 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 patches applied
wiki_add_patches( $wiki, $patchesApplied );

echo "Done!";

echo '</div>';

0 comments on commit d0a598a

Please sign in to comment.