Skip to content

Commit efacd1e

Browse files
Updated sources automatically committed (#4226)
* updated sources modifications tested on alpha * SOURCEBRANCH ignore added to cspell * updated CHANGELOG * logging message on git success * fixed pylint possibly-used-before-assignment * Update UpdatedSourcesReporter.py --------- Co-authored-by: Nicolas Vuillamy <[email protected]>
1 parent ba879fe commit efacd1e

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

.cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"RQASWB",
4747
"SCSSLINT",
4848
"SOURCEBRANCHNAME",
49+
"SOURCEBRANCH",
4950
"SOURCEREPOSITORYURI",
5051
"St\u00e9phane",
5152
"Suero",

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
2525
- Add message in PR comment if FAIL_IF_UPDATED_SOURCES is triggered
2626

2727
- Reporters
28+
- UpdatedSourcesReporter will git commit & push fixed files to source branch if APPLY_FIXES is set
2829
- Fix AzureCommentReporter not adding comments to PR
2930
- Fix AzureCommentReporter fails when target repo contains spaces
3031

megalinter/reporters/UpdatedSourcesReporter.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import logging
77
import os
88
import shutil
9+
import chalk as c
10+
import git
911

1012
from megalinter import Reporter, config, utils
1113

@@ -60,9 +62,54 @@ def produce_report(self):
6062
if len(updated_files) > 0:
6163
logging.info(
6264
f"[Updated Sources Reporter] copied {str(len(updated_files))} fixed source files"
63-
f" in folder {updated_sources_dir}.\n"
64-
"Download it from artifacts then copy-paste it in your local repo to apply linters updates"
65+
f" in folder {updated_sources_dir}."
6566
)
67+
68+
if not config.exists(self.master.request_id, "GITHUB_REPOSITORY"):
69+
apply_fixes = config.get(self.master.request_id, "APPLY_FIXES", "none")
70+
if apply_fixes.lower() != "none":
71+
remote_branch = ""
72+
SYSTEM_PULLREQUEST_SOURCEBRANCH = config.get(
73+
self.master.request_id, "SYSTEM_PULLREQUEST_SOURCEBRANCH", ""
74+
)
75+
if SYSTEM_PULLREQUEST_SOURCEBRANCH != "":
76+
remote_branch = SYSTEM_PULLREQUEST_SOURCEBRANCH
77+
BITBUCKET_BRANCH = config.get(
78+
self.master.request_id, "BITBUCKET_BRANCH", ""
79+
)
80+
if BITBUCKET_BRANCH != "":
81+
remote_branch = BITBUCKET_BRANCH
82+
if remote_branch == "":
83+
logging.error(
84+
c.red(
85+
"❌ [Updated Sources Reporter] Failed to retrieve git source branch"
86+
)
87+
)
88+
else:
89+
try:
90+
repo = git.Repo(os.path.realpath(self.master.github_workspace))
91+
repo.config_writer().set_value("user", "name", "MegaLinter").release()
92+
repo.config_writer().set_value("user", "email", "[email protected]").release()
93+
repo.git.add(update=True)
94+
repo.git.commit('-m', '[MegaLinter] Apply linters fixes')
95+
repo.git.push('origin', f'HEAD:{remote_branch}')
96+
except git.GitCommandError as git_err:
97+
logging.error(
98+
c.red(
99+
"❌ [Updated Sources Reporter] Failed to git push auto fixes: " + str(git_err.stderr)
100+
)
101+
)
102+
logging.warning(
103+
c.yellow(
104+
"⚠️ [Updated Sources Reporter] Download fixed source files from artifacts "
105+
"then copy-paste into your repo to apply linters updates"
106+
)
107+
)
108+
else:
109+
logging.info(
110+
"[Updated Sources Reporter] Fixed source files have automatically "
111+
"been pushed to the source branch"
112+
)
66113
else:
67114
logging.info(
68115
"[Updated Sources Reporter] No source file has been formatted or fixed"

0 commit comments

Comments
 (0)