Hooks are run before the repository is refreshed

Issue #791 on hold
Ton Plomp created an issue

To integrate with Mantis I have created a plugin in Mantis that requests the 'changeset-patch' page.

However this page is returning an error and I see the following in my Rhodecode log file:

2013-03-12 08:39:01.122 INFO  [rhodecode.lib.base] Access for IP:10.192.132.255 allowed
2013-03-12 08:39:01.168 INFO  [rhodecode.lib.middleware.simplehg] pull action on HG repo "test" by "default" from 10.192.132.255
2013-03-12 08:39:01.240 INFO  [rhodecode.lib.base] Access for IP:10.192.132.255 allowed
2013-03-12 08:39:01.283 INFO  [rhodecode.lib.middleware.simplehg] pull action on HG repo "test" by "default" from 10.192.132.255
2013-03-12 08:39:01.368 INFO  [rhodecode.lib.base] Access for IP:10.192.132.255 allowed
2013-03-12 08:39:01.410 INFO  [rhodecode.lib.middleware.simplehg] push action on HG repo "test" by "default" from 10.192.132.255
2013-03-12 08:39:01.707 INFO  [rhodecode.lib.utils] Logging action push:73e89b5cc3cd915147b530fbe0ace24a12433573 on test by <User('id:1:default')>
2013-03-12 08:39:02.034 INFO  [rhodecode.lib.base] IP: 127.0.0.1 User: <AuthUser('id:7:admin|None')> accessed /test/changeset-patch/73e89b5cc3cd915147b530fbe0ace24a12433573
2013-03-12 08:39:02.046 INFO  [rhodecode.lib.auth] user admin is authenticated and granted access to ChangesetController:__before__ using APIAuth
2013-03-12 08:39:02.216 ERROR [rhodecode.controllers.changeset] Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/RhodeCode-1.5.3-py2.6.egg/rhodecode/controllers/changeset.py", line 198, in index
    rev_ranges = [c.rhodecode_repo.get_changeset(revision)]
  File "/usr/local/lib/python2.6/dist-packages/RhodeCode-1.5.3-py2.6.egg/rhodecode/lib/vcs/backends/hg/repository.py", line 430, in get_changeset
    revision = self._get_revision(revision)
  File "/usr/local/lib/python2.6/dist-packages/RhodeCode-1.5.3-py2.6.egg/rhodecode/lib/vcs/backends/hg/repository.py", line 402, in _get_revision
    % (revision, self))
ChangesetDoesNotExistError: Revision u'73e89b5cc3cd915147b530fbe0ace24a12433573' does not exist for this repository <MercurialRepository at /mnt/vssdata/ETI/mercurial/test>

2013-03-12 08:39:02.264 INFO  [rhodecode.lib.base] IP: 127.0.0.1 Request to /test/changeset-patch/73e89b5cc3cd915147b530fbe0ace24a12433573 time: 0.286s
2013-03-12 08:39:02.329 INFO  [rhodecode.lib.base] IP: 127.0.0.1 User: <AuthUser('id:1:default|True')> accessed /
2013-03-12 08:39:02.329 INFO  [rhodecode.lib.auth] user default is authenticated and granted access to HomeController:__before__ using RegularAuth
2013-03-12 08:39:02.407 INFO  [rhodecode.lib.base] IP: 127.0.0.1 Request to / time: 0.134s

Adding a sleep in the hook script does not resolve the issue. On the client side I see the following:

pushing to http://ton@code.dekiwiki.net4c.net/test
searching for changes
remote: Repository size .hg:31.0 kB repo:0 B total:31.0 kB
remote: Last revision is now r65:05ef4efde6ba
remote: Updating Changeset to Mantis Bug Tracker. Repository: test
remote: Changeset: 05ef4efde6baffb4534a8af74e89eb3bf7db30ef
remote: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E

The last line is the error from Mantis.

Changing it to a changegroup hook does not resolve the issue.

Accessing the page after the push has finished does not produce an error.

