This is the 9th day of my participation in the August Wen Challenge.More challenges in August

In practice, we will encounter requirements like this:

Code submission, trigger back-end sonar test, test completed, Jenkins trigger dependent task, execute Python script, meet expectations, call on-line task module, go online, fail to meet expectations, pin group notification.Copy the code

So let’s take a look at what needs to be done.

Let’s configure the sonar test mission

Download SonarQube. Go to the official website to download.

Configure the mysql database at sonary. properties

sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar? useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=falseCopy the code

CREATE DATABASE SONAR CHARACTER SET UTF8 COLLATE UTf8_general_ci;

Create a user and assign permissions to it:

CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL PRIVILEGES ON *.* TO 'sonar'@The '%' IDENTIFIED BY 'sonar' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Copy the code

Above the database table execution

A Java environment configuration is required

SonarQube’s default port is 9000

Configure the Maven environment if Java needs to rely on Maven

perform

mvn sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login=047cf5b2c769ff9e0a3291521dbb8db2d620c031
Copy the code

We’re in Jenkins configuration

Build environment

Prepare SonarQube Scanner environment

Construction:

configuration

sonar.projectKey=testmvndemo\
sonar.projectName=testmvndemo\
sonar.projectVersion=1.1\
sonar.sources=.\
sonar.java.binaries=C:\\Program Files (x86)\\Jenkins\\liwanlei\\liwanlei\\src\\main\\java\\com\\example\\sell\\demo
Copy the code

Debug configuration test tasks,

We are going to implement the code that scans for non-standard nailing notification

import  requests,json,jenkins
  
def sendding(Dingtalk_access_token, content,title) :
    url = Dingtalk_access_token
    pagrem = {        "msgtype": "link"."link": {          
  'title':title,            "text": content,          
 'messageUrl':'http://localhost:9000/dashboard? id=testmvndemo'
        },        "isAtAll": True
    }
    headers = {        
 
'Content-Type': 'application/json'
    }
    requests.post(url, data=json.dumps(pagrem), headers=headers)
yanzhongbiaozhun=0

def huitui() :
    huiguntongzhi='Rollback robot URL'
    url = 'http://localhost:9000/api/issues/search? componentKeys =testmvndemo&s=FILE_LINE&resolved=false&types=BUG&ps= 100&facets=severities%2Ctypes&additionalFields=_all'
    resopnse = requests.get(url).text
    result = json.loads(resopnse)
    zhuyao=0
    yanzhong=0
    code_reslut=[]    

    for item in result['issues'] :if item['severity'] = ='MAJOR':
                zhuyao+=1
                code_reslut.append({'file':item['component']."Exception information":item['message']})        
        elif item['severity'] = ='CRITICAL':
                yanzhong+=1
                code_reslut.append({'file': item['component']."Exception information": item['message']})        
       else:            passif yanzhong>=yanzhongbiaozhun:
            sendresuly={'serious bugs':yanzhong,'major bugs':zhuyao,
         "Results Overview":code_reslut}
            sendding(huiguntongzhi,content=sendresuly,title="Rollback notification")    

else:
        conde=jenkins.Jenkins(url="http://localhost:8080",
    username='liwanlei',password="liwanlei")
        conde.build_job("Backend online")

def xinxitongji() :
    dingdingurl='nailing url'
    url='http://localhost:9000/api/measures/search? projectKeys= testmvndemo&metricKeys=alert_status%2Cbugs%2Creliability_rating%2 Cvulnerabilities%2Csecurity_rating%2Ccode_smells%2Csqale_rating% 2Cduplicated_lines_density%2Ccoverage%2Cncloc%2Cncloc_language_distribution'
    resopnse = requests.get(url).text
    result = json.loads(resopnse)
    bug=0
    code=0
    fugai=0
    chongf=0    
  
for item in result['measures'] :if item['metric'] = ="bugs":
            bug=item['value']       
       elif item['metric'] = ='code_smells':
            code=item['value']       
       elif item['metric'] = ='coverage':
            fugai=item['value']        

       elif item['metric'] = = ['duplicated_lines_density']:
            chongf=item['value']        

       else:            pass
    code_reslut={'bugs':bug,"Possible problem code":code,
    "Coverage":fugai,"Duplicate code":chongf}
    sendding(dingdingurl, content=code_reslut, title="Code Coverage statistics")

if __name__=="__main__":
    huitui()
    xinxitongji()
Copy the code

Select Build after other projects are built, select the dependent project, and call it when stable

Construction:

 python sonar.py
Copy the code

If we meet our expectations, we send statistics. When we need to roll back, we send rollback notification to everyone. Here we need to set the threshold that meets the rollback of the project. It can be adjusted according to the actual project

Let’s see what happens

Notify when we need to back up