Hi @ricky-git, hi @jheimbuck_gl
After all those struggles I can provide some working solution for CodeClimate + SwiftLint for Swift project.
Disclaimer
- My configuration was created to be compliant with GitLab 12. Now my company (finally!) moved to GitLab 13 (enterprise edition) so some parts of configuration could be deprecated soon.
- My iOS projects have Merge Train enabled, therefore I had to duplicate quite a lot in my config file since I don’t know any better
Basically you need to plug-in SwiftLint into your pipeline and then you need to convert the SwiftLint output to CodeClimate’s format. I choose to produce SwiftLint’s output as JSON since it’s fairly easy to convert this into CodeClimate’s JSON. To do so I create simple Python script but I believe that SwiftLint could have already GitLab-friendly option to generate output already formatted.
Part I - Fastlane & SwiftLint
I use Fastlane, so my SwiftLint is wrapped by Fastlane like so:
# just a part of Fastfile
lane :lint do
swiftlint(
reporter: "json",
output_file: "fastlane/swiftlint.json"
)
end
Part II - Python converter
I’m not a super-duper Python coder and I didn’t care that much about hard-coded paths, but please change it as you want
import json
import sys
import uuid
def convert(issue):
codeClimateItem = {
'description': issue["reason"],
'fingerprint': str(uuid.uuid4()),
'location': {
'path': issue["file"],
'lines': {
'begin': issue["line"]
}
}
}
return codeClimateItem
def main(argv):
print("[CodeClimate Converter] Running the script")
with open("fastlane/swiftlint.json", "r") as swiftLintData:
issues = json.load(swiftLintData)
print("[CodeClimate Converter] Loaded fastlane/swiftlint.json file, converting....")
convertedIssues = list(map(convert, issues))
print("[CodeClimate Converter] Converted, writing to file fastlane/codequality_report.json")
with open("fastlane/codequality_report.json", "w") as codeClimateData:
json.dump(convertedIssues, codeClimateData, indent=4, sort_keys=True)
if __name__ == "__main__":
main(sys.argv[1:])
Part III - glue it together with .gitlab-ci.yml
stages:
- linting
- testing
- deploy
variables:
LC_ALL: "en_US.UTF-8"
LANG: "en_US.UTF-8"
before_script:
- git config user.email "gitlab-ci@mycompany.com"
- git config user.name "GitLab CI"
include:
- template: Code-Quality.gitlab-ci.yml
code_quality:
dependencies: []
stage: linting
artifacts:
paths:
- fastlane/codequality_report.json
reports:
codequality: fastlane/codequality_report.json
script:
- fastlane lint
- python3 codeclimate_converter.py
tags:
- ios
code_quality_on_merge_request:
dependencies: []
stage: linting
artifacts:
paths:
- fastlane/codequality_report.json
reports:
codequality: fastlane/codequality_report.json
script:
- fastlane lint
- python3 codeclimate_converter.py
tags:
- ios
only:
- merge_requests
unit_tests:
dependencies: []
stage: testing
artifacts:
paths:
- test_logs/test_report.xml
reports:
junit: test_logs/test_report.xml
script:
- fastlane unit_tests
tags:
- ios
unit_tests_on_merge_request:
dependencies: []
stage: testing
artifacts:
paths:
- test_logs/test_report.xml
reports:
junit: test_logs/test_report.xml
script:
- fastlane unit_tests
tags:
- ios
only:
- merge_requests
deploy_to_test_flight:
dependencies: []
stage: deploy
artifacts:
paths:
- fastlane/screenshots
- fastlane/logs
script:
- fastlane deploy
tags:
- ios
when: manual
@ricky-git As you can see I duplicated some jobs. That’s because when everything was set up to be only: merge_requests
then no CodeClimate report was visible because it has to be stored in between jobs (se my previous discussion with @jheimbuck_gl).
If I removed all the merge_requests
then Merge Train stopped to work
@jheimbuck_gl would you like to have some feedback about my ugly duplication? Is there anything from GitLab 13 that I can use to have just 1 jobs that will make CodeClimate widget and Merge Train happy at the same time?