@@ -37,13 +37,80 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
3737#
3838"""Plugin for Golang projects."""
3939import logging
40+ import os
4041from pathlib import Path
41- from typing import Optional
42+ from typing import Optional, List
43+ from subprocess import check_call
4244from continuous_delivery_scripts.utils.language_specifics_base import BaseLanguage, get_language_from_file_name
4345from continuous_delivery_scripts.spdx_report.spdx_project import SpdxProject
46+ from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable
4447
4548logger = logging.getLogger(__name__)
4649
50+ SRC_DIR = configuration.get_value(ConfigurationVariable.SOURCE_DIR)
51+ ENVVAR_GORELEASER_GIT_TOKEN = "GITHUB_TOKEN"
52+ ENVVAR_GORELEASER_CUSTOMISED_TAG = "GORELEASER_CURRENT_TAG"
53+
54+
55+ def _generate_golds_command_list(output_directory: Path, module: str) -> List[str]:
56+ return ["golds", "-gen", "-wdpkgs-listing=promoted", f"-dir={str(output_directory)}", "-nouses", f"{module}"]
57+
58+
59+ def _generate_goreleaser_release_command_list(changelog: Path) -> List[str]:
60+ return [
61+ "goreleaser",
62+ "release",
63+ "--rm-dist",
64+ "--release-notes",
65+ f"{str(changelog)}",
66+ ]
67+
68+
69+ def _generate_goreleaser_check_command_list() -> List[str]:
70+ return [
71+ "goreleaser",
72+ "check",
73+ ]
74+
75+
76+ def _install_golds_command_list() -> List[str]:
77+ return ["go", "install", "go101.org/golds@latest"]
78+
79+
80+ def _install_goreleaser_command_list() -> List[str]:
81+ return ["go", "install", "github.com/goreleaser/goreleaser@latest"]
82+
83+
84+ def _call_golds(output_directory: Path, module: str) -> None:
85+ """Calls Golds for generating the docs."""
86+ logger.info("Installing Golds if missing.")
87+ check_call(_install_golds_command_list())
88+ logger.info("Creating Golds documentation.")
89+ check_call(_generate_golds_command_list(output_directory, module), cwd=SRC_DIR)
90+
91+
92+ def _call_goreleaser_check(version: str) -> None:
93+ """Calls go releaser check to verify configuration."""
94+ logger.info("Installing GoReleaser if missing.")
95+ check_call(_install_goreleaser_command_list())
96+ logger.info("Checking GoReleaser configuration.")
97+ env = os.environ
98+ env[ENVVAR_GORELEASER_CUSTOMISED_TAG] = version
99+ env[ENVVAR_GORELEASER_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN)
100+ check_call(_generate_goreleaser_check_command_list(), cwd=SRC_DIR)
101+
102+
103+ def _call_goreleaser_release(version: str) -> None:
104+ """Calls go releaser release to upload packages."""
105+ logger.info("Installing GoReleaser if missing.")
106+ check_call(_install_goreleaser_command_list())
107+ logger.info("Release package.")
108+ changelogPath = configuration.get_value(ConfigurationVariable.CHANGELOG_FILE_PATH)
109+ env = os.environ
110+ env[ENVVAR_GORELEASER_CUSTOMISED_TAG] = version
111+ env[ENVVAR_GORELEASER_GIT_TOKEN] = configuration.get_value(ConfigurationVariable.GIT_TOKEN)
112+ check_call(_generate_goreleaser_release_command_list(changelogPath), cwd=SRC_DIR, env=env)
113+
47114
48115class Go(BaseLanguage):
49116 """Specific actions for a Golang project."""
@@ -52,22 +119,30 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
52119 """Gets the related language."""
53120 return get_language_from_file_name(__file__)
54121
55- def package_software(self) -> None:
122+ def get_version_tag(self, version: str):
123+ """Gets tag based on version."""
124+ cleansed_version = version.strip().lstrip("v")
125+ return f"v{cleansed_version}"
126+
127+ def package_software(self, version: str) -> None:
56128 """No operation."""
57- super().package_software()
129+ super().package_software(version)
130+ _call_goreleaser_check(version)
58131
59- def release_package_to_repository(self) -> None:
132+ def release_package_to_repository(self, version: str ) -> None:
60133 """No operation."""
61- super().release_package_to_repository()
134+ super().release_package_to_repository(version)
135+ _call_goreleaser_release(version)
62136
63137 def check_credentials(self) -> None:
64138 """Checks any credentials."""
65139 super().check_credentials()
140+ configuration.get_value(ConfigurationVariable.GIT_TOKEN)
66141
67142 def generate_code_documentation(self, output_directory: Path, module_to_document: str) -> None:
68143 """Generates the code documentation."""
69144 super().generate_code_documentation(output_directory, module_to_document)
70- # TODO
145+ _call_golds(output_directory, "./...")
71146
72147 def can_add_licence_headers(self) -> bool:
73148 """States that licence headers can be added."""
@@ -108,22 +183,30 @@ <h2 class="section-title" id="header-classes">Classes</h2>
108183 """Gets the related language."""
109184 return get_language_from_file_name(__file__)
110185
111- def package_software(self) -> None:
186+ def get_version_tag(self, version: str):
187+ """Gets tag based on version."""
188+ cleansed_version = version.strip().lstrip("v")
189+ return f"v{cleansed_version}"
190+
191+ def package_software(self, version: str) -> None:
112192 """No operation."""
113- super().package_software()
193+ super().package_software(version)
194+ _call_goreleaser_check(version)
114195
115- def release_package_to_repository(self) -> None:
196+ def release_package_to_repository(self, version: str ) -> None:
116197 """No operation."""
117- super().release_package_to_repository()
198+ super().release_package_to_repository(version)
199+ _call_goreleaser_release(version)
118200
119201 def check_credentials(self) -> None:
120202 """Checks any credentials."""
121203 super().check_credentials()
204+ configuration.get_value(ConfigurationVariable.GIT_TOKEN)
122205
123206 def generate_code_documentation(self, output_directory: Path, module_to_document: str) -> None:
124207 """Generates the code documentation."""
125208 super().generate_code_documentation(output_directory, module_to_document)
126- # TODO
209+ _call_golds(output_directory, "./...")
127210
128211 def can_add_licence_headers(self) -> bool:
129212 """States that licence headers can be added."""
@@ -170,7 +253,8 @@ <h3>Methods</h3>
170253</ summary >
171254< pre > < code class ="python "> def check_credentials(self) -> None:
172255 """Checks any credentials."""
173- super().check_credentials()</ code > </ pre >
256+ super().check_credentials()
257+ configuration.get_value(ConfigurationVariable.GIT_TOKEN)</ code > </ pre >
174258</ details >
175259</ dd >
176260< dt id ="continuous_delivery_scripts.plugins.golang.Go.get_current_spdx_project "> < code class ="name flex ">
@@ -202,32 +286,49 @@ <h3>Methods</h3>
202286 return get_language_from_file_name(__file__)</ code > </ pre >
203287</ details >
204288</ dd >
289+ < dt id ="continuous_delivery_scripts.plugins.golang.Go.get_version_tag "> < code class ="name flex ">
290+ < span > def < span class ="ident "> get_version_tag</ span > </ span > (< span > self, version: str)</ span >
291+ </ code > </ dt >
292+ < dd >
293+ < div class ="desc "> < p > Gets tag based on version.</ p > </ div >
294+ < details class ="source ">
295+ < summary >
296+ < span > Expand source code</ span >
297+ </ summary >
298+ < pre > < code class ="python "> def get_version_tag(self, version: str):
299+ """Gets tag based on version."""
300+ cleansed_version = version.strip().lstrip("v")
301+ return f"v{cleansed_version}"</ code > </ pre >
302+ </ details >
303+ </ dd >
205304< dt id ="continuous_delivery_scripts.plugins.golang.Go.package_software "> < code class ="name flex ">
206- < span > def < span class ="ident "> package_software</ span > </ span > (< span > self) ‑> NoneType</ span >
305+ < span > def < span class ="ident "> package_software</ span > </ span > (< span > self, version: str ) ‑> NoneType</ span >
207306</ code > </ dt >
208307< dd >
209308< div class ="desc "> < p > No operation.</ p > </ div >
210309< details class ="source ">
211310< summary >
212311< span > Expand source code</ span >
213312</ summary >
214- < pre > < code class ="python "> def package_software(self) -> None:
313+ < pre > < code class ="python "> def package_software(self, version: str ) -> None:
215314 """No operation."""
216- super().package_software()</ code > </ pre >
315+ super().package_software(version)
316+ _call_goreleaser_check(version)</ code > </ pre >
217317</ details >
218318</ dd >
219319< dt id ="continuous_delivery_scripts.plugins.golang.Go.release_package_to_repository "> < code class ="name flex ">
220- < span > def < span class ="ident "> release_package_to_repository</ span > </ span > (< span > self) ‑> NoneType</ span >
320+ < span > def < span class ="ident "> release_package_to_repository</ span > </ span > (< span > self, version: str ) ‑> NoneType</ span >
221321</ code > </ dt >
222322< dd >
223323< div class ="desc "> < p > No operation.</ p > </ div >
224324< details class ="source ">
225325< summary >
226326< span > Expand source code</ span >
227327</ summary >
228- < pre > < code class ="python "> def release_package_to_repository(self) -> None:
328+ < pre > < code class ="python "> def release_package_to_repository(self, version: str ) -> None:
229329 """No operation."""
230- super().release_package_to_repository()</ code > </ pre >
330+ super().release_package_to_repository(version)
331+ _call_goreleaser_release(version)</ code > </ pre >
231332</ details >
232333</ dd >
233334</ dl >
@@ -265,6 +366,7 @@ <h4><code><a title="continuous_delivery_scripts.plugins.golang.Go" href="#contin
265366< li > < code > < a title ="continuous_delivery_scripts.plugins.golang.Go.check_credentials " href ="#continuous_delivery_scripts.plugins.golang.Go.check_credentials "> check_credentials</ a > </ code > </ li >
266367< li > < code > < a title ="continuous_delivery_scripts.plugins.golang.Go.get_current_spdx_project " href ="#continuous_delivery_scripts.plugins.golang.Go.get_current_spdx_project "> get_current_spdx_project</ a > </ code > </ li >
267368< li > < code > < a title ="continuous_delivery_scripts.plugins.golang.Go.get_related_language " href ="#continuous_delivery_scripts.plugins.golang.Go.get_related_language "> get_related_language</ a > </ code > </ li >
369+ < li > < code > < a title ="continuous_delivery_scripts.plugins.golang.Go.get_version_tag " href ="#continuous_delivery_scripts.plugins.golang.Go.get_version_tag "> get_version_tag</ a > </ code > </ li >
268370< li > < code > < a title ="continuous_delivery_scripts.plugins.golang.Go.package_software " href ="#continuous_delivery_scripts.plugins.golang.Go.package_software "> package_software</ a > </ code > </ li >
269371< li > < code > < a title ="continuous_delivery_scripts.plugins.golang.Go.release_package_to_repository " href ="#continuous_delivery_scripts.plugins.golang.Go.release_package_to_repository "> release_package_to_repository</ a > </ code > </ li >
270372</ ul >
0 commit comments