Chapter 7. Creating custom rules


You can refer to example rules that describe how to create a YAML rule. This assumes that you already have MTA installed. See the MTA CLI Guide for installation instructions.

7.1. Creating a YAML rule template

MTA YAML-based rules have the following basic structure:

when(condition)
 message(message)
 tag(tags)
Copy to Clipboard Toggle word wrap

Procedure

  1. Create a file containing the basic syntax for YAML rules as follows:

    - category: mandatory
      description:
       <DESCRIPTION TITLE>
       <DESCRIPTION TEXT>
      effort: <EFFORT>
      labels:
      - konveyor.io/source=<SOURCE_TECH>
      - konveyor.io/target=<TARGET_TECH>
      links:
     - url: <HYPERLINK>
       title: <HYPERLINK_TITLE>
      message: <MESSAGE>
      tag:
      - <TAG1>
      - <TAG2>
      ruleID: <RULE_ID>
      when:
       <CONDITIONS>
    Copy to Clipboard Toggle word wrap

7.2. Creating a YAML rule

You can create and test your first MTA YAML-based rule by completing the following procedure. This assumes that you have already installed MTA. See Installing and running the CLI in the CLI Guide for installation instructions.

In this example, you will create a rule to discover instances where an application defines a jboss-web.xml file containing a <class-loading> element and to provide a link to the documentation that describes how to migrate the code.

  1. Create a YAML file for the rule. In the following example, the rule is created in your $HOME directory.
$ mkdir /home/<USER>/rule.yaml
Copy to Clipboard Toggle word wrap
  1. Create jboss-web.xml and pom.xml files in a directory:

    mkdir /home/<USER>/data/
    touch /home/<USER>/data/jboss-web.xml
    touch /home/<USER>/data/pom.xml
    Copy to Clipboard Toggle word wrap
  2. In the jboss-web.xml file you created, paste the following content:

    <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 4.2//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
    <jboss-web>
        <class-loading java2ClassLoadingCompliance="false">
            <loader-repository>
                seam.jboss.org:loader=@projectName@
                <loader-repository-config>java2ParentDelegation=false</loader-repository-config>
            </loader-repository>
        </class-loading>
    </jboss-web>
    Copy to Clipboard Toggle word wrap
  3. In the pom.xml file you created, paste the following content:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
      <groupId>test</groupId>
      <artifactId>test</artifactId>
      <version>1.1.0-SNAPSHOT</version>
    
      <properties>
    	<maven.compiler.source>1.7</maven.compiler.source>
    	<maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
      </dependencies>
    </project>
    Copy to Clipboard Toggle word wrap
  4. In the rule.yaml file you created, paste the following contents:

    - ruleID: <UNIQUE_RULE_ID>
      description: <DESCRIPTION>
      when:
        <CONDITION(S)>
      message: <MESSAGE>
      labels: <LABELS>
      effort: <EFFORT>
      links:
      - <LINKS>
    Copy to Clipboard Toggle word wrap

    where:

    ruleID
    Unique ID for your rule. For example, jboss5-web-class-loading.
    description
    Text description of the rule.
    when

    Complete the when block specifying one or more conditions:

    1. Use the builtin provider’s XML capability because this rule checks for a match in an XML file.

      when:
        builtin.xml:
          xpath: /jboss-web/class-loading
      Copy to Clipboard Toggle word wrap
    message
    Helpful message explaining the migration issue. The message is generated in the report when the rule matches. For example:
message: The class-loading element is no longer valid in the jboss-web.xml file.
Copy to Clipboard Toggle word wrap
labels
List of string labels for the rule.
effort
Number of expected story points to resolve this issue.
links
One or more hyperlinks pointing to documentation around the migration issues that you find.
  links:
    - url: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html-single/Migration_Guide/index.html#Create_or_Modify_Files_That_Control_Class_Loading_in_JBoss_Enterprise_Application_Platform_6
  title: Create or Modify Files That Control Class Loading in JBoss EAP 6
Copy to Clipboard Toggle word wrap

The rule is now complete and looks similar to the following:

- ruleID: jboss5-web-class-loading
  description: Find class loading element in JBoss XML file.
  when:
    builtin.xml:
      xpath: /jboss-web/class-loading
  message: The class-loading element is no longer valid in the jboss-web.xml file.
  effort: 3
  links:
  - url: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html-single/Migration_Guide/index.html#Create_or_Modify_Files_That_Control_Class_Loading_in_JBoss_Enterprise_Application_Platform_6
    title: Create or Modify Files That Control Class Loading in JBoss EAP 6
Copy to Clipboard Toggle word wrap
  1. Point the CLI to the rule file you created :

    –rules /home/<USER>/rules.yaml
    Copy to Clipboard Toggle word wrap
  2. To test the rule, point the input to the test data you created and pass the rule using the rules option in MTA CLI:

    mta-cli analyze --input /home/<USER>/data/ --output /home/<USER>/output/ --rules /home/<USER>/rules.yaml
    Copy to Clipboard Toggle word wrap
  3. Review the report to be sure that it provides the expected results.

    Once the analysis is complete, the command outputs the path to the HTML report:

    INFO[0066] Static report created. Access it at this URL:  URL="file:/home/<USER>/output/static-report/index.html"
    Copy to Clipboard Toggle word wrap

    Open /home/<USER_NAME>/output/static-report/index.html in a web browser.

  4. Navigate to the Issues tab in the left menu.
  5. Verify that the rule is executed:

    1. In the Issues table, type JBoss XML in the search bar.
    2. Verify that the issue with the title Find class loading element in JBoss XML file is present in the table.
  6. Click the jboss-web.xml link to open the affected file.

