Skip to content

Commit

Permalink
Updated sources automatically committed (#4226)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
lukelloyd1985 and nvuillam authored Nov 11, 2024
1 parent ba879fe commit efacd1e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"RQASWB",
"SCSSLINT",
"SOURCEBRANCHNAME",
"SOURCEBRANCH",
"SOURCEREPOSITORYURI",
"St\u00e9phane",
"Suero",
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Add message in PR comment if FAIL_IF_UPDATED_SOURCES is triggered

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

Expand Down
51 changes: 49 additions & 2 deletions megalinter/reporters/UpdatedSourcesReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import logging
import os
import shutil
import chalk as c
import git

from megalinter import Reporter, config, utils

Expand Down Expand Up @@ -60,9 +62,54 @@ def produce_report(self):
if len(updated_files) > 0:
logging.info(
f"[Updated Sources Reporter] copied {str(len(updated_files))} fixed source files"
f" in folder {updated_sources_dir}.\n"
"Download it from artifacts then copy-paste it in your local repo to apply linters updates"
f" in folder {updated_sources_dir}."
)

if not config.exists(self.master.request_id, "GITHUB_REPOSITORY"):
apply_fixes = config.get(self.master.request_id, "APPLY_FIXES", "none")
if apply_fixes.lower() != "none":
remote_branch = ""
SYSTEM_PULLREQUEST_SOURCEBRANCH = config.get(
self.master.request_id, "SYSTEM_PULLREQUEST_SOURCEBRANCH", ""
)
if SYSTEM_PULLREQUEST_SOURCEBRANCH != "":
remote_branch = SYSTEM_PULLREQUEST_SOURCEBRANCH
BITBUCKET_BRANCH = config.get(
self.master.request_id, "BITBUCKET_BRANCH", ""
)
if BITBUCKET_BRANCH != "":
remote_branch = BITBUCKET_BRANCH
if remote_branch == "":
logging.error(
c.red(
"❌ [Updated Sources Reporter] Failed to retrieve git source branch"
)
)
else:
try:
repo = git.Repo(os.path.realpath(self.master.github_workspace))
repo.config_writer().set_value("user", "name", "MegaLinter").release()
repo.config_writer().set_value("user", "email", "[email protected]").release()
repo.git.add(update=True)
repo.git.commit('-m', '[MegaLinter] Apply linters fixes')
repo.git.push('origin', f'HEAD:{remote_branch}')
except git.GitCommandError as git_err:
logging.error(
c.red(
"❌ [Updated Sources Reporter] Failed to git push auto fixes: " + str(git_err.stderr)
)
)
logging.warning(
c.yellow(
"⚠️ [Updated Sources Reporter] Download fixed source files from artifacts "
"then copy-paste into your repo to apply linters updates"
)
)
else:
logging.info(
"[Updated Sources Reporter] Fixed source files have automatically "
"been pushed to the source branch"
)
else:
logging.info(
"[Updated Sources Reporter] No source file has been formatted or fixed"
Expand Down

0 comments on commit efacd1e

Please sign in to comment.