Class: LocalPrAlike

Inherits:
Object
  • Object
show all
Defined in:
app/models/local_pr_alike.rb

Overview

An object that is similar enough to PullRequest to be linted. It can be constructed from the CLI tool (compares local working tree to base_branch) or from the JSON payload that the CLI tool sends to the API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filesObject

Returns the value of attribute files.



9
10
11
# File 'app/models/local_pr_alike.rb', line 9

def files
  @files
end

Class Method Details

.from_branch(base_branch) ⇒ Object



19
20
21
22
23
# File 'app/models/local_pr_alike.rb', line 19

def self.from_branch(base_branch)
  LocalPrAlike.new.tap do |pr|
    pr.files = pr.stubs_for_existing(base_branch) + pr.stubs_for_new
  end
end

.from_json(json) ⇒ Object



11
12
13
14
15
16
17
# File 'app/models/local_pr_alike.rb', line 11

def self.from_json(json)
  LocalPrAlike.new.tap do |pr|
    pr.files = json.map do |file_json|
      StubFile.from_json(file_json)
    end
  end
end

Instance Method Details

#as_json(_opts = {}) ⇒ Object



67
68
69
# File 'app/models/local_pr_alike.rb', line 67

def as_json(_opts = {})
  @files.map(&:as_json)
end

#changed_filesObject



59
60
61
# File 'app/models/local_pr_alike.rb', line 59

def changed_files
  files
end

#expected_url_from_path(path) ⇒ Object



63
64
65
# File 'app/models/local_pr_alike.rb', line 63

def expected_url_from_path(path)
  path
end

#raw_diff(base_branch) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'app/models/local_pr_alike.rb', line 37

def raw_diff(base_branch)
  diff = `git diff --no-ext-diff #{base_branch} .`
  unless $CHILD_STATUS.success?
    raise(
      'git diff failed. You may need to set a default branch in .linty_rc.',
    )
  end
  diff
end

#stubs_for_existing(base_branch) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/local_pr_alike.rb', line 25

def stubs_for_existing(base_branch)
  patches = GitDiffParser.parse(raw_diff(base_branch))

  patches.map do |patch|
    StubFile.new(
      path: patch.file,
      blob: File.read(patch.file),
      patch: Patch.new(patch.body),
    )
  end
end

#stubs_for_newObject



47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/local_pr_alike.rb', line 47

def stubs_for_new
  untracked_names = `git ls-files --others --exclude-standard`.split("\n")
  untracked_names.map do |name|
    body = File.read(name)
    StubFile.new(
      path: name,
      blob: body,
      patch: Patch.from_file_body(body),
    )
  end
end

#to_json(_opts = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/local_pr_alike.rb', line 71

def to_json(_opts = {})
  {
    files: as_json.select do |file_json|
      begin
        JSON.dump(file_json)
        file_json
      rescue JSON::GeneratorError
        puts "Ignoring #{file_json[:path]}, possible binary"
        nil
      end
    end,
  }.to_json
end