【问题标题】:SVN Hook for Updating Packagist用于更新 Packagist 的 SVN 挂钩
【发布时间】:2015-04-22 08:18:49
【问题描述】:

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

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


更新

我设法通过下面的相关部分在 packagist 的 this page 上找到了我要查找的内容:

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

【问题讨论】:

  • 我认为您的 REPO_URL 结构有问题。错误提示“无法解析有效负载存储库 URL”,这可能意味着您的 composer.json 文件不在存储库的顶部。您的 REPO_URL 是指向 packagist.org 还是您的 svn 存储库 url?它应该指向你的 svn url。
  • 是的,问题似乎出在有效载荷 url 上。感谢您澄清它应该指向哪个 URL,我现在正在尝试这个(仍然无法正常工作)。也许packagist不支持通过钩子支持SVN协议,即使它手动支持?我需要用修订号或其他东西扩展网址吗?
  • 我刚刚挖掘了代码并找到了它所期望的 url 正则表达式,这似乎取消了 svn 存储库的资格,但支持 git(github.com/composer/packagist/blob/master/src/Packagist/… 中的 updatePackageAction 方法)。这是我从这个错误报告中得出的结论:packagist 的人不支持 SVN 的问题:github.com/composer/packagist/issues/151
  • 您可以通过 http/https 协议公开您的 svn 存储库。设置 apache svn 模块并不是很困难。
  • @Boris 是的,我必须回到那个话题。上次我尝试时卡在“webdav”上

标签: svn hook packagist


【解决方案1】:

你可以通过使用 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 文件中,并在将其放入生产存储库之前对其进行彻底测试。

【讨论】:

  • 感谢您的回答,它包含一些有用的信息,但通读脚本,我无法完全弄清楚发生了什么。我想也许我最初的问题令人困惑。您能否阅读我的进一步更新以解决我的问题?
  • 我的回答只是应该为您提供指导如何在存储库上执行提交后操作。您的脚本看起来应该可以正常工作,除了 REPO_URL 部分。如果您遇到问题,请告诉我,我会注册 packagist.org 网站并尝试调试它。
猜你喜欢
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-11
  • 2012-06-08
  • 2015-12-03
相关资源
最近更新 更多