11<!--
2- -- Copyright (C) 2020-2021 Arm. All rights reserved.
2+ -- Copyright (C) 2020-2021 Arm Limited or its affiliates and Contributors . All rights reserved.
33-- SPDX-License-Identifier: Apache-2.0
44-->
55<!doctype html>
66< html lang ="en ">
77< head >
88< meta charset ="utf-8 ">
99< meta name ="viewport " content ="width=device-width, initial-scale=1, minimum-scale=1 " />
10- < meta name ="generator " content ="pdoc 0.9.2 " />
10+ < meta name ="generator " content ="pdoc 0.9.1 " />
1111< title > continuous_delivery_scripts.assert_news API documentation</ title >
1212< meta name ="description " content ="Checks if valid news files are created for changes in the project. " />
1313< link rel ="preload stylesheet " as ="style " href ="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css " integrity ="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs= " crossorigin >
@@ -46,6 +46,7 @@ <h1 class="title">Module <code>continuous_delivery_scripts.assert_news</code></h
4646from continuous_delivery_scripts.utils.configuration import configuration, ConfigurationVariable
4747from continuous_delivery_scripts.utils.git_helpers import ProjectTempClone, LocalProjectRepository, GitWrapper
4848from continuous_delivery_scripts.utils.logging import log_exception, set_log_level
49+ from continuous_delivery_scripts.utils.news_file import create_news_file
4950
5051logger = logging.getLogger(__name__)
5152
@@ -125,6 +126,39 @@ <h1 class="title">Module <code>continuous_delivery_scripts.assert_news</code></h
125126 validate_news_file(absolute_file_path)
126127
127128
129+ def add_news_files(git: GitWrapper, news_dir: str) -> None:
130+ """Adds a news file if the branch corresponds to an dependency update.
131+
132+ Args:
133+ git: Instance of GitWrapper.
134+ news_dir: Relative path to news directory.
135+ """
136+ current_branch = str(git.get_current_branch())
137+ is_dependency_update, groups = git.is_current_branch_of_type(
138+ str(configuration.get_value(ConfigurationVariable.DEPENDENCY_UPDATE_BRANCH_PATTERN))
139+ )
140+ if not is_dependency_update:
141+ raise EnvironmentError(f"Branch {current_branch} must contain a news file.")
142+ if not configuration.get_value(ConfigurationVariable.AUTOGENERATE_NEWS_FILE_ON_DEPENDENCY_UPDATE):
143+ raise EnvironmentError(f"Branch {current_branch} must contain a news file.")
144+
145+ create_news_file(
146+ news_dir,
147+ str(configuration.get_value(ConfigurationVariable.DEPENDENCY_UPDATE_NEWS_MESSAGE)).format(
148+ message=", ".join(groups)
149+ ),
150+ configuration.get_value(ConfigurationVariable.DEPENDENCY_UPDATE_NEWS_TYPE),
151+ )
152+
153+
154+ def _commit_news_file(git: GitWrapper, news_dir: str) -> None:
155+ logger.info("Committing news file...")
156+ git.add(news_dir)
157+ git.commit("Adding news file")
158+ git.push()
159+ git.pull()
160+
161+
128162def main() -> None:
129163 """Asserts the new PR comprises at least one news file and it adheres to the required standard."""
130164 parser = argparse.ArgumentParser(description="Check correctly formatted news files exist on feature branch.")
@@ -147,7 +181,12 @@ <h1 class="title">Module <code>continuous_delivery_scripts.assert_news</code></h
147181 validate_news_files(git=git, news_dir=news_dir, root_dir=root_dir)
148182 except Exception as e:
149183 log_exception(logger, e)
150- sys.exit(1)
184+ try:
185+ add_news_files(git, absolute_news_dir)
186+ _commit_news_file(git, absolute_news_dir)
187+ except Exception as e2:
188+ log_exception(logger, e2)
189+ sys.exit(1)
151190
152191
153192if __name__ == "__main__":
@@ -161,6 +200,47 @@ <h1 class="title">Module <code>continuous_delivery_scripts.assert_news</code></h
161200< section >
162201< h2 class ="section-title " id ="header-functions "> Functions</ h2 >
163202< dl >
203+ < dt id ="continuous_delivery_scripts.assert_news.add_news_files "> < code class ="name flex ">
204+ < span > def < span class ="ident "> add_news_files</ span > </ span > (< span > git: < a title ="continuous_delivery_scripts.utils.git_helpers.GitWrapper " href ="utils/git_helpers.html#continuous_delivery_scripts.utils.git_helpers.GitWrapper "> GitWrapper</ a > , news_dir: str) ‑> NoneType</ span >
205+ </ code > </ dt >
206+ < dd >
207+ < div class ="desc "> < p > Adds a news file if the branch corresponds to an dependency update.</ p >
208+ < h2 id ="args "> Args</ h2 >
209+ < dl >
210+ < dt > < strong > < code > git</ code > </ strong > </ dt >
211+ < dd > Instance of GitWrapper.</ dd >
212+ < dt > < strong > < code > news_dir</ code > </ strong > </ dt >
213+ < dd > Relative path to news directory.</ dd >
214+ </ dl > </ div >
215+ < details class ="source ">
216+ < summary >
217+ < span > Expand source code</ span >
218+ </ summary >
219+ < pre > < code class ="python "> def add_news_files(git: GitWrapper, news_dir: str) -> None:
220+ """Adds a news file if the branch corresponds to an dependency update.
221+
222+ Args:
223+ git: Instance of GitWrapper.
224+ news_dir: Relative path to news directory.
225+ """
226+ current_branch = str(git.get_current_branch())
227+ is_dependency_update, groups = git.is_current_branch_of_type(
228+ str(configuration.get_value(ConfigurationVariable.DEPENDENCY_UPDATE_BRANCH_PATTERN))
229+ )
230+ if not is_dependency_update:
231+ raise EnvironmentError(f"Branch {current_branch} must contain a news file.")
232+ if not configuration.get_value(ConfigurationVariable.AUTOGENERATE_NEWS_FILE_ON_DEPENDENCY_UPDATE):
233+ raise EnvironmentError(f"Branch {current_branch} must contain a news file.")
234+
235+ create_news_file(
236+ news_dir,
237+ str(configuration.get_value(ConfigurationVariable.DEPENDENCY_UPDATE_NEWS_MESSAGE)).format(
238+ message=", ".join(groups)
239+ ),
240+ configuration.get_value(ConfigurationVariable.DEPENDENCY_UPDATE_NEWS_TYPE),
241+ )</ code > </ pre >
242+ </ details >
243+ </ dd >
164244< dt id ="continuous_delivery_scripts.assert_news.find_news_files "> < code class ="name flex ">
165245< span > def < span class ="ident "> find_news_files</ span > </ span > (< span > git: < a title ="continuous_delivery_scripts.utils.git_helpers.GitWrapper " href ="utils/git_helpers.html#continuous_delivery_scripts.utils.git_helpers.GitWrapper "> GitWrapper</ a > , root_dir: str, news_dir: str) ‑> List[str]</ span >
166246</ code > </ dt >
@@ -233,7 +313,12 @@ <h2 id="returns">Returns</h2>
233313 validate_news_files(git=git, news_dir=news_dir, root_dir=root_dir)
234314 except Exception as e:
235315 log_exception(logger, e)
236- sys.exit(1)</ code > </ pre >
316+ try:
317+ add_news_files(git, absolute_news_dir)
318+ _commit_news_file(git, absolute_news_dir)
319+ except Exception as e2:
320+ log_exception(logger, e2)
321+ sys.exit(1)</ code > </ pre >
237322</ details >
238323</ dd >
239324< dt id ="continuous_delivery_scripts.assert_news.validate_news_file "> < code class ="name flex ">
@@ -410,6 +495,7 @@ <h1>Index</h1>
410495</ li >
411496< li > < h3 > < a href ="#header-functions "> Functions</ a > </ h3 >
412497< ul class ="">
498+ < li > < code > < a title ="continuous_delivery_scripts.assert_news.add_news_files " href ="#continuous_delivery_scripts.assert_news.add_news_files "> add_news_files</ a > </ code > </ li >
413499< li > < code > < a title ="continuous_delivery_scripts.assert_news.find_news_files " href ="#continuous_delivery_scripts.assert_news.find_news_files "> find_news_files</ a > </ code > </ li >
414500< li > < code > < a title ="continuous_delivery_scripts.assert_news.main " href ="#continuous_delivery_scripts.assert_news.main "> main</ a > </ code > </ li >
415501< li > < code > < a title ="continuous_delivery_scripts.assert_news.validate_news_file " href ="#continuous_delivery_scripts.assert_news.validate_news_file "> validate_news_file</ a > </ code > </ li >
@@ -432,7 +518,7 @@ <h4><code><a title="continuous_delivery_scripts.assert_news.NewsFileValidator" h
432518</ nav >
433519</ main >
434520< footer id ="footer ">
435- < p > Generated by < a href ="https://pdoc3.github.io/pdoc "> < cite > pdoc</ cite > 0.9.2 </ a > .</ p >
521+ < p > Generated by < a href ="https://pdoc3.github.io/pdoc "> < cite > pdoc</ cite > 0.9.1 </ a > .</ p >
436522</ footer >
437523</ body >
438524</ html >
0 commit comments