鍍金池/ 教程/ Java/ 使用評論
集成自動(dòng)化部署
架設(shè) CI 服務(wù)器
探索用戶資源
SSH agent 轉(zhuǎn)發(fā)
使用評論
身份認(rèn)證基礎(chǔ)
管理部署密鑰
準(zhǔn)備開始
傳遞部署
遍歷分頁
整合者的最佳做法
數(shù)據(jù)渲染成圖表

使用評論

對于任何一個(gè) Pull Request,GitHub 都提供三種不同的評論 (comment) 視圖: 針對整個(gè) Pull Request 的評論 , 在 Pull Request 內(nèi)針對單行代碼的評論針對某個(gè)具體 commit 的評論。

每種評論都經(jīng)由不同的 GitHub API 組件發(fā)出。在這個(gè)指南中,我們會(huì)帶您探索如何訪問并且操縱每一種評論。本文所有示例都會(huì)使用在 octocat 存儲庫中的這個(gè) Pull Request 樣本。和往常一樣,樣本都能在我們的 platform-samples 存儲庫中找到。

Pull Request 評論

為了能訪問 Pull Request 上的評論,您需要仔細(xì)閱讀 Issue API。也許這一開始看起來違反直覺,不過當(dāng)您一旦了解一個(gè) Pull Request 的本質(zhì)只是一個(gè)帶著代碼的 Issue 時(shí),使用 Issue API 來創(chuàng)建 Pull Request 上的評論便顯得十分合理。

我們將演示如何用 Ruby 腳本 + Octokit.rb 來獲取 Pull Request 評論。您也許還會(huì)需要?jiǎng)?chuàng)建一個(gè)個(gè)人訪問令牌

下述代碼能幫您起步,通過 Octokit.rb 來訪問 Pull Request 上的評論:

    require 'octokit'

    # 在真正的應(yīng)用內(nèi)永遠(yuǎn)不要用硬編碼把值寫死 !
    # 而是設(shè)置環(huán)境變量并測試,和下例所示
    client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']

    client.issue_comments("octocat/Spoon-Knife", 1176).each do |comment|
      username = comment[:user][:login]
      post_date = comment[:created_at]
      content = comment[:body]

     puts "#{username} 在 #{post_date} 時(shí)發(fā)出了一個(gè)評論. 內(nèi)容是:\n'#{content}'\n"
    end

在此,我們特地調(diào)用 Issue API 并提供存儲庫的名稱 (octocat/Spoon-Knife)和我們感興趣的 Pull Request 的 ID (1176)來得到評論 (issue_comments)。接下來,問題就剩下迭代所有的評論來獲取每一個(gè)評論的信息內(nèi)容了。

Pull Request 單行評論

在 diff 視圖內(nèi), 您可以就某個(gè)在 Pull Request 內(nèi)的單獨(dú)的變更來展開討論,這些評論將會(huì)針對在被更改的文件內(nèi)的特定行。這次討論的端點(diǎn) URL 來自于 Pull Request Review API

下述代碼的功能是根據(jù)一個(gè) Pull Request 號碼來獲取所有在文件上發(fā)出的 Pull Request 評論:

    require 'octokit'

    # 在真正的應(yīng)用內(nèi)永遠(yuǎn)不要用硬編碼把值寫死 !
    # 而是設(shè)置環(huán)境變量并測試,和下例所示
    client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']

    client.pull_request_comments("octocat/Spoon-Knife", 1176).each do |comment|
      username = comment[:user][:login]
      post_date = comment[:created_at]
      content = comment[:body]
      path = comment[:path]
      position = comment[:position]

      puts "#{username} 在 #{post_date} 時(shí)發(fā)出了一個(gè)評論,針對文件 #{path} 的第 #{position} 行. 內(nèi)容是:\n'#{content}'\n"
    end

您會(huì)注意到這代碼和上一個(gè)示例非常的接近。該示例和 Pull Request 評論示例的差別只在對話的焦點(diǎn)上,一個(gè)在 Pull Request 上發(fā)出的評論應(yīng)該用來針對代碼的大方向進(jìn)行討論或者提出新想法;作為 Pull Request review 的組成部分的單行評論,則應(yīng)該特別針對一個(gè)文件內(nèi)發(fā)生的一個(gè)特定改動(dòng)。

Commit 評論

最后一種評論,就是針對每個(gè) commit 發(fā)出的。因?yàn)槿绱?,我們需要使?commit 評論 API。 為了獲取在 commit 上的評論,您會(huì)需要這個(gè) commit 的 SHA1 散列值。換句話說,您不會(huì)需要任何和 Pull Request 相關(guān)聯(lián)的識別碼。

接下來是示例:

    require 'octokit'

    # 在真正的應(yīng)用內(nèi)永遠(yuǎn)不要用硬編碼把值寫死 !
    # 而是設(shè)置環(huán)境變量并測試,和下例所示
    client = Octokit::Client.new :access_token => ENV['MY_PERSONAL_TOKEN']

    client.commit_comments("octocat/Spoon-Knife", "cbc28e7c8caee26febc8c013b0adfb97a4edd96e").each do |comment|
      username = comment[:user][:login]
      post_date = comment[:created_at]
      content = comment[:body]

      puts "#{username} 在 #{post_date} 時(shí)發(fā)出了一個(gè)評論. 內(nèi)容是:\n'#{content}'\n"
    end

值得注意的是這個(gè) API 調(diào)用會(huì)同時(shí)得到單行評論和針對整個(gè) commit 的評論。