簡體   English   中英

SonarQube 如何創建配置文件並向其導入新規則

[英]SonarQube how to create Profile and import new rules to it

我正在為 SonarQube 開發一個插件,以便從 .xml 文件中導入規則。 到目前為止,這已經完成,它們確實被導入到 SonarQube 實例中並顯示在“規則”下。 盡管正在創建質量配置文件,但並未將所有導入的規則添加到其中,我不明白為什么。

不想一一添加; 我正在尋找一種方法,將它們從 .xml 文件導入后直接添加到創建的配置文件中。 配置文件只是通過以下方式創建的:

public class MyProfile extends ProfileDefinition {
    @Override
    public RulesProfile createProfile(ValidationMessages validation) {
        return RulesProfile.create("QP", "Java");
    }
}

這是我懷疑會發生這種情況的一些方法代碼:

public class MyRules extends RulesDefinition {

public void define(Context context) {
    List<RulePack> rulePacks = this.rulePackParser.parse();
    parseXml(context);                                                      
    parseRulePacks(context, rulePacks);
    for (NewRepository newRepository : newRepositories.values()) {
        newRepository.done();
    }
}

private void parseXml(Context context) {
    for (Language supportedLanguage : languages.all()) {
        InputStream rulesXml = this.getClass().getResourceAsStream("/rules-TESTE.xml");
        if (rulesXml != null) {
            NewRepository repository = getRepository(context, supportedLanguage.getKey());
            xmlLoader.load(repository, new BufferedReader(new InputStreamReader(rulesXml)));
            repository.done();
        }
    }
}

private void parseRulePacks(Context context, List<RulePack> rulePacks) {
    for (RulePack rulePack : rulePacks) {
        for (AppScanRule rule : rulePack.getRules()) {
            String sqLanguageKey = convertToSQ(rulePack.getRuleLanguage(rule));
            if (this.languages.get(sqLanguageKey) != null && isAnInterestingRule(rule)) {
                processRule(context, rulePack, rule, sqLanguageKey);
            }
        }
    }
}
}

提前致謝。

編輯說明:實際將規則添加到質量配置文件的描述過程可以被視為一種解決方法,因為當時 SonarQube 的引擎存在一個未解決的問題,不允許一次訪問所有規則以便輕松添加到所需的質量配置文件(可以在此處查看此問題)。 所以對於 5.6 的版本,它可以像這樣完成:

  • 自己實現了一個擴展ProfileDefinition的類;
  • 覆蓋方法public RulesProfile createProfile(ValidationMessages messages)並創建配置文件對象RulesProfile profile = RulesProfile.create();
  • 在該方法中,通過Collection<Rule> rules = ruleFinder.findAll(RuleQuery.create().withRepositoryKey(key-of-the-repository-with-the-desired-rules));獲取所有規則Collection<Rule> rules = ruleFinder.findAll(RuleQuery.create().withRepositoryKey(key-of-the-repository-with-the-desired-rules)); (findAll 是被破壞的方法)
  • 使用以下命令激活配置文件上的規則:

    for(Rule rule : rules) { profile.activateRule(rule, null); }

  • 最后,可以設置一些定義,例如配置文件的語言或其名稱。 之后返回新創建的配置文件對象:

    profile.setLanguage("Java"); profile.setName("My Profile"); return profile;


所以我能夠解決這個問題,但采用了一種非常不同的方法。 為了用我的新規則添加質量配置文件,我使用了 SonarQube 的 REST Web API docs.sonarqube.org/display/DEV/Web+Service+API 我用於向/從 API 發送/接收請求的客戶端是Postman nemo.sonarqube.org中也記錄了可用的命令。

經過一些失敗,我發現這個過程似乎有一些使用限制。 為了讓這個工作,我必須:

  • 首先,在我在問題中給出的插件代碼中,我必須使用類RulesDefinitionXmlLoader和方法load 加載包含規則的 .XML 文件,如下所示:

     xmlLoader.load(repository, new BufferedReader(newInputStreamReader(rulesXml)));
  • 這個過程我真的很甜蜜,你可以很容易地加載你的規則。 您所要做的就是確保包含規則的 .XML 文件遵循以下標准模板:

     <rules> <rule> <repositoryKey>java-key</repositoryKey> <key>1</key> <internalKey> da-rule-name-1</internalKey> <name> da-rule-name </name> <description>da-description </description> </rule> </rules>

這里主要關注的是repositoryKey,因為其余列出的字段是強制性的。 您必須確保此處使用的密鑰用於添加質量配置文件的密鑰相同(我接下來將展示)。 這個鍵是在創建存儲庫時擴展 RulesDefinition (我在問題中提供的最重要的代碼)的類中定義的。

如果有幫助,您還可以使用對 Web API 的請求來列出所有存儲庫,這樣您就可以確保使用的密鑰是正確的: 在此處輸入圖片說明

  • 創建一個與上一個類似的 .XML 文件,但包含以下信息。 這是 REST Web API 將用於創建質量配置文件的文件:

     <profile> <name>da-profile-name</name> <language>java</language> <rules> <rule> <repositoryKey>java-key</repositoryKey> <key>1</key> <internalKey> da-rule-name-1</internalKey> <name> da-rule-name </name> <description>da-description </description> </rule> </rules> </profile>
  • 最后,您所要做的就是使用創建的第二個文件將請求發送到 Web API。 為此,您可以像我說的那樣使用 Postman(如果您對使用 REST API 的知識較少或沒有知識,就像我一樣)或命令提示符(在這種情況下需要安裝 Curl )。 在郵遞員中:

    1. 將請求設置為“發布”並添加 URL(假設 SonarQube 在本地機器中並偵聽默認端口): http://localhost:9000/api/qualityprofiles/restore

    2. 設置“授權”。 默認為"admin"/"admin" ;

    3. 在“正文”中,將一個參數設置為“key”=“backup”,將“value”設置為一個文件(選擇向下箭頭),然后選擇創建的第二個文件(帶有規則和額外標簽的文件)

請求配置

發送請求,如果一切順利,您應該能夠在底部窗口中看到成功導入規則的數量!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM