-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpost-receive-webhook-worker.php
executable file
·97 lines (80 loc) · 2.61 KB
/
post-receive-webhook-worker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/php
<?php
/*
* PROJECT: ReactOS GitHub Web Hook
* LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
* PURPOSE: Worker process to update the mirror repository and call the post-receive hook just like Git would do if the commit was pushed to the mirror repository.
* COPYRIGHT: Copyright 2017-2018 Colin Finck ([email protected])
*/
// "post-receive-webhook.php" has passed some arguments.
if ($argc < 4)
die("Parameters missing");
$repo = $argv[1];
$repopath = $argv[2];
$event = $argv[3];
// The following code should not be run concurrently.
// Therefore, make sure that no previous instance of this script is still running.
$lockfile = "/var/log/post-receive-webhook/{$repo}/post-receive-webhook-lock";
$lock_fp = fopen($lockfile, "c+");
if (!$lock_fp)
die("Could not create the lockfile $lockfile");
if (!flock($lock_fp, LOCK_EX))
die("Could not lock the lockfile $lockfile");
///// BEGIN OF CRITICAL SECTION /////
// Clear the mirror logfile.
$logfile = "/var/log/post-receive-webhook/{$repo}/git-remote-update.log";
file_put_contents($logfile, "");
// Update the mirror repository.
chdir($repopath);
$exit_code = 0;
for ($i = 0; $i < 5; $i++)
{
// Write into the logfile about this attempt.
$fp = fopen($logfile, "a");
if ($exit_code != 0)
fwrite($fp, "git remote update exited with code {$exit_code}\n\n");
fwrite($fp, "================ ATTEMPT {$i} ================\n");
fclose($fp);
// Run "git remote update".
$pp = popen("git remote update 1>> {$logfile} 2>&1", "w");
$exit_code = pclose($pp);
if ($exit_code == 0)
break;
// Wait 3 seconds before trying it another time.
sleep(3);
}
// Load the queue.
$queuefile = "/var/log/post-receive-webhook/{$repo}/queue";
$queue = unserialize(file_get_contents($queuefile));
if (!is_array($queue))
$queue = array();
// If this is a push event, we want to enqueue information for the post-receive hook.
if ($event == "push")
{
$payload_before = $argv[4];
$payload_after = $argv[5];
$payload_ref = $argv[6];
$queue[] = array($payload_before, $payload_after, $payload_ref);
}
// Check if "git remote update" above has succeeded.
if ($i < 5)
{
// Call the post-receive hook for all queued updates.
foreach ($queue as $u)
{
$pp = popen("hooks/post-receive 1> /dev/null 2>&1", "w");
fwrite($pp, $u[0] . " " . $u[1] . " " . $u[2] . "\n");
pclose($pp);
}
// All done.
$queue = array();
echo "OK";
}
else
{
echo "git remote update failed";
}
file_put_contents($queuefile, serialize($queue));
///// END OF CRITICAL SECTION /////
flock($lock_fp, LOCK_UN);
fclose($lock_fp);