用于更新Packagist的SVN Hook

时间:2015-02-20 15:32:43

标签: svn hook packagist

我有一个包含多个人一起工作的PHP包的SVN存储库。我们希望将该软件包公之于众,因此我们已将其添加到packagist中,其中包含url svn://svn.xxxx.com/my-package

如何向存储库添加一个钩子,这样每当开发人员进行提交时,packagist会自动更新,类似于Github中提供的服务钩子?


更新

我设法在this page的包装上找到了我正在寻找的内容,其中包含以下相关部分:

enter image description here

我已经用PHP编写了一个脚本为我做这个(因为我对PHP比python更舒服),你需要安装PHP5-cli和php curl包,这可以在ubuntu上完成使用以下命令:

sudo apt-get install php5-cli php5-curl -y

提交后文件

#!/usr/bin/php

<?php

# Code for sending post request taken from:
# https://stackoverflow.com/questions/16920291/post-request-with-json-body

define('API_TOKEN', 'xxx');
define('USER', 'xxx');
define('REPO_URL', 'https://packagist.org/packages/xxx/xxx');
#define('REPO_URL', 'svn://svn.xxx.com/xxx');


$data = array(
    'repository' => array('url' => REPO_URL)
);

// Setup cURL
$url = 'https://packagist.org/api/update-package?username=' . USER . '&apiToken=' . API_TOKEN;
$ch = curl_init($url);

curl_setopt_array($ch, array(
    CURLOPT_POST            => TRUE,
    CURLOPT_RETURNTRANSFER  => TRUE,
    CURLOPT_HTTPHEADER      => array('Content-Type: application/json'),
    CURLOPT_POSTFIELDS      => json_encode($data)
));

// Send the request
$response = curl_exec($ch);

// Check for errors
if ($response === FALSE)
{
    die(curl_error($ch));
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Print the date from the response
var_dump($responseData);

创建后确保chmod +x文件。

当前问题

不幸的是,我遇到了以下响应错误消息:

array(2) {
  ["status"]=>
  string(5) "error"
  ["message"]=>
  string(38) "Could not parse payload repository URL"
}

当我发送var_dump我发送的数据时,它如下:

string(43) "{"repository":{"url":"xxx/table-creator"}}"

1 个答案:

答案 0 :(得分:0)

您可以使用svn post-commit hook来完成此操作。此代码非常接近您的要求。它是用python编写的,你可以轻松地用bash甚至php重写它。我假设你有标准的svn存储库结构与trunk,tags和branches目录。此外,您还必须更改包名称和目录路径。

#!/usr/bin/env python

import sys
import tempfile
import subprocess
import tarfile
import shutil

SVN = '/usr/bin/svn'
SVNLOOK = '/usr/bin/svnlook'
PACKAGE_NAME = 'my-package'

if __name__ == '__main__':
    repository = sys.argv[1]
    revision = sys.argv[2]

    tmpdir = tempfile.mkdtemp()
    exportdir = tmpdir + '/' + PACKAGE_NAME
    archive = tmpdir + '/' + PACKAGE_NAME + '.tar.gz'

    # Stop hook from running recursively
    cmd = SVNLOOK + ' dirs-changed -r ' + str(revision) + ' ' + repository
    output = subprocess.check_output(cmd, shell = True)
    if 'tags' in output:
        sys.exit(0)

    cmd = SVN + ' export ' + 'file://' + repository + '/trunk ' + exportdir
    output = subprocess.check_output(cmd, shell = True)
    if 'Exported' in output:
        tar = tarfile.open(archive, 'w:gz')
        tar.add(exportdir, arcname = PACKAGE_NAME)
        tar.close()

        # Copy archive to the final destination or add package back to your repository
        # Example 1:
        # shutil.copyfile(archive, '/path/to/new/location/file.tar.gz')

        # Example 2:
        cmd = SVN + ' rm file://' + repository + '/tags/' + PACKAGE_NAME + '.tar.gz -m \"Removed old package.\"'
        output = subprocess.check_output(cmd, shell = True)
        cmd = SVN + ' import ' +  archive + ' file://' + repository + '/tags/' + PACKAGE_NAME + '.tar.gz --force -m \"Imported new package.\"'
        output = subprocess.check_output(cmd, shell = True)

    shutil.rmtree(tmpdir)
    sys.exit(0)

确保将其添加到/ path / to / repository / hooks / post-commit文件中,请在将其放入生产存储库之前对其进行全面测试。