-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set temporary branch for external MRs
If it's to be reliable, CI should be run on the target project, even when the MR comes from a fork. This commit introduces the temp_branch argument for specifying the temporary branch name that will be used for running the CI in the target project.
- Loading branch information
Showing
6 changed files
with
120 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
from collections import namedtuple | ||
from datetime import datetime, timedelta | ||
|
||
from . import git | ||
from . import git, gitlab | ||
from .commit import Commit | ||
from .interval import IntervalUnion | ||
from .project import Project | ||
from .user import User | ||
|
@@ -114,19 +115,23 @@ def add_trailers(self, merge_request): | |
return sha | ||
|
||
def get_mr_ci_status(self, merge_request, commit_sha=None): | ||
temp_branch = self.opts.temp_branch | ||
if commit_sha is None: | ||
commit_sha = merge_request.sha | ||
pipelines = Pipeline.pipelines_by_branch( | ||
merge_request.source_project_id, | ||
merge_request.source_branch, | ||
self._api, | ||
) | ||
if temp_branch and merge_request.source_project_id != self._project.id: | ||
pid = self._project.id | ||
ref = temp_branch | ||
self.update_temp_branch(merge_request, commit_sha) | ||
else: | ||
pid = merge_request.source_project_id | ||
ref = merge_request.source_branch | ||
pipelines = Pipeline.pipelines_by_branch(pid, ref, self._api) | ||
current_pipeline = next(iter(pipelines), None) | ||
|
||
if current_pipeline and current_pipeline.sha == commit_sha: | ||
ci_status = current_pipeline.status | ||
else: | ||
log.warning('No pipeline listed for %s on branch %s', commit_sha, merge_request.source_branch) | ||
log.warning('No pipeline listed for %s on branch %s', commit_sha, ref) | ||
ci_status = None | ||
|
||
return ci_status | ||
|
@@ -196,7 +201,7 @@ def fetch_source_project(self, merge_request): | |
remote = 'source' | ||
remote_url = source_project.ssh_url_to_repo | ||
self._repo.fetch( | ||
remote=remote, | ||
remote_name=remote, | ||
remote_url=remote_url, | ||
) | ||
return source_project, remote_url, remote | ||
|
@@ -281,6 +286,43 @@ def update_from_target_branch_and_push( | |
else: | ||
assert source_repo_url is not None | ||
|
||
def update_temp_branch(self, merge_request, commit_sha): | ||
api = self._api | ||
project_id = self._project.id | ||
temp_branch = self.opts.temp_branch | ||
waiting_time_in_secs = 30 | ||
|
||
try: | ||
sha_branch = Commit.last_on_branch(project_id, temp_branch, api).id | ||
except gitlab.NotFound: | ||
sha_branch = None | ||
if sha_branch != commit_sha: | ||
log.info('Setting up %s in target project', temp_branch) | ||
self.delete_temp_branch(merge_request.source_project_id) | ||
self._project.create_branch(temp_branch, commit_sha, api) | ||
self._project.protect_branch(temp_branch, api) | ||
merge_request.comment( | ||
('The temporary branch **{branch}** was updated to [{sha:.8}](../commit/{sha}) ' + | ||
'and local pipelines will be used.').format( | ||
branch=temp_branch, sha=commit_sha | ||
) | ||
) | ||
|
||
time.sleep(waiting_time_in_secs) | ||
|
||
def delete_temp_branch(self, source_project_id): | ||
temp_branch = self.opts.temp_branch | ||
|
||
if temp_branch and source_project_id != self._project.id: | ||
try: | ||
self._project.unprotect_branch(temp_branch, self._api) | ||
except gitlab.ApiError: | ||
pass | ||
try: | ||
self._project.delete_branch(temp_branch, self._api) | ||
except gitlab.ApiError: | ||
pass | ||
|
||
|
||
def _get_reviewer_names_and_emails(approvals, api): | ||
"""Return a list ['A. Prover <[email protected]', ...]` for `merge_request.`""" | ||
|
@@ -298,6 +340,7 @@ def _get_reviewer_names_and_emails(approvals, api): | |
'embargo', | ||
'ci_timeout', | ||
'use_merge_strategy', | ||
'temp_branch', | ||
] | ||
|
||
|
||
|
@@ -312,7 +355,8 @@ def requests_commit_tagging(self): | |
def default( | ||
cls, *, | ||
add_tested=False, add_part_of=False, add_reviewers=False, reapprove=False, | ||
approval_timeout=None, embargo=None, ci_timeout=None, use_merge_strategy=False | ||
approval_timeout=None, embargo=None, ci_timeout=None, use_merge_strategy=False, | ||
temp_branch="" | ||
): | ||
approval_timeout = approval_timeout or timedelta(seconds=0) | ||
embargo = embargo or IntervalUnion.empty() | ||
|
@@ -326,6 +370,7 @@ def default( | |
embargo=embargo, | ||
ci_timeout=ci_timeout, | ||
use_merge_strategy=use_merge_strategy, | ||
temp_branch=temp_branch, | ||
) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters