Class: CTodo::Github

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/ctodo/github.rb

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ Github

Returns a new instance of Github.



7
8
9
10
11
12
13
14
15
# File 'lib/ctodo/github.rb', line 7

def initialize(conf)
	@enabled = (!conf[:gh_user].nil? and !conf[:git_repo_dir].nil?)
	if @enabled
		@login = conf[:gh_user]
		# needed to locate .git/config to look for other remotes
		@repo_dir = Pathname.new(conf[:git_repo_dir])
		@repo_name = File.basename(conf[:git_repo_dir])
	end
end

Instance Method Details

#find_remote(ini_sections, name) ⇒ Object



85
86
87
88
89
90
# File 'lib/ctodo/github.rb', line 85

def find_remote(ini_sections, name)
	ini_sections.each do |s|
		return s if s.start_with?("remote") and s.include?(name)
	end
	nil
end

#get_issues(issues) ⇒ Object



17
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
44
45
46
47
48
49
50
51
# File 'lib/ctodo/github.rb', line 17

def get_issues(issues)
	return if not @enabled
	
	status_msgs = []
	
	# try current user first
	gh_issues = get_issues_for(@login, @repo_name)
	if !gh_issues.nil?
		# don't output as this is the default
		#status_msgs << "User: #{@login}"
		parse_issues(gh_issues, issues)
	else
		# find alternative login from "remote origin" config entry
		 = ("origin")
		if !.nil? and  != @login
			alt_gh_issues = get_issues_for(, @repo_name)
			if !alt_gh_issues.nil?
				status_msgs << "User: #{}"
				parse_issues(alt_gh_issues, issues)
			end
		end
	end
	
	# optional "upstream"
	 = ("upstream")
	if !.nil?
		up_gh_issues = get_issues_for(, @repo_name)
		if !up_gh_issues.nil?
			status_msgs << "Up: #{}"
			parse_issues(up_gh_issues, issues)
		end
	end
	
	puts status_msgs.join(', ') if not status_msgs.empty?
end

#get_issues_for(login, repo_name) ⇒ Object



53
54
55
56
57
# File 'lib/ctodo/github.rb', line 53

def get_issues_for(, repo_name)
	r = self.class.get "/repos/#{}/#{repo_name}/issues"
	return r if r.code == 200
	nil
end

#parse_issues(gh_issues, issues) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ctodo/github.rb', line 59

def parse_issues(gh_issues, issues)
	gh_issues.each do |i|
		loc = i['html_url']
		assig = (i['assignee'].nil? ? i['user']['login'] : i['assignee']['login'])
		tags = i['labels'].collect { |lab| Tag.new(lab['name'], lab['color']) }
		tags << Tag.new(i['milestone']['title'], "000000") if not i['milestone'].nil?
		issues << Issue.new(i['title'], loc, assig, tags)
	end
end

#remote_login(name) ⇒ Object

.git/config parsing



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ctodo/github.rb', line 71

def (name)
	git_dir = Pathname.new(!ENV['GIT_DIR'].nil? ? ENV['GIT_DIR'] : '.git')
	# parse ini file
	git_config = IniFile.new(@repo_dir + git_dir + 'config')
	origin = find_remote(git_config.sections, name)
	return nil if origin.nil?
	origin_url = git_config[origin]['url']
	return nil if origin_url.nil?
	m = origin_url.match(/github.com\/([a-zA-Z0-9_-]+)\//)
	return nil if m.nil?
	# should be user login
	m[1]
end