11<?php
22
3- use CzProject \GitPhp \Git ;
4- use CzProject \GitPhp \GitException ;
53use Pantheon \Terminus \Config \ConfigAwareTrait ;
64use Pantheon \Terminus \Helpers \CommandCoverageReport ;
75use Pantheon \Terminus \Terminus ;
@@ -208,124 +206,6 @@ public function setTerminus(Terminus $terminus): void
208206 $ this ->terminus = $ terminus ;
209207 }
210208
211- /**
212- * Generates a test commit.
213- *
214- * @throws \Pantheon\Terminus\Exceptions\TerminusException
215- * @throws GitException
216- */
217- public function generateTestCommit ()
218- {
219- $ this ->output ()->writeln ('Getting the Site Repo ' );
220- // get the git host and port from terminus
221- $ commandResponse = $ this ->getTerminus ()->execute (
222- '%s connection:info %s.dev --fields=git_host,git_port --format=json ' ,
223- [
224- $ this ->getProjectPath () . "/terminus.phar " ,
225- $ this ->getSiteName (),
226- ]
227- );
228-
229- // check if the command was successful
230- if ($ commandResponse [1 ] !== 0 ) {
231- $ this ->output ()->writeln ('Failed to retrieve git host and port ' );
232- exit (1 );
233- }
234-
235- // decode the json response
236- $ gitInfo = json_decode ($ commandResponse [0 ], true );
237- $ this ->output ()->writeln ('Retrieved git host and port ' . print_r ($ gitInfo , true ));
238-
239- // check if the git host and port were retrieved
240- if (!isset ($ gitInfo ['git_host ' ]) || !isset ($ gitInfo ['git_port ' ])) {
241- $ this ->output ()->writeln ('Failed to retrieve git host and port ' );
242- exit (1 );
243- }
244-
245- // Does the known_hosts file exist?
246- if (!file_exists (sprintf ("%s/.ssh/known_hosts " , getenv ("HOME " )))) {
247- // if not, create one
248- touch (sprintf ("%s/.ssh/known_hosts " , getenv ("HOME " )));
249- }
250-
251- // get the contents of the known_hosts file
252- $ knownHosts = file_get_contents (sprintf ("%s/.ssh/known_hosts " , getenv ("HOME " )));
253- // check if the git host is already in the known_hosts file
254- if (!str_contains ($ knownHosts , $ gitInfo ['git_host ' ])) {
255- // if not, add it
256- $ this ->output ()->writeln ('Adding the git host to known hosts file ' );
257- $ addGitHostToKnownHostsCommand = sprintf (
258- 'ssh-keyscan -p %d %s >> ~/.ssh/known_hosts ' ,
259- $ gitInfo ['git_port ' ],
260- $ gitInfo ['git_host ' ]
261- );
262- $ this ->output ()->writeln ($ addGitHostToKnownHostsCommand );
263- exec ($ addGitHostToKnownHostsCommand );
264- }
265-
266- // checkout the branch related to this test run
267- $ clonedPath = sprintf (
268- "%s/pantheon-local-copies/%s " ,
269- getenv ("HOME " ),
270- $ this ->getSiteName ()
271- );
272- if (is_dir ($ clonedPath )) {
273- // make sure you're working with a clean copy of the repo
274- exec ("rm -rf {$ clonedPath }" );
275- }
276- $ this ->output ()->writeln (sprintf ('Cloning the site repository to %s ' , $ clonedPath ));
277- // get the git host and port from terminus
278- $ commandResponse = $ this ->getTerminus ()->execute (
279- '%s local:clone %s ' ,
280- [
281- $ this ->getProjectPath () . "/terminus.phar " ,
282- $ this ->getSiteName (),
283- ]
284- );
285-
286- $ response = "" ;
287- try {
288- $ git = new Git ();
289- $ repo = $ git ->open ($ clonedPath );
290- chdir ($ clonedPath );
291- $ branches = $ repo ->getBranches ();
292- if (!in_array ($ this ->getSiteEnv (), $ branches )) {
293- $ this ->output ()->writeln (sprintf ('Creating the %s branch ' , $ this ->getSiteEnv ()));
294- // Create the branch
295- $ repo ->createBranch ($ this ->getSiteEnv ());
296- }
297- // Check out the branch in question
298- $ repo ->checkout ($ this ->getSiteEnv ());
299- // create a text file
300- $ testFilePath = sprintf ('%s/test.txt ' , $ clonedPath );
301- file_put_contents ($ testFilePath , 'test ' );
302- // add the file to the repository
303- $ repo ->addFile ("test.txt " );
304- // commit the file
305- $ repo ->commit ('Test commit ' );
306- // push the commit
307- $ response = $ repo ->execute (
308- 'push ' ,
309- 'origin ' ,
310- $ this ->getSiteEnv (),
311- );
312- } catch (GitException $ e ) {
313- $ this ->output ()->writeln (["Git Exception: " , $ e ->getMessage ()]);
314- $ this ->output ()->writeln (print_r ($ response , true ));
315- exit (1 );
316- } catch (Exception $ e ) {
317- $ this ->output ()->writeln ($ e ->getMessage ());
318- $ this ->output ()->writeln (print_r ($ response , true ));
319- exit (1 );
320- }
321-
322- // get the last commit
323- $ commit = $ repo ->getLastCommit ();
324- // output the commit id
325- $ this ->output ()->writeln ($ commit ->getId ());
326- return $ commit ->getId ();
327- }
328-
329209 /**
330210 * Returns the absolute path to the project.
331211 *
@@ -335,21 +215,4 @@ private function getProjectPath(): string
335215 {
336216 return dirname (__FILE__ );
337217 }
338-
339-
340- /**
341- * @return string
342- */
343- private function getSiteName (): string
344- {
345- return getenv ('TERMINUS_SITE ' ) ?? 'ci-terminus-composer ' ;
346- }
347-
348- /**
349- * @return string
350- */
351- private function getSiteEnv (): string
352- {
353- return getenv ('TERMINUS_ENV ' ) ?? 'dev ' ;
354- }
355218}
0 commit comments