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, noop = false) ⇒ Locker

Returns a new instance of Locker.



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

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

Instance Method Details

#error(message) ⇒ Object

Print ERROR message



122
123
124
# File 'lib/locker.rb', line 122

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

#get_closed_issuesObject

Fetches all closed, unlocked issues closed before cutoff date



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/locker.rb', line 45

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)

    unless Array === new_issues then
      abort "bad response: %p" % new_issues
    end

    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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/locker.rb', line 17

def lock
  notify "Not locking anything due to -n flag" if @noop
  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..."

    if @noop then
      total = issues.size

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

      return
    end

    lock_old_closed_issues issues
  end
end

#lock_old_closed_issues(issues) ⇒ Object

Expects array of issues from API call



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/locker.rb', line 82

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, * = URI.split issue["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



112
113
114
# File 'lib/locker.rb', line 112

def locked
  puts 'locked!'
end

#locking(number, item, total) ⇒ Object

Print locking message



107
108
109
# File 'lib/locker.rb', line 107

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

#notify(message) ⇒ Object

Print INFO message



117
118
119
# File 'lib/locker.rb', line 117

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