|
6 | 6 | import logging
|
7 | 7 | import os
|
8 | 8 | import shutil
|
| 9 | +import chalk as c |
| 10 | +import git |
9 | 11 |
|
10 | 12 | from megalinter import Reporter, config, utils
|
11 | 13 |
|
@@ -60,9 +62,54 @@ def produce_report(self):
|
60 | 62 | if len(updated_files) > 0:
|
61 | 63 | logging.info(
|
62 | 64 | 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}." |
65 | 66 | )
|
| 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 | + ) |
66 | 113 | else:
|
67 | 114 | logging.info(
|
68 | 115 | "[Updated Sources Reporter] No source file has been formatted or fixed"
|
|
0 commit comments