Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to create and update comments on a story. #84

Merged
merged 2 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ story = TrackerApi::Resources::Story.new( client: client,
id: 847762630) # Use the Story resource to get the story
comments = story.comments # comments without first fetching the story

comment = story.create_comment(text: "Use the force!") # Create a new comment on the story

comment.text += " (please be careful)"
comment.save # Update text of an existing comment

task = story.tasks.first # Get story tasks
task.complete = true
task.save # Mark a task complete
Expand Down
22 changes: 20 additions & 2 deletions lib/tracker_api/endpoints/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,27 @@ def get(project_id, story_id, params={})
raise Errors::UnexpectedData, 'Array of comments expected' unless data.is_a? Array

data.map do |comment|
Resources::Comment.new({ story_id: story_id }.merge(comment))
Resources::Comment.new({ client: client,
project_id: project_id,
story_id: story_id }.merge(comment))
end
end

def create(project_id, story_id, params={})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move create and update into a new Endpoints::Comment class? See Endpoints::Tasks and Endpoints::Task. Just trying to keep the collection and singular resource behavior together and separate.

data = client.post("/projects/#{project_id}/stories/#{story_id}/comments", params: params).body
Resources::Comment.new({ client: client, project_id: project_id }.merge(data))
end

def update(comment, params={})
raise ArgumentError, 'Valid comment required to update.' unless comment.instance_of?(Resources::Comment)

data = client.put("/projects/#{comment.project_id}/stories/#{comment.story_id}/comments/#{comment.id}",
params: params).body

comment.attributes = data
comment.clean!
comment
end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/tracker_api/resources/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module Resources
class Comment
include Shared::Base

attribute :client

attribute :project_id, Integer
attribute :story_id, Integer
attribute :epic_id, Integer
attribute :text, String
Expand All @@ -14,6 +17,19 @@ class Comment
attribute :commit_identifier, String
attribute :commit_type, String
attribute :kind, String

class UpdateRepresenter < Representable::Decorator
include Representable::JSON

property :id
property :text
end

def save
raise ArgumentError, 'Cannot update a comment with an unknown story_id.' if story_id.nil?

Endpoints::Comments.new(client).update(self, UpdateRepresenter.new(Comment.new(self.dirty_attributes)))
end
end
end
end
6 changes: 6 additions & 0 deletions lib/tracker_api/resources/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def create_task(params)
Endpoints::Task.new(client).create(project_id, id, params)
end

# @param [Hash] params attributes to create the comment with
# @return [Comment] newly created Comment
def create_comment(params)
Endpoints::Comments.new(client).create(project_id, id, params)
end

# Save changes to an existing Story.
def save
raise ArgumentError, 'Can not update a story with an unknown project_id.' if project_id.nil?
Expand Down
35 changes: 35 additions & 0 deletions test/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require_relative 'minitest_helper'

describe TrackerApi::Resources::Comment do
let(:pt_user) { PT_USER_1 }
let(:client) { TrackerApi::Client.new token: pt_user[:token] }
let(:project_id) { pt_user[:project_id] }
let(:project) { VCR.use_cassette('get project') { client.project(project_id) } }
let(:story_id) { '66728004' }
let(:story) { VCR.use_cassette('get story') { project.story(story_id) } }
let(:comments) { VCR.use_cassette('get comments') { story.comments } }
let(:existing_comment) { comments.first }

it 'can create a comment given a story' do
text = "Test creating a comment"
comment = nil
VCR.use_cassette('create comment', record: :new_episodes) do
comment = story.create_comment(text: text)
end

comment.text.must_equal text
comment.clean?.must_equal true
end

it 'can update an existing comment' do
new_text = "#{existing_comment.text}+"
existing_comment.text = new_text

VCR.use_cassette('save comment', record: :new_episodes) do
existing_comment.save
end

