On Gitlab, is it possible to receive mail as administrator when a repository is deleted…
- User
On Gitlab, is it possible to receive mail as administrator when a repository is deleted or archived?
- Assistant
On Gitlab, is it possible to receive mail as administrator when a repository is deleted or archived?
As of my last update, GitLab does not have a built-in feature to notify administrators specifically via email when a repository is deleted or archived. This type of notification isn’t provided directly within the built-in GitLab settings.
However, you can achieve this functionality through custom solutions:
GitLab allows administrators to write server-side hooks. You can create a custom post-receive hook that sends an email when a repository is deleted or archived. This involves writing your script and placing it in the appropriate directory on the GitLab server.
Here is a basic outline of how you could go about it:
Create Custom Hook Script: Write a script that sends an email. This can be done in any language you're comfortable with (e.g., Python, Bash).
Place the Script Appropriately:
Put your custom script in the GitLab server’s hooks directory, typically found in /opt/gitlab/embedded/service/gitlab-shell/hooks.
#!/bin/bash
# Example of a post-receive hook that sends an email
# Make sure to adjust paths and email addresses accordingly
REPO_NAME=$(basename `git rev-parse --show-toplevel`)
ACTION=$1
# Send an email using a tool like sendmail
if [ "$ACTION" == "delete" ] || [ "$ACTION" == "archive" ]; then
echo "Subject: GitLab Repository $ACTION Notification
To: admin@example.com
Repository $REPO_NAME has been $ACTION." | /usr/sbin/sendmail -t
fi
chmod +x /path/to/your_script
Consider using GitLab's audit logs (available in GitLab Premium and higher). Although this won't send an email directly, you can monitor these logs for repository deletions and archives. You can then set up a cron job or a monitoring tool to check these logs regularly and send an email notification if it detects such actions.
Use external monitoring and alerting tools like Prometheus, Nagios, or any other monitoring infrastructure you have. These tools can be configured to monitor GitLab’s activity and trigger notifications.
You can configure project or system hooks in GitLab. Specifically, repository events can be sent to a specified URL where a listener service can then handle sending the appropriate email.
You can also periodically query the GitLab API for changes in repository status. Write a script that checks for recent deletions or archives via the API and sends an email if it detects any such actions.
import requests
def send_email(subject, body, to_email):
import smtplib
from email.mime.text import MIMEText
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = 'gitlab@example.com'
msg['To'] = to_email
with smtplib.SMTP('localhost') as server:
server.sendmail(msg['From'], [msg['To']], msg.as_string())
def check_repositories():
url = 'https://gitlab.example.com/api/v4/projects?simple=true'
headers = {'Private-Token': 'your_access_token'}
response = requests.get(url, headers=headers)
for project in response.json():
if project['archived']:
send_email('Repository Archived', f"{project['name']} has been archived.", 'admin@example.com')
if __name__ == '__main__':
check_repositories()
If you are not comfortable with implementing these custom solutions, consider submitting a feature request to GitLab or looking for existing plugins or integrations developed by the community that might offer this functionality.