9

I have created spec/lint/rubocop_spec.rb which runs Rubocop style checker on the files changed between current branch and master. This works when I test locally but not when the test run on the build server Circle.ci. I suspect it is because only the branch in question is downloaded, so it does not find any differences between master. Is there a better way than git co master && git pull origin master? Can I query the Github API perhaps to get the files changed listed?

require 'spec_helper'

describe 'Check that the files we have changed have correct syntax' do
  before do
    current_sha = `git rev-parse --verify HEAD`.strip!
    files = `git diff master #{current_sha} --name-only | grep .rb`
    files.tr!("\n", ' ')
    @report = 'nada'
    if files.present?
      puts "Changed files: #{files}"

      @report = `rubocop #{files}`
      puts "Report: #{@report}"
    end
  end

  it { @report.match('Offenses').should_not be true }
end
4

6 回答 6

11

您不必使用 github api,甚至 ruby​​(除非您想包装响应),您可以运行:

git fetch && git diff-tree -r --no-commit-id --name-only master@\{u\} head | xargs ls -1 2>/dev/null | xargs rubocop --force-exclusion

请参阅http://www.red56.uk/2017/03/26/running-rubocop-on-changed-files/以了解更长的文章

于 2017-03-27T15:42:52.443 回答
6

我通过查询 api.github.com 修复了它。这将在 current_sha 和 master 分支之间更改的所有文件上运行 rubocop。

require 'spec_helper'

describe 'Check that the files we have changed have correct syntax' do
  before do
    current_sha = `git rev-parse --verify HEAD`.strip!
    token = 'YOUR GITHUB TOKEN'
    url = 'https://api.github.com/repos/orwapp/orwapp/compare/' \
          "master...#{current_sha}?access_token=#{token}"
    files = `curl -i #{url} | grep filename | cut -f2 -d: | grep \.rb | tr '"', '\ '`
    files.tr!("\n", ' ')
    @report = 'nada'
    if files.present?
      puts "Changed files: #{files}"

      @report = `rubocop #{files}`
      puts "Report: #{@report}"
    end
  end

  it { expect(@report.match('Offenses')).to be_falsey }
end
于 2015-09-14T10:34:59.310 回答
4

我发现https://github.com/m4i/rubocop-git效果很好。但是,它适用于您的 git diff(可以选择使用 --cached),因此它不允许您比较分支。

于 2015-11-17T12:45:38.083 回答
0

您可以使用https://github.com/AtakamaLLC/lint-diffs

除了 ruby​​ 之外,它还适用于你的 repo 中的任何语言(甚至是 bash 脚本和 README 文件),并且适用于任何类型的源代码控制,而不仅仅是 git。

这样您就可以在任何地方使用相同的工具。

您必须在配置中启用 rubocop 扩展。

于 2020-02-23T20:46:04.167 回答
0

我没有足够高的声誉来评论答案,因此我发布了一个答案以添加我认为有用的改进:

git fetch && git diff-tree -r --no-commit-id --name-only master@\{u\} HEAD | xargs ls -1 2>/dev/null | grep '\.rb$' | xargs bundle exec rubocop --force-exclusion

添加--force-exclusion使 RuboCop 尊重其配置文件中的 Exclude 声明(这里使用 default ./.rubocop.yml)。你把这些声明放进去是有原因的,对吧?!;)

于 2020-10-08T23:41:17.283 回答
0

这是将当前分支与其他分支进行比较的另一种选择origin/master(应该与任何回购托管一起使用 - 只需在带有 bitbucket 回购的 circleci 上尝试过)。它还传递了一个.rubocop.yml配置文件选项(如果不需要,可以删除该部分)。

require 'spec_helper'

RSpec.describe 'Check that the files we have changed have correct syntax' do
  before do
    current_sha = 'origin/master..HEAD'
    @files = `git diff-tree --no-commit-id --name-only -r #{current_sha} | grep .rb`
    @files.tr!("\n", ' ')
  end

  it 'runs rubocop on changed ruby files' do
    if @files.empty?
      puts "Linting not performed. No ruby files changed."
    else
      puts "Running rubocop for changed files: #{@files}"
      result = system "bundle exec rubocop --config .rubocop.yml --fail-level warn #{@files}"
      expect(result).to be(true)
    end
  end
end

原文要点:https ://gist.github.com/djburdick/5104d15f612c15dde65f#gistcomment-2029606

于 2017-03-16T23:04:53.413 回答