Skip to content
Snippets Groups Projects
Commit be38b208 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

fixes #56

parent 947a62d2
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ namespace TracToGitlab\EventHandlers; ...@@ -3,6 +3,7 @@ namespace TracToGitlab\EventHandlers;
use Gitlab; use Gitlab;
use TracToGitlab\Hooks; use TracToGitlab\Hooks;
use TracToGitlab\Models\GitlabReviewAppJob;
use TracToGitlab\Traits\BranchHasOpenMR; use TracToGitlab\Traits\BranchHasOpenMR;
use TracToGitlab\Traits\GetBuildImageJob; use TracToGitlab\Traits\GetBuildImageJob;
...@@ -33,10 +34,11 @@ final class AllAutomaticJobsHaveSucceeded extends Hooks\Pipeline ...@@ -33,10 +34,11 @@ final class AllAutomaticJobsHaveSucceeded extends Hooks\Pipeline
return; return;
} }
$this->gitlab_client->jobs()->play( $job_id = $this->getBuildImageJob($payload['builds'])['id'];
$payload['project']['id'],
$this->getBuildImageJob($payload['builds'])['id'] $this->gitlab_client->jobs()->play($payload['project']['id'], $job_id);
);
GitlabReviewAppJob::create([$branch, $job_id]);
} }
private function allAutomaticsHaveSucceeded($builds): bool private function allAutomaticsHaveSucceeded($builds): bool
......
<?php <?php
namespace TracToGitlab\EventHandlers; namespace TracToGitlab\EventHandlers;
use Config;
use Gitlab;
use TracToGitlab\BytistConnector; use TracToGitlab\BytistConnector;
use TracToGitlab\Hooks; use TracToGitlab\Hooks;
use TracToGitlab\Models\GitlabReviewApp; use TracToGitlab\Models\GitlabReviewApp;
use TracToGitlab\Models\GitlabReviewAppJob;
final class BranchDeleted extends Hooks\Push final class BranchDeleted extends Hooks\Push
{ {
private Gitlab\Client $gitlab_client;
private BytistConnector $connector; private BytistConnector $connector;
public function __construct(BytistConnector $connector) public function __construct(
{ Gitlab\Client $client,
BytistConnector $connector
) {
$this->gitlab_client = $client;
$this->connector = $connector; $this->connector = $connector;
} }
...@@ -19,6 +26,18 @@ final class BranchDeleted extends Hooks\Push ...@@ -19,6 +26,18 @@ final class BranchDeleted extends Hooks\Push
if ($this->isBranchDeleted($payload)) { if ($this->isBranchDeleted($payload)) {
$branch = substr($payload['ref'], strlen('refs/heads/')); $branch = substr($payload['ref'], strlen('refs/heads/'));
GitlabReviewAppJob::findEachBySQL(
function (GitlabReviewAppJob $job): void {
$this->gitlab_client->jobs()->cancel(
Config::get()->getValue('TRAC2GITLAB_GITLAB_PROJECT_ID'),
$job->job_id
);
$job->delete();
},
'branch = ?',
[$branch]
);
$this->connector->decommission($branch); $this->connector->decommission($branch);
GitlabReviewApp::deleteBySQL('branch = ?', [$branch]); GitlabReviewApp::deleteBySQL('branch = ?', [$branch]);
} }
......
...@@ -5,6 +5,7 @@ use Gitlab; ...@@ -5,6 +5,7 @@ use Gitlab;
use TracToGitlab\BytistConnector; use TracToGitlab\BytistConnector;
use TracToGitlab\Hooks; use TracToGitlab\Hooks;
use TracToGitlab\Models\GitlabReviewApp; use TracToGitlab\Models\GitlabReviewApp;
use TracToGitlab\Models\GitlabReviewAppJob;
use TracToGitlab\Traits\BranchHasOpenMR; use TracToGitlab\Traits\BranchHasOpenMR;
use TracToGitlab\Traits\GetBuildImageJob; use TracToGitlab\Traits\GetBuildImageJob;
...@@ -43,6 +44,11 @@ final class BuildImageJobSucceeded extends Hooks\Pipeline ...@@ -43,6 +44,11 @@ final class BuildImageJobSucceeded extends Hooks\Pipeline
$app = new GitlabReviewApp($branch); $app = new GitlabReviewApp($branch);
$app->store(); $app->store();
GitlabReviewAppJob::deleteBySQL(
'branch = ? AND job_id = ?',
[$branch, $this->getBuildImageJob($payload['builds'])['id']]
);
} }
private function buildImageJobHasSucceeded(array $builds): bool private function buildImageJobHasSucceeded(array $builds): bool
......
<?php <?php
namespace TracToGitlab\Models; namespace TracToGitlab\Models;
/**
* @property string $id
* @property string $branch
* @property int $mkdate
* @property int $chdate
*/
final class GitlabReviewApp extends \SimpleORMap final class GitlabReviewApp extends \SimpleORMap
{ {
protected static function configure($config = []) protected static function configure($config = [])
......
<?php
namespace TracToGitlab\Models;
/**
* @property array $id
* @property string $branch
* @property int $job_id
*/
final class GitlabReviewAppJob extends \SimpleORMap
{
protected static function configure($config = [])
{
$config['db_table'] = 'gitlab_review_app_jobs';
parent::configure($config);
}
}
<?php
return new class extends Migration
{
public function up()
{
$query = "CREATE TABLE IF NOT EXISTS `gitlab_review_app_jobs` (
`branch` VARCHAR(255) COLLATE `latin1_bin` NOT NULL,
`job_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`branch`, `job_id`)
)";
DBManager::get()->exec($query);
}
protected function down()
{
$query = "DROP TABLE IF EXISTS `gitlab_review_apps_jobs`";
DBManager::get()->exec($query);
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment