iOS 通过社区版SonarQube实现代码质量扫描踩坑记

上车前言

SonarQube是一个开源的代码质量分析工具,分为四个版本,分别为Community、Developer、Enterprise、Data Center,其中Community社区版免费开源,其他三个版本都是收费版,一般来说,社区版就符合大多数开发者的需求,针对很多语言都可以免费扫描。不过对于iOS,社区版不支持Objective-CSwift的扫描,只有开发版以上才支持,所以我们不得不借助开源插件Sonar-swift来实现我们iOS工程的代码扫描分析。。。

后续还需要做代码上架审核扫描功能,有经验的大佬可以提供一下思路或者参考文章吗?需求大概就是扫描关键词,例如UIWebview,alipay,wechat等特殊字符,还有扫描私有api或者触及苹果审核任何相关项,跪求了。。。

所需环境配置和软件安装

1.安装SonarQube

注意点:不要使用最新的SonarQube版本,因为有同学说Sonar-swiftjar包并不支持最新版本,我这边使用的是官方长期支持和维护的版本SonarQube 8.9.7,下载后,放到你想要的目录,最好是一个扫描工具的单独目录!

2.安装Java 11

注意点:为什么需要安装Java 11 而不是Java 8或者其它版本的。因为有同学说SonarQube scannerSonarQube server 支持Java 11,其它版本的Java版本不被SonarQube官方支持!

下载Java 11, 前往官网下载Java SE Development Kit 11.0.16,这个要注册Oracle的账号之类的...安装手册可以查看 Installation of the JDK on macOS
安装完之后执行以下查看是否成功:

java 11.0.16 2022-03-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.16+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.16+9-LTS, mixed mode)

在没安装Java 11之前我使用的Java 8 导致打开 http://localhost:9000打开不开!!!

3.安装Sonar-scranner

使用Homebrew安装

brew install sonar-scanner

查看是否安装成功还有版本

% sonar-scanner --version
INFO: Scanner configuration file: /opt/homebrew/Cellar/sonar-scanner/4.7.0.2747/libexec/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.7.0.2747
INFO: Java 11.0.15 Homebrew (64-bit)
INFO: Mac OS X 12.4 aarch64
4.安装PostgreSQL数据库

使用Homebrew安装

brew install postgresql

安装完成后初始化数据库

initdb /usr/local/var/postgres

启动服务

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

创建数据库和账户
Mac安装Postgresql后不会创建用户名数据库,执行命令:

createdb

接着输入下面命令登录Postgresql控制台:

psql

数据库安装好之后,我们要提供一个数据库和账号给SonarQube使用

CREATE USER sonarqube WITH PASSWORD 'sonarqube';//创建用户
CREATE DATABASE sonar OWNER sonarqube;//创建属于该用户的数据库

然后我们去sonarqube-8.9.7/conf目录下编辑sonar.properties,将数据库的相关配置改为如下:

sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

编辑完之后,我们打开终端到sonarqube-8.9.7.52159/bin/macosx-universal-64下执行以下指令:

% ./sonar.sh restart
Gracefully stopping SonarQube...
Waiting for SonarQube to exit...
Stopped SonarQube.
Starting SonarQube...
Started SonarQube.

之后我们在浏览器打开:http://localhost:9000

sonar主界面

5.SonarQube插件包安装
  • 下载sonar-swift-0.4.6 , 从已下载的sonar-swift-0.4.6中,拷贝backelite-sonar-swift-plugin-0.4.6.jar文件到sonarqube-8.9.7/extensions/plugins里。
  • 下载汉化 sonar-l10n-zh-plugin-1.28,拷贝sonar-l10n-zh-plugin-1.28.jar文件到sonarqube-8.9.7/extensions/plugins里。
  • 下载sonar-cnes-report,最新的版本跟SonarQube 8.9.7不适配,所以选择3.3.1版本即可,把下载的sonar-cnes-report-3.3.1.jar拷贝到sonarqube-8.9.7/extensions/plugins里。
6.下载其它插件
1. 安装xcpretty
  • 使用sudo gem install -n /usr/local/bin xcpretty ,这样会有安装风险。

  • 使用sonar-swift官方推荐安装

git clone https://github.com/Backelite/xcpretty.git
cd xcpretty
git checkout fix/duration_of_failed_tests_workaround
gem build xcpretty.gemspec
sudo gem install --both xcpretty-0.3.0.gem
2.安装swiftlint
brew install swiftlint

查看安装版本:

%swiftlint --version
0.47.1
3. 安装lizard
sudo pip3 install lizard
4.安装Tailor
brew install tailor

查看安装版本:

% tailor --version
0.12.0
5.安装slather
sudo gem install -n /usr/local/bin slather

查看安装版本:

% slather version
slather 2.7.2
6.安装OCLint
  • 使用homebrew安装,但是有可能OCLint的版本不匹配你的Xcode版本导致检查出错
brew tap oclint/formulae  
brew install oclint

接着配置环境变量,根目录下打开vim .zshrc

OCLint_PATH=$HOME/XXXX/oclint-21.10
export PATH=$OCLint_PATH/bin:$PATH

保存退出后执行source .zshrc
查看安装版本:

% oclint --version
OCLint (http://oclint.org/):
OCLint version 21.10.
Built Oct 26 2021 (10:19:05).

具体可以查看的OCLint使用手册

7.新建测试项目和配置sonar文件
  • 复制 sonar-project.properties到你要代码扫描的根目录,跟 .xcodeproj同级,然后编辑sonar-project.properties文件
# Swift
sonar.swift.project=xxxxDemo.xcodeproj
sonar.swift.workspace=xxxxxDemo.xcworkspace
sonar.swift.appScheme=xxxxxDemo
sonar.swift.excludedPathsFromCoverage=.*Tests.*,.*Specs.*
#这个可以通过xcodebuild -showsdks查看本地的sdk
sonar.swift.simulator=platform=iOS Simulator,name=iPhone 13,OS=15.2
sonar.swift.tailor.config=--no-color --max-line-length=100 --max-file-length=500 --max-name-length=40 --max-name-length=40 --min-name-length=4
sonar.swift.skipTests=xxxxxDemoUITests

# OC
sonar.objectivec.project=xxxxxDemo.xcodeproj
sonar.objectivec.workspace=xxxxxDemo.xcworkspace 
sonar.objectivec.appScheme=xxxxxDemo 
sonar.objectivec.excludedPathsFromCoverage=.*Tests.*,.*Specs.*
sonar.objectivec.simulator=platform=iOS Simulator,name=iPhone 13,OS=15.2
sonar.objectivec.tailor.config=--no-color --max-line-length=100 --max-file-length=500 --max-name-length=40 --max-name-length=40 --min-name-length=4

# Common
#这个通过在本地的SonarQube主页上生成项目的时候获得
sonar.projectKey= xxxxxDemoProjKey
sonar.projectName=xxxxxDemo
# Be careful! If you'd like to upload source files that contains Pods directory, 
# you must remove Pods from .gitignore file. Otherwise nothing will be uploaded.
sonar.sources=OCSwiftDemo
sonar.projectDescription="Scanning code smells and upload to SonarQube." 
sonar.sourceEncoding=UTF-8 
# Comment if you have a project with mixed ObjC / Swift
sonar.language= Swift

# SonarQube
sonar.host.url=http://localhost:9000
#这个login通过本地的SonarQube主页生成的口令
sonar.login=dbe0f3944f65563094e86a81bf041a4edfbee873
#这个初始账号和密码都是admin,后面可以改成你自己的的
sonar.password=your sonar password
8.扫描
  • 通过run-sonar-swift.sh扫描,在扫描的工程根目录执行以下命令,即可在在SonarQube网页上浏览结果
bash run-sonar-swift.sh -nounittests -v
  • 通过SonarQube推荐的指令集
sonar-scanner \           
  -Dsonar.projectKey=TestPod \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=生成的口令,你创建扫描工程的时候会自动生成此条命令

打开http://localhost:9000找到项目查看结果

项目问题.png

9.可能遇到的问题
  • OCLint版本和Xcode版本对不上产生的脚本报错
ERROR - Command 'oclint-json-compilation-database -v --include ../TestPod/TestPod/ -- -rc 
LONG_LINE=250 -max-priority-1 10000 -max-priority-2 10000 -max-priority-3 10000 -report-type pmd
 -o sonar-reports/TestPod_-oclint.xml' failed with error code: 6
  • slather coverage报错
Computing coverage report
slather coverage --input-format profdata --cobertura-xml --output-directory
sonar-reports --scheme xxxxxDemo xxxxxxDemo.xcodeproj
.....
.....
ERROR - Command 'slather coverage --input-format profdata --cobertura-xml --output-directory 
sonar-reports --scheme xxxxxDemo xxxxxDemo.xcodeproj' failed with error code: 1

解决: Product -> Scheme -> Edit Scheme -> Test -> Options -> Code Coverage打勾。

10.设置成开机启动
  • 配置sonar开机自启,在/Users/xxx/Document下创建Config文件夹,在Config下创建startup.sh文件,添加以下脚本
# PostgreSQL开机启动
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

# Sonar开机启动,sonar的位置
sh /usr/lxxxx/sonarqube-8.9.7/bin/macosx-universal-64/sonar.sh start

# Jenkins开机自启 并做外网端口映射,jenkins的位置
java -jar /usr/local/Cellar/jenkins/2.271/libexec/jenkins.war --httpPort=8080
  • run startup.sh加入到登录启动项
    打开 系统偏好设置-> 用户和群组 -> 登录项 -> + -> 导入写好的startup.sh脚本

参考

iOS 用sonar-swift实现SonarQube代码质量扫描
iOS 静态代码分析(SonarQube + Objective-C、Swift)
sonarqube进行iOS静态代码分析
Mac安装PostgreSQL
sonar-swift

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,928评论 6 509
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,748评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,282评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,065评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,101评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,855评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,521评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,414评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,931评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,053评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,191评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,873评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,529评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,074评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,188评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,491评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,173评论 2 357

推荐阅读更多精彩内容