Skip to Content
Technical Articles

Static ABAP Pull Request Code Review with abapLint on Jenkins using piper-lib

More and more, versioning ABAP code with GitHub becomes usual. Versioning code it beneficious on its own, and it also gives us the opportunity to improve quality in other ways. On this tutorial I am going to show how to set up Github + Jenkins and check if ABAP code is compliant according to abapLint properties you set.

Pre Requisites:

  • ABAPGit
  • A working Jenkins instance.
  • A GitHub Repository.
  • Defined Code Conventions (optional).

Jenkins:

It is necessary to have a Jenkins instance up and running in order to setup your pipeline. Please check another tutorial for that.

Once you have Jenkins instance working you need to create a space to insert your repository. So let’s set it up.

  • New Item
  • Choose a name for it
  • Select Multibranch Pipeline (we are going to use piper-lib framework, and the ready-made continuous delivering pipeline of a “Project Piper” must run this way according to documentation.
  • On Branch Source select GitHub.
  • Inform “Repository HTTPS URL” – this is the same url you use for cloning.
  • If you are using a private repository, you need to Add a new Credentials with SSH personal token access, extracted from your GitHub Account.

GitHub Repository:

  • In your repository go to Settings
  • Go to Hooks
  • Click on Add WebHook
  • On Payload URL inform Jenkins url and insert /github-webhook/ at the end.
  • Content type – Choose application/json
  • Secret no needed
  • Select events – Mark Pull Requests. This way Jenkins will evaluate your PR when created or when a new commit is added.

Piper-lib

Well, when I started to set up this project, I tried to use a regular Jenkins pipeline written with DSL syntax. However, for security and performance reasons we decided to use a docker image instead of Node.js to run the Linter. As we decided to use a docker image and call this image inside Jenkins  piper-lib became the best choice. When trying to use a regular syntax pipeline, we had so much trouble trying to make Jenkins understand our external resource. After several attempts, finally we started a test with piper-lib library and it easily understood and let us to use our docker image with abaplint.

Project Piper Documentation: https://sap.github.io/jenkins-library/

The same way you had to enable GitHub plugin on Jenkins, you need to make sure that Piper-lib plugin is enabled.

After that, you can make a simple test

  • Create a Jenkins file on the root of your repository.

In your Jenkinsfile insert the following content:

@Library('piper-lib-os') _
node() {
    stage('prepare') {
        checkout scm
        setupCommonPipelineEnvironment script:this
    }
}

This step synchronizes the repository and initializes the project settings.

You also need a .pipeline/config.yml in your repository.

On this case we setup the following configuration on this file:

general:
     gitSshKeyCredentialsId: 'githubcredentialsyougavetojenkins@sap.com'
     verbose: true
     productiveBranch: develop

*in case your productive branch is master, is no need to declare

You can already test.  You can run the pipeline on Jenkins directly or in case you had created locally those files, you can commit and push it to your repository and then create a Pull Request. The PR should already start Jenkins pipeline.

Ok so now your pipeline is working, but no significant results because we hadn’t tested anything regarding to your repository code.

So let’s add another stage in our pipeline. Reopen your Jenkinsfile.

@Library(['piper-lib', 'piper-lib-os']) _
    stage('prepare') {
        node {
          checkout scm
          setupCommonPipelineEnvironment script:this  
        }
    }
    stage('test') {
       node {
         dockerExecute(script: this, dockerImage: abaplint/abaplint'){
           echo 'Running abaplint docker'
           sh 'npm install -g @abaplint/cli'
           sh 'abaplint'
           currentBuild.result = 'SUCCESS'                  
           }
         }
    }

 

On this example I called the new step as test, but you can choose any name you like. The goal of this stage is make our code to be analyzed by abaplint.

On this stage, we use the piper command dockerExecute, pointing the script context to the docker image like this:  dockerExecute(script: this, dockerImage: ‘abaplint/abaplint’).

Inside this context, we can run any command we want because we are talking to abaplint docker image now. On the example I choose to print the message ‘Running abaplint docker’, followed by the command to install the linter – getting the latest content – and run with the command abaplint.

In case the code does not break any rule, the Success message will return, and the pipeline result will be green on Jenkins and on your Pull Request.

 

No%20findings%20on%20abapLint%20for%20your%20PR

No findings on abapLint for your PR

AbapLint

Abaplint is a linter for ABAP. On this example, we are using this as final steps of our code review. We intend to make this static check when a Pull Request is created/edited, in order to automate this part of code review, so that the person that is going to review the code can focus on logic and more complexes details. Check all available resources at https://abaplint.org/

So far, we wouldn’t get any abaplint issue because we haven’t set any rule.

On this page https://rules.abaplint.org/ you can check all the rules available for abaplint. You don’t need to use all of them, you can choose whatever fits to your team’s code convention and also adapt it. On the root of your repository insert a abaplint.json file. This file will contain the set of rules you want to use. If you set the content below on it, you are going to check if keywords are uppercase, if line length is longer than 20 – that is absurd, but is just a test we want to break. We are also checking if the indentation is fine.

{
	  "global": {
	    "files": "/src/**/*.*",
	    "skipGeneratedGatewayClasses": true,
	    "skipGeneratedPersistentClasses": true,
	    "skipGeneratedFunctionGroups": true
	  },
	  "syntax": {
	    "version": "v755",
	    "errorNamespace": "^(Z|Y)"
	  },
	  "rules": {
	    "keyword_case": {
	      "style": "upper",
	    },
	    "line_length": {
	      "length": 20
	    },
	    "indentation": {
	      "ignoreExceptions": true,
	      "alignTryCatch": false
           }
}

After inserting this file on your GitHub repository, Jenkins will use it on the stage test.

Create a new Pull Request with ABAP Code inside of it and check the errors Jenkins will show you by clicking on Details.

On%20GitHub%20click%20on%20Details%20after%20Jenkins%20run%20to%20see%20the%20report

On GitHub click on Details after Jenkins run to see the report

PR%20with%20Abaplint%20findings%20on%20Jenkins

PR with abaplint findings on Jenkins

abapLint%20report%20on%20Jenkins

abapLint report on Jenkins

 

Also, you can always test your rules or your Code before create a PR on https://playground.abaplint.org/.

 

 

Thanks

Be the first to leave a comment
You must be Logged on to comment or reply to a post.