-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsubsplit-worker.php
More file actions
98 lines (75 loc) · 3.43 KB
/
subsplit-worker.php
File metadata and controls
98 lines (75 loc) · 3.43 KB
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
98
<?php
require __DIR__.'/../vendor/autoload.php';
$configFilename = file_exists(__DIR__.'/../config.json')
? __DIR__.'/../config.json'
: __DIR__.'/../config.json.dist';
$config = json_decode(file_get_contents($configFilename), true);
$start = time();
$redis = new Predis\Client(array('read_write_timeout' => 0,));
while ($body = $redis->brpoplpush('dflydev-git-subsplit:incoming', 'dflydev-git-subsplit:processing', 0)) {
$data = json_decode($body, true);
$name = null;
$project = null;
$data['dflydev_git_subsplit'] = array(
'processed_at' => time(),
);
foreach ($config['projects'] as $testName => $testProject) {
if ($testProject['url'] === $data['repository']['url']) {
$name = $testName;
$project = $testProject;
break;
}
}
if (null === $name) {
print(sprintf('Skipping request for URL %s (not configured)', $data['repository']['url'])."\n");
$redis->lrem('dflydev-git-subsplit:processing', 1, $body);
$redis->lpush('dflydev-git-subspilt:failures', json_encode($data));
continue;
}
$data['dflydev_git_subsplit']['name'] = $name;
$data['dflydev_git_subsplit']['project'] = $project;
$ref = $data['ref'];
$publishCommand = array(
'git subsplit publish',
escapeshellarg(implode(' ', $project['splits'])),
);
if (preg_match('/refs\/tags\/(.+)$/', $ref, $matches)) {
$publishCommand[] = escapeshellarg('--rebuild-tags');
$publishCommand[] = escapeshellarg('--no-heads');
$publishCommand[] = escapeshellarg(sprintf('--tags=%s', $matches[1]));
} elseif (preg_match('/refs\/heads\/(.+)$/', $ref, $matches)) {
$publishCommand[] = escapeshellarg('--no-tags');
$publishCommand[] = escapeshellarg(sprintf('--heads=%s', $matches[1]));
} else {
print sprintf('Skipping request for URL %s (unexpected reference detected: %s)', $data['repository']['url'], $ref)."\n";
$redis->lrem('dflydev-git-subsplit:processing', 1, $body);
$redis->lpush('dflydev-git-subspilt:failures', json_encode($data));
continue;
}
$repositoryUrl = isset($project['repository-url'])
? $project['repository-url']
: $project['url'];
print sprintf('Processing subsplit for %s (%s)', $name, $ref)."\n";
$projectWorkingDirectory = $config['working-directory'].'/'.$name;
if (!file_exists($projectWorkingDirectory)) {
print sprintf('Creating working directory for project %s (%s)', $name, $projectWorkingDirectory)."\n";
mkdir($projectWorkingDirectory, 0750, true);
}
$command = implode(' && ', array(
sprintf('cd %s', escapeshellarg($projectWorkingDirectory)),
sprintf('( git subsplit init %s || true )', escapeshellarg($repositoryUrl)),
'git subsplit update',
implode(' ', $publishCommand)
));
passthru($command, $exitCode);
if (0 !== $exitCode) {
print sprintf('Command %s had a problem, exit code %s', $command, $exitCode)."\n";
$redis->lrem('dflydev-git-subsplit:processing', 1, $body);
$redis->lpush('dflydev-git-subspilt:failures', json_encode($data));
continue;
}
$redis->lrem('dflydev-git-subsplit:processing', 1, $body);
$redis->rpush('dflydev-git-subsplit:processed', json_encode($data));
}
$seconds = time() - $start;
throw new \RuntimeException(sprintf('Something strange happened after %s seconds', $seconds));