Skip to content

Commit

Permalink
Check agent is correctly installed during plugin startup
Browse files Browse the repository at this point in the history
Users currently submit a number of reports for Sonarqube reporting that
branch analysis is not available despite them having the plugin
installed, which are typically triggered by the user not having set up
the Java agent on one of the components correctly. This is compounded by
the Sonarqube plugin screen showing the plugin as being installed in
these scenarios even where the plugin's classes and configuration have
not been fully loaded into Sonarqube. To overcome this, the plugin
bootstrap class is now checking for the agent having made an alteration
to one of the bootstrap methods as an indication that the agent has run
successfully for both the Compute Engine and Web components, with the
plugin failing to start if either component doesn't detect the agent
modifications, and therefore preventing the Sonarqube server starting.
Whilst this won't fully resolve the problem of users not installing the
plugin properly, it prevents them believing the plugin is installed and
then only finding out things aren't right at the point they try and
submit an analysis with branch or pull request properties.
  • Loading branch information
mc1arke committed Nov 17, 2024
1 parent c28e16e commit 6259650
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ public static void premain(String args, Instrumentation instrumentation) throws
Component component = Component.fromString(args).orElseThrow(() -> new IllegalArgumentException("Invalid/missing agent argument"));

if (component == Component.CE) {
redefineEdition(instrumentation, "com.github.mc1arke.sonarqube.plugin.CommunityBranchPluginBootstrap", redefineIsAvailableFlag());
redefineEdition(instrumentation, "org.sonar.core.platform.PlatformEditionProvider", redefineOptionalEditionGetMethod());
redefineEdition(instrumentation, "org.sonar.server.almsettings.MultipleAlmFeature", redefineIsAvailableFlag());
} else if (component == Component.WEB) {
redefineEdition(instrumentation, "com.github.mc1arke.sonarqube.plugin.CommunityBranchPluginBootstrap", redefineIsAvailableFlag());
redefineEdition(instrumentation, "org.sonar.server.almsettings.MultipleAlmFeature", redefineIsAvailableFlag());
redefineEdition(instrumentation, "org.sonar.server.newcodeperiod.ws.SetAction", redefineConstructorEditionProviderField(EditionProvider.Edition.DEVELOPER));
redefineEdition(instrumentation, "org.sonar.server.newcodeperiod.ws.UnsetAction", redefineConstructorEditionProviderField(EditionProvider.Edition.DEVELOPER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.github.mc1arke.sonarqube.plugin.classloader.DefaultElevatedClassLoaderFactoryProvider;
import com.github.mc1arke.sonarqube.plugin.classloader.ElevatedClassLoaderFactory;
import com.github.mc1arke.sonarqube.plugin.classloader.ElevatedClassLoaderFactoryProvider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.Plugin;
import org.sonar.api.SonarQubeSide;

Expand All @@ -43,20 +46,32 @@
*/
public class CommunityBranchPluginBootstrap implements Plugin {

private static final Logger LOGGER = LoggerFactory.getLogger(CommunityBranchPluginBootstrap.class);

private final ElevatedClassLoaderFactoryProvider elevatedClassLoaderFactoryProvider;
private final boolean available;

public CommunityBranchPluginBootstrap() {
this(DefaultElevatedClassLoaderFactoryProvider.getInstance());
this(DefaultElevatedClassLoaderFactoryProvider.getInstance(), false);
}

/*package*/ CommunityBranchPluginBootstrap(ElevatedClassLoaderFactoryProvider elevatedClassLoaderFactoryProvider) {
/*package*/ CommunityBranchPluginBootstrap(ElevatedClassLoaderFactoryProvider elevatedClassLoaderFactoryProvider, boolean available) {
super();
this.elevatedClassLoaderFactoryProvider = elevatedClassLoaderFactoryProvider;
this.available = available;
}

@Override
public void define(Context context) {
if (SonarQubeSide.SCANNER != context.getRuntime().getSonarQubeSide()) {
SonarQubeSide sonarQubeSide = context.getRuntime().getSonarQubeSide();
if (SonarQubeSide.COMPUTE_ENGINE == sonarQubeSide || SonarQubeSide.SERVER == sonarQubeSide) {
if (isAvailable()) {
LOGGER.info("Expected agent runtime modifications detected for component: {}", sonarQubeSide);
} else {
throw new IllegalStateException(String.format("The plugin did not detect agent modifications so SonarQube is unlikely to work with Pull Requests or Branches. Please check the Java Agent has been correctly set for the %s component", sonarQubeSide));
}
}
if (SonarQubeSide.SCANNER != sonarQubeSide) {
return;
}
try {
Expand Down Expand Up @@ -84,11 +99,16 @@ public boolean equals(Object o) {
return false;
}
CommunityBranchPluginBootstrap that = (CommunityBranchPluginBootstrap) o;
return Objects.equals(elevatedClassLoaderFactoryProvider, that.elevatedClassLoaderFactoryProvider);
return Objects.equals(elevatedClassLoaderFactoryProvider, that.elevatedClassLoaderFactoryProvider)
&& available == that.available;
}

@Override
public int hashCode() {
return Objects.hash(elevatedClassLoaderFactoryProvider);
return Objects.hash(elevatedClassLoaderFactoryProvider, available);
}

boolean isAvailable() {
return available;
}
}
Loading

0 comments on commit 6259650

Please sign in to comment.