<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Code Quality > 原文:[https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html](https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html) * [Use cases](#use-cases) * [Example configuration](#example-configuration) * [Previous job definitions](#previous-job-definitions) * [Configuring jobs using variables](#configuring-jobs-using-variables) * [Implementing a custom tool](#implementing-a-custom-tool) * [Code Quality reports](#code-quality-reports) * [Extending functionality](#extending-functionality) * [Using Analysis Plugins](#using-analysis-plugins) * [Troubleshooting](#troubleshooting) * [Changing the default configuration has no effect](#changing-the-default-configuration-has-no-effect) * [No Code Quality report is displayed in a Merge Request](#no-code-quality-report-is-displayed-in-a-merge-request) * [Only a single Code Quality report is displayed, but more are defined](#only-a-single-code-quality-report-is-displayed-but-more-are-defined) # Code Quality[](#code-quality-starter "Permalink") [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/1984) in [GitLab Starter](https://about.gitlab.com/pricing/) 9.3. 確保項目的代碼保持簡單,易讀和易于貢獻可能會引起問題. 借助[GitLab CI / CD](../../../ci/README.html) ,您可以使用 GitLab 代碼質量分析源代碼質量. 代碼質量: * 使用[Code Climate Engines](https://codeclimate.com) ,它們是免費的開放源代碼. 代碼質量不需要"代碼氣候"訂閱. * 使用[默認的 Code Climate 配置](https://gitlab.com/gitlab-org/ci-cd/codequality/-/tree/master/codeclimate_defaults)在[GitLab Code Quality](https://gitlab.com/gitlab-org/ci-cd/codequality)項目中構建的 Docker 映像在[管道中](../../../ci/pipelines/index.html)運行. * 可以利用[模板](#example-configuration) . * 可與[Auto DevOps 一起使用](../../../topics/autodevops/stages.html#auto-code-quality-starter) . * 可以通過[分析插件](https://docs.codeclimate.com/docs/list-of-engines)或[自定義工具](#implementing-a-custom-tool)進行擴展. 更進一步,GitLab 可以在合并請求小部件區域中顯示"代碼質量"報告: [![Code Quality Widget](https://img.kancloud.cn/f9/95/f995e676cd1a9927dc196581c868fd26_1710x430.png)](img/code_quality.png) 觀看實踐中的代碼質量快速演練: 觀看視頻: [代碼質量:快速運行](https://www.youtube.com/watch?v=B32LxtJKo9M) . <figure class="video-container"><iframe src="https://www.youtube.com/embed/B32LxtJKo9M" frameborder="0" allowfullscreen=""></iframe></figure> **注意:**對于一位客戶,審核員發現,在 GitLab CI / CD 中使代碼質量,SAST 和容器掃描全部自動化幾乎比手動審核要好! [閱讀更多](https://about.gitlab.com/customers/bi_worldwide/) . 另請參閱" [可維護性支持的語言](https://docs.codeclimate.com/docs/supported-languages-for-maintainability)的代碼氣候列表". ## Use cases[](#use-cases "Permalink") 例如,考慮以下工作流程: 1. 您的后端團隊成員將開始新的實施,以更快地使您的應用中的某些功能. 2. 通過代碼質量報告,他們可以分析其實施如何影響代碼質量. 3. The metrics show that their code degrades the quality by 10 points. 4. 您要求同事來幫助他們進行此修改. 5. 他們都將對更改進行處理,直到"代碼質量"報告顯示不降級,僅顯示改進. 6. 您批準合并請求并授權其部署到暫存. 7. 驗證后,其更改將部署到生產中. ## Example configuration[](#example-configuration "Permalink") **注意:** GitLab 11.11 和更高版本支持以下所示的作業定義. 它還需要 GitLab Runner 11.5 或更高版本. 對于早期版本,請使用[先前的作業定義](#previous-job-definitions) . 本示例說明如何使用 GitLab CI / CD 和 Docker 在代碼上運行代碼質量. 首先,您需要配置 GitLab Runner: * 對于[Docker-in-Docker 工作流程](../../../ci/docker/using_docker_build.html#use-docker-in-docker-workflow-with-docker-executor) . * 有足夠的磁盤空間來處理生成的代碼質量文件. 例如,在[GitLab 項目上](https://gitlab.com/gitlab-org/gitlab) ,文件約為 7 GB. 設置運行器后,在您的 CI 配置中包括代碼質量模板: ``` include: - template: Code-Quality.gitlab-ci.yml ``` 上面的示例將在 CI / CD 管道中創建一個`code_quality`作業,該作業將掃描源代碼以查看代碼質量問題. 該報告將另存為["代碼質量"報告工件](../../../ci/pipelines/job_artifacts.html#artifactsreportscodequality-starter) ,您以后可以下載和分析該[工件](../../../ci/pipelines/job_artifacts.html#artifactsreportscodequality-starter) . 通過設置`CODE_QUALITY_IMAGE`變量,也可以覆蓋 URL 到"代碼質量"圖像. 如果您想鎖定特定版本的 Code Quality 或使用其中的一個分支,這將特別有用: ``` include: - template: Code-Quality.gitlab-ci.yml code_quality: variables: CODE_QUALITY_IMAGE: "registry.example.com/codequality-fork:latest" ``` 默認情況下,報告工件不可下載. 如果需要在工作詳細信息頁面上下載它們,則可以將`gl-code-quality-report.json`到工件路徑,如下所示: ``` include: - template: Code-Quality.gitlab-ci.yml code_quality: artifacts: paths: [gl-code-quality-report.json] ``` 包含的`code_quality`作業正在`test`階段運行,因此需要將其包含在 CI 配置中,如下所示: ``` stages: - test ``` **提示:**該信息將被自動提取并顯示在合并請求小部件中.**注意:**在自我管理的實例上,如果惡意行為者破壞了 Code Quality 作業定義,則他們將能夠在 Runner 主機上執行特權的 Docker 命令. 擁有適當的訪問控制策略,可以通過僅允許訪問受信任的參與者來減輕這種攻擊. ### Previous job definitions[](#previous-job-definitions "Permalink") **警告:**在 GitLab 11.5 之前,必須專門命名代碼質量作業和工件以自動提取報告數據并將其顯示在合并請求小部件中. 盡管這些舊的作業定義仍然保留,但它們已被棄用,并且在 GitLab 12.0 或更高版本中不再受支持. 建議您更新`.gitlab-ci.yml`配置以反映該更改. 對于 GitLab 11.5 及更高版本,該工作應如下所示: ``` code_quality: image: docker:stable variables: DOCKER_DRIVER: overlay2 allow_failure: true services: - docker:stable-dind script: - export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/') - docker run --env SOURCE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock "registry.gitlab.com/gitlab-org/ci-cd/codequality:$SP_VERSION" /code artifacts: reports: codequality: gl-code-quality-report.json ``` 在 GitLab 12.6 中,"代碼質量"切換到了[新的版本控制方案](https://gitlab.com/gitlab-org/ci-cd/codequality#versioning-and-release-cycle) . 強烈建議包括代碼質量模板,如[示例配置](#example-configuration)所示,該模板使用新的版本控制方案. 如果不使用模板,則可以將`SP_VERSION`變量硬編碼為使用新的映像版本: ``` code_quality: image: docker:stable variables: DOCKER_DRIVER: overlay2 SP_VERSION: 0.85.6 allow_failure: true services: - docker:stable-dind script: - docker run --env SOURCE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock "registry.gitlab.com/gitlab-org/ci-cd/codequality:$SP_VERSION" /code artifacts: reports: codequality: gl-code-quality-report.json ``` 對于 GitLab 11.4 和更早版本,該工作應如下所示: ``` code_quality: image: docker:stable variables: DOCKER_DRIVER: overlay2 allow_failure: true services: - docker:stable-dind script: - export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/') - docker run --env SOURCE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock "registry.gitlab.com/gitlab-org/ci-cd/codequality:$SP_VERSION" /code artifacts: paths: [gl-code-quality-report.json] ``` 或者,作業名稱可以是`codeclimate`或`codequality` ,工件名稱可以是`codeclimate.json` . 這些名稱已在 GitLab 11.0 中棄用,并可能在下一個主要版本 GitLab 12.0 中刪除. 對于 GitLab 10.3 及更早版本,該工作應如下所示: ``` codequality: image: docker:latest variables: DOCKER_DRIVER: overlay services: - docker:dind script: - docker pull codeclimate/codeclimate:0.69.0 - docker run --env CODECLIMATE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc codeclimate/codeclimate:0.69.0 init - docker run --env CODECLIMATE_CODE="$PWD" --volume "$PWD":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc codeclimate/codeclimate:0.69.0 analyze -f json > codeclimate.json || true artifacts: paths: [codeclimate.json] ``` ## Configuring jobs using variables[](#configuring-jobs-using-variables "Permalink") 代碼質量作業支持環境變量,用戶可以將其設置為在運行時配置作業執行. For a list of available environment variables, see [Environment variables](https://gitlab.com/gitlab-org/ci-cd/codequality#environment-variables). ## Implementing a custom tool[](#implementing-a-custom-tool "Permalink") 可以使用自定義工具在 GitLab 中提供代碼質量報告. 去做這個: 1. 在`.gitlab-ci.yml`文件中定義一個生成[代碼質量報告工件的作業](../../../ci/pipelines/job_artifacts.html#artifactsreportscodequality-starter) . 2. 配置您的工具以將代碼質量報告工件作為 JSON 文件生成,該文件實現了[Code Climate 規范](https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types)的子集. 代碼質量報告工件 JSON 文件必須包含具有以下屬性的對象數組: | Name | Description | | --- | --- | | `description` | A description of the code quality violation. | | `fingerprint` | 用于識別代碼質量違規的唯一指紋. 例如,MD5 哈希. | | `location.path` | 包含代碼質量違規的文件的相對路徑. | | `location.lines.begin` | 發生代碼質量違規的行. | Example: ``` [ { "description": "'unused' is assigned a value but never used.", "fingerprint": "7815696ecbf1c96e6894b779456d330e", "location": { "path": "lib/index.js", "lines": { "begin": 42 } } } ] ``` **注意:**盡管 Code Climate 規范支持更多屬性,但 GitLab 會忽略這些屬性. ## Code Quality reports[](#code-quality-reports "Permalink") 代碼質量工作完成后: * 管道生成的違反代碼質量的完整列表可在"管道詳細信息"頁面的"代碼質量"選項卡中找到. * 代碼質量的潛在更改直接在合并請求中顯示. 合并請求中的"代碼質量"窗口小部件比較分支基礎和頭部的報告,然后列出合并分支時將解決或創建的所有違例. * 完整的 JSON 報告可作為`code_quality`作業的可[下載工件](../../../ci/pipelines/job_artifacts.html#downloading-artifacts)獲得. ## Extending functionality[](#extending-functionality "Permalink") ### Using Analysis Plugins[](#using-analysis-plugins "Permalink") 如果有需要延長的代碼質量所提供的默認功能,如在規定[的代碼質量](#code-quality-starter) , [分析插件](https://docs.codeclimate.com/docs/list-of-engines)可供選擇. 例如,要使用[SonarJava 分析器](https://docs.codeclimate.com/docs/sonar-java) ,請在存儲庫的根目錄中添加一個名為`.codeclimate.yml`的文件, `.codeclimate.yml`包含插件的[啟用代碼](https://docs.codeclimate.com/docs/sonar-java#enable-the-plugin) : ``` version: "2" plugins: sonar-java: enabled: true ``` 這會將 SonarJava 添加到項目中[默認`.codeclimate.yml`](https://gitlab.com/gitlab-org/ci-cd/codequality/-/blob/master/codeclimate_defaults/.codeclimate.yml)的`plugins:`部分. 對`plugins:`部分的更改不會影響 defeault `.codeclimate.yml`的`exclude_patterns`部分. 有關更多詳細信息,請參見代碼氣候文檔以[排除文件和文件夾](https://docs.codeclimate.com/docs/excluding-files-and-folders) . 這是[一個示例項目](https://gitlab.com/jheimbuck_gl/jh_java_example_project) , [該示例項目](https://gitlab.com/jheimbuck_gl/jh_java_example_project)將 Code Quality 與`.codeclimate.yml`文件一起使用. ## Troubleshooting[](#troubleshooting "Permalink") ### Changing the default configuration has no effect[](#changing-the-default-configuration-has-no-effect "Permalink") 一個普遍的問題是`Code Quality` (特定于 GitLab)和`Code Climate` (GitLab 使用的引擎)這兩個術語非常相似. 您必須添加**`.codeclimate.yml`**文件來更改默認配置, **而不是** `.codequality.yml`文件. 如果使用錯誤的文件名,仍將使用[默認的`.codeclimate.yml`](https://gitlab.com/gitlab-org/ci-cd/codequality/-/blob/master/codeclimate_defaults/.codeclimate.yml) . ### No Code Quality report is displayed in a Merge Request[](#no-code-quality-report-is-displayed-in-a-merge-request "Permalink") 這可能是由于多種原因: * 您剛剛在`.gitlab-ci.yml`添加了代碼質量工作. 該報告尚無可比較的內容,因此無法顯示任何信息. 將來的合并請求將具有可比性. * 如果未[檢測到降級或錯誤](https://docs.codeclimate.com/docs/maintainability#section-checks) ,則不會顯示任何內容. * [`artifacts:expire_in`](../../../ci/yaml/README.html#artifactsexpire_in) CI / CD 設置可能導致代碼質量構件過期快于所需. * `codeclimate.json`較大的`codeclimate.json`文件(尤其是> 10 MB)會[阻止報告的顯示](https://gitlab.com/gitlab-org/gitlab/-/issues/2737) . 解決方法是,嘗試刪除[GitLab 忽略的](#implementing-a-custom-tool) [屬性](https://github.com/codeclimate/platform/blob/master/spec/analyzers/SPEC.md#data-types) . 您可以: * 配置代碼質量工具以不輸出那些類型. * 在作業完成之前,請在`.gitlab-ci.yml`腳本中使用`sed` , `awk`或類似命令來編輯`codeclimate.json` . ### Only a single Code Quality report is displayed, but more are defined[](#only-a-single-code-quality-report-is-displayed-but-more-are-defined "Permalink") GitLab 僅使用最新創建的作業(具有最大的作業 ID)的代碼質量工件. 如果管道中的多個作業生成代碼質量工件,則較早作業的那些將被忽略. 為避免混淆,僅配置一個作業即可生成`codeclimate.json` .
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看