Class: Locker
- Inherits:
-
Object
- Object
- Locker
- Defined in:
- lib/locker.rb
Overview
Automatically locks old issues that have been closed already
Instance Method Summary collapse
-
#error(message) ⇒ Object
Print ERROR message.
-
#get_closed_issues ⇒ Object
Fetches all closed, unlocked issues closed before cutoff date.
-
#initialize(user, repo, token, old_days = 120, noop = false, level = 2) ⇒ Locker
constructor
A new instance of Locker.
-
#lock ⇒ Object
Locks old closed issues.
-
#lock_old_closed_issues(issues) ⇒ Object
Expects array of issues from API call.
-
#locked ⇒ Object
Print locked message.
-
#locking(number, item, total) ⇒ Object
Print locking message.
-
#notify(message) ⇒ Object
Print INFO message.
Constructor Details
#initialize(user, repo, token, old_days = 120, noop = false, level = 2) ⇒ Locker
Returns a new instance of Locker.
8 9 10 11 12 13 14 15 |
# File 'lib/locker.rb', line 8 def initialize user, repo, token, old_days = 120, noop = false, level = 2 @user = user @repo = repo @token = token @old_days = old_days.to_i @noop = noop @level = level || 0 end |
Instance Method Details
#error(message) ⇒ Object
Print ERROR message
134 135 136 |
# File 'lib/locker.rb', line 134 def error warn "[ERROR] #{message}" end |
#get_closed_issues ⇒ Object
Fetches all closed, unlocked issues closed before cutoff date
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 80 81 82 83 84 85 86 |
# File 'lib/locker.rb', line 46 def get_closed_issues issues = [] path = "/repos/#@user/#@repo/issues?state=closed&per_page=100&sort=updated&direction=asc" page = 1 headers = { 'Authorization' => "Bearer #@token", 'Accept' => 'application/vnd.github+json', 'X-GitHub-Api-Version' => '2022-11-28' } 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, headers) 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'] and 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 |
#lock ⇒ Object
Locks old closed issues
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 43 |
# File 'lib/locker.rb', line 18 def lock notify "Not locking anything due to -n flag" if @noop notify "Getting closed issues for %s/%s..." % [@user, @repo] 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.sort_by { |h| h["number"] }.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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/locker.rb', line 89 def lock_old_closed_issues issues headers = {'Accept' => 'application/vnd.github+json', 'Content-Length' => '0', # required for PUT with no body 'Authorization' => "Bearer #@token", 'X-GitHub-Api-Version' => '2022-11-28', 'Content-Type' => 'application/json', } 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", '{"lock_reason":"off-topic"}', headers) if response.code == "204" # 204 means it worked, apparently locked else error response.inspect end end end end |
#locked ⇒ Object
Print locked message
123 124 125 |
# File 'lib/locker.rb', line 123 def locked puts 'locked!' end |
#locking(number, item, total) ⇒ Object
Print locking message
117 118 119 120 |
# File 'lib/locker.rb', line 117 def locking number, item, total return unless @level >= 1 print "[INFO] Locking #{number} (#{item + 1}/#{total})..." end |
#notify(message) ⇒ Object
Print INFO message
128 129 130 131 |
# File 'lib/locker.rb', line 128 def notify return unless @level >= 2 puts "[INFO] #{message}" end |