Skip to content

Instantly share code, notes, and snippets.

@kernoeb
Last active August 12, 2020 11:48
Show Gist options
  • Save kernoeb/3c44eecececa8aa6147f741ad24a2065 to your computer and use it in GitHub Desktop.
Save kernoeb/3c44eecececa8aa6147f741ad24a2065 to your computer and use it in GitHub Desktop.
Check for new commits on GitLab with desktop notification (see tutorial.md)
#!/bin/bash
# @kernoeb
# PLEASE SEE tutorial.md
token="gitlab_token"
directory="/path/to/scripts/directory" # script directory
config="/home/USER/.config/checkAllCommits" # config directory
file="checkCommitsNotif.sh"
/bin/bash $directory/$file $config $directory $token repo name master # please write "master"
/bin/bash $directory/$file $config $directory $token repo name dev # another branch
/bin/bash $directory/$file $config $directory $token repo name2 master
#!/bin/bash
default_orga="$4"
default_project="$5"
token="$3"
theDirectory="$1"
directory="$2"
branch="$6"
tmpname=$default_orga-$default_project-$branch
theFile="$theDirectory/$tmpname"
git="https://github.com/vlevit/notify-send.sh"
display=:1 # If the browser does not open on notification clic, try :0
mkdir -p $theDirectory
[[ ! -f "$theFile" ]] && touch $theDirectory/$tmpname && echo nothing > $theDirectory/$tmpname
commits=`curl -s --header "PRIVATE-TOKEN: $token" "https://gitlab.com/api/v4/projects/$default_orga%2F$default_project/repository/commits/$branch"`
new=`jq -rj .committed_date <<< $commits`
author_name=`jq -rj .author_name <<< $commits`
title=`jq -rj .title <<< $commits`
url=`jq -rj .web_url <<< $commits`
last=`cat $theDirectory/$tmpname`
if [ $new != $last ]
then
echo "New commit!"
echo $new > $theDirectory/$tmpname
[ ! -d $directory/notify-send.sh ] && git clone $git $directory/notify-send.sh
cd $directory
XDG_RUNTIME_DIR=/run/user/$(id -u) /bin/bash ./notify-send.sh/notify-send.sh -d "export DISPLAY=$display && xdg-open $url" "$title | $default_orga/$default_project ($branch)" "$author_name"
fi

Usage :

  1. Save checkCommitsNotif.sh in a file, wherever you want

  2. Save checkAllProjects.sh in another file, in the same directory

  3. Edit file (configuration) :

    • Replace USER (line 4) with your user (echo $USER)
    • Replace gitlab_token with your GitLab token (see here)
    • Replace repo name with something like "google Python", for example.
  4. Now, open a terminal

  5. Install theses packages :

sudo apt install libnotify-bin
sudo apt install jq
  1. Run this command
crontab -e

At the end of the file, add :

* * * * * /bin/bash /location/of/checkAllProjects.sh

Note : That means every minute, see crontab.guru

You're done ! 🎉 Now, you'll get a notification when there's a new commit. Clicking on the notification will open the URL on your browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment