reconquest / atlassian-external-hooks

External Hooks plugin for Atlassian Bitbucket
https://external-hooks.reconquest.io
Other
45 stars 37 forks source link

[10.2.1] On the first push of an new branch not scripts are running #114

Closed tuxmaster5000 closed 4 years ago

tuxmaster5000 commented 4 years ago

When new brach is pushed to the server, than no scripts are running. to reproduce:

git brach new_one
git co new_one
break the code
git push --set-upstream origin new_one

Now defect code are pushed to server :( When now do an second push, then the scripts will run gain.

kovetskiy commented 4 years ago

@tuxmaster5000 Hi!

I was not able to reproduce the issue on 10.2.1.

Here is my log, please let me know if I've missed any steps:

 ∞ /tmp/manrepo ∅ touch initial
 ∞ /tmp/manrepo ∅ git add .
 ∞ /tmp/manrepo ∅ git commit -m initial
[master (root-commit) 7438afd] initial
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 initial
 ∞ /tmp/manrepo master✓ git push origin
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 213 bytes | 213.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: pre-receive-ok
remote: arguments passed: 1
remote: argument #0: 123
To http://localhost:7990/scm/man/repo.git
 * [new branch]      master -> master
 ∞ /tmp/manrepo master✓ git branch new_one
 ∞ /tmp/manrepo master✓ git checkout new_one
Switched to branch 'new_one'
 ∞ /tmp/manrepo new_one✓ touch new_one
 ∞ /tmp/manrepo new_one↕ git add .
 ∞ /tmp/manrepo new_one↕ git commit -m new_one
[new_one cb9c10d] new_one
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new_one
 ∞ /tmp/manrepo new_one✓ git push --set-upstream origin new_one
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 247 bytes | 247.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: pre-receive-ok
remote: arguments passed: 1
remote: argument #0: 123
remote:
remote: Create pull request for new_one:
remote:   https://bitbucket.local/projects/MAN/repos/repo/pull-requests?create&sourceBranch=refs/heads/new_one
remote:
To http://localhost:7990/scm/man/repo.git
 * [new branch]      new_one -> new_one
Branch 'new_one' set up to track remote branch 'new_one' from 'origin'.
kovetskiy commented 4 years ago

Could you also share more details:

Thanks

tuxmaster5000 commented 4 years ago

We have one pre receive hook and we are using version 7.3.0 of bitbucket. Here my git push log of the first push:

Total 3 (delta 2), reused 0 (delta 0)
remote: 
remote: Create pull request for bug:
remote:   https://XXXXX/projects/PUPPET/repos/muelltonne/pull-requests?create&sourceBranch=refs/heads/bug
remote: 
To https://XXX/scm/puppet/muelltonne.git
 * [new branch]          bug -> bug

Nothing about an hook :( Do an second push in to the branch:

Total 3 (delta 2), reused 0 (delta 0)
remote: -----------------------------------------------
remote: WARNING: bad styling for file nodes.pp:
remote: ERROR: two-space soft tabs not used on line 6
remote: ERROR: two-space soft tabs not used on line 611
remote: ERROR: tab character found on line 6
remote: -----------------------------------------------
remote: NOTE: https://docs.puppet.com/puppet/latest/style_guide.html
remote: 
remote: Create pull request for bug:
remote:   https://XXXX/projects/PUPPET/repos/muelltonne/pull-requests?create&sourceBranch=refs/heads/bug
remote: 
To https://XXXX/scm/puppet/muelltonne.git
   b8bd6eeb4..1452e7ec7  bug -> bug

As you can see, the pre receive has done its job. I only have change on line in the source file, to get an delta.

tuxmaster5000 commented 4 years ago

The only error I see, but this is before the code in my script runs is: remote: fatal: this operation must be run in a work tree

kovetskiy commented 4 years ago

I've just tested the mentioned scenario on 7.3.0 with pre-receive hook and it worked as expected.

remote: fatal: this operation must be run in a work tree

I didn't get this error during the test, could you elaborate more on this point? Where do you see this mistake?

Also, could you try to add some echo test instruction to your pre-receive hook script before any other lines and check if it's starting?

kovetskiy commented 4 years ago

If you are still not able to see any prints, could you turn on debug logs for add-on and send them at we@reconquest.io?

Here is a command that you can use to turn on logs for com.ngs.stash.externalhooks:

curl -s -u LOGIN:PASSWORD -X PUT -d "" -H "Content-Type: application/json" \
          http://bitbucket.local/rest/api/latest/logs/logger/com.ngs.stash.externalhooks/debug
tuxmaster5000 commented 4 years ago

After some debugging I fond the problem. The error and the problem with the example. It don't catch the new/delete branch situations. Here the fix for it:

while read from_ref to_ref ref_name; do
if [ 0  -ne $(expr "${from_ref}" : "0*$") ]; then
                # new branch
                files=$(git diff --name-only master ${to_ref} )
        elif [ 0 -ne $(expr "${to_ref}" : "0*$") ]; then
                # delete brach, nothing to check
                continue;
        else
                # normal push
                files=$(git diff --name-only ${from_ref} ${to_ref})
        fi
...
done

The problem was, that on an delete the to_ref are zero and on an new branch the from_ref is zero, so code like "${from_ref} ${to_ref})" will fail in this case.

kovetskiy commented 4 years ago

@tuxmaster5000 thanks for letting me know! Happy to hear that you solved it. :+1:

I'm closing the issue, feel free to re-open in any case.