Skip to content

Commit 7ece838

Browse files
authored
Language configurable release tagging (#30)
1 parent c7520b7 commit 7ece838

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

continuous_delivery_scripts/plugins/golang.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
from continuous_delivery_scripts.utils.language_specifics_base import BaseLanguage, get_language_from_file_name
1212
from continuous_delivery_scripts.spdx_report.spdx_project import SpdxProject
1313
from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable
14-
from continuous_delivery_scripts.utils.git_helpers import LocalProjectRepository
14+
from continuous_delivery_scripts.utils.git_helpers import LocalProjectRepository, GitWrapper
1515

1616
logger = logging.getLogger(__name__)
1717

18-
SRC_DIR = configuration.get_value(ConfigurationVariable.SOURCE_DIR)
19-
ROOT_DIR = configuration.get_value(ConfigurationVariable.PROJECT_ROOT)
18+
SRC_DIR = Path(str(configuration.get_value(ConfigurationVariable.SOURCE_DIR)))
19+
ROOT_DIR = Path(str(configuration.get_value(ConfigurationVariable.PROJECT_ROOT)))
2020
ENVVAR_GORELEASER_GIT_TOKEN = "GITHUB_TOKEN"
2121
ENVVAR_GORELEASER_CUSTOMISED_TAG = "GORELEASER_CURRENT_TAG"
22+
ENVVAR_GO_MOD = "GO111MODULE"
23+
GO_MOD_ON_VALUE = "on"
2224

2325

2426
def _generate_golds_command_list(output_directory: Path, module: str) -> List[str]:
@@ -53,20 +55,43 @@ def _install_goreleaser_command_list() -> List[str]:
5355
def _call_golds(output_directory: Path, module: str) -> None:
5456
"""Calls Golds for generating the docs."""
5557
logger.info("Installing Golds if missing.")
56-
check_call(_install_golds_command_list())
58+
env = os.environ
59+
env[ENVVAR_GO_MOD] = GO_MOD_ON_VALUE
60+
check_call(_install_golds_command_list(), env=env)
5761
logger.info("Creating Golds documentation.")
58-
check_call(_generate_golds_command_list(output_directory, module), cwd=SRC_DIR)
62+
check_call(_generate_golds_command_list(output_directory, module), cwd=SRC_DIR, env=env)
5963

6064

6165
def _call_goreleaser_check(version: str) -> None:
6266
"""Calls go releaser check to verify configuration."""
6367
logger.info("Installing GoReleaser if missing.")
64-
check_call(_install_goreleaser_command_list())
65-
logger.info("Checking GoReleaser configuration.")
6668
env = os.environ
69+
env[ENVVAR_GO_MOD] = GO_MOD_ON_VALUE
70+
check_call(_install_goreleaser_command_list(), env=env)
71+
logger.info("Checking GoReleaser configuration.")
6772
env[ENVVAR_GORELEASER_CUSTOMISED_TAG] = version
6873
env[ENVVAR_GORELEASER_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN)
69-
check_call(_generate_goreleaser_check_command_list(), cwd=ROOT_DIR)
74+
check_call(_generate_goreleaser_check_command_list(), cwd=ROOT_DIR, env=env)
75+
76+
77+
def _determine_go_module_tag(version) -> Optional[str]:
78+
"""Determines go module for tagging.
79+
80+
See https://golang.org/ref/mod#vcs-version.
81+
and https://github.com/golang/go/wiki/Modules#should-i-have-multiple-modules-in-a-single-repository.
82+
"""
83+
module = ""
84+
try:
85+
module = str(SRC_DIR.relative_to(ROOT_DIR))
86+
except ValueError:
87+
try:
88+
module = str(ROOT_DIR.relative_to(SRC_DIR))
89+
except ValueError as exception:
90+
logger.warning(exception)
91+
if module == "." or len(module) == 0:
92+
return None
93+
module = module.rstrip("/")
94+
return f"{module}/{version}"
7095

7196

7297
class Go(BaseLanguage):
@@ -118,10 +143,19 @@ def should_clean_before_packaging(self) -> bool:
118143
"""States whether the repository must be cleaned before packaging happens."""
119144
return True
120145

146+
def tag_release(self, git: GitWrapper, version: str) -> None:
147+
"""Tags release commit."""
148+
super().tag_release(git, version)
149+
go_tag = _determine_go_module_tag(self.get_version_tag(version))
150+
if go_tag:
151+
git.create_tag(go_tag, message=f"Golang module release: {go_tag}")
152+
121153
def _call_goreleaser_release(self, version: str) -> None:
122154
"""Calls go releaser release to upload packages."""
123155
logger.info("Installing GoReleaser if missing.")
124-
check_call(_install_goreleaser_command_list())
156+
env = os.environ
157+
env[ENVVAR_GO_MOD] = GO_MOD_ON_VALUE
158+
check_call(_install_goreleaser_command_list(), env=env)
125159
tag = self.get_version_tag(version)
126160
# The tag of the release must be retrieved
127161
# See https://github.com/goreleaser/goreleaser/discussions/1426
@@ -132,7 +166,6 @@ def _call_goreleaser_release(self, version: str) -> None:
132166
git.checkout(f"tags/{tag}")
133167
logger.info("Release package.")
134168
changelogPath = configuration.get_value(ConfigurationVariable.CHANGELOG_FILE_PATH)
135-
env = os.environ
136169
env[ENVVAR_GORELEASER_CUSTOMISED_TAG] = tag
137170
env[ENVVAR_GORELEASER_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN)
138171
check_call(_generate_goreleaser_release_command_list(changelogPath), cwd=ROOT_DIR, env=env)

continuous_delivery_scripts/tag_and_release.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ def _update_repository(mode: CommitType, is_new_version: bool, version: str, cur
9292
if mode == CommitType.RELEASE:
9393
_commit_release_changes(git, version, commit_message)
9494
if is_new_version:
95-
logger.info("Tagging commit")
96-
git.create_tag(get_language_specifics().get_version_tag(version), message=f"release {version}")
95+
get_language_specifics().tag_release(git, version)
9796
git.force_push_tag()
9897

9998

continuous_delivery_scripts/utils/language_specifics_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from continuous_delivery_scripts.spdx_report.spdx_project import SpdxProject
1313
from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable
14+
from continuous_delivery_scripts.utils.git_helpers import GitWrapper
1415

1516
logger = logging.getLogger(__name__)
1617

@@ -73,6 +74,11 @@ def should_clean_before_packaging(self) -> bool:
7374
"""States whether the repository must be cleaned before packaging happens."""
7475
return False
7576

77+
def tag_release(self, git: GitWrapper, version: str) -> None:
78+
"""Tags release commit."""
79+
logger.info(f"Tagging commit as release {version}")
80+
git.create_tag(self.get_version_tag(version), message=f"release {version}")
81+
7682
@abstractmethod
7783
def generate_code_documentation(self, output_directory: Path, module_to_document: str) -> None:
7884
"""Generates the code documentation."""

news/202108251834.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Made release tagging language configurable.

0 commit comments

Comments
 (0)