Skip to content

Commit

Permalink
Fix Github auth on Azure Pipelines (#1202)
Browse files Browse the repository at this point in the history
* Fix Github auth on Azure Pipelines

Fix issue when using `VALIDATE_ALL_CODEBASE: false` on Azure Pipelines by defining auth header in CI env variable GIT_AUTHORIZATION_BEARER ([#1125](#1125))

* [MegaLinter] Apply linters fixes

* Update doc for Azure

Co-authored-by: nvuillam <nvuillam@users.noreply.github.com>
  • Loading branch information
nvuillam and nvuillam committed Jan 25, 2022
1 parent 8d849f4 commit e9f7f0b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-l

- New reporter **GITLAB_COMMENT_REPORTER** allowing to post MegaLinter results as comments on Gitlab merge requests
- Fix v5 doc deployment when there is a new release ([1190](https://github.com/megalinter/megalinter/issues/1190))
- Fix issue when using `VALIDATE_ALL_CODEBASE: false` on Azure Pipelines by defining auth header in CI env variable GIT_AUTHORIZATION_BEARER ([#1125](https://github.com/megalinter/megalinter/issues/1125))

- Linter versions upgrades
- [stylelint](https://stylelint.io) from 14.2.0 to **14.3.0** on 2022-01-23
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ You may activate [File.io reporter](https://megalinter.github.io/reporters/FileI
steps:
- script: |
docker pull megalinter/megalinter:v5
docker run -v $(System.DefaultWorkingDirectory):/tmp/lint megalinter/megalinter
displayName: 'Code Scan using MegaLinter'
docker run -v $(System.DefaultWorkingDirectory):/tmp/lint -e GIT_AUTHORIZATION_BEARER=$(System.AccessToken) megalinter/megalinter:v5
displayName: 'MegaLinter analysis'
```

### Jenkins
Expand Down
9 changes: 8 additions & 1 deletion megalinter/MegaLinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,13 @@ def list_files_git_diff(self):
"Listing updated files in [" + self.github_workspace + "] using git diff."
)
repo = git.Repo(os.path.realpath(self.github_workspace))
# Add auth header if necessary
if config.get("GIT_AUTHORIZATION_BEARER", "") != "":
auth_bearer = "Authorization: Bearer " + config.get(
"GIT_AUTHORIZATION_BEARER"
)
repo.config_writer().set_value("http", "extraheader", auth_bearer).release()
# Fetch base branch content
default_branch = config.get("DEFAULT_BRANCH", "HEAD")
default_branch_remote = f"origin/{default_branch}"
if default_branch_remote not in [ref.name for ref in repo.refs]:
Expand All @@ -507,7 +514,7 @@ def list_files_git_diff(self):
local_ref = f"refs/remotes/{default_branch_remote}"
# Try to fetch default_branch from origin, because it isn't cached locally.
repo.git.fetch("origin", f"{remote_ref}:{local_ref}")

# Make git diff to list files
diff = repo.git.diff(default_branch_remote, name_only=True)
logging.info(f"Modified files:\n{diff}")
all_files = list()
Expand Down

0 comments on commit e9f7f0b

Please sign in to comment.