1

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

如何向存储库添加挂钩,以便每当开发人员提交时,packagist 会自动更新,类似于 Github 中可用的服务挂钩?


更新

我设法在下面的相关部分中找到了我在这个packagegist 页面上寻找的内容:

在此处输入图像描述

我已经用 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"}}"
4

1 回答 1

0

你可以通过使用 svn post-commit 钩子来做到这一点。此代码非常接近您的要求。它是用 python 编写的,你可以很容易地用 bash 甚至 php 重写它。我假设你有标准的 svn 存储库结构,包括主干、标签和分支目录。此外,您将不得不更改包名称和目录路径。

#!/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 文件中,并在将其放入生产存储库之前对其进行彻底测试。

于 2015-02-21T02:31:46.067 回答