Class: Locker

Inherits:
Object
  • Object
show all
Defined in:
lib/locker.rb

Overview

Automatically locks old issues that have been closed already

Instance Method Summary collapse

Constructor Details

#initialize(user, repo, token, old_days = 120) ⇒ Locker

Returns a new instance of Locker.



8
9
10
11
12
13
# File 'lib/locker.rb', line 8

def initialize user, repo, token, old_days = 120
  @user = user
  @repo = repo
  @token = token
  @old_days = old_days
end

Instance Method Details

#error(message) ⇒ Object

Print ERROR message



102
103
104
# File 'lib/locker.rb', line 102

def error message
  warn "[ERROR] #{message}"
end

#get_closed_issuesObject

Fetches all closed, unlocked issues closed before cutoff date



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/locker.rb', line 30

def get_closed_issues
  issues = []
  path = "/repos/#@user/#@repo/issues?state=closed&access_token=#@token&sort=updated&direction=asc"
  page = 1
  http = Net::HTTP.start("api.github.com", 443, nil, nil, nil, nil, use_ssl: true)

  loop do
    notify "Retrieving page #{page}..."

    resp = http.get(path)
    new_issues = JSON.parse(resp.body)
    issues += new_issues

    # Pagination
    if resp['Link'].match /<https:\/\/api\.github\.com(\/[^>]+)>; rel="next",/
      path = $1
      page = path.match(/page=(\d+)/)[1]
    else
      http.finish
      break
    end
  end

  cutoff_date = (Date.today - @old_days).iso8601

  issues.reject do |issue|
    issue["locked"] or
      issue["closed_at"] > cutoff_date
  end
end

#lockObject

Locks old closed issues



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/locker.rb', line 16

def lock
  notify "Getting closed issues..."
  issues = get_closed_issues

  if issues.empty?
    notify "No issues to lock"
  else
    notify "Received #{issues.length} issues"
    notify "Locking old closed issues..."
    lock_old_closed_issues issues
  end
end

#lock_old_closed_issues(issues) ⇒ Object

Expects array of issues from API call



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/locker.rb', line 62

def lock_old_closed_issues issues
  headers = {'Accept' => 'application/vnd.github.the-key-preview+json', # required for new lock API
             'Content-Length' => '0', # required for PUT with no body
             'Authorization' => "Basic #{Base64.strict_encode64("#@user:#@token")}"}

  Net::HTTP.start("api.github.com", 443, nil, nil, nil, nil, use_ssl: true) do |http|
    total = issues.length

    issues.each_with_index do |issue, i|
      number = issue['number']
      locking number, i, total

      path = issue["url"][22..-1] # pull path from full URL
      response = http.put("#{path}/lock", '', headers)

      if response.code == "204" # 204 means it worked, apparently
        locked
      else
        error response.inspect
      end
    end
  end
end

#lockedObject

Print locked message



92
93
94
# File 'lib/locker.rb', line 92

def locked
  puts 'locked!'
end

#locking(number, item, total) ⇒ Object

Print locking message



87
88
89
# File 'lib/locker.rb', line 87

def locking number, item, total
  print "[INFO] Locking #{number} (#{item + 1}/#{total})..."
end

#notify(message) ⇒ Object

Print INFO message



97
98
99
# File 'lib/locker.rb', line 97

def notify message
  puts "[INFO] #{message}"
end