Skip to content

Commit be804a3

Browse files
Meraj AnsariMeraj Ansari
authored andcommitted
Enhance build status management in LambdaTest service
1 parent 4f59d1b commit be804a3

File tree

4 files changed

+125
-4
lines changed

4 files changed

+125
-4
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wdio-lambdatest-service",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "A WebdriverIO service that manages tunnel and job metadata for LambdaTest.",
55
"author": "LambdaTest <[email protected]>",
66
"contributors": [
@@ -90,7 +90,7 @@
9090
"recursive-readdir": "^2.2.2",
9191
"rimraf": "^3.0.0",
9292
"tempy": "^0.3.0",
93-
"typescript": "^3.6.3",
93+
"typescript": "^5.9.3",
9494
"vitest": "^0.34.4"
9595
},
9696
"husky": {

src/launcher.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { performance, PerformanceObserver } from 'perf_hooks'
66
import logger from '@wdio/logger'
77
import LambdaTestTunnelLauncher from '@lambdatest/node-tunnel'
88
import { TUNNEL_START_FAILED, TUNNEL_STOP_FAILED, TUNNEL_STOP_TIMEOUT } from './constants.js'
9+
import { updateBuildStatusForSession } from './util.js'
910
const log = logger('@wdio/lambdatest-service')
1011
const colors = require('colors');
1112
export default class LambdaTestLauncher {
@@ -150,7 +151,21 @@ export default class LambdaTestLauncher {
150151
)
151152
}
152153

153-
onComplete() {
154+
async onComplete(exitCode, config) {
155+
try {
156+
const updateBuildStatus = this.options?.updateBuildStatusOnRetry === true;
157+
if (updateBuildStatus && exitCode === 0 && config?.product === 'appAutomation' && config?.sessionId) {
158+
const lambdaCredentials = {
159+
username: config.user,
160+
accessKey: config.key,
161+
isApp: config?.product === 'appAutomation' ? true : false
162+
};
163+
await updateBuildStatusForSession(config.sessionId, lambdaCredentials, exitCode)
164+
}
165+
}catch(error){
166+
console.error(error.message);
167+
}
168+
154169
if (
155170
!this.lambdatestTunnelProcess ||
156171
typeof this.lambdatestTunnelProcess.isRunning !== 'function' ||

src/util.d.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,30 @@ export declare function getParentSuiteName(fullTitle: string, testSuiteTitle: st
1313
* @param {any} lambdaCredentials
1414
* @returns
1515
*/
16-
export declare function updateSessionById(sessionId: string, data: any, lambdaCredentials: any, callback: any): Promise<void>;
16+
export declare function updateSessionById(sessionId: string, data: any, lambdaCredentials: any, callback: any): Promise<void>;
17+
18+
/**
19+
* Updates a build by id
20+
* @param {string} buildId
21+
* @param {('passed'|'failed')} status
22+
* @param {any} lambdaCredentials
23+
* @returns
24+
*/
25+
export declare function updateBuildById(buildId: string, lambdaCredentials: any, status: string): Promise<void>;
26+
27+
/**
28+
* Get a session by id
29+
* @param {string} sessionId
30+
* @param {any} lambdaCredentials
31+
* @returns
32+
*/
33+
export declare function getSessionById(sessionId: string, lambdaCredentials: any): Promise<any>;
34+
35+
/**
36+
* Update a build status based on a session id and exitCode
37+
* @param {string} sessionId
38+
* @param {any} lambdaCredentials { username, accessKey, isApp? }
39+
* @param {number} exitCode 0 => passed, >0 => failed
40+
* @returns
41+
*/
42+
export declare function updateBuildStatusForSession(sessionId: string, lambdaCredentials: any, exitCode: number): Promise<void>;

src/util.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,83 @@ export async function updateSessionById(sessionId, data, lambdaCredentials){
4848
logger.error(error);
4949
}
5050
}
51+
52+
/**
53+
* Updates a build by id
54+
* @param {string} buildId
55+
* @param {('passed'|'failed')} status
56+
* @param {any} lambdaCredentials
57+
*/
58+
export async function updateBuildById(buildId, lambdaCredentials, status){
59+
if (!buildId) return;
60+
const buildsUrl = lambdaCredentials.isApp
61+
? `${baseUrlApp}${appVersion.latestVersion}/builds/${buildId}`
62+
: `${baseUrl}${version.latestVersion}/builds/${buildId}`;
63+
const body = { status };
64+
try {
65+
let config = {
66+
method: 'patch',
67+
maxBodyLength: Infinity,
68+
url: buildsUrl,
69+
headers: {
70+
'accept': 'application/json',
71+
'Authorization': `Basic ${Buffer.from(lambdaCredentials.username + ':' + lambdaCredentials.accessKey).toString('base64')}`,
72+
'Content-Type': 'application/json'
73+
},
74+
data: body
75+
};
76+
let response = await axios.request(config);
77+
logger.info(response?.data);
78+
} catch (error) {
79+
logger.error(error);
80+
}
81+
}
82+
83+
/**
84+
* Get a session by id
85+
* @param {string} sessionId
86+
* @param {any} lambdaCredentials
87+
*/
88+
export async function getSessionById(sessionId, lambdaCredentials){
89+
if (!sessionId) return undefined;
90+
const sessionUrl = lambdaCredentials?.isApp
91+
? `${baseUrlApp}${appVersion.latestVersion}/sessions/${sessionId}`
92+
: `${baseUrl}${version.latestVersion}/sessions/${sessionId}`;
93+
try {
94+
const config = {
95+
method: 'get',
96+
maxBodyLength: Infinity,
97+
url: sessionUrl,
98+
headers: {
99+
'accept': 'application/json',
100+
'Authorization': `Basic ${Buffer.from(lambdaCredentials.username + ':' + lambdaCredentials.accessKey).toString('base64')}`
101+
}
102+
};
103+
const response = await axios.request(config);
104+
return response?.data;
105+
} catch (error) {
106+
logger.error(error);
107+
return undefined;
108+
}
109+
}
110+
111+
/**
112+
* Update a build status based on a session id and exitCode
113+
* @param {string} sessionId
114+
* @param {any} lambdaCredentials
115+
* @param {number} exitCode 0 => passed, >0 => failed
116+
*/
117+
export async function updateBuildStatusForSession(sessionId, lambdaCredentials, exitCode) {
118+
try {
119+
const session = await getSessionById(sessionId, lambdaCredentials);
120+
const buildId = session?.data?.build_id;
121+
if (!buildId) {
122+
logger.error('Could not determine build_id from session');
123+
return;
124+
}
125+
const status = exitCode === 0 ? 'passed' : 'failed';
126+
await updateBuildById(buildId, lambdaCredentials, status);
127+
} catch (error) {
128+
logger.error(error);
129+
}
130+
}

0 commit comments

Comments
 (0)