代码风格检查

PH_LINT

#!/bin/bash -x

echo "--------------------"
env
echo "--------------------"
echo ${PHID}
echo ${REVISION_ID}
echo ${REPO}
echo ${BR}
echo ${DIFF_ID}
echo "------------------------------------------------------------------------------------------------------------"

repo_name=$(python3 /root/.ssh/lint_config/cicd/tools/phquery.py -r ${REVISION_ID} )
branch_name=$(python3 /root/.ssh/lint_config/cicd/tools/phquery.py -b ${REVISION_ID} )


all_in_one="/root/.ssh/lint_config/cicd/tools/"
# chmod -R a+x /root/.ssh/lint_config/cicd
# clean ,clone
rm -rf ./*
echo "repo=${repo_name} : branch=${branch_name}"> .build_desc




git clone --depth 1 -b ${branch_name} ${repo_name} lintcode


#-----------------------------------------------
cd  lintcode

GIT_COMMIT=$(git rev-parse HEAD)
echo "code base: $GIT_COMMIT"

## patch , check
patch_ret=$( arc patch --diff "$DIFF_ID" --nobranch --force --conduit-uri=https://cr.hobot.cc/  2>&1 )
echo $patch_ret
ls
cmd_ret=$?
echo "patch D${REVISION_ID} result: \n $patch_ret"
if [[ "$patch_ret" =~ 'Patch Failed!' ]] || [[ "$patch_ret" =~ "Command failed with error" ]]; then
echo "{\"Error: Apply patch D${REVISION_ID} Failed. there have conflict with the target branch!  \"}"
##echo "{\"ERORR: Lint check Failed, found $error_count errors by CI Robot \"}" >> .phabricator-comment
echo "Error: Apply patch D${REVISION_ID} Failed. there have conflict with the target branch!" >> .phabricator-comment
cp -f .phabricator-comment  ../.phabricator-comment
exit -1
fi

## get changed lines
cp -f /root/.ssh/get_changed_lines_new.py .
python3 get_changed_lines_new.py -o .changed_file_lines.json -b $GIT_COMMIT

## if user use previous old ph.hobot.cc
if [ -e .arcconfig ]; then
arc_config=$(cat .arcconfig)
if [[  "$arc_config" =~ "ph.hobot.cc" ]]; then
sed -i -e 's/http://ph.hobot.cc/https://cr.hobot.cc/g' .arcconfig
git add .arcconfig
git commit -m "force change for download code from cr"
fi
fi


# cp -f /root/.ssh/lint_config/cicd/.arclint  .
python3 /root/.ssh/lint_config/cicd/tools/parse_lint.py
cat .arclint
cp -f $all_in_one/checkstyle-6.13-all.jar  .
cp -f $all_in_one/google_checks.xml  .

if [ -e src/main.rs ]; then
cp -f $all_in_one/Cargo.toml  .
fi



echo "------------------------------------------------------------------------------------------------------------"
##arc lint --output json --only-changed --rev $GIT_COMMIT --outfile lint_result.json
arc lint --output json  --only-changed  --rev $GIT_COMMIT --outfile .phabricator-lint.bak
##check lint result
if [ -e .phabricator-lint.bak ] ; then
cat .phabricator-lint.bak
else
echo "Error: arc lint failed"
echo " " >> .phabricator-lint.bak
fi

## select lint result by changed lines
cp -f /root/.ssh/lint_config/cicd/tools/select_lint_by_changed_lines.py .
python3 select_lint_by_changed_lines.py -l .phabricator-lint.bak -f .changed_file_lines.json

echo "cat .phabricator-lint.org"
cat .phabricator-lint.org

## select lint by changed lines
cp -f /root/.ssh/lint_config/cicd/tools/phformatter.py ./
python3 phformatter.py -c .phabricator-lint.org

## check --nolint
cp -f /root/.ssh/lint_config/cicd/tools/check_nolint.py ./
python3 check_nolint.py -d ${DIFF_ID}

echo "cat .phabricator-lint"
echo "------------------------------------------------------------------------------------------------------------"
cat .phabricator-lint
lint_result=$(grep "global_lint_CFG" ../.build_desc | wc -l)
if [ ${lint_result} -gt 0 ] || [ -e ./nolint ] ; then
cp -f .phabricator-lint  ../.phabricator-lint
fi
echo "------------------------------------------------------------------------------------------------------------"

echo '{}' > ../.phabricator-lint
cat '../.phabricator-lint'

##get error numbers
error_count=$(grep -i '"severity": "error"' .phabricator-lint | wc -l)
pwd
python3 /root/.ssh/lint_config/cicd/tools/analyzeNloc.py -p $(pwd) -b $GIT_COMMIT -r ${repo_name}
if [ -f "error.txt" ]; then
error_count=$(($error_count + 1))
fi

##error 2,warning 1
if [ ${error_count} -gt 0 ] ; then
echo "{\"ERROR: Lint check Failed, found $error_count errors by CI Robot \"}"
echo "ERROR: Lint check Failed, found $error_count errors by CI Robot " >> .phabricator-comment
echo "cat .phabricator-console"
echo 'Lint 报错信息为:'
if [ -f "error.txt" ]; then
cat error.txt
cat error.txt >> .phabricator-comment
fi
cat .phabricator-console
cat .phabricator-console >> .phabricator-comment
cp -f .phabricator-comment  ../.phabricator-comment
exit -1
else
echo "INFO: Lint check Passed, no error found by CI Robot " >> .phabricator-comment
cp -f .phabricator-comment  ../.phabricator-comment
fi

exit 0;

MR_LINT

pipeline {
    agent {label 'CI_LINT_NODE_NEW'}
    parameters {
        string defaultValue: 'MERGE', description: 'Enter gitlabActionType', name: 'gitlabActionType_replay', trim: true
        string defaultValue: '', description: 'Enter gitlabSourceBranch', name: 'gitlabSourceBranch_replay', trim: true
        string defaultValue: '', description: 'Enter gitlabTargetBranch', name: 'gitlabTargetBranch_replay', trim: true
        string defaultValue: '', description: 'Enter gitlabSourceRepoHomepage', name: 'gitlabSourceRepoHomepage_replay', trim: true
        string defaultValue: '', description: 'Enter gitlabMergeRequestIid', name: 'gitlabMergeRequestIid_replay', trim: true
        string defaultValue: '', description: 'Enter gitlabMergeRequestLastCommit', name: 'gitlabMergeRequestLastCommit_replay', trim: true
        string defaultValue: '', description: 'Enter gitlabSourceRepoSshUrl', name: 'gitlabSourceRepoSshUrl_replay', trim: true
    }
    environment {
        LANG = 'en_US.UTF-8'
        // SSH_KEY_FILE = credentials('725a97cb-ba0c-45ad-a59f-f366e217a017')
        gitlabActionType = "${env.gitlabActionType ? "${env.gitlabActionType}" : "${env.gitlabActionType_replay}"}"
        gitlabSourceBranch = "${env.gitlabSourceBranch ? "${env.gitlabSourceBranch}" : "${env.gitlabSourceBranch_replay}"}"
        gitlabTargetBranch = "${env.gitlabTargetBranch ? "${env.gitlabTargetBranch}" : "${env.gitlabTargetBranch_replay}"}"
        gitlabSourceRepoHomepage = "${env.gitlabSourceRepoHomepage ? "${env.gitlabSourceRepoHomepage}" : "${env.gitlabSourceRepoHomepage_replay}"}"
        gitlabMergeRequestIid = "${env.gitlabMergeRequestIid ? "${env.gitlabMergeRequestIid}" : "${env.gitlabMergeRequestIid_replay}"}"
        gitlabMergeRequestLastCommit = "${env.gitlabMergeRequestLastCommit ? "${env.gitlabMergeRequestLastCommit}" : "${env.gitlabMergeRequestLastCommit_replay}"}"
        gitlabSourceRepoSshUrl = "${env.gitlabSourceRepoSshUrl ? "${env.gitlabSourceRepoSshUrl}" : "${env.gitlabSourceRepoSshUrl_replay}"}"
    }
    triggers {
        gitlab(
                triggerOnPush: true,
                triggerOnMergeRequest: true,
                triggerOpenMergeRequestOnPush: "never",
                triggerOnNoteRequest: true,
                noteRegex: 'mr_lint_restart',
                skipWorkInProgressMergeRequest: true,
                ciSkip: true,
                pendingBuildName: "MR_LINT",
                setBuildDescription: true,
                addNoteOnMergeRequest: true,
                addCiMessage: true,
                addVoteOnMergeRequest: true,
                acceptMergeRequestOnSuccess: true,
                branchFilterType: "All",
        )
    }
    stages{
        stage('mr lint check') {
            steps {
                script {
                    deleteDir()
                    updateGitlabCommitStatus name: "MR_LINT", state: 'running'
                    add_link()
                    sh 'env'
                    run_lint()
                }
            }
        }
    }
    options {
      gitLabConnection('Gitlab connector by hobot.ci')
      timeout(10)
    }
    post {
      failure {
        updateGitlabCommitStatus name: 'MR_LINT', state: 'failed'
        // addGitLabMRComment(comment: "Lint Result: SUCCESS\n\nJob Link: ${env.BUILD_URL}console\n\n")
        addGitLabMRComment(comment: "Lint Result: failed\n\nJob Link: ${env.BUILD_URL}\n\n")
      }
      success {
        updateGitlabCommitStatus name: 'MR_LINT', state: 'success'
        //addGitLabMRComment(comment: "Lint Result: ${currentBuild.currentResult}\n\nJob Link: ${env.BUILD_URL}console\n\n")
      }
      unstable {
        updateGitlabCommitStatus name: "MR_LINT", state: 'success'
      }
      aborted {
        updateGitlabCommitStatus name: "MR_LINT", state: 'success'
      }
    }
}

def run_lint(){
    sh '''#!/bin/bash
        tool_dir=/root/.ssh/lint_config/cicd
 
        git clone "${gitlabSourceRepoSshUrl}" -b "${gitlabTargetBranch}" lintcode
        
        cd lintcode
        GIT_COMMIT=$(git rev-parse HEAD)
        echo "code base: $GIT_COMMIT" 
        merge_ret=$(git merge origin/"${gitlabSourceBranch}" --message "mr check merge")
        cmd_ret=$?
        echo "merge result: \n $merge_ret"
        if [[  $cmd_ret -ne 0 ]];then
          echo "Error: Merge Failed"
          exit -1
        fi
        
        cp -f /root/.ssh/get_changed_lines_new.py .
        python3 get_changed_lines_new.py -o .changed_file_lines.json -b $GIT_COMMIT
        
        if [ -e .arcconfig ]; then
            arc_config=$(cat .arcconfig)
            if [[  "$arc_config" =~ "ph.hobot.cc" ]]; then 
                sed -i -e 's#http://ph.hobot.cc#https://cr.hobot.cc#g' .arcconfig
            fi
        fi
        
        # cp -f ${tool_dir}/.arclint  ./
        python3 /root/.ssh/lint_config/cicd/tools/parse_lint.py
        cp -f ${tool_dir}/tools/checkstyle-6.13-all.jar  .
        cp -f ${tool_dir}/tools/google_checks.xml  .
        
        if [ -e src/main.rs ]; then
          cp -f $all_in_one/Cargo.toml  .
        fi
        
        
        arc lint --output json  --only-changed --rev $GIT_COMMIT --outfile .phabricator-lint.bak

        if [ ! -e .phabricator-lint.bak ] ; then 
            echo "Error: arc lint failed"
            exit -1
        fi
        
        cp -f /root/.ssh/select_lint_by_changed_lines.py .
        python3 select_lint_by_changed_lines.py -l .phabricator-lint.bak -f .changed_file_lines.json

        cp -f ${tool_dir}/tools/phformatter.py ./
        python3 phformatter.py -c .phabricator-lint.org
        echo 'Lint 报错信息为:'
        cat .phabricator-console
        sleep 1
        error_count=$(grep -i '"severity": "error"' .phabricator-lint | wc -l)
        python3 /root/.ssh/lint_config/cicd/tools/analyzeNloc.py -p $(pwd) -b $GIT_COMMIT -r ${gitlabSourceRepoSshUrl}
        if [ -f "error.txt" ]; then
        error_count=$(($error_count + 1))
        fi
        if [ ${error_count} -gt 0 ] ; then 
            if [ -f "error.txt" ]; then
            cat error.txt
            fi
            echo "ERORR: Lint check Failed, found $error_count errors by CI Robot"
            exit -1 
        fi 
        exit 0;
    '''
}

def add_link() {
    short_text = "ACTION: ${env.gitlabActionType} - FROM: ${env.gitlabSourceBranch} TO: ${env.gitlabTargetBranch} - REPO: ${env.gitlabSourceRepoSshUrl}"
    short_link = "${env.gitlabSourceRepoHomepage}/merge_requests/${env.gitlabMergeRequestIid}" 
    addShortText(
        text: "${short_text}", 
        padding: "1", 
        border: "0", 
        margin: "0px", 
        background: "transparent", 
        color: "blue", 
        link: "${short_link}"
    )
}

GERRIT_LINT

pipeline {
    agent {label 'Gerrit_LINT_NODE'}
    environment {
        LANG = 'en_US.UTF-8'
        SSH_KEY_FILE = credentials('')
        USERNAME = 'hobot.ci'
        SSH_URL = "ssh://${USERNAME}@gerrit.hobot.cc:29418"
        MANIFEST_PROJECT = "${SSH_URL}/Test.git"
        MANIFEST_XML = 'manifest.xml'
        REPO_URL = "${SSH_URL}/SCM/git-repo.git"
        LINT_CICD_PATH = "/home/lint/cicd"
        GERRIT_CREDENTIALS_ID = ''
        
    }
    stages{
        stage('LINT') {
          steps{
                script {
                    try {
                        init_code()
                    } catch (Exception e) {
                        gerritReview labels: ["Ci-Lint": -1], message: "Patch failed! Rebase to branch HEAD!"
                        currentBuild.getRawBuild().getExecutor().interrupt(Result.FAILURE)
		                sleep(5)
                    }
                    try {
                        lint()
                    } catch (Exception e) {
                        gerritReview labels: ["Ci-Lint": -1], message: "LINT Execution FAILED!"
                        currentBuild.getRawBuild().getExecutor().interrupt(Result.FAILURE)
		                sleep(5)
                    }
                }
                
            }
        }
        stage('Analysis') {
            steps {
                analysis()
            }
        }
    }
    post {
        success {
            gerritReview labels: ["Ci-Lint": +1], message: "LINT SUCCESSFUL"
        }
    }
}

def init_code()
{
    sh '''#!/bin/bash -x
    echo "--------------------"
    env
    echo "--------------------"
    echo ${REPO_URL}
    echo ${GERRIT_BRANCH}
    echo ${GERRIT_REFSPEC}
    echo "------------------------------------------------------------------------------------------------------------"
    
    
    rm -rf ./*
    git clone --depth 3 -b ${GERRIT_BRANCH}  ${SSH_URL}/${GERRIT_PROJECT} lintcode 
    '''
    env.BASE_COMMIT = sh (returnStdout:true, script: 'cd lintcode && git rev-parse HEAD').trim()
    echo "BASE_COMMIT: ${env.BASE_COMMIT}" 

    sh '''
    cd lintcode
    patch_ret=$( git pull ${SSH_URL}/${GERRIT_PROJECT} ${GERRIT_REFSPEC}  2>&1 ) 
    # echo $patch_ret
    '''
}

def lint() {
    sh '''
    cd lintcode
    cp -f $LINT_CICD_PATH/tools/parse_lint_gerrit.py .
    python3 parse_lint_gerrit.py
    
    # cp -f $LINT_CICD_PATH/.arclint  .    
    cp -f $LINT_CICD_PATH/tools/checkstyle-6.13-all.jar  .
    cp -f $LINT_CICD_PATH/tools/google_checks.xml  .
    

    if [ -e src/main.rs ]; then
          cp -f $LINT_CICD_PATH/tools/Cargo.toml  .
    fi
    

    cp -f $LINT_CICD_PATH/tools/get_changed_lines_new.py .
    python3 get_changed_lines_new.py -o .changed_file_lines.json -b $BASE_COMMIT
    # cat .changed_file_lines.json

    echo "------------------------------------------------------------------------------------------------------------"
    arc lint --output json  --only-changed  --outfile .phabricator-lint.bak
    
    cat .phabricator-lint.bak
    if [ ! -e .phabricator-lint.bak ] ; then 
        echo "Error: arc lint failed"
        exit -1
    fi
    
    if [ ! -s .phabricator-lint.bak ] ; then 
        echo "No problem found!"
        echo "{"noproblemfound":[]}" > .phabricator-lint.bak
    fi
    

    '''
}

def gerrit_rest_url(rest_type, gerrit_url, gerrit_data) {
    resp_json_f = sh (script: '''
    if touch "${WORKSPACE}/rest_response.json" > /dev/null 2>&1; then
        echo -n "${WORKSPACE}/rest_response.json"
    else
        echo -n '-'
    fi
    ''',
    returnStdout: true)
    writeFile file: "${WORKSPACE}/rest_data.json", text: gerrit_data
    withCredentials([usernamePassword(
        credentialsId: "${env.GERRIT_CREDENTIALS_ID}", usernameVariable: 'username', passwordVariable: 'password')]) {
        script {
        sh """
            echo "Sending data from ${WORKSPACE}/rest_data.json"
            cat ${WORKSPACE}/rest_data.json

            curl -X ${rest_type} \
                 -u $username:$password \
                 -H "Content-Type: application/json; charset=UTF-8" \
                 -H "Accept: application/json" \
                 -o ${resp_json_f} \
                 -d @"${WORKSPACE}/rest_data.json" \
                 ${gerrit_url}
            sed -i "/^)]}'\$/d" ${resp_json_f}
            cat ${resp_json_f}
        """
        }
    }
}


def gerrit_rest_all_patch(rest_type, gerrit_app, gerrit_data) {
    gerrit_url = "https://gerrit.hobot.cc:8443/a/changes/${env.GERRIT_CHANGE_NUMBER}/${gerrit_app}"
    gerrit_rest_url(rest_type, gerrit_url, gerrit_data)
}


def gerrit_resolve_comment(id, commit_id, message) {
    data = """{
        "reason": '${message}'
    }"""
    gerrit_rest_all_patch('POST', "revisions/${commit_id}/comments/${id}/delete", data)
}

def gerrit_resolve_comment_by_tag(target_tag) {
    gerrit_rest_all_patch("GET", 'comments', '')
    def comments_resp = readJSON file: "${WORKSPACE}/rest_response.json"
    comments_resp.each { cmt_target, comments ->
        if (cmt_target != "/PATCHSET_LEVEL") {
            comments.each {cmt_json->
                cmt_tag = cmt_json.tag
                cmt_unresolved = cmt_json.unresolved
                if (cmt_tag != null && cmt_tag == target_tag && cmt_unresolved == true) {
                    cmt_id = cmt_json.id
                    cmt_commit_id = cmt_json.commit_id
                    cmt_message = cmt_json.message
                    gerrit_resolve_comment(cmt_id, cmt_commit_id, cmt_message)
                }
            }
        }
    }
}

def gerrit_rest(rest_type, gerrit_app, gerrit_data) {
    gerrit_url = "https://gerrit.hobot.cc:8443/a/changes/${env.GERRIT_CHANGE_NUMBER}/revisions/${env.GERRIT_PATCHSET_REVISION}/${gerrit_app}"
    gerrit_rest_url(rest_type, gerrit_url, gerrit_data)
}


def gerrit_review_post(gerrit_data) {
    gerrit_rest('POST', 'review', gerrit_data)
}

def gerrit_line_comments_combine(file_list, tag) {
    if (file_list.size() == 0)
    {
        echo "No comments to be processed!"
        return 0
    }
    data_cmt_str = ""
    file_list.each{ file, cmts ->
        cmts_str = ""
        cmts.each{ cmt_map ->
            cmt_map.each{ line, line_cmt ->
                echo "Processing ${file}:${line} ${line_cmt}"
                // line_cmt = line_cmt.replaceAll('$', '\\$')
                // echo "After replaceAll: ${line_cmt}"
                cmts_str = """
                {
                    "line": ${line},
                    "message": \"${line_cmt}\",
                    "unresolved": true
                },${cmts_str}"""
            }
        }
        cmts_str.trim()
        if (data_cmt_str != "")
        {
            data_cmt_str += ","
        }
        data_cmt_str += """
        "${file}":[
            ${cmts_str}]
        """
    }
    data = """{
        "tag": "${tag}",
        "message": "Line Comments for ${tag}",
        "comments": {
            ${data_cmt_str}
        }
    }"""
    gerrit_review_post(data)
}

def analysis()
{
    lint_err_str = readFile file: "lintcode/.phabricator-lint.bak"
    changed_file_lines = readJSON file: "lintcode/.changed_file_lines.json"
    lint_failed = false
    def file_list = [:]
    def file_list_tmp = [:]
    lint_err_str.split("\\r?\\n").each {lint_err_line ->
        if (lint_err_line == '') {
            return
        }
        lint_errors = readJSON text: lint_err_line
        lint_errors.each { file, file_errors ->
            echo "Processing ${file}"
            if (changed_file_lines.containsKey(file)) {
                cur_f_changed_lines = changed_file_lines[file]
                file_errors.each { file_error ->
                    if (cur_f_changed_lines.contains(file_error.line) && file_error.severity == 'error') {
                        if (!file_list[file]) {
                            file_list[file] = []
                        }
                        lint_failed = true
                        msg_str = """Error: ${file_error.name}, ${file_error.description}"""
                        file_list[file].add("${file_error.line}": msg_str)
                        echo "${file}:${file_error.line}, ${msg_str}"
                    }
                }
            }
        }
        
    }
    gerrit_resolve_comment_by_tag('lint')
    if (lint_failed) {
        gerritReview labels: ["Ci-Lint": -1], message: "LINT Error Found! $BUILD_URL"
        gerrit_line_comments_combine(file_list, 'lint')
        currentBuild.getRawBuild().getExecutor().interrupt(Result.FAILURE)
        sleep(5)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值