commit cc710435cb1f11174bee8e3044c969671175e47e Author: Knut Ahlers Date: Tue Jun 2 18:52:56 2015 +0200 Initial version diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..93508ec --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +source 'https://rubygems.org' +ruby '2.1.6' + +gem "octokit", "~> 3.0" +gem 'thor', '0.19.1' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..6c98dab --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,20 @@ +GEM + remote: https://rubygems.org/ + specs: + addressable (2.3.6) + faraday (0.9.0) + multipart-post (>= 1.2, < 3) + multipart-post (1.2.0) + octokit (3.8.0) + sawyer (~> 0.6.0, >= 0.5.3) + sawyer (0.6.0) + addressable (~> 2.3.5) + faraday (~> 0.8, < 0.10) + thor (0.19.1) + +PLATFORMS + ruby + +DEPENDENCIES + octokit (~> 3.0) + thor (= 0.19.1) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4fde5d2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,13 @@ +Copyright 2015 Knut Ahlers + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ceb913a --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# Luzifer / github-masswatch + +[![License: Apache 2.0](http://badge.luzifer.io/v1/badge?color=5d79b5&title=license&text=Apache%202.0)](LICENSE) + +This project is a small helper for mass-watching / unwatching in GitHub repositories. If you are in a bigger organization like me you might have switched off auto-watching for new repositories as your mailbox gets flooded with stuff you don't even like to see. So after turning off the automatic watching you will not notice new repositories and you have to subscribe them one by one. To make this a bit more easy and have a chance to do this with a Jenkins job or similar I wrote this little tool. + +## Features + +- List all repositories you have access to +- List all repositories you are currently subscribed to +- Test a RegExp against that repository list +- Watch and unwatch repositories by providing a RegExp + +## Simple usage example + +```bash +# bundle install +[...] + +# ruby watch.rb list --token= +Luzifer/alarmclock +Luzifer/alfred-fold +Luzifer/alfred-pwdgen +Luzifer/AMZWishlist +Luzifer/awsenv +[...] + +# ruby watch.rb watch '^Luzifer/.*' --token= +Subscribing to Luzifer/blog.knut.me... +Subscribing to Luzifer/go-selfupdate... +Subscribing to Luzifer/golang-builder... +Subscribing to Luzifer/habitrpg... +Subscribing to Luzifer/homebrew... +Subscribing to Luzifer/homebrew-tools... +Subscribing to Luzifer/license... +Subscribing to Luzifer/radiopi... +Subscribing to Luzifer/simpleproxy... +``` diff --git a/watch.rb b/watch.rb new file mode 100755 index 0000000..a821181 --- /dev/null +++ b/watch.rb @@ -0,0 +1,77 @@ +#!/usr/bin/env ruby + +require "Octokit" +require "thor" + +class WatchCLI < Thor + class_option :token, :type => :string + + desc "list ", "lists all repos, useful to test your watch regex" + def list(regex='.*') + get_repos().each do |repo| + puts repo.full_name if /#{regex}/.match(repo.full_name) + end + end + + desc "watched ", "show all watched repos, useful to test your unwatch regex" + def watched(regex='.*') + get_subscriptions().each do |repo| + puts repo if /#{regex}/.match(repo) + end + end + + desc "watch ", "watch all repos matching your regex" + def watch(regex) + subscriptions = get_subscriptions() + get_repos().each do |repo| + if !subscriptions.include?(repo.full_name) and /#{regex}/.match(repo.full_name) + puts "Subscribing to #{repo.full_name}..." + get_octokit().update_subscription(repo.full_name, {subscribed: true}) + end + end + end + + desc "unwatch ", "unwatch all repos matching your regex" + def unwatch(regex) + subscriptions = get_subscriptions() + get_repos().each do |repo| + puts "Unsubscribing from #{repo.full_name}..." + get_octokit().update_subscription(repo.full_name, {subscribed: false}) if subscriptions.include?(repo.full_name) and /#{regex}/.match(repo.full_name) + end + end + + no_commands { + def get_octokit + if !options[:token] + puts "Please specify --token" + exit(1) + end + client = Octokit::Client.new(:access_token => options[:token]) + client.auto_paginate = true + return client + end + + def get_repos + client = get_octokit + + repos = client.repositories(client.user.login) + + orgs = client.organizations() + orgs.each do |org| + client.organization_repositories(org.login).each do |repo| + repos << repo + end + end + + return repos + end + + def get_subscriptions + client = get_octokit + + return client.subscriptions(client.user.login).map { |repo| repo.full_name } + end + } +end + +WatchCLI.start(ARGV)