reconquest / atlassian-external-hooks

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

Compatibility with Stash 3.2.0 #10

Closed linjer closed 10 years ago

linjer commented 10 years ago

After upgrading to Stash 3.2.0, my post-receive hooks stopped working. It looks like it is related to the reorganization the Stash home directory.

From the atlassian-stash.log file, I found this:

java.io.IOException: Cannot run program "/var/atlassian/application-data/stash/stash_slack.sh" (in directory "/var/atlassian/application-data/stash/data/repositories/27"): error=2, No such file or directory
t java.lang.ProcessBuilder.start(ProcessBuilder.java:1047) ~[na:1.7.0_65]
    at com.ngs.stash.externalhooks.hook.ExternalPreReceiveHook.onReceive(ExternalPreReceiveHook.java:59) ~[plugin.6579998661888744121.external-hooks-1.3-8_1401424832000.jar:na]
    at com.ngs.stash.externalhooks.hook.ExternalPostReceiveHook.postReceive(ExternalPostReceiveHook.java:36) [plugin.6579998661888744121.external-hooks-1.3-8_1401424832000.jar:na]
    at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter$1.visit(AsyncPostReceiveRepositoryHookAdapter.java:49) [stash-service-impl-3.2.0.jar:na]
    at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter$1.visit(AsyncPostReceiveRepositoryHookAdapter.java:45) [stash-service-impl-3.2.0.jar:na]
    at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService$RepositoryHookPagedTransactionCallback.doInTransaction(DefaultRepositoryHookService.java:616) [stash-service-impl-3.2.0.jar:na]
    at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService$RepositoryHookPagedTransactionCallback.doInTransaction(DefaultRepositoryHookService.java:592) [stash-service-impl-3.2.0.jar:na]
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) [spring-tx-4.0.5.RELEASE.jar:4.0.5.RELEASE]
    at com.atlassian.stash.internal.hook.repository.DefaultRepositoryHookService.visitEnabledHooks(DefaultRepositoryHookService.java:304) [stash-service-impl-3.2.0.jar:na]
    at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter.postReceive(AsyncPostReceiveRepositoryHookAdapter.java:45) [stash-service-impl-3.2.0.jar:na]
    at com.atlassian.stash.internal.hook.repository.AsyncPostReceiveRepositoryHookAdapter.onRefsChangedEvent(AsyncPostReceiveRepositoryHookAdapter.java:40) [stash-service-impl-3.2.0.jar:na]
    at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:36) [atlassian-event-2.3.5.jar:na]
    at com.atlassian.stash.internal.event.AsyncBatchingInvokersTransformer$AsyncInvokerBatch.invoke(AsyncBatchingInvokersTransformer.java:100) [stash-platform-3.2.0.jar:na]
    at com.atlassian.event.internal.AsynchronousAbleEventDispatcher$1$1.run(AsynchronousAbleEventDispatcher.java:48) [atlassian-event-2.3.5.jar:na]
    at com.atlassian.sal.core.executor.ThreadLocalDelegateRunnable.run(ThreadLocalDelegateRunnable.java:38) [sal-core-2.12.1.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_65]
    at java.lang.Thread.run(Thread.java:745) [na:1.7.0_65]
    ... 31 frames trimmed
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method) ~[na:1.7.0_65]
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:186) ~[na:1.7.0_65]
    at java.lang.ProcessImpl.start(ProcessImpl.java:130) ~[na:1.7.0_65]
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ~[na:1.7.0_65]
    ... 17 common frames omitted

The directory /var/atlassian/application-data/stash/data no longer exists under Stash 3.2.0. It seems to have moved to /var/atlassian/application-data/stash/shared/data.

seletskiy commented 10 years ago

Thanks, will fix soon!

seletskiy commented 10 years ago

It's fixed in 1.3-9 version, that is available on marketplace. Thanks for reporting!

linjer commented 10 years ago

Awesome sir! Thanks!

linjer commented 10 years ago

I just got around to testing this out and it looks like something is still wrong for me. The script I reference now executes, but incorrectly. I think it may not be executing in the same working path as before?

The git log -format="%H %h \"%an\" \"%s\" \"%ar\"" $from_ref..$to_ref command returns fatal: Not a git repository (or any of the parent directories): .git.

pwd from inside the script shows /var/atlassian/application-data/stash as opposed to the expected respective repository.

This may be a bit of a messy read, but here is my script:

#!/bin/bash
# Notify commits to slack

# Set Slack channel, passed as 2nd argument
channel=$2

# Get push information from Git STDIN
read from_ref to_ref ref_name
# Generate commit list
pushed_commits=`git log --format="%H %h \"%an\" \"%s\" \"%ar\"" $from_ref..$to_ref`
commit_count=`echo "$pushed_commits" | wc -l`

text="[<$3|${STASH_REPO_NAME}>] ${STASH_USER_NAME} <$STASH_USER_EMAIL> pushed $commit_count commit"
if [ $commit_count -gt 1 ]; then text="${text}s"; fi
text="${text} to ${ref_name#*/}"

payload="payload={\"channel\":\"$channel\",\"text\":\"$text\",\"attachments\":[{\"fallback\":\"Table of commits in this push.\",\"color\":\"good\",\"fields\":["

# Indexes
# [0] Full hash
# [1] Abbrev hash
# [2] Author name
# [3] Commit message
# [4] Author relative date
comm_desc_col="{\"title\":\"Commit\",\"short\":true,\"value\":\""
author_time_col="{\"title\":\"Author\",\"short\":true,\"value\":\""
while read -r line; do
    eval array=($line)
    commit_link="<$3/commits/${array[0]}|${array[1]}>"
    comm_desc_col="${comm_desc_col}${commit_link}: ${array[3]}\n"
    author_time_col="${author_time_col}${array[2]}, ${array[4]}\n"
done <<< "$pushed_commits"

comm_desc_col="${comm_desc_col}\"},"
author_time_col="${author_time_col}\"}"

payload="${payload}${comm_desc_col}${author_time_col}]}]}"
echo "$payload"
curl -X POST --data-urlencode "$payload" $1
seletskiy commented 10 years ago

Hmm, this is really strange.

I've just tested 1.3-9 version on the Stash 3.2.0 and part of your script that get git log works just fine.

Try following:

  1. As a Pre Receive Hook specify Executable as "/bin/sh". Positional parameters:
-c
pwd > /tmp/pre_receive
  1. As a Post Receive Hook: "/bin/sh". Params:
-c
pwd > /tmp/post_receive

And take a look what's appears in the /tmp/pre|post_receive

linjer commented 10 years ago

Hey,

I do get the correct path when I execute hook as you have described. Perhaps it has to do with the actual location of my script? In the script, calling pwd yields /var/atlassian/application-data/stash/ which is where the script resides. Any idea?

seletskiy commented 10 years ago

Do you invoke your script directly from the hook? Or from another script?

Also, check out your .bashrc and friend files.

Try to create script with following contents:

#!/bin/sh

pwd >> /tmp/hook_repsonse

Check out /tmp/hook_response and then replace /bin/sh to /bin/bash and try again.

BTW, there is logical error in your script. Script will read only first line from stdin, so if user pushes two or more refs to the repo at once you'll get notification only about first ref.

linjer commented 10 years ago

I just did the tests and indeed its working fine! I messed around with changing the owner of the file, since stash 3.2 runs under a different user now, and it shows the correct working directory from there as well. Sorry for the trouble.

It's still not working, however. The git log... command seems to just return a blank line. Did you get the entire script to run?

And thanks for pointing that out. I didn't understand why you put the refs in a loop in your example. That would explain it. :)

seletskiy commented 10 years ago

Yep, your script works fine both on the 3.1.3 and 3.2.0 versions of Stash using latest version of plugin (1.3-10).

I see following output when run your script (without curl line) as pre-receive hook:

Password for 'http://admin@atla.s:7990': 
Counting objects: 27, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (20/20), done.
Writing objects: 100% (27/27), 1.96 KiB | 0 bytes/s, done.
Total 27 (delta 13), reused 0 (delta 0)
remote: payload={"channel":"","text":"[<|rep_1>] admin <admin@example.com> pushed 13 commits to heads/master","attachments":[{"fallback":"Table of commits in this push.","color":"good","fields":[{"title":"Commit","short":true,"value":"</commits/660ce3b57a25594a8fd9242862bedf92d4aaeb3c|660ce3b>: a\n</commits/b0d60e71620f73d1baf22e9ebd8eaf561419c358|b0d60e7>: a\n</commits/b4b22195e27ef86dd64bc03caf410fb1e105aee6|b4b2219>: a\n</commits/4b2c887029790e4e9b5279e4e847448be0454050|4b2c887>: a\n</commits/53a90332de97e0d0eb5b6cae201dd96fa1220707|53a9033>: a\n</commits/ae88f3e0f97a1c4bd102abae531b0a3d5d250a5e|ae88f3e>: a\n</commits/ebdf6b24a059121ab946e4d5f4340c92f3069cf8|ebdf6b2>: a\n</commits/3657ebd47bfe58ad5399f634c4d90477151cf42c|3657ebd>: a\n</commits/4489372aa88f4a51dd784b1fe60267558d487d2c|4489372>: a\n</commits/eac52285c5cf2fa47a5cdf62d97284558693e349|eac5228>: a\n</commits/9fc45aedd3dd55a3cfc0aaa4864c05bb65a99483|9fc45ae>: a\n</commits/20791f8b445822c5b08a0b4bd325eb68c8489e51|20791f8>: a\n</commits/12a24af9645641f4d63bedf0bb4a1ea87c484b62|12a24af>: a\n"},{"title":"Author","short":true,"value":"Stanislav Seletskiy, 43 seconds ago\nStanislav Seletskiy, 12 minutes ago\nStanislav Seletskiy, 25 minutes ago\nStanislav Seletskiy, 4 hours ago\nStanislav Seletskiy, 4 hours ago\nStanislav Seletskiy, 25 hours ago\nStanislav Seletskiy, 26 hours ago\nStanislav Seletskiy, 26 hours ago\nStanislav Seletskiy, 26 hours ago\nStanislav Seletskiy, 26 hours ago\nStanislav Seletskiy, 26 hours ago\nStanislav Seletskiy, 2 days ago\nStanislav Seletskiy, 3 days ago\n"}]}]}
To http://admin@atla.s:7990/stash/scm/project_1/rep_1.git
   0a943a2..660ce3b  master -> master

I see same in log file when I set up script as a Post Receive Hook.

linjer commented 10 years ago

Thanks for working on this with me. It seems clearly on my end. I'll try to figure it out on my own. Thanks again.

seletskiy commented 10 years ago

No problem. Let me know if you find something.

linjer commented 10 years ago

Finally got back to this and figured it out... during my upgrade to Stash 3.2, I also had updated the git version on my CentOS server. Since the yum package is old, I compiled from source git 2.0 and installed it to /usr/local/bin . I had added this to the path in the /etc/bashrc file but it seems the shell external hook does not source it.

I just added the path to my script and all is well again. I also fixed the single ref issue as you pointed out.

Thanks!

seletskiy commented 10 years ago

Glad to see problem's resolved.