hubload 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Hunter Gillane
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,76 @@
1
+ = Hubload
2
+ Parses post-receive payloads from GitHub
3
+
4
+ == Install
5
+
6
+ One day I will get around to packaging this up as a gem. For now, I guess you can just clone it.
7
+
8
+ == Usage
9
+
10
+ To use a post-receive hook for your repo, you'll need to go into the admin section (or ask someone that has access to it) and click on Post-Receive URLs. See http://help.github.com/post-receive-hooks/ for more on setting one up.
11
+
12
+ Largely, this just parses and wraps the values sent from GitHub. However, it adds a couple convenience methods as well for formatting and more intuitive naming( ie - private? method for a repo, etc).
13
+
14
+ When any commits are pushed to that repo, GitHub will wrap up the commit info in a JSON payload and POST it to the URL you specify. At your post-receive URL, you'll then be able to parse the payload:
15
+
16
+ @parsed_load = Hubload.new(params[:payload])
17
+
18
+ This will give you a Hubload object, which has references to the commits details, namely: before, after, ref, repo, and commits.
19
+
20
+ === Ref
21
+ ref = @parsed_load.ref # => Hubload::Ref object
22
+ ref.value # => 'refs/heads/master' (or whatever branch)
23
+
24
+
25
+ === After / Before
26
+
27
+ before = @parsed_load.before # => Hubload::After object
28
+ before.value # => 027fa2043b65e7d4092ba310966980db5d36a265
29
+
30
+ after = @parsed_load.after # => Hubload::After object
31
+ after.value # => 027fa2043b65e7d4092ba310966980db5d36a264
32
+
33
+ === Repo
34
+
35
+ You also get info about the commit's repo
36
+
37
+ repo = @parsed_load.repo # => Hubload::Repo object
38
+
39
+ Check out the source for all the info provided by Hubload::Repo. A few examples:
40
+
41
+ repo.fork? # => false
42
+ repo.publicly_visible? # => true
43
+ repo.owner # => Hunter Gillane
44
+
45
+ === Commits
46
+ GitHub's post-receive payload can potentially more than one commit. For that reason you'll get an array of commits in the payload.
47
+
48
+ commits = @parsed_load.commits # => Hubload::Commit []
49
+
50
+ Here are some of the methods available for a single commit:
51
+
52
+ commit_1 = commits[0]
53
+ commit_1.pretty_time # => "07:48 PM"
54
+ commit_1.pretty_date # => "Jan 1, 2010"
55
+ commit_1.message => "added some stuff"
56
+
57
+ etc.
58
+
59
+ == One More Thing
60
+
61
+ Enjoy!
62
+
63
+
64
+ == Note on Patches/Pull Requests
65
+
66
+ * Fork the project.
67
+ * Make your feature addition or bug fix.
68
+ * Add tests for it. This is important so I don't break it in a
69
+ future version unintentionally.
70
+ * Commit, do not mess with rakefile, version, or history.
71
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
72
+ * Send me a pull request. Bonus points for topic branches.
73
+
74
+ == Copyright
75
+
76
+ Copyright (c) 2010 Hunter Gillane. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "hubload"
8
+ gemspec.summary = "Parses post-receive payloads from GitHub"
9
+ gemspec.description = "Parses post-receive payloads from GitHub"
10
+ gemspec.email = "hunter.gillane@gmail.com"
11
+ gemspec.homepage = "http://github.com/pinecon3/hubload"
12
+ gemspec.authors = ["Hunter Gillane"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: gem install jeweler"
17
+ end
18
+
19
+ Spec::Rake::SpecTask.new do |t|
20
+ t.spec_opts = ["--color"]
21
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/hubload.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'pathname'
4
+
5
+ dir = Pathname(__FILE__).dirname.expand_path
6
+
7
+ class Hubload
8
+
9
+ attr_accessor :ref, :before, :after, :repo, :commits
10
+
11
+ def initialize(payload)
12
+ load_data = JSON.parse(payload)
13
+ @ref = Hubload::Ref.new(load_data['ref'])
14
+ @before = Hubload::Before.new(load_data['before'])
15
+ @after = Hubload::After.new(load_data['after'])
16
+ @repo = Hubload::Repo.new(load_data['repository'])
17
+ @commits = parse_commits(load_data['commits'])
18
+ end
19
+
20
+ def parse_commits(commits)
21
+ payload_commits = []
22
+ commits.each do |c|
23
+ payload_commits << Commit.new(c)
24
+ end
25
+ payload_commits
26
+ end
27
+
28
+ private :parse_commits
29
+ end
30
+
31
+ require dir + 'hubload/ref'
32
+ require dir + 'hubload/before'
33
+ require dir + 'hubload/after'
34
+ require dir + 'hubload/repo'
35
+ require dir + 'hubload/commit'
@@ -0,0 +1,9 @@
1
+ class Hubload
2
+ class After
3
+ attr_accessor :value
4
+
5
+ def initialize(val)
6
+ @value = val
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class Hubload
2
+ class Before
3
+ attr_accessor :value
4
+
5
+ def initialize(val)
6
+ @value = val
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,49 @@
1
+ require 'time'
2
+
3
+ class Hubload
4
+ class Commit
5
+
6
+ attr_accessor :message, :commit_id, :timestamp, :url, :author, :modified_files,
7
+ :removed_files, :added_files
8
+
9
+ def initialize(commit)
10
+ @message = commit['message']
11
+ @commit_id = commit['id']
12
+ @timestamp = commit['timestamp']
13
+ @url = commit['url']
14
+ @author = Hubload::Commit::Author.new(commit['author']['name'],
15
+ commit['author']['email'])
16
+ @modified_files = commit['modified']
17
+ @removed_files = commit['removed']
18
+ @added_files = commit['added']
19
+ end
20
+
21
+ # Returns the commit's time in a nice format and normalizes
22
+ # AM and PM times
23
+ #
24
+ def pretty_time
25
+ time = Time.parse(@timestamp)
26
+ if time.hour > 12
27
+ (time - 43200).strftime("%H:%M") << " PM"
28
+ else
29
+ time.strftime("%H:%M") << " AM"
30
+ end
31
+ end
32
+
33
+ # Returns the commit's date in a nice format
34
+ # Month Day, Year
35
+ def pretty_date
36
+ Date.parse(@timestamp).strftime("%b%e, %Y")
37
+ end
38
+
39
+ class Author
40
+
41
+ attr_accessor :email, :name
42
+
43
+ def initialize(name, email)
44
+ @name = name
45
+ @email = email
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,9 @@
1
+ class Hubload
2
+ class Ref
3
+ attr_accessor :value
4
+
5
+ def initialize(ref)
6
+ @value = ref
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ class Hubload
2
+ class Repo
3
+
4
+ attr_accessor :forks, :description, :watchers, :open_issues, :url, :fork,
5
+ :publicly_visible, :homepage, :owner, :name
6
+
7
+ def initialize(repo)
8
+ @forks = repo['forks']
9
+ @description = repo['description']
10
+ @watchers = repo['watchers']
11
+ @open_issues = repo['open_issues']
12
+ @url = repo['url']
13
+ @fork = repo['fork']
14
+ @publicly_visible = !repo['private']
15
+ @homepage = repo['homepage']
16
+ @owner = Owner.new(repo['owner']['name'], repo['owner']['email'])
17
+ @name = repo['name']
18
+ end
19
+
20
+ # Returns true if the repo is public, false otherwise
21
+ def publicly_visible?
22
+ @publicly_visible
23
+ end
24
+
25
+ # Returns true if the repo is a fork, false otherwise
26
+ def fork?
27
+ @fork
28
+ end
29
+
30
+ class Owner
31
+ attr_accessor :name, :email
32
+
33
+ def initialize(name, email)
34
+ @name = name
35
+ @email = email
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,174 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
2
+
3
+ describe Hubload do
4
+ describe "parse single commit payload" do
5
+ before(:each) do
6
+ @parsed_load = Hubload.new(single_commit_payload)
7
+ end
8
+
9
+ describe "should parse the payload's ref" do
10
+ it "should return a Hubload::Ref object" do
11
+ @parsed_load.ref.class.should == Hubload::Ref
12
+ end
13
+
14
+ it "should correctly parse the ref value" do
15
+ @parsed_load.ref.value.should == "refs/heads/master"
16
+ end
17
+ end
18
+
19
+ describe "should parse the payload's before" do
20
+ it "should return a Hubload::Before object" do
21
+ @parsed_load.before.class.should == Hubload::Before
22
+ end
23
+
24
+ it "should correctly parse the payload's before value" do
25
+ @parsed_load.before.value.should == "34891069f142cbfc80cc880e963b41a0104305c7"
26
+ end
27
+ end
28
+
29
+ describe "should parse the payload's after" do
30
+ it "should return a Hubload::After object" do
31
+ @parsed_load.after.class.should == Hubload::After
32
+ end
33
+
34
+ it "should correctly parse the payload's after value" do
35
+ @parsed_load.after.value.should == "027fa2043b65e7d4092ba310966980db5d36a264"
36
+ end
37
+ end
38
+
39
+ describe "should parse the payload's repository" do
40
+ it "should return a Hubload::Repo object" do
41
+ @parsed_load.repo.class.should == Hubload::Repo
42
+ end
43
+
44
+ it "should parse the number of forks for the repo" do
45
+ @parsed_load.repo.forks.should == 0
46
+ end
47
+
48
+ it "should parse the repo's descriptions" do
49
+ @parsed_load.repo.description.should == "just for testing"
50
+ end
51
+
52
+ it "should parse the repo's watchers" do
53
+ @parsed_load.repo.watchers.should == 1
54
+ end
55
+
56
+ it "should parse the repo's open issues" do
57
+ @parsed_load.repo.open_issues.should == 0
58
+ end
59
+
60
+ it "should parse the repo's url" do
61
+ @parsed_load.repo.url.should == "http://github.com/hunt3131/post-receive-testing"
62
+ end
63
+
64
+ it "should determine if this repo is a fork" do
65
+ @parsed_load.repo.fork?.should == false
66
+ end
67
+
68
+ it "should parse the visibility of the repo" do
69
+ @parsed_load.repo.publicly_visible?.should == true
70
+ end
71
+
72
+ it "should parse the repo's homepage" do
73
+ @parsed_load.repo.homepage.should == "http://homepage.net"
74
+ end
75
+
76
+ it "should parse the repo's owner" do
77
+ @parsed_load.repo.owner.email.should == "hunter.gillane@gmail.com"
78
+ @parsed_load.repo.owner.name.should == "hunt3131"
79
+ end
80
+
81
+ it "should parse the repo's name" do
82
+ @parsed_load.repo.name.should == "repo_1"
83
+ end
84
+ end
85
+
86
+ describe "should parse the payloads single commit" do
87
+ it "should return an array of Hubload::Commit objects" do
88
+ commits = @parsed_load.commits
89
+ commits.each do |c|
90
+ c.class.should == Hubload::Commit
91
+ end
92
+ end
93
+
94
+ it "should return a commit array with size 1" do
95
+ @parsed_load.commits.size.should == 1
96
+ end
97
+
98
+ describe "should correctly parse the values of the first commit" do
99
+
100
+ before(:each) do
101
+ @commit = @parsed_load.commits[0]
102
+ end
103
+
104
+ it "should parse the commit's message" do
105
+ @commit.message.should == "a text file"
106
+ end
107
+
108
+ it "should parse the commit's id" do
109
+ @commit.commit_id.should == "027fa2043b65e7d4092ba310966980db5d36a264"
110
+ end
111
+
112
+ it "should parse the commit's timestamp" do
113
+ @commit.timestamp.should == "2010-01-03T19:48:03-08:00"
114
+ end
115
+
116
+ describe "parsing the commits time and date" do
117
+ it "should return a formatted time for the commit" do
118
+ @commit.pretty_time.should == "07:48 PM"
119
+ end
120
+
121
+ it "should return a formatted date for the commit" do
122
+ @commit.pretty_date.should == "Jan 3, 2010"
123
+ end
124
+ end
125
+
126
+ it "should parse the commit's url" do
127
+ @commit.url.should == "http://github.com/hunt3131/post-receive-testing/commit/027fa2043b65e7d4092ba310966980db5d36a264"
128
+ end
129
+
130
+ describe "it should parse the commit's author" do
131
+ it "return a Hubload::Commit::Author object" do
132
+ @commit.author.class.should == Hubload::Commit::Author
133
+ end
134
+
135
+ it "should parse the author's name" do
136
+ @commit.author.name.should == "Hunter Gillane"
137
+ end
138
+
139
+ it "should parse the author's email" do
140
+ @commit.author.email.should == "hunter.gillane@gmail.com"
141
+ end
142
+ end
143
+
144
+ it "should create a list of the files changed" do
145
+ @commit.modified_files.should == ["modified_file_1.txt", "modified_file_2.txt"]
146
+ end
147
+
148
+ it "should create a list of the files removed" do
149
+ @commit.removed_files.should == ["removed_file_1.txt", "removed_file_2.txt"]
150
+ end
151
+
152
+ it "should create a list of the files added" do
153
+ @commit.added_files.should == ["new_file_1.txt", "new_file_2.txt"]
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ describe "parse a multi-commit payload" do
160
+ before(:each) do
161
+ @parsed_load = Hubload.new(multi_commit_payload)
162
+ @commit_1 = @parsed_load.commits[0]
163
+ @commit_2 = @parsed_load.commits[1]
164
+ end
165
+
166
+ it "should return an array of two commits" do
167
+ @parsed_load.commits.size.should == 2
168
+ end
169
+
170
+ it "should return a unique commit id for both commits" do
171
+ @commit_1.commit_id.should_not == @commit_2.commit_id
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'hubload')
2
+
3
+ def single_commit_payload
4
+ '{"commits":[{"removed":["removed_file_1.txt", "removed_file_2.txt"],"message":"a text file","modified":["modified_file_1.txt", "modified_file_2.txt"],"url":"http://github.com/hunt3131/post-receive-testing/commit/027fa2043b65e7d4092ba310966980db5d36a264","added":["new_file_1.txt", "new_file_2.txt"],"author":{"email":"hunter.gillane@gmail.com","name":"Hunter Gillane"},"timestamp":"2010-01-03T19:48:03-08:00","id":"027fa2043b65e7d4092ba310966980db5d36a264"}],"repository":{"forks":0,"description":"just for testing","watchers":1,"open_issues":0,"url":"http://github.com/hunt3131/post-receive-testing","fork":false,"private":false,"homepage":"http://homepage.net","owner":{"email":"hunter.gillane@gmail.com","name":"hunt3131"},"name":"repo_1"},"ref":"refs/heads/master","before":"34891069f142cbfc80cc880e963b41a0104305c7","after":"027fa2043b65e7d4092ba310966980db5d36a264"}'
5
+ end
6
+
7
+ def multi_commit_payload
8
+ '{"commits":[{"removed":["removed_file_1.txt", "removed_file_2.txt"],"message":"a text file","modified":["modified_file_1.txt", "modified_file_2.txt"],"url":"http://github.com/hunt3131/post-receive-testing/commit/027fa2043b65e7d4092ba310966980db5d36a264","added":["new_file_1.txt", "new_file_2.txt"],"author":{"email":"hunter.gillane@gmail.com","name":"Hunter Gillane"},"timestamp":"2010-01-03T19:48:03-08:00","id":"027fa2043b65e7d4092ba310966980db5d36a264"}, {"removed": [], "message": "second commit", "modified": [],"url": "http://github.com/hunt3131/post-receive-testing/commit/027fa2043b65e7d4092ba310966980db5d36a265","added": ["commit_2_file.txt" ], "author": { "email": "someoneelse@gmail.com", "name": "Some guy" }, "timestamp": "2010-01-03T19:48:03-09:00", "id": "027fa2043b65e7d4092ba310966980db5d36a265" }],"repository":{"forks":0,"description":"just for testing","watchers":1,"open_issues":0,"url":"http://github.com/hunt3131/post-receive-testing","fork":false,"private":false,"homepage":"http://homepage.net","owner":{"email":"hunter.gillane@gmail.com","name":"hunt3131"},"name":"repo_1"},"ref":"refs/heads/master","before":"34891069f142cbfc80cc880e963b41a0104305c7","after":"027fa2043b65e7d4092ba310966980db5d36a264"}'
9
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hubload
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hunter Gillane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-09 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Parses post-receive payloads from GitHub
17
+ email: hunter.gillane@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION
30
+ - lib/hubload.rb
31
+ - lib/hubload/after.rb
32
+ - lib/hubload/before.rb
33
+ - lib/hubload/commit.rb
34
+ - lib/hubload/ref.rb
35
+ - lib/hubload/repo.rb
36
+ - spec/hubload_spec.rb
37
+ - spec/spec_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/pinecon3/hubload
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Parses post-receive payloads from GitHub
66
+ test_files:
67
+ - spec/hubload_spec.rb
68
+ - spec/spec_helper.rb