From c1467f5d97c04c22e2119ace084bb016f8f53d48 Mon Sep 17 00:00:00 2001
From: Michael Greene <michael.greene@gmail.com>
Date: Tue, 5 Apr 2016 13:04:11 -0500
Subject: [PATCH] Allow back dating notes on creation

---
 CHANGELOG                       |  1 +
 doc/api/notes.md                |  1 +
 lib/api/notes.rb                |  5 +++++
 spec/requests/api/notes_spec.rb | 13 +++++++++++++
 4 files changed, 20 insertions(+)

diff --git a/CHANGELOG b/CHANGELOG
index e5b8b9d7ce6..9baf6516ef6 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -23,6 +23,7 @@ v 8.7.0 (unreleased)
   - Fix Error 500 after renaming a project path (Stan Hu)
   - Fix a bug whith trailing slash in teamcity_url (Charles May)
   - Allow back dating on issues when created or updated through the API
+  - Allow back dating on issue notes when created through the API
   - Fix avatar stretching by providing a cropping feature
   - API: Expose `subscribed` for issues and merge requests (Robert Schilling)
   - Allow SAML to handle external users based on user's information !3530
diff --git a/doc/api/notes.md b/doc/api/notes.md
index 2e0936f11b5..7aa1c2155bf 100644
--- a/doc/api/notes.md
+++ b/doc/api/notes.md
@@ -89,6 +89,7 @@ Parameters:
 - `id` (required) - The ID of a project
 - `issue_id` (required) - The ID of an issue
 - `body` (required) - The content of a note
+- `created_at` (optional) - Date time string, ISO 8601 formatted, e.g. 2016-03-11T03:45:40Z
 
 ### Modify existing issue note
 
diff --git a/lib/api/notes.rb b/lib/api/notes.rb
index a1c98f5e8ff..71a53e6f0d6 100644
--- a/lib/api/notes.rb
+++ b/lib/api/notes.rb
@@ -61,6 +61,7 @@ module API
         #   id (required) - The ID of a project
         #   noteable_id (required) - The ID of an issue or snippet
         #   body (required) - The content of a note
+        #   created_at (optional) - The date
         # Example Request:
         #   POST /projects/:id/issues/:noteable_id/notes
         #   POST /projects/:id/snippets/:noteable_id/notes
@@ -73,6 +74,10 @@ module API
            noteable_id: params[noteable_id_str]
           }
 
+          if params[:created_at] && (current_user.is_admin? || user_project.owner == current_user)
+            opts[:created_at] = params[:created_at]
+          end
+
           @note = ::Notes::CreateService.new(user_project, current_user, opts).execute
 
           if @note.valid?
diff --git a/spec/requests/api/notes_spec.rb b/spec/requests/api/notes_spec.rb
index a467bc935af..ec9eda0a2ed 100644
--- a/spec/requests/api/notes_spec.rb
+++ b/spec/requests/api/notes_spec.rb
@@ -158,6 +158,19 @@ describe API::API, api: true  do
         post api("/projects/#{project.id}/issues/#{issue.id}/notes"), body: 'hi!'
         expect(response.status).to eq(401)
       end
+
+      context 'when an admin or owner makes the request' do
+        it 'accepts the creation date to be set' do
+          creation_time = 2.weeks.ago
+          post api("/projects/#{project.id}/issues/#{issue.id}/notes", user),
+            body: 'hi!', created_at: creation_time
+          expect(response.status).to eq(201)
+          expect(json_response['body']).to eq('hi!')
+          expect(json_response['author']['username']).to eq(user.username)
+          expect(Time.parse(json_response['created_at'])).to be_within(1.second).of(creation_time)
+        end
+      end
+
     end
 
     context "when noteable is a Snippet" do
-- 
GitLab