0

My team is working on developing automation tests, and we are facing the following problem: when a developer commits a change that affects the UI of our application, the automation test fails.

Is there a way, in Git, for me to get notified by email whenever a specific file type (in my case .jsp) is changed?

Note that setting up an email notification is not the problem, here; I know how I could use a hook for that. Rather, the problem is about how to detect that a file of a given type (in my case .jsp) has changed. Any idea?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91

2 Answers2

2

It would be easy with a simple post-receive hook, but it would not be good solution to the ultimate problem.

When developer commits change to the UI, they should also update the tests so they pass again in the same commit or at least in the same push. That means the tests should be in the same repository as the code. Well, since they are intimately tied with the way the UI works, they should.

Then it's just a matter of using Continuous Integration (I am used to Jenkins; there are other options too) and/or automatically deploying the changes to the test environment against which the tests run (see e.g. Deploy a project using Git push).


That said, you can do it with hooks.

First, to find whether any *.jsp files have been changed between revisions $old and $new you call:

git diff --exit-code --quiet $old..$new -- '*.jsp'

and if exits with false (non-zero) status, then some have changed (without the --quiet it will show you the changes, with --name-status it will list the files).

So now you just need to put something like

while read old new ref
do
    if !git diff --exit-code --quiet $old..$new -- '*.jsp'
    then
        git diff --name-status $old $new | \
            mail -s "*.jsp files changed in $ref" \
                you@company.com
    fi
done

to post-receive hook in your repository.

Or you could adjust the command to prevent developers pushing *.jsp files directly to master (e.g. via gitolite) to force them to push such changes to some staging branch where the automation will be updated by the other team and only then integrated by the other team or designated gatekeeper.

Community
  • 1
  • 1
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • I agree - the thing is that the developer and the automation team, are seperate teams. and a developer is not in charge of changing the automation script. this is why i need a notification when changing ONLY a .jsp file – Shahar Hamuzim Rajuan Sep 10 '14 at 13:41
  • @ShacharHamuzimRajuan: Can't you try to reorganize the project? Or improve communication and procedure so that when a developer changes the UI, they first send the changes to an automator (via feature branch or mail or bundle or whatever), the automator adjusts the test suite and then pushes the result to master? – Jan Hudec Sep 10 '14 at 19:48
  • @ShacharHamuzimRajuan: I've added appropriate git diff invocation to find whether any files matching pattern were added and some description where you could stick it. – Jan Hudec Sep 11 '14 at 13:30
0

I finally created this hook, as a post-recieve hook:

for file in $(git show --pretty="format:" --name-only $1..$2 $3 | grep -i .jsp)
    do
       git diff $1..$2 -- $file | \
       mail -s "A '*.jsp' file has changed in the Repo" \
       sample1@israel.com,sample2@israel.com
    exit
done
Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91