This repository was archived by the owner on Dec 31, 2020. It is now read-only.
forked from dd32/Github-to-WordPress-Plugins-Sync
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpush.php
More file actions
238 lines (194 loc) · 6.96 KB
/
push.php
File metadata and controls
238 lines (194 loc) · 6.96 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* Sync a Github Repository to WordPress.org Plugins SVN
* Relies upon the Github 'push' webhook variables.
*/
class Github_to_Worg_SVN {
private $config = array(
'svns_location' => '',
'repos' => array(),
);
private $repo = array(
'secret' => '',
'svn_url' => '',
'username' => '',
'password' => '',
'github_svn' => '',
'checkout_dir' => '',
);
// Used for signature validation
private $post_data = '';
function __construct() {
$this->populate_config();
$this->populate_post_vars();
$this->log_request();
$this->handle_request();
}
function populate_config() {
if ( file_exists( __DIR__ . '/config.php' ) ) {
include __DIR__ . '/config.php';
}
$this->config = get_defined_vars();
// Defaults
if ( empty( $this->config['svns_location'] ) ) {
$this->config['svns_location'] = __DIR__ . '/svns/';
}
}
function populate_post_vars() {
if ( 'application/json' === $_SERVER['CONTENT_TYPE'] || 'application/json' === $_SERVER['HTTP_CONTENT_TYPE'] ) {
$_POST = @json_decode( file_get_contents('php://input'), true );
} else {
// Assuming Magic Quotes disabled like a good host.
$_POST = @json_decode( $_POST['payload'], true );
}
}
function setup_repo_config( $github_repo ) {
if ( ! $github_repo || ! isset( $this->config['repos'][ $github_repo ] ) ) {
return false;
}
$this->repo = &$this->config['repos'][ $github_repo ];
$this->repo['github_svn'] = $_POST['repository']['svn_url'];
$this->repo['checkout_dir'] = $this->config['svns_location'] . basename( $this->repo['svn_url'] ) . '-' . sha1( $this->repo['svn_url'] );
return true;
}
function handle_request() {
$github_repo = $_POST['repository']['full_name'];
if ( ! $this->setup_repo_config( $github_repo ) ) {
header( 'HTTP/1.1 400 Repo Not Configured.', true, 400 );
die( 'Repo Not Configured.' );
}
if ( ! $this->verify_github_signature() ) {
header( 'HTTP/1.1 400 Not Github.', true, 404 );
die( 'Not Github.' );
}
$this->initialize_svn_repo();
$this->overlay_github();
// \-> calls $this->perform_text_replacements();
$this->check_in_changes( $this->generate_commit_message() );
}
function verify_github_signature() {
if ( empty( $this->repo['secret'] ) ) {
return true;
}
if ( empty( $_SERVER['HTTP_X_HUB_SIGNATURE'] ) ) {
return false;
}
list( $algo, $hash ) = explode( '=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2 );
$hmac = hash_hmac( $algo, file_get_contents('php://input' ), $this->repo['secret'] );
return $hash === $hmac;
}
function initialize_svn_repo( $rev = 'HEAD' ) {
if ( ! is_dir( $this->repo['checkout_dir'] ) ) {
$this->exec( "svn co --non-interactive --force -r {rev} {svn_url} {checkout_dir}", compact( 'rev' ) );
}
$this->exec( "svn up --non-interactive --force -r {rev} {checkout_dir}", compact( 'rev' ) );
}
function overlay_github( $rev = 'HEAD' ) {
$export_output = $this->exec( "svn export --non-interactive --force -r {rev} {github_svn} {checkout_dir}", compact( 'rev' ) );
$github_svn_rev_exported = $rev;
if ( preg_match( '!Exported revision (\d+)\.!i', $export_output, $svn_rev_match ) ) {
$github_svn_rev_exported = $svn_rev_match[1];
}
// Move branches/assets to /assets
$this->exec( "rm -rf {checkout_dir:/assets}" );
$this->exec( "mv {checkout_dir:/trunk/assets} {checkout_dir}" );
// And remove them from tag/branch builds (Github will have it, but that's okay)
$this->exec( "rm -rf {checkout_dir:/}{branches,tags}/*/assets" );
// Add all files
$this->exec( "svn add --non-interactive --force {checkout_dir:/}*" );
// Handle all replacements
$this->perform_text_replacements( $github_svn_rev_exported );
}
function perform_text_replacements( $rev ) {
$replacements = array(
'%GITHUB_MERGE_DATE%' => gmdate( 'Y-m-d' ),
'%GITHUB_MERGE_DATETIME%' => gmdate( 'Y-m-d h:i:s' ),
'%GITHUB_MERGE_SVN_REV%' => $rev
);
foreach ( $replacements as $token => $value ) {
$this->exec(
"grep -rli '%GITHUB_' --exclude-dir='.svn' {checkout_dir} | xargs -i@ sed -i 's/{token}/{value}/g' @",
compact( 'token', 'value' ),
true,
array( 'token' => true, 'value' => true )
);
}
}
function check_in_changes( $message ) {
// Display a diff
$this->exec( "svn diff --non-interactive --no-diff-deleted {checkout_dir}" );
$this->exec( "svn ci --non-interactive --username {username} --password {password} -m {message} {checkout_dir}", compact( 'message' ) );
}
function generate_commit_message() {
$commit_message = '';
foreach ( (array)$_POST['commits'] as $i => $commit ) {
if ( $i > 0 ) {
$commit_message .= "\n-----\n";
}
$commit_message .= "{$commit['message']}\n";
if ( $commit['author']['username'] == $commit['committer']['username'] ) {
$commit_message .= "Author: {$commit['author']['username']} @ {$commit['timestamp']}\n";
} else {
$commit_message .= "Author: {$commit['author']['username']} ";
$commit_message .= "Commited by: {$commit['committer']['username']} @ {$commit['timestamp']}\n";
}
$commit_message .= "{$commit['url']}";
}
$commit_message .= "\n\nMerged from Github: {$_POST['compare']}";
return $commit_message;
}
/* Log the incoming request if enabled */
function log_request() {
if ( ! $this->config['save_log'] ) {
return;
}
file_put_contents(
$this->config['save_log'],
json_encode($_POST) . "\n\n",
FILE_APPEND
);
}
/**
* Execute a Shell command, with helpful placeholders and suffixing.
* The placeholder can exist in $args, $repo_config, or $config itself.
* The placeholders will be escaped, unless they appear in $pre_escaped.
* Placeholders can suffix: {variable:/trunk/} will expand to '{variable}/trunk/' when escaped
*
* Example: exec( "svn up --username {username} {checkout_dir:/trunk}", array( 'checkout_dir' => __DIR__ ) );
* resulting in: "svn up --username 'alfred' '/home/....../trunk'"
*/
function exec( $command, $args = array(), $echo = true, $pre_escaped = array() ) {
$config = $this->config;
$repo = $this->repo;
$command = preg_replace_callback( '!{([^}: ]+)(:([^} ]+))?}!i', function( $match ) use ( $args, $config, $repo, $pre_escaped ) {
$placeholder = $match[1];
$suffix = ! empty( $match[3] ) ? $match[3] : '';
foreach ( array( 'args', 'repo' ) as $blob ) {
if ( isset( ${$blob}[ $placeholder ] ) ) {
$replacement = ${$blob}[ $placeholder ];
// Special case to keep the password out of the CLI arg
if ( 'password' == $placeholder ) {
putenv( "PHP_SVN_PASSWORD={$replacement}{$suffix}" );
return '$PHP_SVN_PASSWORD';
}
if ( !empty( $pre_escaped[ $placeholder ] ) ) {
return $replacement . $suffix;
} else {
return escapeshellarg( $replacement . $suffix );
}
}
}
// Return the input
return $match[0];
}, $command );
$command .= ' 2>&1';
$output = shell_exec( $command );
// Cleanup
putenv( "PHP_SVN_PASSWORD=" );
if ( $echo ) {
echo "Command: {$command}\n{$output}\n";
}
return $output;
}
}
new Github_to_Worg_SVN();