Skip to content

Support for approve workflows #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.7",
"incenteev/composer-parameter-handler": "~2.0",
"knplabs/github-api": "^3.2",
"knplabs/github-api": "^3.3",
"nyholm/psr7": "^1.3",
"sensio/framework-extra-bundle": "^5.2",
"symfony/console": "^5.2",
Expand Down
21 changes: 12 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions config/packages/github_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ services:
Github\Api\Search:
factory: ['@Github\Client', api]
arguments: [search]

Github\Api\Repository\Actions\WorkflowRuns:
factory: ['@Github\Api\Repo', workflowRuns]
5 changes: 4 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ parameters:
- 'App\Subscriber\UnsupportedBranchSubscriber'
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
- 'App\Subscriber\ApproveCiForNonContributors'
secret: '%env(SYMFONY_SECRET)%'

symfony/symfony-docs:
Expand All @@ -31,6 +32,7 @@ parameters:
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
- 'App\Subscriber\UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber'
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
- 'App\Subscriber\ApproveCiForNonContributors'
secret: '%env(SYMFONY_DOCS_SECRET)%'

# used in a functional test
Expand All @@ -51,6 +53,7 @@ parameters:
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
- 'App\Subscriber\UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber'
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
- 'App\Subscriber\ApproveCiForNonContributors'

services:
_defaults:
Expand All @@ -75,12 +78,12 @@ services:
resource: '../src/Subscriber/*'
autoconfigure: false


App\Api\Issue\IssueApi: '@App\Api\Issue\GithubIssueApi'
App\Api\Label\LabelApi: '@App\Api\Label\GithubLabelApi'
App\Api\Milestone\MilestoneApi: '@App\Api\Milestone\GithubMilestoneApi'
App\Api\PullRequest\PullRequestApi: '@App\Api\PullRequest\GithubPullRequestApi'
App\Api\Status\StatusApi: '@App\Api\Status\GitHubStatusApi'
App\Api\Workflow\WorkflowApi: '@App\Api\Workflow\GithubWorkflowApi'

App\Service\RepositoryProvider:
arguments: [ '%repositories%' ]
Expand Down
1 change: 1 addition & 0 deletions config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ services:
App\Api\Milestone\MilestoneApi: '@App\Api\Milestone\StaticMilestoneApi'
App\Api\PullRequest\PullRequestApi: '@App\Api\PullRequest\NullPullRequestApi'
App\Api\Status\StatusApi: '@App\Api\Status\NullStatusApi'
App\Api\Workflow\WorkflowApi: '@App\Api\Workflow\NullWorkflowApi'
39 changes: 39 additions & 0 deletions src/Api/Workflow/GithubWorkflowApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Api\Workflow;

use App\Model\Repository;
use Github\Api\Repository\Actions\WorkflowRuns;
use Github\ResultPager;
use Psr\Log\LoggerInterface;

/**
* @author Tobias Nyholm <[email protected]>
*/
class GithubWorkflowApi implements WorkflowApi
{
private ResultPager $resultPager;
private WorkflowRuns $workflowApi;
private LoggerInterface $logger;

public function __construct(ResultPager $resultPager, WorkflowRuns $workflowApi, LoggerInterface $logger)
{
$this->resultPager = $resultPager;
$this->workflowApi = $workflowApi;
$this->logger = $logger;
}

public function approveWorkflowsForPullRequest(Repository $repository, string $headRepository, string $headBranch): void
{
$runs = $this->resultPager->fetchAllLazy($this->workflowApi, 'all', [$repository->getVendor(), $repository->getName(), [
'branch' => $headBranch,
'status' => 'action_required',
]]);

foreach ($runs as $run) {
if ($headRepository === $run['head_repository']['full_name']) {
$this->workflowApi->approve($repository->getVendor(), $repository->getName(), $run['id']);
}
}
}
}
12 changes: 12 additions & 0 deletions src/Api/Workflow/NullWorkflowApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Api\Workflow;

