Skip to content

Instantly share code, notes, and snippets.

@eugeneius
Last active February 21, 2017 14:06
Show Gist options
  • Save eugeneius/c5e6510c777c7e10978df5af1a8721a4 to your computer and use it in GitHub Desktop.
Save eugeneius/c5e6510c777c7e10978df5af1a8721a4 to your computer and use it in GitHub Desktop.
Labels and closes stale issues, modelled on the process Rails uses.
#!/usr/bin/env ruby
require "octokit"
require "netrc"
# Pass the repo to reap:
# bin/reaper username/repository
repo = ARGV[0]
# Set the FORCE environment variable to "true" to actually perform actions:
# FORCE=true bin/reaper username/repository
force = ENV["FORCE"] == "true"
# Add credentials to ~/.netrc:
# echo -e "machine api.github.com\n login USERNAME\n password ACCESS_TOKEN" >> ~/.netrc; chmod 600 ~/.netrc
$client = Octokit::Client.new(netrc: true, auto_paginate: true)
def one_year_ago
(Time.now - 365 * 24 * 60 * 60).strftime("%Y-%m-%d")
end
def five_days_ago
(Time.now - 5 * 24 * 60 * 60).strftime("%Y-%m-%d")
end
def new_stale_issues(repo, label: "stale", threshold: one_year_ago)
$client.search_issues("is:issue is:open repo:#{repo} -label:#{label} updated:<#{threshold} sort:updated-asc")
end
def existing_stale_issues(repo, label: "stale", threshold: five_days_ago)
$client.search_issues("is:issue is:open repo:#{repo} label:#{label} updated:<#{threshold} sort:updated-asc")
end
def label_and_comment!(repo, issue)
$client.add_labels_to_an_issue(repo, issue.number, ["stale"])
$client.add_comment(repo, issue.number, <<~COMMENT)
This issue has been automatically marked as stale because it has had no activity in over a year.
If it's still valid, please add a comment describing the current status and remove the `stale` label.
Otherwise it will be automatically closed in a few days.
COMMENT
end
def close_and_unlabel!(repo, issue)
$client.close_issue(repo, issue.number)
$client.remove_label(repo, issue.number, "stale")
end
new_stale_issues(repo).each do |issue|
puts "labelling #{issue.title} (#{issue.html_url})"
label_and_comment!(repo, issue) if force
end
existing_stale_issues(repo).each do |issue|
puts "closing #{issue.title} (#{issue.html_url})"
close_and_unlabel!(repo, issue) if force
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment