Custom Plugin development

I have defined a SchedulerProperties class below for my scheduler to get values like accessToken and organization/ I have also defined one property that is a toggle which allows the plugin to start the scheduler and further post data.
package org.sonarsource.plugins.example.settings;

import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;

import java.util.List;

import static java.util.Arrays.asList;

public class SchedulerProperties {

  public static final String SCHEDULER_KEY = "sonar.scheduler.schedulerKey";
  public static final String ORGANIZATION = "sonar.scheduler.organization";
  public static final String ACCESS_TOKEN = "sonar.scheduler.accessToken";
  public static final String CATEGORY = "Scheduler Properties";

  private SchedulerProperties() {
    // only statics
  }

  public static List<PropertyDefinition> getProperties() {
    return asList(
      PropertyDefinition.builder(SCHEDULER_KEY)
        .name("Scheduler")
        .description("Set Scheduler to fetch the updated sonar report data and save it to the smart contract")
        .defaultValue(String.valueOf(false))
              .type(PropertyType.BOOLEAN)
        .category(CATEGORY).index(3)
        .build(), PropertyDefinition.builder(ORGANIZATION)
                    .name("Organization")
                    .description("Organization Name for the Smart Contract")
                    .defaultValue("")
                    .category(CATEGORY).index(1)
                    .build(), PropertyDefinition.builder(ACCESS_TOKEN)
                    .name("Access Token")
                    .description("Access Token for the Smart Contract")
                    .defaultValue("")
                    .category(CATEGORY).index(2)
                    .build());
  }
}

I am checking the values of the properties by overriding a onChange function in the class SchedulerSettingsChangeListener which extends GlobalPropertyChangeHandler.

@Override
    public void onChange(PropertyChange change) {
        System.out.println("In onChange function: ");
        Loggers.get(getClass()).info("In onChange function: ");
        if(change.getKey().equals("sonar.scheduler.schedulerKey")) {
            Loggers.get(getClass()).info("In onChange function if: ");
            Loggers.get(getClass()).info("scheduler key: " + change.getNewValue());
            Loggers.get(getClass()).info("Server URL getPublicRootUrl() : " + server.getPublicRootUrl());

            if (change.getNewValue().equals("true")) {
                configuration.get(SchedulerProperties.ORGANIZATION).ifPresentOrElse(value -> organization = value, () -> organization = "");
                Loggers.get(getClass()).info("In toggle true logic organization: " + organization);
                configuration.get(SchedulerProperties.ACCESS_TOKEN).ifPresentOrElse(value -> accessToken = value, () -> accessToken = "");
                Loggers.get(getClass()).info("In toggle true logic accessToken: " + accessToken);
                if (StringUtils.isNotEmpty(organization) && StringUtils.isNotEmpty(accessToken)) {
                    scheduler = Executors.newScheduledThreadPool(1);
                    scheduler.scheduleAtFixedRate(() -> {
                        Loggers.get(getClass()).info("Scheduler start: ");
                        List<PostData> postDataList = new ArrayList<>();
                        fetchDataFromAPI(SONAR_HOST_URL, postDataList);
                        postToExternalAPI(postDataList);
                    }, initialDelay, period, TimeUnit.SECONDS);
                }
                else {
                    Loggers.get(getClass()).info("Organization or accessToken is empty. Setting scheduler key to false");
                    
                }
            }
            else {
                Loggers.get(getClass()).info("In else condition Scheduler shutdown: ");
                if(scheduler != null) {
                    Loggers.get(getClass()).info("Scheduler is not Null Scheduler shutdown: ");
                    scheduler.shutdownNow();
                }
            }
        }
        if(change.getKey().equals("sonar.scheduler.organization")) {
            Loggers.get(getClass()).info("In onChange function: ");
            Loggers.get(getClass()).info("organization: " + change.getNewValue());
            organization = change.getNewValue();
        }
        if(change.getKey().equals("sonar.scheduler.accessToken")) {
            Loggers.get(getClass()).info("In onChange function: ");
            Loggers.get(getClass()).info("accessToken: " + change.getNewValue());
            accessToken = change.getNewValue();
        }
    }

Q: I want to reset the toggle value programmatically if organization or accessToken is empty. I am not able to find any set function in the org.sonar.api.config.Configuration nor in the org.sonar.api.ce.measure.Settings class. Can anyone suggest how should I do that?

Hi,

Could you explain what you’re trying to accomplish?

 
Thx,
Ann

Hi Ann,
Thanks for the reply, I am trying to read the sonarqube report details and posting it to my application using the plugin (plugin code that have been shared in the documentation).

Hi,

Sorry, but we just don’t support that. What you can do though is

  • get the Quality Gate metric values sent to the location of your choice using a webhook
  • use the Web API to pull data to craft your report external to SonarQube

 
HTH,
Ann