def test_sync_repo_clone(self, makedirs, isdir, _call_git, _call_hook):
     isdir.return_value = False
     git = Git("/tmp", self.payload)
     git.sync_repo()
     makedirs.assert_called_once_with("/tmp/foo/bar.git", 0o755)
     self.assertEqual(isdir.call_args_list, [mock.call("/tmp/foo/bar.git/objects"), mock.call("/tmp/foo/bar.git")])
     _call_git.assert_called_once_with(["clone", "--bare", "https://github.com/foo/bar", "."])
     _call_hook.assert_called_once_with()
 def test_call_hook(self, access, isfile, check_output):
     access.return_value = True
     isfile.return_value = True
     git = Git("/tmp", self.payload, git_cmd="foo")
     git._call_hook()
     access.assert_called_once_with("/tmp/foo/bar.git/hooks/github-repo-mirror", os.X_OK)
     isfile.assert_called_once_with("/tmp/foo/bar.git/hooks/github-repo-mirror")
     check_output.assert_called_once_with(
         "/tmp/foo/bar.git/hooks/github-repo-mirror", shell=True, cwd="/tmp/foo/bar.git", stderr=-2
     )
 def test_sync_repo_fetch(self, makedirs, isdir, _call_git, _call_hook):
     isdir.return_value = True
     git = Git("/tmp", self.payload)
     git.sync_repo()
     self.assertFalse(makedirs.called)
     isdir.assert_called_once_with("/tmp/foo/bar.git/objects")
     self.assertEqual(
         _call_git.call_args_list,
         [
             mock.call(["remote", "set-url", "origin", "https://github.com/foo/bar"]),
             mock.call(["fetch", "origin", "+refs/heads/*:refs/heads/*"]),
             mock.call(["fetch", "origin", "+refs/tags/*:refs/tags/*"]),
         ],
     )
     _call_hook.assert_called_once_with()
    def test_sync_repo_fetch_without_origin(self, makedirs, isdir, _call_git, _call_hook):
        def _call_git_side_effect(args):
            if "set-url" in args:
                raise RuntimeError("bola")
            return ""

        _call_git.side_effect = _call_git_side_effect
        isdir.return_value = True
        git = Git("/tmp", self.payload)
        git.sync_repo()
        self.assertFalse(makedirs.called)
        isdir.assert_called_once_with("/tmp/foo/bar.git/objects")
        self.assertEqual(
            _call_git.call_args_list,
            [
                mock.call(["remote", "set-url", "origin", "https://github.com/foo/bar"]),
                mock.call(["remote", "add", "--mirror=fetch", "origin", "https://github.com/foo/bar"]),
                mock.call(["fetch", "origin", "+refs/heads/*:refs/heads/*"]),
                mock.call(["fetch", "origin", "+refs/tags/*:refs/tags/*"]),
            ],
        )
        _call_hook.assert_called_once_with()
def github():
    if not request_allowed(request.remote_addr,
                           app.config['GITHUB_PUBLIC_NETWORK'],
                           app.debug):
        abort(403)
    if request.authorization is None:
        return authenticate()
    if request.authorization.username != app.config['GITHUB_HOOK_USERNAME'] or \
       request.authorization.password != app.config['GITHUB_HOOK_PASSWORD']:
        return authenticate()
    if 'payload' not in request.form:
        abort(400)
    try:
        payload = GithubPayload(request.form['payload'])
        git = Git(app.config['GITHUB_REPO_ROOT'], payload)
        app.logger.info('Syncing repo: %s\n%s' %
                        (payload.repository_path,
                         git.sync_repo(app.config['GITHUB_AUTH_USERNAME'],
                                       app.config['GITHUB_AUTH_PASSWORD'])))
        return 'Ok\n'
    except Exception as err:
        app.logger.error('%s: %s\n%s' % (err.__class__.__name__, str(err),
                                         traceback.format_exc()))
        return 'Fail\n', 500
 def test_call_git(self, check_output):
     git = Git("/tmp", self.payload, git_cmd="foo")
     git._call_git(["yay"])
     check_output.assert_called_once_with(["foo", "yay"], cwd="/tmp/foo/bar.git", stderr=subprocess.STDOUT)