Monitor your git project contributions
Table of Contents
Why would you want to monitor your git project contributions ?
There are plenty of reasons why you would want to monitor your git project contributions:
- If you’d like to react when a new Pull Request or Merge Request is created
- If you’d like to be sure that your Pull Request or Merge Request have been handled before a certain time
- If you’re a monitoring geek like me :-)
Github and Gitlab both have WebHooks that can be used to react on certain events. This is really cool.
Both have their pros and cons.
I’m usually a huge fan of the
push mode provided by the webhooks since they are really easy to handle. They are also easy to setup and usually provide a good response time with minimal resource waste. Indeed, since your webhook is only triggered when it needs to, you don’t waste useless calls to check if anything has changed since last check like you’re doing in
But sometimes, when you’re self-hosted, like I am, you may just don’t have a public webhook handler exposed to the outside world. In such a case,
pull mode is your go to solution.
My personal motivation
My motivation to monitor the Opened Pull Requests made on some of my public repositories is the integration to my personal naemon infrastructure.
This infrastructure just gives me a central view of all the different services I’m relying on.
One of them is this very website 🗺️📍
Long story short:
- this website is a static website and a pull request is created each time someone submits a comment.
- I need to manually approve the pull request (the comment). This is how comments are moderated.
- Once a comment is approved, the website is automatically rebuilt and the comment is displayed 🪄. This is one of the many ways to achieve dynamic like behaviors in the static websites world.
So in conclusion, yes, my original motivation to monitor pull requests on a git repository was to review user comments quickly 🤷♂️.
A generic monitoring plugin for pull requests and merge requests
https://github.com/riton/nagios-plugin-git-hosted-project-merge-requests is a monitoring plugin whose purpose is to help project maintainers hosted on Gitlab, Github, etc… to keep track of their staled Merge Request / Pull Requests.
Using packages or pre-built binaries
Thanks to the awesome tool goreleaser, pre-built packages and binaries can be found at https://github.com/riton/nagios-plugin-git-hosted-project-merge-requests/releases/.
Compiling the plugin yourself
You’ll need a GO compiler with support for go modules to build this plugin.
Then all you have to do is:
$ go mod tidy $ CGO_ENABLED=0 go build -ldflags "-s -w -extldflags '-static'" -o check_git_project_merge_requests .
$ check_git_project_merge_requests -h Checks that a github / gitlab / gitea project has opened merge requests Usage: nagios-plugin-git-hosted-project-merge-requests [flags] Flags: --api-token string API Token used for authentication -c, --config string config file (default is /etc/nagios-plugin-git-hosted-project-merge-requests/config.yaml) --critical-last-update duration critical if last-update was that delay ago (default 24h0m0s) -d, --debug Enable debug -p, --git-provider string git provider can be one of gitlab -h, --help help for nagios-plugin-git-hosted-project-merge-requests -H, --host string host to check (API endpoint) -P, --project string project to check for opened MergeRequests --target-branch string Only consider merge requests with this target-branch (default "master") -t, --timeout duration Global timeout (default 30s) --warning-last-update duration warning if last-update was that delay ago (default 6h0m0s)
This plugin requires an
API_TOKEN to authenticate to the git provider.
Passing a secret on the command line is always a bad idea. This plugin, expects to retrieve the
- from the environment
API_TOKEN=XXXXXXX check_git_project_merge_requests ...
- from the configuration file
--- api-token: 'XXXXXXX'
Other parameters can safely be specified on the command line.
Check for staled merge requests on a
gitlab.com hosted project
Here is how to monitor a project reachable at
https://gitlab.com/username/project_name and return a CRITICAL failure if a merge request is opened for more than 24 hours:
$ API_TOKEN=XXXXXXX check_git_project_merge_requests -H https://gitlab.com -P "username/project_name" -p gitlab CRITICAL: Merge request 42 last activity was 21m43.664245342s ago | 'total_duration'=0.795784589s;;;; 'opened_merge_requests'=1;;;; 'oldest_merge_request'=1303.664245342s;;;;
Future / contributions
This plugin is still very young and lack important features.
One of the most important feature this plugin is missing is a cool project avatar ! If a photoshop or gimp guru is reading this article, feel free to contact me 📞.
Here are the main features I’d like to implement:
- Add option to filter assigned merge requests.
- Add option to filter merge requests based on their
Feel free to open an issue If you’d like to discuss or suggest a missing feature.