trying to search the record on the different URL

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 10:26 PM
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");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 10:32 PM
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");
}
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 10:45 PM
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