trying to search the record on the different URL

Suvarna Deokar2
Tera Contributor

I have below code to compare "TestGroup" is available or not. Its giving the result as "Script: HTTP Status: 200
* Script: Group 'TestGroup' exists" though "TestGroup" is not available in the group records.

 

 

try {
var groupName = 'TestGroup';
var r = new sn_ws.RESTMessageV2('SonarQube Request Permissions', 'GET User_Group');
r.setQueryParameter('name', groupName);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.print("HTTP Status: " + httpStatus);

if (httpStatus == 200) {
var parsedResponse = JSON.parse(responseBody);

if (parsedResponse.groups && parsedResponse.groups.length > 0) {
gs.print("Group '" + groupName + "' exists");
} else {
gs.print("Group '" + groupName + "' does not exist");
}
} else {
gs.print("Error: " + responseBody);
}
} catch (ex) {
gs.error("Error: " + ex, "global.SonarQube_Utils");
}

2 REPLIES 2

Anil Lande
Kilo Patron

Hi,

Check with the API team that is called from your script.

The API should return failure status code like 403 instead of 200.

Please try below code and print the Response body in logs and see what response you are getting.

try {
var groupName = 'TestGroup';
var r = new sn_ws.RESTMessageV2('SonarQube Request Permissions', 'GET User_Group');
r.setQueryParameter('name', groupName);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.print("HTTP Status: " + httpStatus);

if (httpStatus == 200) {
var parsedResponse = JSON.parse(responseBody);

gs.info('SonarQube Response Body :'+JSON.stringify(responseBody)); // added this line

if (parsedResponse.groups && parsedResponse.groups.length > 0) {
gs.print("Group '" + groupName + "' exists");
} else {
gs.print("Group '" + groupName + "' does not exist");
}
} else {
gs.print("Error: " + responseBody);
}
} catch (ex) {
gs.error("Error: " + ex, "global.SonarQube_Utils");
}
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Community Alums
Not applicable

Hi @Suvarna Deokar2 ,

 

The code seems designed to check if "TestGroup" exists using an API call. It might show that 'TestGroup' exists due to how the API responds or handles groups. Double-check the response structure and how the group name is sent in the query parameter. Sometimes outdated data or the API's behavior might cause this. Try reviewing the response details or how the code interprets the data to pinpoint the issue.

 

A 200 status code only indicates successful communication, not necessarily the existence of the group. Instead of simply checking for a 200 status, use the parsed response to confirm the group's presence.

 

If you're still facing issues, could you please share additional details about the 'SonarQube Request Permissions' REST message and the 'GET User_Group' method? Providing a screenshot or more information about these elements could help the community understand the setup better.

 

If you found this helpful, a 'like' is the secret handshake of appreciation!

 

- Prasad