use App\Model\Repository;

class NullWorkflowApi implements WorkflowApi
{
public function approveWorkflowsForPullRequest(Repository $repository, string $headRepository, string $headBranch): void
{
}
}
16 changes: 16 additions & 0 deletions src/Api/Workflow/WorkflowApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Api\Workflow;

use App\Model\Repository;

/**
* @author Tobias Nyholm <[email protected]>
*/
interface WorkflowApi
{
/**
* Find workflow runs related to the PR and approve them.
*/
public function approveWorkflowsForPullRequest(Repository $repository, string $headRepository, string $headBranch): void;
}
45 changes: 45 additions & 0 deletions src/Subscriber/ApproveCiForNonContributors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Subscriber;

use App\Api\Workflow\WorkflowApi;
use App\Event\GitHubEvent;
use App\GitHubEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Make sure CI is running for all PRs.
*
* @author Tobias Nyholm <[email protected]>
*/
class ApproveCiForNonContributors implements EventSubscriberInterface
{
private WorkflowApi $workflowApi;

public function __construct(WorkflowApi $workflowApi)
{
$this->workflowApi = $workflowApi;
}

public function onPullRequest(GitHubEvent $event)
{
$data = $event->getData();
if (!in_array($data['action'], ['opened', 'reopened', 'synchronize'])) {
return;
}

$repository = $event->getRepository();
$headRepository = $data['pull_request']['head']['repo']['full_name'];
$headBranch = $data['pull_request']['head']['ref'];
$this->workflowApi->approveWorkflowsForPullRequest($repository, $headRepository, $headBranch);

$event->setResponseData(['approved_run' => true]);
}

public static function getSubscribedEvents()
{
return [
GitHubEvents::PULL_REQUEST => 'onPullRequest',
];
}
}
8 changes: 4 additions & 4 deletions tests/Controller/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ public function getTests()
'On pull request opened' => [
'pull_request',
'pull_request.opened.json',
['pull_request' => 3, 'status_change' => 'needs_review', 'pr_labels' => ['Console', 'Bug'], 'unsupported_branch' => '2.5'],
['pull_request' => 3, 'status_change' => 'needs_review', 'pr_labels' => ['Console', 'Bug'], 'unsupported_branch' => '2.5', 'approved_run' => true],
],
'On draft pull request opened' => [
'pull_request',
'pull_request.opened_draft.json',
['pull_request' => 3, 'draft_comment' => true],
['pull_request' => 3, 'draft_comment' => true, 'approved_run' => true],
],
'On pull request draft to ready' => [
'pull_request',
Expand All @@ -87,7 +87,7 @@ public function getTests()
'On pull request opened with target branch' => [
'pull_request',
'pull_request.opened_target_branch.json',
['pull_request' => 3, 'status_change' => 'needs_review', 'pr_labels' => ['Bug'], 'milestone' => '4.4'],
['pull_request' => 3, 'status_change' => 'needs_review', 'pr_labels' => ['Bug'], 'milestone' => '4.4', 'approved_run' => true],
],
'On issue labeled bug' => [
'issues',
Expand All @@ -102,7 +102,7 @@ public function getTests()
'Welcome first users' => [
'pull_request',
'pull_request.new_contributor.json',
['pull_request' => 4, 'status_change' => 'needs_review', 'pr_labels' => [], 'new_contributor' => true, 'squash_comment' => true],
['pull_request' => 4, 'status_change' => 'needs_review', 'pr_labels' => [], 'new_contributor' => true, 'squash_comment' => true, 'approved_run' => true],
],
'Waiting Code Merge' => [
'pull_request',
Expand Down
2 changes: 1 addition & 1 deletion tests/webhook_examples/pull_request.opened.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"repo": {
"id": 21151556,
"name": "symfony",
"full_name": "carsonbot-playground/symfony",
"full_name": "weaverryan/symfony",
"owner": {
"login": "weaverryan",
"id": 121003,
Expand Down