Class: Xambassador::ProtectedFiles

Inherits:
StatusCheck show all
Defined in:
lib/xambassador/status_checks/protected_files.rb

Overview

Check peer review status

Instance Method Summary collapse

Methods inherited from StatusCheck

#failure, #pending, #success

Constructor Details

#initialize(connection, pull_request) ⇒ ProtectedFiles

Returns a new instance of ProtectedFiles.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/xambassador/status_checks/protected_files.rb', line 12

def initialize(connection, pull_request)
  super(connection, pull_request, 'No Protected Files')

  @description_success = ''
  @description_failure = 'Edited protected files'

  url = pull_request['head']['repo']['trees_url']

  url_template = URITemplate.new(url)
  fetch_changed_files(url_template.expand('sha' => @sha))
end

Instance Method Details

#check_files(files) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xambassador/status_checks/protected_files.rb', line 36

def check_files(files)
  protected_files = ['app.config', 'web.config']
  changed_protected_files = []

  unless files.empty?
    files.each do |file|
      if protected_files.include?(File.basename(file['path']).downcase)
        changed_protected_files.push(file['path'])
      end
    end
  end

  report_status(changed_protected_files)
end

#check_labels(labels, files) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/xambassador/status_checks/protected_files.rb', line 71

def check_labels(labels, files)
  pass = false

  labels.each do |label|
    pass = true if label['name'] == 'edited .config'
  end

  if pass
    success
  else
    @description_failure = "You edited #{files}"
    failure
  end
end

#fetch_changed_files(url) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xambassador/status_checks/protected_files.rb', line 24

def fetch_changed_files(url)
  uri = URI.parse(url)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.request(Net::HTTP::Get.new(url))

  body = JSON.parse(response.body)
  check_files(body['tree'])
end

#fetch_labels(url, files) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/xambassador/status_checks/protected_files.rb', line 59

def fetch_labels(url, files)
  uri = URI.parse(url)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.request(Net::HTTP::Get.new(url))

  body = JSON.parse(response.body)
  check_labels(body['labels'], files)
end

#report_status(files) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/xambassador/status_checks/protected_files.rb', line 51

def report_status(files)
  if files.empty?
    success
  else
    fetch_labels(@pull_request['issue_url'], files)
  end
end