From 2b04c2a67fb981a1ef3491be4d7775de7f2a221d Mon Sep 17 00:00:00 2001
From: Nihad Abbasov <narkoz.2008@gmail.com>
Date: Fri, 11 Nov 2011 12:11:27 +0400
Subject: [PATCH] create atom feed for commits

---
 app/controllers/commits_controller.rb |  5 +++--
 app/views/commits/index.atom.builder  | 23 +++++++++++++++++++++++
 app/views/layouts/project.html.haml   |  2 ++
 spec/requests/commits_spec.rb         |  9 +++++++++
 4 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 app/views/commits/index.atom.builder

diff --git a/app/controllers/commits_controller.rb b/app/controllers/commits_controller.rb
index 79703cf104a6b..1afe25a745c46 100644
--- a/app/controllers/commits_controller.rb
+++ b/app/controllers/commits_controller.rb
@@ -13,7 +13,7 @@ def index
     load_refs # load @branch, @tag & @ref
 
     @repo = project.repo
-    limit, offset = (params[:limit] || 20), (params[:offset] || 0) 
+    limit, offset = (params[:limit] || 20), (params[:offset] || 0)
 
     if params[:path]
       @commits = @repo.log(@ref, params[:path], :max_count => limit, :skip => offset)
@@ -24,6 +24,7 @@ def index
     respond_to do |format|
       format.html # index.html.erb
       format.js
+      format.atom { render :layout => false }
     end
   end
 
@@ -32,7 +33,7 @@ def show
     @notes = project.notes.where(:noteable_id => @commit.id, :noteable_type => "Commit").order("created_at DESC").limit(20)
     @note = @project.notes.new(:noteable_id => @commit.id, :noteable_type => "Commit")
 
-    respond_to do |format| 
+    respond_to do |format|
       format.html
       format.js { respond_with_notes }
     end
diff --git a/app/views/commits/index.atom.builder b/app/views/commits/index.atom.builder
new file mode 100644
index 0000000000000..2a352dac1b799
--- /dev/null
+++ b/app/views/commits/index.atom.builder
@@ -0,0 +1,23 @@
+xml.instruct!
+xml.feed "xmlns" => "http://www.w3.org/2005/Atom", "xmlns:media" => "http://search.yahoo.com/mrss/" do
+  xml.title   "Recent commits to #{@project.name}:#{@ref}"
+  xml.link    :href => project_commits_url(@project, :atom, :ref => @ref), :rel => "self", :type => "application/atom+xml"
+  xml.link    :href => project_commits_url(@project), :rel => "alternate", :type => "text/html"
+  xml.id      project_commits_url(@project)
+  xml.updated @commits.first.committed_date.strftime("%Y-%m-%dT%H:%M:%SZ") if @commits.any?
+
+  @commits.each do |commit|
+    xml.entry do
+      xml.id      project_commit_url(@project, :id => commit.id)
+      xml.link    :href => project_commit_url(@project, :id => commit.id)
+      xml.title   truncate(commit.safe_message, :length => 80)
+      xml.updated commit.committed_date.strftime("%Y-%m-%dT%H:%M:%SZ")
+      xml.media   :thumbnail, :width => "40", :height => "40", :url => gravatar_icon(commit.author_email)
+      xml.author do |author|
+        xml.name commit.author_name
+        xml.email commit.author_email
+      end
+      xml.summary commit.safe_message
+    end
+  end
+end
diff --git a/app/views/layouts/project.html.haml b/app/views/layouts/project.html.haml
index 0550d89e20794..927d62db29584 100644
--- a/app/views/layouts/project.html.haml
+++ b/app/views/layouts/project.html.haml
@@ -5,6 +5,8 @@
       GitLab #{" - #{@project.name}" if @project && !@project.new_record?}
     = stylesheet_link_tag    "application"
     = javascript_include_tag "application"
+    - if current_page?(tree_project_path(@project)) || current_page?(project_commits_path(@project))
+      = auto_discovery_link_tag(:atom, project_commits_url(@project, :atom, :ref => @ref), :title => "Recent commits to #{@project.name}:#{@ref}")
     = csrf_meta_tags
     = javascript_tag do
       REQ_URI = "#{request.env["REQUEST_URI"]}";
diff --git a/spec/requests/commits_spec.rb b/spec/requests/commits_spec.rb
index 79bb03f53b6ce..2bbd6b9f10447 100644
--- a/spec/requests/commits_spec.rb
+++ b/spec/requests/commits_spec.rb
@@ -25,6 +25,15 @@
       page.should have_content(commit.author)
       page.should have_content(commit.message)
     end
+
+    it "should render atom feed" do
+      visit project_commits_path(project, :atom)
+
+      page.response_headers['Content-Type'].should have_content("application/atom+xml")
+      page.body.should have_selector("title", :text => "Recent commits to #{project.name}")
+      page.body.should have_selector("author email", :text => commit.author_email)
+      page.body.should have_selector("entry summary", :text => commit.message)
+    end
   end
 
   describe "GET /commits/:id" do
-- 
GitLab