การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

SonarQube เป็นแพลตฟอร์มการประกันคุณภาพโค้ดแบบโอเพ่นซอร์สที่รองรับภาษาการเขียนโปรแกรมที่หลากหลาย และให้การรายงานเกี่ยวกับตัวชี้วัด เช่น การทำสำเนาโค้ด การปฏิบัติตามมาตรฐานการเข้ารหัส ความครอบคลุมของการทดสอบ ความซับซ้อนของโค้ด จุดบกพร่องที่อาจเกิดขึ้น และอื่นๆ SonarQube แสดงภาพผลการวิเคราะห์ได้อย่างสะดวก และช่วยให้คุณติดตามการเปลี่ยนแปลงของการพัฒนาโครงการเมื่อเวลาผ่านไป

วัตถุประสงค์: แสดงให้นักพัฒนาทราบถึงสถานะของการควบคุมคุณภาพซอร์สโค้ดใน SonarQube

มีสองวิธี:

  • เรียกใช้สคริปต์เพื่อตรวจสอบสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube หากการควบคุมคุณภาพของซอร์สโค้ดใน SonarQube ไม่ผ่าน แสดงว่าแอสเซมบลีล้มเหลว
  • แสดงสถานะการควบคุมคุณภาพซอร์สโค้ดบนหน้าโครงการหลัก

การติดตั้ง SonarQube

ในการติดตั้ง sonarqube จากแพ็คเกจ rpm เราจะใช้พื้นที่เก็บข้อมูล https://harbottle.gitlab.io/harbottle-main.

มาติดตั้งแพ็คเกจด้วยที่เก็บสำหรับ CentOS 7

yum install -y https://harbottle.gitlab.io/harbottle-main/7/x86_64/harbottle-main-release.rpm

เราติดตั้ง sonarqube เอง

yum install -y sonarqube

ระหว่างการติดตั้ง ปลั๊กอินส่วนใหญ่จะถูกติดตั้ง แต่คุณต้องติดตั้ง findbugs และ pmd

yum install -y sonarqube-findbugs sonarqube-pmd

เปิดบริการและเพิ่มลงในการเริ่มต้น

systemctl start sonarqube
systemctl enable sonarqube

หากใช้เวลานานในการโหลด ให้เพิ่มตัวสร้างตัวเลขสุ่ม /dev/./urandom ต่อท้ายตัวเลือก sonar.web.javaOpts

sonar.web.javaOpts=другие параметры -Djava.security.egd=file:/dev/urandom

การเรียกใช้สคริปต์เพื่อตรวจสอบสถานะของการควบคุมคุณภาพซอร์สโค้ดใน SonarQube

น่าเสียดายที่ปลั๊กอิน sonar-break-maven-plugin ยังไม่ได้รับการอัปเดตเป็นเวลานาน งั้นเรามาเขียนสคริปต์ของเราเองกันดีกว่า

สำหรับการทดสอบเราจะใช้พื้นที่เก็บข้อมูล https://github.com/uweplonus/spotbugs-examples.

นำเข้าสู่ Gitlab เพิ่มไฟล์ .gitlab-ci.yml:

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=~/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  SONAR_HOST_URL: "http://172.26.9.226:9000"
  LOGIN: "admin" # логин sonarqube
  PASSWORD: "admin" # пароль sonarqube

cache:
  paths:
    - .m2/repository

build:
  image: maven:3.3.9-jdk-8
  stage: build
  script:
    - apt install -y jq || true
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.failure.ignore=true org.jacoco:jacoco-maven-plugin:0.8.5:prepare-agent clean verify org.jacoco:jacoco-maven-plugin:0.8.5:report
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.skip=true verify sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$LOGIN -Dsonar.password=$PASSWORD -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
    - export URL=$(cat target/sonar/report-task.txt | grep ceTaskUrl | cut -c11- ) #URL where report gets stored
    - echo $URL
    - |
      while : ;do
          curl -k -u "$LOGIN":"$PASSWORD" "$URL" -o analysis.txt
          export status=$(cat analysis.txt | jq -r '.task.status') #Status as SUCCESS, CANCELED, IN_PROGRESS or FAILED
          echo $status
          if [ ${status} == "SUCCESS" ];then
            echo "SONAR ANALYSIS SUCCESS";
            break
          fi
          sleep 5
      done
    - curl -k -u "$LOGIN":"$PASSWORD" "$URL" -o analysis.txt
    - export status=$(cat analysis.txt | jq -r '.task.status') #Status as SUCCESS, CANCELED or FAILED
    - export analysisId=$(cat analysis.txt | jq -r '.task.analysisId') #Get the analysis Id
    - |
      if [ "$status" == "SUCCESS" ]; then
        echo -e "SONAR ANALYSIS SUCCESSFUL...ANALYSING RESULTS";
        curl -k -u "$LOGIN":"$PASSWORD" "$SONAR_HOST_URL/api/qualitygates/project_status?analysisId=$analysisId" -o result.txt; #Analysis result like critical, major and minor issues
        export result=$(cat result.txt | jq -r '.projectStatus.status');

        if [ "$result" == "ERROR" ];then
          echo -e "91mSONAR RESULTS FAILED";
          echo "$(cat result.txt | jq -r '.projectStatus.conditions')"; #prints the critical, major and minor violations
          exit 1 #breaks the build for violations
        else
          echo -e "SONAR RESULTS SUCCESSFUL";
          echo "$(cat result.txt | jq -r '.projectStatus.conditions')";
          exit 0
        fi
      else
          echo -e "e[91mSONAR ANALYSIS FAILEDe[0m";
          exit 1 #breaks the build for failure in Step2
      fi
  tags:
    - docker

ไฟล์ .gitlab-ci.yml ไม่สมบูรณ์ ทดสอบว่างานการสแกนใน sonarqube จบลงด้วยสถานะ: "SUCCESS" หรือไม่ จนถึงขณะนี้ยังไม่มีสถานะอื่น ทันทีที่มีสถานะอื่นๆ ฉันจะแก้ไข .gitlab-ci.yml ในโพสต์นี้

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดบนหน้าโครงการหลัก

การติดตั้งปลั๊กอินสำหรับ SonarQube

yum install -y sonarqube-qualinsight-badges

เราไป SonarQube ที่ http://172.26.9.115:9000/
สร้างผู้ใช้ทั่วไป เช่น "ตราสัญลักษณ์"
เข้าสู่ระบบ SonarQube ภายใต้ผู้ใช้รายนี้

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

ไปที่ “บัญชีของฉัน” สร้างโทเค็นใหม่ เช่น ด้วยชื่อ “read_all_repository” แล้วคลิก “สร้าง”

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

เราเห็นว่ามีโทเค็นปรากฏขึ้น เขาจะปรากฏเพียง 1 ครั้งเท่านั้น

เข้าสู่ระบบในฐานะผู้ดูแลระบบ

ไปที่การกำหนดค่า -> ป้าย SVG

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

คัดลอกโทเค็นนี้ลงในช่อง "โทเค็นป้ายกิจกรรม" แล้วคลิกปุ่มบันทึก

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

ไปที่การดูแลระบบ -> ความปลอดภัย -> เทมเพลตการอนุญาต -> เทมเพลตเริ่มต้น (และเทมเพลตอื่น ๆ ที่คุณจะมี)

ผู้ใช้ป้ายจะต้องทำเครื่องหมายในช่อง "เรียกดู"

การทดสอบ

ตัวอย่างเช่น เรามาทำโครงการกัน https://github.com/jitpack/maven-simple.

มานำเข้าโครงการนี้กันเถอะ

เพิ่มไฟล์ .gitlab-ci.yml ไปยังรูทโปรเจ็กต์โดยมีเนื้อหาดังต่อไปนี้

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=~/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  SONAR_HOST_URL: "http://172.26.9.115:9000"
  LOGIN: "admin" # логин sonarqube
  PASSWORD: "admin" # пароль sonarqube

cache:
  paths:
    - .m2/repository

build:
  image: maven:3.3.9-jdk-8
  stage: build
  script:
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.failure.ignore=true org.jacoco:jacoco-maven-plugin:0.8.5:prepare-agent clean verify org.jacoco:jacoco-maven-plugin:0.8.5:report
    - mvn $MAVEN_CLI_OPTS -Dmaven.test.skip=true verify sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$LOGIN -Dsonar.password=$PASSWORD -Dsonar.gitlab.project_id=$CI_PROJECT_PATH -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
  tags:
    - docker

ใน SonarQube โครงการจะมีลักษณะดังนี้:

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

เพิ่มกระเป๋าไปที่ README.md และจะมีลักษณะดังนี้:

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

รหัสที่แสดงป้ายมีลักษณะดังนี้:

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

แยกวิเคราะห์สตริงการแสดงป้าย:

[![Quality Gate](http://172.26.9.115:9000/api/badges/gate?key=com.github.jitpack:maven-simple)](http://172.26.9.115:9000/dashboard?id=com.github.jitpack%3Amaven-simple)
[![Название](http://172.26.9.115:9000/api/badges/gate?key=Project Key)](http://172.26.9.115:9000/dashboard?id=id-проекта)
[![Coverage](http://172.26.9.115:9000/api/badges/measure?key=com.github.jitpack:maven-simple&metric=coverage)](http://172.26.9.115:9000/dashboard?id=com.github.jitpack%3Amaven-simple)
[![Название Метрики](http://172.26.9.115:9000/api/badges/measure?key=Project Key&metric=МЕТРИКА)](http://172.26.9.115:9000/dashboard?id=id-проекта)

จะรับ/ตรวจสอบรหัสโครงการและรหัสโครงการได้ที่ไหน

รหัสโครงการอยู่ที่ด้านล่างขวา URL มีรหัสโครงการ

การแสดงสถานะการควบคุมคุณภาพซอร์สโค้ดใน SonarQube แก่นักพัฒนา

ตัวเลือกในการรับเมตริกสามารถเป็นได้ ดูที่นี่.

คำขอดึงทั้งหมดสำหรับการปรับปรุงแก้ไขข้อบกพร่อง ส่งไปยังที่เก็บนี้.

โทรเลขสนทนาเกี่ยวกับ SonarQube https://t.me/sonarqube_ru
แชททางโทรเลขเกี่ยวกับ DevSecOps - DevOps ที่ปลอดภัย https://t.me/sec_devops

ที่มา: will.com

เพิ่มความคิดเห็น