使用代码仓库托管平台的WebHook来自动推送Packagist更新

技巧 Mar 25, 2016

貌似Git@OSC并没有Packagist的服务,它的WebHook推动的JSON的格式也不符合Packagist的API要求的格式。这就难办了,没法直接用它的WebHook来进行更新,每次推送上那些仓库托管平台还得去Packagist点一下”Update”。只好使用自己的服务器进行个中转,于是我就自己写了个简单的脚本。

公用API

我把它放上我的API那了,自己没有服务器的话可以直接用我的。
https://api.ixnet.work/update-packagist.php?username=<你的用户名>&apiToken=<你的apiToken>&packageURL=<你仓库的地址>

示例:
https://api.ixnet.work/update-packagist.php?username=fsgmhoward&apiToken=abcdefghijklmn&packageURL=https://git.oschina.net/fsgmhoward/IXNetwork-PHPLib.git

实际上这个脚本并不检查POST的内容,只要访问这个地址就能更新你的Packagist的repo。所以在任何的支持WebHook的代码托管平台新建一个Push和Tag Push的钩子,地址按照上面地址的格式填即可。

源码

如果你想放到你自己的服务器那也没所谓,这里有源码:

&lt;?php
// Released under MIT
// Author: Howard Liu &lt;[email protected]&gt;

if(!isset($_GET[&#039;username&#039;]) || !isset($_GET[&#039;apiToken&#039;]) || !isset($_GET[&#039;packageURL&#039;])) {
    header(&quot;HTTP/1.1 405 Method Not Allowed&quot;);
    exit(&quot;405 Error: Method Not Allowed&quot;);
}
$data = &#039;{&quot;repository&quot;:{&quot;url&quot;:&quot;&#039;.$_GET[&#039;packageURL&#039;].&#039;&quot;}}&#039;;
$ch = curl_init(&#039;https://packagist.org/api/update-package?username=&#039;.$_GET[&#039;username&#039;].&#039;&amp;apiToken=&#039;.$_GET[&#039;apiToken&#039;]);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &quot;POST&quot;);                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    &#039;Content-Type: application/json&#039;,                                                                                
    &#039;Content-Length: &#039; . strlen($data))                                                                       
);

exit(curl_exec($ch));

标签

Howard Liu

在读大学生,平常使用战争雷霆提高血压和听网课使自己更快入睡。