Comments (9)

  1. Marcin Kuzminski repo owner

    are you running some custom code inside hooks ? I don't understand where that error happens ?

  2. Ton Plomp reporter

    The hook is calling a page on the Mantis server via wget. The Mantis server is requesting the changeset-patch page for the changeset. (/test/changeset-patch/73e89b5cc3cd915147b530fbe0ace24a12433573)

    However the changeset is not yet ready in Rhodecode.

    If i try to call the changeset-patch page from the hook I get an 302 page:

    Hook-Code:

    PROJECT=${PWD##*/}
    echo "Mercurial project: "$PROJECT
    echo "Mercurial node: "${HG_NODE}
    CURL=/usr/bin/curl
    echo "Updating Changeset to Mantis Bug Tracker"
    RC_apikey="XXXXXXXXXXXXXXXXXXXXX"
    URL="http://rhodecode/"$PROJECT"/changeset-patch/"${HG_NODE}"?api_key="$RC_apikey
    echo ${URL}
    ${CURL} ${URL}
    

    This returns to the client:

    d:\local\code\TSTS>hg commit -m "commit" 
    
    d:\local\code\TSTS>hg push 
    pushing to http://ton:***@RHODECODE/tsts
    searching for changes
    remote: Repository size .hg:33.9 MB repo:1.6 MB total:35.5 MB
    remote: Last revision is now r283:88d6e206b562
    remote: Mercurial project: tsts
    remote: Mercurial node: 88d6e206b562696891c7b3d0fcd4f1fb3e6327d9
    remote: Updating Changeset to Mantis Bug Tracker
    remote: http://RHODECODE/tsts/changeset-patch/88d6e206b562696891c7b3d0fcd4f1fb3e6327d9?api_key=XXXXXXXXXXXX
    remote:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    remote:                                  Dload  Upload   Total   Spent    Left  Speed
    remote: 
    remote:   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
    remote: 100   229  100   229    0     0    605      0 --:--:-- --:--:-- --:--:--   607
    remote: <html>
    remote:  <head>
    remote:   <title>302 Found</title>
    remote:  </head>
    remote:  <body>
    remote:   <h1>302 Found</h1>
    remote:   The resource was found at <a href="http://RHODECODE/">http://RHODECODE/</a>;
    remote: you should be redirected automatically.
    remote: 
    remote: 
    remote:  </body>
    remote: </html>
    
  3. Marcin Kuzminski repo owner

    I propose you move that commit hook code into rcextensions push hook. I have some code that does the same thing and it works there for me.

    Marcin Kuzminski

  4. Ton Plomp reporter

    I am stuck in trying to create a RhodeCode Extension. Here's what I have :

    import requests
    from rhodecode.lib.compat import json
    BASE_URL="xxxxxxxxxx/plugin.php?page=Source/checkin.php"
    MANTIS_API_KEY="xx"
    #==============================================================================
    # POST PUSH HOOK
    #==============================================================================
    
    # this function will be executed after each push it's runned after the build-in
    # hook that rhodecode uses for logging pushes
    def _pushhook(*args, **kwargs):
        """
        Post push hook
        kwargs available:
    
          :param username: name of user who pushed
          :param ip: ip of who pushed
          :param action: pull
          :param repository: repository name
          :param pushed_revs: generator of pushed revisions
        """
        from rhodecode.model.db import Repository
        from rhodecode.lib import helpers as h
        changesets = []
    
        for r in kwargs['pushed_revs']:
            cs = repo.scm_instance.get_changeset(r)
            changesets.append(cs)
        for cs in changesets:
            params = urllib.urlencode({'repo_name': repository, 'data': cs.raw_id})
            headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
            conn = httplib.HTTPConnection(BASE_URL)
            conn.request("POST", "/cgi-bin/query", params, headers)
            response = conn.getresponse()
            sys.stdout.write( response.status, response.reason)
            data = response.read()
            conn.close()
            sys.stdout.write(data)
    
    return 0
    
    PUSH_HOOK = _pushhook
    

    I have placed this in the /usr/local/lib/python2.7/dist-packages/RhodeCode-1.5.4-py2.7.egg/rcextensions/init.py file, but I have a feeling it is not running correctly.

  5. Marcin Kuzminski repo owner

    1) rcextensions should be placed where the .ini file is, if in doubt use paster make-rcext 2) you can use repo.scm_instance_no_cache that always has latest data (it doesn't use cache)

    Let me know if it works, simplest way to test it is putting a print in one of the functions and watching if hg push prints it out

  6. Ton Plomp reporter

    Sorry, however I moved to Redmine instead of Mantis, Redmine doesn't use the web interface but the repository itself.

  7. thirumalaik

    I'm facing this issue too frequently in Mantis connector with Rhodecode. How can I fix this? So currently I'm manually importing changeset.

  8. Log in to comment