<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans"><generator uri="https://jekyllrb.com/" version="4.4.1">Jekyll</generator><link href="/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" hreflang="zh-Hans" /><updated>2025-11-05T15:17:56+08:00</updated><id>/feed.xml</id><title type="html">Keinoki 的博客</title><subtitle>You see see you. 
</subtitle><author><name>keinoki</name><email>ikoniek@outlook.com</email></author><entry><title type="html">sonarqube-community-branch-plugin 对 GitLab MR 评论的异常</title><link href="/2021/05/23/sonarqube-community-branch-plugin-post-commit-build-commit-not-found.html" rel="alternate" type="text/html" title="sonarqube-community-branch-plugin 对 GitLab MR 评论的异常" /><published>2021-05-23T00:00:00+08:00</published><updated>2021-05-23T00:00:00+08:00</updated><id>/2021/05/23/sonarqube-community-branch-plugin-post-commit-build-commit-not-found</id><content type="html" xml:base="/2021/05/23/sonarqube-community-branch-plugin-post-commit-build-commit-not-found.html"><![CDATA[<p><a href="https://github.com/mc1arke/sonarqube-community-branch-plugin">sonarqube-community-branch-plugin</a> 是一个可以让 SonarQube Community 版支持分支功能的插件，但是在通过此插件对 GitLab Merge Request 进行分析时有时无法将检测出的问题以评论的方式输出到 MR 上。</p>

<!--more-->

<h2 id="问题">问题</h2>
<p>因为这个插件将检测出的问题输出到 MR 评论的时机是在 SonarQube CE 分析完成后，所以我们可以在 SonarQube 的 ce.log 看到这个插件输出的日志。
在分析完成后，发现实际上在 <a href="https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit">Post commit build status</a> 时就被一个 404 Not Found 中止了接下来的评论逻辑。<br />
按照经验来说，MR 的 Commit 就算未合入，通过 MR 目标仓库的 API 也可以找到 Commit 的信息。
而且这个问题在源分支和目标分支同项目时就不会触发，只有通过 Fork 出来的仓库向目标仓库提交 MR 时才会触发这个问题。</p>

<h2 id="解决">解决</h2>
<p>经过几次尝试，发现这个 API 实际上是用来设置 Commit 对应的 Pipeline 状态的。
而对于 <a href="https://docs.gitlab.com/ee/ci/merge_request_pipelines">MR Pipeline</a> 来说，GitLab 有以下解释</p>
<blockquote>
  <p>Pipelines for merge requests can run when you:</p>
  <ul>
    <li>Create a new merge request.</li>
    <li>Commit changes to the source branch for the merge request.</li>
    <li>Select the Run pipeline button from the Pipelines tab in the merge request.</li>
  </ul>
</blockquote>

<p>那么也就可以解释清楚为什么会 404 Not Found 了，因为对 GitLab MR 来说，MR 绑定的 Pipeline 实际上是在源仓库运行的 Pipeline，通过目标仓库的 API 自然是设置不了源仓库的 Pipeline 状态的。</p>

<p>这个问题实际上只需要修改以下代码即可解决</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">String</span> <span class="n">statusUrl</span> <span class="o">=</span> <span class="n">projectURL</span> <span class="o">+</span> <span class="nc">String</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="s">"/statuses/%s"</span><span class="o">,</span> <span class="n">revision</span><span class="o">);</span>
</code></pre></div></div>
<p>更改为</p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">final</span> <span class="nc">String</span> <span class="n">statusUrl</span> <span class="o">=</span> <span class="n">sourceProjectURL</span> <span class="o">+</span> <span class="nc">String</span><span class="o">.</span><span class="na">format</span><span class="o">(</span><span class="s">"/statuses/%s"</span><span class="o">,</span> <span class="n">revision</span><span class="o">);</span>
</code></pre></div></div>
<p>这个问题已在 <a href="https://github.com/mc1arke/sonarqube-community-branch-plugin/pull/308">此 PR</a> 中得到解决，从 1.7.0 版本开始此问题已被修复。</p>]]></content><author><name>keinoki</name><email>ikoniek@outlook.com</email></author><category term="SonarQube" /><category term="sonarqube-community-branch-plugin" /><summary type="html"><![CDATA[sonarqube-community-branch-plugin 是一个可以让 SonarQube Community 版支持分支功能的插件，但是在通过此插件对 GitLab Merge Request 进行分析时有时无法将检测出的问题以评论的方式输出到 MR 上。]]></summary></entry></feed>