7.3. Creating a custom Go rule

You can create custom rules for Golang (Go) applications based on the following example.

You can use the following custom rule to check if MTA triggers an incident when it detects the v1beta1.CustomResourceDefinition pattern in the go file.

Procedure

  1. Create a go-rule-001.yml file in a directory.
  2. Copy the following rule in the yaml file:

    - message: golang apiextensions/v1/customresourcedefinitions found
      description: "golang apiextensions/v1/customresourcedefinitions found"
      ruleID: go-lang-ref-001
      effort: 1
      when:
        go.referenced:
          pattern: "v1beta1.CustomResourceDefinition"
    Copy to Clipboard Toggle word wrap
  3. Create a test go file named example.go in your Home directory.
  4. Paste the following code in the example.go file:

    package main
    
    import (
    	"fmt"
    
    	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
    )
    
    func main() {
    	fmt.Println(v1beta1.CustomResourceDefinition{})
    
    }
    Copy to Clipboard Toggle word wrap
  5. Run the following command in the MTA CLI:

    $ ./mta-cli analyze -i _<path-to-Go-project>_ \
    -o _<path-to-report-directory>_ --run-local=false --rules \
    _<path-to-go-rule-001.yml>_
    Copy to Clipboard Toggle word wrap
    Note

    Add the --overwrite option if you want to use the same directory for the report when you run subsequent tests. MTA overwrites the current report with the result of the latest analysis that you run.

  6. Open the static report at /home/<USER>/output/static-report/ in your browser.
  7. Navigate to the issues to verify the golang apiextensions/v1/customresourcedefinitions found issue.

7.4. Creating a custom Node.js rule

You must create custom rules to analyze Node.js applications by using MTA. A Node.js rule contains the nodejs.referenced capability that supports the pattern field.

The following example uses a custom rule to check if a .tsx file in the Node.js project imports the React framework.

Procedure

  1. Create the test-nodejs directory.

    $ mkdir -p ~/test-nodejs
    Copy to Clipboard Toggle word wrap
  2. Save the following rule as nodejs-rule-001.yml in the test-nodejs directory:

    - ruleID: test-tsx-support-00000
      description: Found React import in .tsx file
      message: Found React import in .tsx file
      effort: 1
      when:
        nodejs.referenced:
         pattern: "React"
    Copy to Clipboard Toggle word wrap
  3. Create the following test application in the Component.tsx file:

    import React from 'react';
     export const MyComponent: React.FC = () => <div>Hello</div>;
    Copy to Clipboard Toggle word wrap
  4. Run the following command in the MTA CLI:

    $ ./mta-cli analyze -i ~/test-nodejs/ -o \
    ~/test-nodejs/report --run-local=false \
    --rules ~/test-nodejs/nodejs-rule-001.yml
    Copy to Clipboard Toggle word wrap
    Note

    Add the --overwrite option if you want to use the same directory for the report when you run subsequent tests. MTA overwrites the current report with the result of the latest analysis that you run.

  5. Open the static report at ~/test-nodejs/report/static-report/index.html in your browser.
  6. Click the <application_name> to open the Dashboard.
  7. Review the incidents in the Issues tab.

7.5. Creating custom Python rules

You must create custom rules to analyze Python applications by using MTA. A Python rule contains the python.referenced capability with the supported fields.

The following example uses two custom rules:

  • The first rule checks if bad_method is specified
  • The second rule checks if hello_world is specified in file_a.py in your project.

Procedure

  1. Create the directory test-python.

    $ mkdir -p ~/test-python
    Copy to Clipboard Toggle word wrap
  2. Create a python-rule-001.yml file in the directory and add the following rule:

    - category: mandatory
      ruleID: python-rule-001
      effort: 1
      description: "Bad method"
      when:
        python.referenced:
          pattern: "bad_method"
    Copy to Clipboard Toggle word wrap
  3. Create a python-rule-002.yml file in the directory and add the following rule:

      - category: mandatory
        ruleID: python-rule-002
        effort: 1
        message: "Found a python"
        when:
          python.referenced:
            pattern: "hello_world"
    Copy to Clipboard Toggle word wrap
  4. Save the following Python code as file_b.py.

    import deprecated
    def hello_world():
              return "Hello, world!"
    @deprecated.deprecated("This method is bad!")
    def bad_method():
             return "I'm a bad method!"
    Copy to Clipboard Toggle word wrap
  5. Save the following code as a second file, file_a.py.

    import file_b
    print(file_b.hello_world())
    print(file_b.bad_method())
    Copy to Clipboard Toggle word wrap
  6. Run the following command in the MTA CLI:

    $ ./mta-cli analyze -i ~/test-python/ -o \
    ~/test-python/report --run-local=false \
    --rules ~/test-python/python-rule-001. \
    --rules ~/test-python/python-rule-002.yml
    Copy to Clipboard Toggle word wrap
    Note

    Add the --overwrite option if you want to use the same directory for the report when you run subsequent tests. MTA overwrites the current report with the result of the latest analysis that you run.

  7. Open the static report at ~/test-python/report/static-report/index.html in your browser.
  8. Click the <application_name> to open the Dashboard.
  9. Review the incidents in the Issues tab.
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2026 Red Hat
トップに戻る