Sha256: ff8e24c88d0d2f101f459e3b21775a96292f9a9ae30127d4e45725d3796e681d

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

class Ghn
  class Commands < Thor
    class_option :all, type: :boolean, aliases: '-a', desc: 'List/Open all unread notifications'

    desc 'list NAME', 'List unread notifications'
    def list(name = nil)
      repo_full_name = aliases.find(name) || name
      puts collect(repo_full_name)
    end

    desc 'open NAME', 'Open unread notifications in browser'
    def open(name = nil)
      repo_full_name = aliases.find(name) || name
      collect(repo_full_name).each do |url|
        system "open #{url}"
      end
    end

    private

    def collect(repo_full_name = nil)
      threads = []
      collector = Collector.new(client, repo_full_name)
      # Unfortunately, GitHub v3 Notifications API doesn't accept octokit's auto_paginate option
      # because this API doesn't return Link header so far.
      loop.with_index(1) do |_, page|
        threads += collector.collect(page)
        break unless options['all']
        break unless collector.has_next?
      end

      threads
    end

    def client
      @client ||= Octokit::Client.new(access_token: access_token)
    end

    def aliases
      @aliases ||= Aliases.new
    end

    def access_token
      @access_token ||=
        case
        when ENV['GHN_ACCESS_TOKEN']
          ENV['GHN_ACCESS_TOKEN']
        when !`git config ghn.token`.chomp.empty?
          `git config ghn.token`.chomp
        else
          puts <<MESSAGE
** Authorization required.

Please set ghn.token to your .gitconfig.
    $ git config --global ghn.token [Your GitHub access token]

To get new token, visit
https://github.com/settings/tokens/new

MESSAGE
          exit!
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ghn-2.0.0.pre3 lib/ghn/commands.rb
ghn-2.0.0.pre2 lib/ghn/commands.rb
ghn-2.0.0.pre1 lib/ghn/commands.rb