Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Gitlab comment reporter #1215

Merged
merged 3 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ mega-linter:
expire_in: 1 week
```

Create a Gitlab access token and define it in a variable **GITLAB_ACCESS_TOKEN_MEGALINTER** in the project CI/CD masked variables

![config-gitlab-access-token](https://user-images.githubusercontent.com/17500430/151674446-1bcb1420-d9aa-4ae1-aaae-dcf51afb36ab.gif)

![Screenshot](https://github.com/megalinter/megalinter/blob/main/docs/assets/images/TextReporter_gitlab_1.jpg?raw=true>)

### Concourse
Expand Down
2 changes: 2 additions & 0 deletions docs/reporters/GitlabCommentReporter.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Click on hyperlinks to access detailed logs (click on **Download** in **Artifact
- Create an [access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token) with scope **api**
- Paste the access token in a [masked CI/CD variable](https://docs.gitlab.com/ee/ci/variables/#add-a-cicd-variable-to-a-project) named **GITLAB_ACCESS_TOKEN_MEGALINTER** in your project (repository)

![config-gitlab-access-token](https://user-images.githubusercontent.com/17500430/151674446-1bcb1420-d9aa-4ae1-aaae-dcf51afb36ab.gif)

| Variable | Description | Default value |
|--------------------------------|-------------------------------------------------------------------------------------------|--------------------------|
| GITLAB_COMMENT_REPORTER | Activates/deactivates reporter | true |
Expand Down
46 changes: 32 additions & 14 deletions megalinter/reporters/GitlabCommentReporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,21 @@ def produce_report(self):
gl = gitlab.Gitlab(
gitlab_server_url, job_token=config.get("CI_JOB_TOKEN")
)
project = gl.projects.get(gitlab_project_id)
# Get gitlab project
try:
project = gl.projects.get(gitlab_project_id)
except gitlab.GitlabGetError as e:
logging.warning(
"[Gitlab Comment Reporter] No project has been found with "
f"id {gitlab_project_id}, so no comment has been posted\n"
)
self.display_auth_error(e)
return
except Exception as e:
self.display_auth_error(e)
return

# Get merge request
try:
mr = project.mergerequests.get(gitlab_merge_request_id)
except gitlab.GitlabGetError:
Expand All @@ -64,8 +78,11 @@ def produce_report(self):
logging.warning(
"[Gitlab Comment Reporter] No merge request has been found with "
f"id {gitlab_merge_request_id}, so no comment has been posted\n"
+ str(e)
)
self.display_auth_error(e)
return
except Exception as e:
self.display_auth_error(e)
return

# Ignore if PR is already merged
Expand All @@ -77,18 +94,10 @@ def produce_report(self):
try:
existing_comments = mr.notes.list()
except gitlab.GitlabAuthenticationError as e:
logging.error(
"[Gitlab Comment Reporter] You need to define a masked Gitlab CI/CD variable "
"GITLAB_ACCESS_TOKEN_MEGALINTER containing a personal token with api access\n"
+ str(e)
)
self.display_auth_error(e)
return
except Exception as e:
logging.error(
"[Gitlab Comment Reporter] You need to define a masked Gitlab CI/CD variable "
"MEGALINTER_ACCESS_TOKEN containing a personal token with scope 'api'\n"
+ str(e)
)
self.display_auth_error(e)
return

# Check if there is already a MegaLinter comment
Expand All @@ -114,14 +123,23 @@ def produce_report(self):
)
except gitlab.GitlabError as e:
logging.warning(
f"[GitHub Comment Reporter] Unable to post merge request comment: {str(e)}"
"[GitHub Comment Reporter] Unable to post merge request comment"
)
self.display_auth_error(e)
except Exception as e:
logging.warning(
f"[Gitlab Comment Reporter] Error while posting comment: \n{str(e)}"
"[Gitlab Comment Reporter] Error while posting comment"
)
self.display_auth_error(e)
# Not in gitlab context
else:
logging.debug(
"[Gitlab Comment Reporter] No Gitlab Token found, so skipped post of MR comment"
)

def display_auth_error(self,e):
logging.error(
"[Gitlab Comment Reporter] You may need to define a masked Gitlab CI/CD variable "
"MEGALINTER_ACCESS_TOKEN containing a personal token with scope 'api'\n"
+ str(e)
)