I am planning to run some validations against the pull request in a CI pipeline and, based on the validation results, I wish to automatically create an issue and assign it to developers.
Is this achievable in a GitLab pipeline?
Thanks!
I am planning to run some validations against the pull request in a CI pipeline and, based on the validation results, I wish to automatically create an issue and assign it to developers.
Is this achievable in a GitLab pipeline?
Thanks!
You should be able to call the New Issue API through a curl call (as seen here)
curl --request POST --header "PRIVATE-TOKEN: PlainTextToken" "https://gitlab.com/api/v4/projects/:id/issues?title=a title"
You would need to add a private token as a masked variable.
If you just need to create a single issue, go with the cURL command. However, if you require more complex logic, the python-gitlab library can be a useful tool in your arsenal.
To create a project issue:
import gitlab
import os
gl = gitlab.Gitlab(os.environ['CI_SERVER_URL'], private_token=os.environ['PRIVATE_TOKEN'])
project = gl.projects.get(os.environ['CI_PROJECT_ID'])
issue_details = {
'title': f'Validation failed in {os.environ["CI_PROJECT_NAME"]}',
'description': f'Pipeline: {os.environ["CI_PIPELINE_URL"]}',
'assignee_ids': [111, 222]
}
issue = project.issues.create(issue_details )
This assumes you have created a masked variable called PRIVATE_TOKEN. The other variables are pre-defined variables.
You can add the above code and other logic to a Python script and call it in gitlab-ci.yml like this:
# Use whatever image you need, but make sure it has Python installed
image: python:3.7
...
handle-failure:
when: on_failure
before_script:
- pip install -r ./cicd/gitlab/requirements.txt
script:
- python -m ./cicd/gitlab/create_issue.py