existing_comment.text.must_equal new_text
existing_comment.clean?.must_equal true
end
end
1 change: 1 addition & 0 deletions test/vcr/cassettes/create_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http_interactions":[{"request":{"method":"post","uri":"https://www.pivotaltracker.com/services/v5/projects/1027488/stories/66728004/comments","body":{"encoding":"UTF-8","string":"{\"text\":\"Test creating a comment\"}"},"headers":{"User-Agent":["Ruby/2.2.3 (x86_64-darwin14; ruby) TrackerApi/1.4.1 Faraday/0.9.2"],"X-TrackerToken":["d55c3bc1f74346b843ca84ba340b29bf"],"Content-Type":["application/json"]}},"response":{"status":{"code":200,"message":null},"headers":{"Access-Control-Allow-Credentials":["false"],"Access-Control-Allow-Headers":["X-TrackerToken,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Tracker-Warn-Unless-Project-Version-Is"],"Access-Control-Allow-Methods":["GET, POST, PUT, DELETE, OPTIONS"],"Access-Control-Allow-Origin":["*"],"Cache-Control":["max-age=0, private, must-revalidate"],"Content-Type":["application/json; charset=utf-8"],"Date":["Wed, 16 Nov 2016 14:17:47 GMT"],"Etag":["\"05a4afa88e8d89128c99e3803eb746cd\""],"Server":["nginx + Phusion Passenger"],"Status":["200 OK"],"Strict-Transport-Security":["max-age=31536000; includeSubDomains"],"X-Powered-By":["Phusion Passenger"],"X-Rack-Cache":["invalidate, pass"],"X-Request-Id":["c64522554bfcacb90f63fea5c9025473"],"X-Runtime":["0.148598"],"X-Tracker-Client-Pinger-Interval":["20"],"X-Tracker-Project-Version":["155"],"X-Ua-Compatible":["IE=Edge,chrome=1"],"X-Vcap-Request-Id":["27b5c8de-19f9-46c8-42b0-8fd3f82ed5cb"],"Content-Length":["178"],"Connection":["keep-alive"]},"body":{"encoding":"ASCII-8BIT","string":"{\"kind\":\"comment\",\"id\":155658605,\"story_id\":66728004,\"text\":\"Test creating a comment\",\"person_id\":1266314,\"created_at\":\"2016-11-16T14:17:47Z\",\"updated_at\":\"2016-11-16T14:17:47Z\"}"},"http_version":null},"recorded_at":"Wed, 16 Nov 2016 14:17:48 GMT"}],"recorded_with":"VCR 3.0.3"}
1 change: 1 addition & 0 deletions test/vcr/cassettes/get_comments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http_interactions":[{"request":{"method":"get","uri":"https://www.pivotaltracker.com/services/v5/projects/1027488/stories/66728004/comments","body":{"encoding":"US-ASCII","string":""},"headers":{"User-Agent":["Ruby/2.2.3 (x86_64-darwin14; ruby) TrackerApi/1.4.1 Faraday/0.9.2"],"X-TrackerToken":["d55c3bc1f74346b843ca84ba340b29bf"]}},"response":{"status":{"code":200,"message":null},"headers":{"Access-Control-Allow-Credentials":["false"],"Access-Control-Allow-Headers":["X-TrackerToken,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Tracker-Warn-Unless-Project-Version-Is"],"Access-Control-Allow-Methods":["GET, POST, PUT, DELETE, OPTIONS"],"Access-Control-Allow-Origin":["*"],"Cache-Control":["max-age=0, private, must-revalidate"],"Content-Type":["application/json; charset=utf-8"],"Date":["Wed, 16 Nov 2016 14:14:33 GMT"],"Etag":["\"0deb8dbe1ce2f0d1f95892eb3114789a\""],"Server":["nginx + Phusion Passenger"],"Status":["200 OK"],"Strict-Transport-Security":["max-age=31536000; includeSubDomains"],"X-Powered-By":["Phusion Passenger"],"X-Rack-Cache":["miss"],"X-Request-Id":["4e0fe5c2f0ad5f4243b5961ebbf3e767"],"X-Runtime":["0.042615"],"X-Tracker-Client-Pinger-Interval":["20"],"X-Tracker-Project-Version":["154"],"X-Ua-Compatible":["IE=Edge,chrome=1"],"X-Vcap-Request-Id":["7b308ac4-1cf5-4318-7843-5dde89abea3d"],"Content-Length":["355"],"Connection":["keep-alive"]},"body":{"encoding":"ASCII-8BIT","string":"[{\"kind\":\"comment\",\"id\":120836920,\"story_id\":66728004,\"text\":\"This is a comment.\",\"person_id\":1266314,\"created_at\":\"2015-12-17T22:13:55Z\",\"updated_at\":\"2015-12-17T22:13:55Z\"},{\"kind\":\"comment\",\"id\":120836938,\"story_id\":66728004,\"text\":\"This is another comment.\",\"person_id\":1266314,\"created_at\":\"2015-12-17T22:14:04Z\",\"updated_at\":\"2015-12-17T22:14:04Z\"}]"},"http_version":null},"recorded_at":"Wed, 16 Nov 2016 14:14:34 GMT"}],"recorded_with":"VCR 3.0.3"}
1 change: 1 addition & 0 deletions test/vcr/cassettes/save_comment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"http_interactions":[{"request":{"method":"put","uri":"https://www.pivotaltracker.com/services/v5/projects/1027488/stories/66728004/comments/120836920","body":{"encoding":"UTF-8","string":"{\"text\":\"This is a comment.+\"}"},"headers":{"User-Agent":["Ruby/2.2.3 (x86_64-darwin14; ruby) TrackerApi/1.4.1 Faraday/0.9.2"],"X-TrackerToken":["d55c3bc1f74346b843ca84ba340b29bf"],"Content-Type":["application/json"]}},"response":{"status":{"code":200,"message":null},"headers":{"Access-Control-Allow-Credentials":["false"],"Access-Control-Allow-Headers":["X-TrackerToken,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Tracker-Warn-Unless-Project-Version-Is"],"Access-Control-Allow-Methods":["GET, POST, PUT, DELETE, OPTIONS"],"Access-Control-Allow-Origin":["*"],"Cache-Control":["max-age=0, private, must-revalidate"],"Content-Type":["application/json; charset=utf-8"],"Date":["Wed, 16 Nov 2016 14:20:30 GMT"],"Etag":["\"d4458c709c842d1e8544e2b3397418f9\""],"Server":["nginx + Phusion Passenger"],"Status":["200 OK"],"Strict-Transport-Security":["max-age=31536000; includeSubDomains"],"X-Powered-By":["Phusion Passenger"],"X-Rack-Cache":["invalidate, pass"],"X-Request-Id":["87382a7f1e4c3dd82514cde390832f7f"],"X-Runtime":["0.211884"],"X-Tracker-Client-Pinger-Interval":["20"],"X-Tracker-Project-Version":["156"],"X-Ua-Compatible":["IE=Edge,chrome=1"],"X-Vcap-Request-Id":["3ba8192f-073e-44f9-50b2-74fca707f240"],"Content-Length":["174"],"Connection":["keep-alive"]},"body":{"encoding":"ASCII-8BIT","string":"{\"kind\":\"comment\",\"id\":120836920,\"story_id\":66728004,\"text\":\"This is a comment.+\",\"person_id\":1266314,\"created_at\":\"2015-12-17T22:13:55Z\",\"updated_at\":\"2016-11-16T14:20:30Z\"}"},"http_version":null},"recorded_at":"Wed, 16 Nov 2016 14:20:30 GMT"}],"recorded_with":"VCR 3.0.3"}