11package com .lambdatest .jenkins .freestyle ;
22
3+ import java .io .BufferedReader ;
4+ import java .io .FileReader ;
35import java .io .IOException ;
46import java .io .Serializable ;
57import java .lang .reflect .Field ;
2527import com .lambdatest .jenkins .freestyle .api .Constant ;
2628import com .lambdatest .jenkins .freestyle .api .auth .UserAuthResponse ;
2729import com .lambdatest .jenkins .freestyle .api .service .CapabilityService ;
30+ import com .lambdatest .jenkins .freestyle .api .service .AppAutomationCapabilityService ;
2831import com .lambdatest .jenkins .freestyle .data .LocalTunnel ;
2932import com .lambdatest .jenkins .freestyle .service .LambdaTunnelService ;
3033import com .lambdatest .jenkins .freestyle .service .LambdaWebSocketTunnelService ;
4346public class MagicPlugBuildWrapper extends BuildWrapper implements Serializable {
4447
4548 private List <JSONObject > seleniumCapabilityRequest ;
49+ private List <JSONObject > appAutomationCapabilityRequest ;
50+ private String appId ;
4651 private String credentialsId ;
4752 private String username ;
4853 private Secret accessToken ;
@@ -65,7 +70,7 @@ public class MagicPlugBuildWrapper extends BuildWrapper implements Serializable
6570 private static final Logger logger = Logger .getLogger (MagicPlugBuildWrapper .class .getName ());
6671
6772 @ DataBoundConstructor
68- public MagicPlugBuildWrapper (StaplerRequest req , @ CheckForNull List <JSONObject > seleniumCapabilityRequest ,
73+ public MagicPlugBuildWrapper (StaplerRequest req , @ CheckForNull List <JSONObject > seleniumCapabilityRequest , @ CheckForNull List < JSONObject > appAutomationCapabilityRequest ,
6974 @ CheckForNull String credentialsId , String choice , boolean useLocalTunnel , LocalTunnel localTunnel ,
7075 ItemGroup context ) throws Exception {
7176 try {
@@ -77,6 +82,13 @@ public MagicPlugBuildWrapper(StaplerRequest req, @CheckForNull List<JSONObject>
7782 validateTestInput (seleniumCapabilityRequest );
7883 this .seleniumCapabilityRequest = seleniumCapabilityRequest ;
7984 }
85+ if (appAutomationCapabilityRequest == null ) {
86+ // prevent null pointer
87+ this .appAutomationCapabilityRequest = new ArrayList <JSONObject >();
88+ } else {
89+ validateTestInput (appAutomationCapabilityRequest );
90+ this .appAutomationCapabilityRequest = appAutomationCapabilityRequest ;
91+ }
8092 // Setting up credentials in both case if input capabilities are there or not
8193 this .choice = choice ;
8294 setLambdaTestCredentials (credentialsId , context );
@@ -160,8 +172,13 @@ public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener l
160172 }
161173
162174 // Create Grid URL
163- this .gridURL = CapabilityService .buildHubURL (this .username , this .accessToken .getPlainText (),"production" );
164- logger .info (this .gridURL );
175+ if (!CollectionUtils .isEmpty (seleniumCapabilityRequest )) {
176+ this .gridURL = CapabilityService .buildHubURL (this .username , this .accessToken .getEncryptedValue (),"production" );
177+ } else if (!CollectionUtils .isEmpty (appAutomationCapabilityRequest )) {
178+ logger .info ("appAutomationCR : " + appAutomationCapabilityRequest );
179+ this .gridURL = AppAutomationCapabilityService .appAutomationBuildHubURL (this .username , this .accessToken .getEncryptedValue (),"production" );
180+ }
181+ logger .info ("grid URL : " + this .gridURL );
165182 return new MagicPlugEnvironment (build );
166183 }
167184
@@ -207,7 +224,17 @@ public void buildEnvVars(Map<String, String> env) {
207224 env .put (Constant .LT_BROWSER_VERSION , seleniumCapability .getString (Constant .BROWSER_VERSION ));
208225 env .put (Constant .LT_RESOLUTION , seleniumCapability .getString (Constant .RESOLUTION ));
209226 }
227+ if (!CollectionUtils .isEmpty (appAutomationCapabilityRequest )) {
228+ JSONObject appAutomationCapability = appAutomationCapabilityRequest .get (0 );
229+ env .put (Constant .LT_PLATFORM_NAME , appAutomationCapability .getString (Constant .PLATFORM_NAME ));
230+ env .put (Constant .LT_BRAND_NAME , appAutomationCapability .getString (Constant .BRAND_NAME ));
231+ env .put (Constant .LT_DEVICE_NAME , appAutomationCapability .getString (Constant .DEVICE_NAME ));
232+ env .put (Constant .LT_DEVICE_VERSION , appAutomationCapability .getString (Constant .DEVICE_VERSION ));
233+ env .put (Constant .LT_APP_ID , appAutomationCapability .getString (Constant .APP_ID ));
234+ }
210235 env .put (Constant .LT_BROWSERS , createBrowserJSON (seleniumCapabilityRequest ));
236+ env .put (Constant .LT_BRANDS , createBrandJSON (appAutomationCapabilityRequest ));
237+ env .put (Constant .LT_DEVICES , createDeviceJSON (appAutomationCapabilityRequest ));
211238 env .put (Constant .LT_GRID_URL , gridURL );
212239 env .put (Constant .LT_BUILD_NAME , buildname );
213240 env .put (Constant .LT_BUILD_NUMBER , buildnumber );
@@ -246,40 +273,95 @@ private String createBrowserJSON(List<JSONObject> seleniumCapabilityRequests) {
246273 }
247274 return config ;
248275 }
276+ private String createDeviceJSON (List <JSONObject > appAutomationCapabilityRequests ) {
277+ String config = Constant .NOT_AVAILABLE ;
278+ try {
279+ ObjectMapper objectMapper = new ObjectMapper ();
280+ objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
281+ config = objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (appAutomationCapabilityRequests );
282+ } catch (JsonProcessingException e ) {
283+ logger .warning (e .getMessage ());
284+ }
285+ return config ;
286+ }
287+
288+ private String createBrandJSON (List <JSONObject > appAutomationCapabilityRequests ) {
289+ String config = Constant .NOT_AVAILABLE ;
290+ try {
291+ ObjectMapper objectMapper = new ObjectMapper ();
292+ objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
293+ config = objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (appAutomationCapabilityRequests );
294+ } catch (JsonProcessingException e ) {
295+ logger .warning (e .getMessage ());
296+ }
297+ return config ;
298+ }
299+
249300
250301 @ Override
251302 public boolean tearDown (AbstractBuild build , BuildListener listener ) throws IOException , InterruptedException {
252303 /*
253304 * Runs after the build
254305 */
306+ String buildnumber = String .valueOf (build .getNumber ());
307+
255308 try {
256309 logger .info ("tearDown" );
257- int x =1 ;
258- while (x !=-1 ) {
259- x =stopTunnel ();
310+ int result = stopTunnel (buildnumber , build .getWorkspace ());
311+
312+ if (result == -1 ) {
313+ logger .info ("Tunnel was not active." );
314+ } else {
315+ logger .info ("Tunnel stopped successfully." );
260316 }
261317 } catch (Exception e ) {
262- logger .warning ("Forcefully Tear Down due to :" + e .getMessage ());
318+ logger .warning ("Forcefully Tear Down due to: " + e .getMessage ());
263319 if (tunnelProcess != null && tunnelProcess .isAlive ()) {
264320 tunnelProcess .destroy ();
265321 }
266322 }
267323 return super .tearDown (build , listener );
268- }
269-
270- private int stopTunnel () throws IOException , InterruptedException {
324+ }
325+
326+ private int stopTunnel (String buildnumber , FilePath workspacePath ) throws IOException , InterruptedException {
271327 if (tunnelProcess != null && tunnelProcess .isAlive ()) {
272- logger .info ("tunnel is active, going to stop tunnel binary" );
328+ logger .info ("Tunnel is active, going to stop the tunnel binary" );
329+
273330 if (OSValidator .isWindows ()) {
274331 tunnelProcess .destroy ();
275332 logger .info ("Windows Tunnel Stopped" );
276333 return 10 ;
277334 }
278- long tunnelProcessId =getPidOfProcess (tunnelProcess );
335+
336+ long tunnelProcessId = getPidOfProcess (tunnelProcess );
337+
338+ logger .info ("Tunnel is active, with tunnelProcessId: " + tunnelProcessId );
339+
340+ if (tunnelProcessId == -1 ) {
341+ String pidFilePath = workspacePath + "/lambda-tunnel/" + buildnumber + ".pid" ;
342+ logger .info (pidFilePath );
343+
344+ try (BufferedReader br = new BufferedReader (new FileReader (pidFilePath ))) {
345+ String pidString = br .readLine ();
346+
347+ if (pidString != null ) {
348+ long pid = Integer .parseInt (pidString .trim ());
349+ stopTunnelProcessUsingPID (pid );
350+ return 10 ;
351+ } else {
352+ logger .info ("PID not found in the file." );
353+ return -1 ;
354+ }
355+ } catch (IOException e ) {
356+ logger .info ("Error reading PID file: " + e .getMessage ());
357+ return -1 ;
358+ }
359+ }
360+
279361 stopTunnelProcessUsingPID (tunnelProcessId );
280362 Thread .sleep (2000 );
281363 return 10 ;
282- }else {
364+ } else {
283365 logger .info ("Tunnel Stopped" );
284366 return -1 ;
285367 }
@@ -320,6 +402,21 @@ public List<JSONObject> getSeleniumCapabilityRequest() {
320402 public void setSeleniumCapabilityRequest (List <JSONObject > seleniumCapabilityRequest ) {
321403 this .seleniumCapabilityRequest = seleniumCapabilityRequest ;
322404 }
405+ public List <JSONObject > getAppAutomationCapabilityRequest () {
406+ return appAutomationCapabilityRequest ;
407+ }
408+
409+ public void setAppAutomationCapabilityRequest (List <JSONObject > appAutomationCapabilityRequest ) {
410+ this .appAutomationCapabilityRequest = appAutomationCapabilityRequest ;
411+ }
412+
413+ public String getAppId () {
414+ return appId ;
415+ }
416+
417+ public void setAppid (String appId ) {
418+ this .appId = appId ;
419+ }
323420
324421 public String getUsername () {
325422 return username ;
0 commit comments