Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check agent is correctly installed during plugin startup #1000

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading