|
| 1 | +# |
| 2 | +# Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +"""Plugin for CI projects.""" |
| 6 | +import logging |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | +from subprocess import check_call |
| 10 | +from typing import List |
| 11 | + |
| 12 | +from continuous_delivery_scripts.plugins.ci import CI |
| 13 | +from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable |
| 14 | +from continuous_delivery_scripts.utils.definitions import CommitType |
| 15 | +from continuous_delivery_scripts.utils.language_specifics_base import get_language_from_file_name |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | +ENVVAR_GITHUB_CLI_GIT_TOKEN = "GITHUB_TOKEN" |
| 19 | + |
| 20 | + |
| 21 | +def _generate_github_cli_release_command_list( |
| 22 | + changelog: Path, version: str, tag: str, is_latest: bool, is_prerelease: bool |
| 23 | +) -> List[str]: |
| 24 | + cmd = [ |
| 25 | + "ght", |
| 26 | + "release", |
| 27 | + "--latest", |
| 28 | + "--notes-file", |
| 29 | + f"{str(changelog)}", |
| 30 | + ] |
| 31 | + title = f":sparkles: Release {version}" |
| 32 | + if is_latest: |
| 33 | + cmd.append("--latest") |
| 34 | + if is_prerelease: |
| 35 | + cmd.append("--prerelease") |
| 36 | + title = f":news: Pre-release {version}" |
| 37 | + cmd.append("--title") |
| 38 | + cmd.append(title) |
| 39 | + cmd.append(tag) |
| 40 | + return cmd |
| 41 | + |
| 42 | + |
| 43 | +def _generate_github_cli_check_command_list() -> List[str]: |
| 44 | + return [ |
| 45 | + "gh", |
| 46 | + "--version", |
| 47 | + ] |
| 48 | + |
| 49 | + |
| 50 | +def _call_github_cli_check() -> None: |
| 51 | + """Calls gh to verify its accessibility.""" |
| 52 | + logger.info("Checking GitHub Actions is correctly installed.") |
| 53 | + env = os.environ |
| 54 | + env[ENVVAR_GITHUB_CLI_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN) |
| 55 | + check_call(_generate_github_cli_check_command_list(), env=env) |
| 56 | + |
| 57 | + |
| 58 | +class GitHubActions(CI): |
| 59 | + """Specific actions for a GitHub Action project.""" |
| 60 | + |
| 61 | + def package_software(self, mode: CommitType, version: str) -> None: |
| 62 | + """No operation.""" |
| 63 | + super().package_software(mode, version) |
| 64 | + _call_github_cli_check() |
| 65 | + |
| 66 | + def get_related_language(self) -> str: |
| 67 | + """Gets the related language.""" |
| 68 | + return get_language_from_file_name(__file__) |
| 69 | + |
| 70 | + def release_package_to_repository(self, mode: CommitType, version: str) -> None: |
| 71 | + """No operation.""" |
| 72 | + super().release_package_to_repository(mode, version) |
| 73 | + is_latest = mode == CommitType.RELEASE |
| 74 | + is_prerelease = mode == CommitType.BETA |
| 75 | + self._call_github_cli_release(version, is_latest, is_prerelease) |
| 76 | + |
| 77 | + def _call_github_cli_release(self, version: str, is_latest: bool, is_prerelease: bool) -> None: |
| 78 | + """Calls github cli to create a release.""" |
| 79 | + tag = self.get_version_tag(version) |
| 80 | + logger.info(f"Create a GitHub Release {version}") |
| 81 | + changelogPath = configuration.get_value(ConfigurationVariable.CHANGELOG_FILE_PATH) |
| 82 | + env = os.environ |
| 83 | + env[ENVVAR_GITHUB_CLI_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN) |
| 84 | + |
| 85 | + check_call( |
| 86 | + _generate_github_cli_release_command_list(changelogPath, version, tag, is_latest, is_prerelease), env=env |
| 87 | + ) |
0 commit comments