forked from bhaak/github-feeds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-feeds.rb
More file actions
executable file
·63 lines (48 loc) · 1.77 KB
/
github-feeds.rb
File metadata and controls
executable file
·63 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
require 'rss'
require 'net/http'
require 'json'
require 'optparse'
require 'fileutils'
pulls = false
owner = nil
repository = nil
directory = '.'
OptionParser.new do |parser|
parser.separator "List last commits of all reachable git branches"
parser.separator ""
parser.separator "Options:"
parser.on('-p', '--pulls', 'List pull requests instead of issues') { pulls = true }
parser.on('-o', '--owner=OWNER', '') {|o| owner = o }
parser.on('-r', '--repository=REPOSITORY', '') {|r| repository = r }
parser.on('-d', '--directory=DIRECTORY', '') {|d| directory = d }
end.parse!
raise OptionParser::MissingArgument.new('--owner is required') if owner.nil?
raise OptionParser::MissingArgument.new('--repository is required') if repository.nil?
type = pulls ? 'pull request' : 'issue'
issues = pulls ? 'pulls' : 'issues'
url = "https://api.github.com/repos/#{owner}/#{repository}/#{issues}"
response = Net::HTTP.get(URI(url))
json = JSON.parse(response, symbolize_names: true)
feed = RSS::Rss.new("2.0")
channel = RSS::Rss::Channel.new
channel.title = "GitHub #{type}s for #{owner}/#{repository}"
channel.description = " "
channel.link = "https://github.com/#{owner}/#{repository}"
channel.lastBuildDate = DateTime.now.rfc2822
json.each {|data|
item = RSS::Rss::Channel::Item.new
next if data[:pull_request]
item.title = data[:title]
item.author = data[:user][:login]
link = data[:html_url]
item.link = link
item.guid = RSS::Rss::Channel::Item::Guid.new
item.guid.content = link
item.guid.isPermaLink = true
item.pubDate = DateTime.parse(data[:created_at]).rfc2822
channel.items << item
}
feed.channel = channel
FileUtils.mkdir_p directory
File.open("#{directory}/#{owner}_#{repository}_#{type.gsub(' ', '_')}s.rss", "w") {|f| f.write(feed.to_s) }