1
votes

I created a custom widget in Sonar to show some data for a particular project. There are 3 projects in the list and only one of them should display this widget.

I tried setting the Widget Scope @WidgetScope(value = PROJECT). Also tried not setting anything as default scope is supposed to be Project.

I'm using v5.1.2 SonarQube.

But it still shows up on all the projects with the same data.

Here is the code I'm using:

import org.sonar.api.web.AbstractRubyTemplate;
import org.sonar.api.web.Description;
import org.sonar.api.web.RubyRailsWidget;
import org.sonar.api.web.UserRole;
import org.sonar.api.web.WidgetCategory;
import org.sonar.api.web.WidgetProperties;
import org.sonar.api.web.WidgetProperty;
import org.sonar.api.web.WidgetPropertyType;
import org.sonar.api.web.WidgetScope;
import org.sonar.api.resources.Project;
import static org.sonar.api.web.WidgetScope.PROJECT;


@UserRole(UserRole.USER)
@Description("An Internal Plugin to display some data in  a project")
@WidgetCategory("some-data Plugin")
@WidgetScope(value = PROJECT)
@WidgetProperties({
  @WidgetProperty(key = "Index",type=WidgetPropertyType.STRING
  ),
})
public class OneMoreRubyWidget extends AbstractRubyTemplate implements RubyRailsWidget {

  @Override
  public String getId() {
    return "SomeData";
  }

  @Override
  public String getTitle() {
    return "SomeData";
  }

  protected String getTemplatePath() {
          return "/example/somedata.html.erb";  

  }
}

Is there any way I can get the "project name" from the Sonar Dashboard so that I can return different values in the widget for different projects?

1
Can you show the code of somedata.html.erb? If you want to hide the widget for some conditions, you have to make changes there. - Johannes Zink

1 Answers

0
votes

Your best bet is to suppress display of the widget when there's no relevant data, then store data only for relevant projects.

Several widgets do conditional display already. Take a look at this Widget Lab widget which makes display conditional.