Is it possible to add a label to a GitLab issue upon issue creation or change? For example:
# .gitlab-ci.yml
job:
script: echo "Update label."
rules:
- if:
- event: "issue:open"
- event: "issue:update"
- not:
- labels:
- regexp: "^priority::"
actions:
- label: "priority::Medium"
or do GitLab rules not apply to issue events?
As I know there is no ability to do this explicitly. But you can write some kind of custom script which will be triggered by webhook to add labels
Something like that:
import requests
import json
GITLAB_API_URL = "https://gitlab.com/api/{your-version}”
ACCESS_TOKEN = {your_personal_access_token}
PROJECT_ID = {your_project_id}
def add_label_to_issue(issue_id, label):
url = f"{GITLAB_API_URL}/projects/{PROJECT_ID}/issues/{issue_id}"
headers = {
"Private-Token": ACCESS_TOKEN,
"Content-Type": "application/json"
}
data = {
"labels": label
}
response = requests.put(url, headers=headers, data=json.dumps(data))
if response.status_code == 200:
print("Label added")
else:
print("Failed to add:", response.content)
def handle_issue_event(event_data):
issue_id = event_data["object_attributes"]["id"]
current_labels = event_data["labels"]
if not any(label["title"].startswith("priority::") for label in current_labels):
add_label_to_issue(issue_id, "priority::Medium")