Skip to content

Instantly share code, notes, and snippets.

@bluerabbit
Created October 30, 2018 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluerabbit/7e03257ea193886cd8858b3be3f94997 to your computer and use it in GitHub Desktop.
Save bluerabbit/7e03257ea193886cd8858b3be3f94997 to your computer and use it in GitHub Desktop.
CIでテストが落ちたらGitHub Pull RequestのDescriptionに落ちたファイルの情報で更新する

.circleci/config.yml

  - run: bundle exec rspec --format json --out rspec.json
  - run:
      name: Create CircleCI Test Summary
      when: always
      command: |
        gem install circleci-test_report
        circleci-test_report -f rspec.json -o /tmp/test-results/rspec.xml
  - run:
      name: Update github pull request
      when: on_fail
      command: |
        bundle exec rake github:update_pull_request
  - store_test_results:
      path: /tmp/test-results

lib/tasks/github.rake

require "octokit"

namespace :github do
  task update_pull_request: :environment do
    if ENV["CI_PULL_REQUEST"].present? && File.exist?("#{Rails.root}/rspec.json")
      json = JSON.parse(File.read("#{Rails.root}/rspec.json"))

      examples     = json["examples"].select {|e| e["status"] == "failed" }
      error_files  = examples.map {|e| "#{e["file_path"]}:#{e["line_number"]}" }
      fail_message = error_files.map {|v| "- [ ] `#{v}`" }.join("\n")

      client    = Octokit::Client.new(access_token: ENV["GITHUB_ACCESS_TOKEN"])
      repo      = "#{ENV["CIRCLE_PROJECT_USERNAME"]}/#{ENV["CIRCLE_PROJECT_REPONAME"]}"
      pr_number = ENV["CI_PULL_REQUEST"].split("/").last.to_i
      body      = client.pull_request(repo, pr_number)[:body]
      body      = "#{body}\n##  [:cry: #{Time.current.strftime("%Y/%m/%d %H:%M")}](#{ENV["CIRCLE_BUILD_URL"]})\n#{fail_message}"
      client.update_pull_request(repo, pr_number, { body: body })
    end
  end
end

こんな感じになる

##  [:cry: 2018/10/30 15:00](https://circleci.com/gh/bluerabbit/repo/1)

- [ ] `./spec/models/user_spec.rb:4`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment