Skip to content

Instantly share code, notes, and snippets.

@bkemper
Created September 8, 2016 10:55
Show Gist options
  • Save bkemper/ecb6d7a04a56d5222b9f1d3687fa8080 to your computer and use it in GitHub Desktop.
Save bkemper/ecb6d7a04a56d5222b9f1d3687fa8080 to your computer and use it in GitHub Desktop.
Script to "Not Watch" all your organizations repositories
# Simple utility script to "Not Watch" all the repositories in your GitHub
# organization
#
# @example
# $ GITHUB_ACCESS_TOKEN=MYACCESSTOKEN ruby stop_watching.rb staqapp
require "faraday"
require "json"
NO_CONTENT = 204
PER_PAGE = 100 # @see https://developer.github.com/v3/#pagination
SUCCESS = 200
access_token = ENV.fetch("GITHUB_ACCESS_TOKEN") { abort "Please provide your GitHub access token." }
organization = ARGV.fetch(0) { abort "Please provide an organization." }
# Establish connection to GitHub's API
connection = Faraday.new(headers: { Authorization: "token #{access_token}" },
url: "https://api.github.com")
# Authenticate
# @see https://developer.github.com/v3/users/#get-the-authenticated-user
connection.get("/user").tap do |response|
abort "Failed to authenticate." unless response.status == SUCCESS
puts "Logged in as: #{JSON(response.body).fetch("login")}"
end
# Fetch all organization repositories
# @note This endpoint is paginated and requires multiple requests. The maximum
# number of pages is five.
repositories = (1..5).to_a.reduce([]) do |res, page|
# @see https://developer.github.com/v3/repos/#list-organization-repositories
connection.get("/orgs/#{organization}/repos", page: page, per_page: PER_PAGE).tap do |response|
response_body = JSON(response.body)
# Concat the current page of repos to the reduced result
res.concat(response_body)
# Exit early if you hit the last page
break res if response_body.count < PER_PAGE
end
res
end
puts "Found #{organization} has #{repositories.count} repositories."
# Loop through all the organization's repositories and change subscription to
# not watch it
repositories.each do |repository|
full_name = repository.fetch("full_name")
# @see https://developer.github.com/v3/activity/watching/#get-a-repository-subscription
connection.get("/repos/#{full_name}/subscription").tap do |response|
# Ignore repositories you are not watching
next unless response.status == SUCCESS
# Ignore repositories you want to completely ignore
next if JSON(response.body).fetch("ignored")
end
# "If you would like to watch a repository, set subscribed to true. If you would
# like to ignore notifications made within a repository, set ignored to true.
# If you would like to stop watching a repository, delete the repository's
# subscription completely."
# @see https://developer.github.com/v3/activity/watching/#set-a-repository-subscription
connection.delete("/repos/#{full_name}/subscription").tap do |response|
next unless response.status == NO_CONTENT
end
puts "No longer watching, #{full_name}"
end
@adamchenwei
Copy link

@bkemper how to use it? is there any instruction?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment