@@ -37,13 +37,80 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
37
37
#
38
38
"""Plugin for Golang projects."""
39
39
import logging
40
+ import os
40
41
from pathlib import Path
41
- from typing import Optional
42
+ from typing import Optional, List
43
+ from subprocess import check_call
42
44
from continuous_delivery_scripts.utils.language_specifics_base import BaseLanguage, get_language_from_file_name
43
45
from continuous_delivery_scripts.spdx_report.spdx_project import SpdxProject
46
+ from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable
44
47
45
48
logger = logging.getLogger(__name__)
46
49
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
+
47
114
48
115
class Go(BaseLanguage):
49
116
"""Specific actions for a Golang project."""
@@ -52,22 +119,30 @@ <h1 class="title">Module <code>continuous_delivery_scripts.plugins.golang</code>
52
119
"""Gets the related language."""
53
120
return get_language_from_file_name(__file__)
54
121
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:
56
128
"""No operation."""
57
- super().package_software()
129
+ super().package_software(version)
130
+ _call_goreleaser_check(version)
58
131
59
- def release_package_to_repository(self) -> None:
132
+ def release_package_to_repository(self, version: str ) -> None:
60
133
"""No operation."""
61
- super().release_package_to_repository()
134
+ super().release_package_to_repository(version)
135
+ _call_goreleaser_release(version)
62
136
63
137
def check_credentials(self) -> None:
64
138
"""Checks any credentials."""
65
139
super().check_credentials()
140
+ configuration.get_value(ConfigurationVariable.GIT_TOKEN)
66
141
67
142
def generate_code_documentation(self, output_directory: Path, module_to_document: str) -> None:
68
143
"""Generates the code documentation."""
69
144
super().generate_code_documentation(output_directory, module_to_document)
70
- # TODO
145
+ _call_golds(output_directory, "./...")
71
146
72
147
def can_add_licence_headers(self) -> bool:
73
148
"""States that licence headers can be added."""
@@ -108,22 +183,30 @@ <h2 class="section-title" id="header-classes">Classes</h2>
108
183
"""Gets the related language."""
109
184
return get_language_from_file_name(__file__)
110
185
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:
112
192
"""No operation."""
113
- super().package_software()
193
+ super().package_software(version)
194
+ _call_goreleaser_check(version)
114
195
115
- def release_package_to_repository(self) -> None:
196
+ def release_package_to_repository(self, version: str ) -> None:
116
197
"""No operation."""
117
- super().release_package_to_repository()
198
+ super().release_package_to_repository(version)
199
+ _call_goreleaser_release(version)
118
200
119
201
def check_credentials(self) -> None:
120
202
"""Checks any credentials."""
121
203
super().check_credentials()
204
+ configuration.get_value(ConfigurationVariable.GIT_TOKEN)
122
205
123
206
def generate_code_documentation(self, output_directory: Path, module_to_document: str) -> None:
124
207
"""Generates the code documentation."""
125
208
super().generate_code_documentation(output_directory, module_to_document)
126
- # TODO
209
+ _call_golds(output_directory, "./...")
127
210
128
211
def can_add_licence_headers(self) -> bool:
129
212
"""States that licence headers can be added."""
@@ -170,7 +253,8 @@ <h3>Methods</h3>
170
253
</ summary >
171
254
< pre > < code class ="python "> def check_credentials(self) -> None:
172
255
"""Checks any credentials."""
173
- super().check_credentials()</ code > </ pre >
256
+ super().check_credentials()
257
+ configuration.get_value(ConfigurationVariable.GIT_TOKEN)</ code > </ pre >
174
258
</ details >
175
259
</ dd >
176
260
< dt id ="continuous_delivery_scripts.plugins.golang.Go.get_current_spdx_project "> < code class ="name flex ">
@@ -202,32 +286,49 @@ <h3>Methods</h3>
202
286
return get_language_from_file_name(__file__)</ code > </ pre >
203
287
</ details >
204
288
</ 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 >
205
304
< 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 >
207
306
</ code > </ dt >
208
307
< dd >
209
308
< div class ="desc "> < p > No operation.</ p > </ div >
210
309
< details class ="source ">
211
310
< summary >
212
311
< span > Expand source code</ span >
213
312
</ summary >
214
- < pre > < code class ="python "> def package_software(self) -> None:
313
+ < pre > < code class ="python "> def package_software(self, version: str ) -> None:
215
314
"""No operation."""
216
- super().package_software()</ code > </ pre >
315
+ super().package_software(version)
316
+ _call_goreleaser_check(version)</ code > </ pre >
217
317
</ details >
218
318
</ dd >
219
319
< 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 >
221
321
</ code > </ dt >
222
322
< dd >
223
323
< div class ="desc "> < p > No operation.</ p > </ div >
224
324
< details class ="source ">
225
325
< summary >
226
326
< span > Expand source code</ span >
227
327
</ 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:
229
329
"""No operation."""
230
- super().release_package_to_repository()</ code > </ pre >
330
+ super().release_package_to_repository(version)
331
+ _call_goreleaser_release(version)</ code > </ pre >
231
332
</ details >
232
333
</ dd >
233
334
</ dl >
@@ -265,6 +366,7 @@ <h4><code><a title="continuous_delivery_scripts.plugins.golang.Go" href="#contin
265
366
< 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 >
266
367
< 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 >
267
368
< 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 >
268
370
< 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 >
269
371
< 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 >
270
372
</ ul >
0 commit comments