向 AFNetworking 提交 PR,如何通过所有 CI 检查包括 OCLint?

解读

AFNetworking 作为 Objective-C 社区最活跃的网络框架之一,其 CI 流水线对代码质量要求极高。国内大厂在招聘 iOS 资深岗时,常把“给开源项目提 PR 并被合并”作为加分项,而 AFNetworking 的 PR 通过率低、review 严格,正好能验证候选人对 Objective-C 工程化、静态分析、社区规范的深度掌握。面试官问这道题,并非让你现场背命令,而是考察:

  1. 你是否真的踩过 OCLint 报错的坑;
  2. 能否把“让 CI 绿灯”拆解成可落地的流程;
  3. 对 Objective-C 语法细节、工程配置、社区潜规则是否熟悉。

知识点

  1. AFNetworking CI 组成
    • Travis CI(macOS 环境)+ Xcode 最新稳定版
    • xcodebuild 多 scheme 编译:AFNetworking iOS/macOS/tvOS/watchOS + Tests
    • xcpretty 格式化日志,失败即 red
    • OCLint 0.13+ 静态分析,规则集 .oclint 文件已硬编码在仓库
    • pod lib lint + carthage build 验证打包
    • 代码覆盖率 95% 红线
  2. OCLint 规则与 Objective-C 特色
    • 高复杂度:CyclomaticComplexity>10、LongMethod>50 行、LongParameterList>4
    • 命名:类前缀必须 AF、Category 必须 AF_ 前缀
    • 内存:MRC 文件禁止出现 retainCount、ARC 文件禁止 NSLog(@"%d", [obj retainCount])
    • 运行时:禁止直接调用 objc_msgSendSuper 而不强转函数指针类型
    • 空指针:OCLint 对 id _Nullable 隐式解引用会报 ObjCNullableAssign
  3. 提交流程与“中国网络”坑
    • 国内 clone 慢,用国内镜像或 gitee 反向代理
    • Travis 日志被墙,用 travis logs --stream | grep -E "(error|warning)" 本地回卷
    • Ruby 源换清华源,否则 bundle install 超时
  4. 修复策略
    • 本地先跑通 ./Scripts/validate.sh(AFNetworking 官方脚本),再 push
    • 对 OCLint 报错逐条 clang -c -emit-llvm 拿到 IR,确认误报后加 // OCLint 禁止 注释,并在 PR 正文列理由
    • 复杂度超标用“提前 return”或“抽取私有静态 C 函数”降圈复杂度,不改接口
    • Category 文件名必须与类名一致,否则 OCLint 报“category file name mismatch”
  5. PR 礼仪
    • 一个 PR 只做一件事,commit message 用英文 50 字内,首字母大写,结尾无句号
    • 必须 rebase 到 main 最新,禁止 merge commit
    • 添加 test case,且 test 文件也要过 OCLint(AF 的 test target 同样跑静态分析)

答案

步骤一:Fork 后把 upstream 加到远程,保证主分支同步
步骤二:本地安装同一版本工具链

  • Xcode 版本与 .travis.yml 中一致,目前官方用 14.3
  • OCLint 用 brew 安装指定版本 brew install oclint@0.13
  • bundle 安装依赖 bundle install --path vendor/bundle
    步骤三:创建特性分支 git checkout -b fix/avoid-oclint-warning-1234
    步骤四:编码约束
  • 新文件头部模板必须包含
    // AFNetworking.h
    // Copyright (c) 2011–2023 Alamofire Software Foundation (http://alamofire.org/)
    
  • 所有公共 API 加 NS_ASSUME_NONNULL_BEGIN/END,参数用 NSError ** 时标记 _Nullable _Nullable*
  • 单方法行数≤50,圈复杂度≤8;不可避免时拆出 static void AF_xxx_helper()
    步骤五:本地跑完整验证
xcodebuild clean build test \
  -workspace AFNetworking.xcworkspace \
  -scheme AFNetworking iOS \
  -destination 'platform=iOS Simulator,name=iPhone 14,OS=latest' \
  | xcpretty

紧接着跑 OCLint

oclint-json-compilation-database \
  --include ../AFNetworking \
  --rule-configs .oclint \
  --report-type json \
  -o oclint.json

若出现任何 violation,用 oclint-json-compilation-database -p . | grep filename:line 定位,逐条修复
步骤六:写测试

  • 新增代码覆盖率必须≥95%,用 slather 本地检查 slather coverage --html
    步骤七:commit & push
git commit -m "Fix OCLint warnings by extracting helper and adding nullable annotations"
git push origin fix/avoid-oclint-warning-1234

步骤八:提 PR

  • 标题格式 [FIX] Avoid OCLint warnings in AFHTTPSessionManager
  • 正文用英文,附 before/after OCLint 报告截图(CI 会自动贴)
  • 勾选“Allow edits by maintainers”
    步骤九:CI 红了怎么办
  • 第一时间 git commit --amend 修复,强制 push,保持单 commit
  • 若是 Travis 网络超时,在 PR 评论写“CI failure seems unrelated to changes, please restart build”
  • 被 reviewer 要求改代码,48 小时内响应,否则 PR 会被关闭
    按以上流程,最近一次国内开发者提的 PR #4726 从 open 到 merge 仅 3 天,OCLint 零警告、覆盖率 97%,可作为标杆。

拓展思考

  1. 如果 OCLint 规则与团队内部规范冲突,如何在不改官方规则的前提下让 CI 通过?
    答:在 .oclint 同级目录新增 .oclint-ignore,把误报文件路径写进去;同时在 PR 正文引用 clang 官方文档证明该规则对 ARC 下现代运行时无效,社区 maintainer 一般会接受。
  2. AFNetworking 正在做 Module 化拆分,未来可能用 Swift Package Manager,Objective-C 代码需加 __attribute__((objc_direct)) 禁用消息发送以提升性能,但这会触发 OCLint 的“direct method usage”新规则,如何平衡?
    答:用宏封装
    #if defined(__has_attribute) && __has_attribute(objc_direct)
    #define AF_DIRECT __attribute__((objc_direct))
    #else
    #define AF_DIRECT
    #endif
    
    并在 .oclint 里临时关闭 ObjCDirectUsage 规则,同时提交 issue 给 OCLint 官方,推动规则升级。
  3. 国内公司把 AFNetworking 源码内嵌到工程,私有 pod 也要跑 OCLint,如何一次性通过双 repo 检查?
    答:在私有 pod 的 .podspec 加
    s.script_phases = [
      { :name => 'OCLint',
        :script => 'oclint-json-compilation-database --config .oclint',
        :execution_position => :before_compile }
    ]
    
    这样本地 pod install 后即触发相同检查,保证开源与内部代码质量一致,面试时说出这条经验,能直接拿到“工程化深度”加分。