-
Notifications
You must be signed in to change notification settings - Fork 105
Add installchecker bot for Gitea workflow #3303
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46fa1da
staging-installcheck: move the InstallChecker class to its own loadab…
g7 f11abc9
git-installcheck: add installchecker bot for Gitea workflows
g7 33fbb45
systemd: add service and timer for git-installcheck
g7 a5170ab
config: add default environment configuration for osrt-git-installcheck
g7 45069db
Makefile: ship staginginstallchecker module and create working direct…
g7 36040d8
dist: adjust packaging for osrt-git-installcheck
g7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copy this file to /etc/default/osrt-git-installcheck.env.<instance_name> | ||
| # where <instance_name> is the name of the unit instance (which should also be | ||
| # the name of the build service/gitea user) | ||
|
|
||
| # GITEA_URL: the base URL of the Gitea instance | ||
| GITEA_URL="https://src.suse.de" | ||
|
|
||
| # GITEA_ACCESS_TOKEN: the access token for the user | ||
| GITEA_ACCESS_TOKEN="<INSERT_TOKEN_HERE>" | ||
|
|
||
| # GIT_ALLOW_REPOS: allowed repositories to process. | ||
| # For the installcheck bot, these need to be specified otherwise | ||
| # the bot would refuse processing even when the bot itself | ||
| # is added as a reviewer. | ||
| # Use a comma to specify multiple repositories: | ||
| # GIT_ALLOW_REPOS="products/SLFO,products/SLES" | ||
| GIT_ALLOW_REPOS="products/SLFO" | ||
|
|
||
| # API_URL: the Open Build Service apiurl | ||
| API_URL="https://api.suse.de" | ||
|
|
||
| # OSC_CONFIG: the path to the oscrc configuration file | ||
| # to use to interact with the Open Build Service | ||
| OSC_CONFIG="/etc/openSUSE-release-tools/oscrc.example" | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| #!/usr/bin/python3 | ||
| import os | ||
| import sys | ||
| import ReviewBot | ||
|
|
||
| import logging | ||
|
|
||
| import traceback | ||
|
|
||
| import urllib.error | ||
|
|
||
| import shutil | ||
|
|
||
| from osc import conf | ||
| from osclib.conf import Config | ||
| from osclib.stagingapi import StagingAPI | ||
| from staginginstallchecker.installchecker import InstallChecker, CheckResult | ||
|
|
||
| from osclib.cache_manager import CacheManager | ||
|
|
||
| DEFAULT_AUTOGITS_REVIEWER = "autogits_obs_staging_bot" | ||
| DEFAULT_ARCHITECTURES = "x86_64 s390x ppc64le aarch64" | ||
|
|
||
| CACHEDIR = CacheManager.directory("repository-meta") | ||
|
|
||
|
|
||
| class GitInstallCheckBot(ReviewBot.ReviewBot): | ||
| """A review bot that runs staging-installcheck on staging QA projects""" | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| ReviewBot.ReviewBot.__init__(self, *args, **kwargs) | ||
|
|
||
| conf.get_config() | ||
|
|
||
| self.apiurl = conf.config["apiurl"] | ||
|
|
||
| self.allowed_repositories = [] | ||
|
|
||
| # This is heavily dependent on the GITEA platform | ||
| if self.platform.name != "GITEA": | ||
| raise Exception("Unsupported platform: this bot is only supported on Gitea") | ||
|
|
||
| def get_git_staging_configuration(self, owner, project, commit_sha): | ||
| # FIXME: support JWCC | ||
| return self.platform.get_path( | ||
| f"repos/{owner}/{project}/raw/staging.config?ref={commit_sha}" | ||
| ).json() | ||
|
|
||
| @staticmethod | ||
| def is_request_approved_by(request, approver): | ||
| for review in request.reviews: | ||
| if review.by == approver and review.state == "accepted": | ||
| # We skip dismissed reviews, so we can afford returning | ||
| # as soon as we find a matching review | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| @staticmethod | ||
| def get_request_from_src_rev(requests, src_rev): | ||
| for request in requests: | ||
| if request.actions[0].src_rev == src_rev: | ||
| return request | ||
|
|
||
| return None | ||
|
|
||
| def check_source_submission( | ||
| self, src_owner, src_project, src_rev, target_owner, target_package | ||
| ): | ||
| self.logger.info(f"Checking {src_project}: {src_owner} -> {target_owner}") | ||
|
|
||
| try: | ||
| result = self.run_installcheck( | ||
| src_owner, src_project, src_rev, target_owner, target_package | ||
| ) | ||
| except Exception: | ||
| self.review_messages["declined"] = ( | ||
| f"Unhandled exception:\n\n```{traceback.format_exc()}```" | ||
| ) | ||
| return False | ||
|
|
||
| if result is None: | ||
| return None | ||
| elif result.success: | ||
| self.review_messages["accepted"] = "installcheck ran successfully" | ||
| else: | ||
| self.review_messages["declined"] = "\n".join(result.comment) | ||
|
|
||
| return result.success | ||
|
|
||
| def run_installcheck( | ||
| self, src_owner, src_project, src_rev, target_owner, target_package | ||
| ): | ||
| """ | ||
| Runs repo_checker. | ||
|
|
||
| :return: either a CheckResult, or None (should skip/retry later) | ||
| """ | ||
|
|
||
| request = self.get_request_from_src_rev(self.requests, src_rev) | ||
| if not request: | ||
| self.logger.warning(f"Request for src_rev {src_rev} not found") | ||
| return None | ||
|
|
||
| if f"{request._owner}/{request._repo}" not in self.allowed_repositories: | ||
| self.logger.info( | ||
| f"{request._owner}/{request._repo} is not in the allowed repositories list" | ||
| ) | ||
| return None | ||
|
|
||
| base_commit = request.actions[0].tgt_rev | ||
| staging_configuration = self.get_git_staging_configuration( | ||
| target_owner, target_package, base_commit | ||
| ) | ||
|
|
||
| main_project = staging_configuration["ObsProject"] | ||
|
|
||
| codestream_project = ( | ||
| f"{staging_configuration['StagingProject']}:{request._pr_id}" | ||
| ) | ||
|
|
||
| Config(self.apiurl, main_project) | ||
| target_config = conf.config[main_project] | ||
|
|
||
| main_repo = target_config["main-repo"] | ||
|
|
||
| enabled_architectures = target_config.get( | ||
| "repo_checker-arch-whitelist", DEFAULT_ARCHITECTURES | ||
| ).split(" ") | ||
|
|
||
| approver = target_config.get("repo_checker-approver", DEFAULT_AUTOGITS_REVIEWER) | ||
| if not self.is_request_approved_by(request, approver): | ||
| return None | ||
|
|
||
| api = StagingAPI(self.apiurl, codestream_project) | ||
| tool = InstallChecker(api, target_config) | ||
|
|
||
| try: | ||
| api.get_prj_meta(codestream_project) | ||
| except urllib.error.HTTPError as e: | ||
| if e.code == 404: | ||
| return CheckResult( | ||
| success=True, comment="Staging bot didn't create a project" | ||
| ) | ||
| else: | ||
| raise | ||
|
|
||
| try: | ||
| return tool.staging_installcheck( | ||
| codestream_project, main_repo, enabled_architectures, devel=True | ||
| ) | ||
| finally: | ||
| # Clean-up dynamic PR data - in the git workflow we | ||
| # have dynamic build projects, so the repo_mirrorer's stale | ||
| # object clean-up won't trigger | ||
| project_cache = os.path.join(CACHEDIR, codestream_project) | ||
|
|
||
| if os.path.exists(project_cache) and not os.path.exists( | ||
| os.path.join(project_cache, ".lock") | ||
| ): | ||
| # Lock being present should actually never happen - we | ||
| # are the only users, and we run the check sequentially, | ||
| # however, let's check for a lock file anyway. Better safe | ||
| # than sorry. | ||
| self.logger.debug(f"Cleaning up {project_cache}") | ||
| shutil.rmtree(project_cache) | ||
| else: | ||
| # If the lock file is present, log an error, but don't | ||
| # try to remove it | ||
| self.logger.error( | ||
| f"{project_cache} has a lock file, and cannot be removed. This shouldn't happen. Skipping cleanup" | ||
| ) | ||
|
|
||
|
|
||
| class CommandLineInterface(ReviewBot.CommandLineInterface): | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__(*args, *kwargs) | ||
| self.clazz = GitInstallCheckBot | ||
|
|
||
| def get_optparser(self): | ||
| parser = super().get_optparser() | ||
|
|
||
| # Add bot-specific options | ||
| # If ReviewBot/Cmdln moves to ArgumentParser, we can turn this into a | ||
| # string directly and use nargs=*. | ||
| parser.add_option( | ||
| "--git-allow-repos", | ||
| default="", | ||
| help="allowed git repositories (e.g. products/SLFO,products/SLES)", | ||
| ) | ||
|
|
||
| return parser | ||
|
|
||
| def setup_checker(self): | ||
| instance = super().setup_checker() | ||
|
|
||
| instance.allowed_repositories = self.options.git_allow_repos.split(",") | ||
|
|
||
| return instance | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app = CommandLineInterface() | ||
| logging.basicConfig(level=logging.DEBUG) | ||
|
|
||
| sys.exit(app.main()) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.