Lately I’ve been having a lot of fun playing around with the mingle api via active resource. I’ve developed a couple of small projects including one for reporting feature prioritization, as well a release notes generator. As it turns out, the release notes generator is a pretty big hit with our software support staff and the new feature requests are rolling in. Most recently a new feature required accessing the comment history of a story.
The Mingle api is powerful and simple and it makes little projects like these easy and fun. If you haven’t used the mingle api with active resource before, please take a look at my previous post (mingle automation with active resource) or this simple tutorial which I used to get started (tutorial on using mingle with active resource).
As part of our release notes we call out certain stories as having a high support implications, particularly if they modify some legacy database objects or legacy code. Our support department has found this to be instrumental in helping them quickly come up to speed on a new release. In addition to knowing which stories are high profile, it’s also important to know a little detail about these stories. Enter mingle card comments. As part of the release notes we want to include the last comment for each card under the assumption that developers will provide relavant comments before closing the card.
The mingle api documentation says that the comment attribute is only available in the historical versions of a card. Because of this, simply finding a card and grabbing the comment attribute will fail with an undefined method error.
require 'rubygems'
require 'activeresource'
class Card < ActiveResource::Base
Project = "my_project"
MyMingleServer = "mingle.weromans.com"
self.site = "http://#{MyMingleServer}/projects/#{Project}"
end
Card.user = "mroman"
Card.password = "*******"
card = Card.find 555
# this will fail with undefined method
card.comment
The above code fails because I didn’t get a historical version of the card. To do that I need to specify the specific card version we want to retrieve. Since I want to get the last comment posted, I’m most likely going to loop backward through the version history until I find my comment. To do this I’ll need to start with my cards most recent version. I can just open the mingle UI to do that but since I’m already writing code, I’ll do it in an irb prompt, but first i’ll fix my Card class to get rid of that error.
require 'rubygems'
require 'activeresource'
class Card < ActiveResource::Base
Project = "my_project"
MyMingleServer = "mingle.weromans.com"
self.site = "http://#{MyMingleServer}/projects/#{Project}"
end
Card.user = "mroman"
Card.password = "*******"
Now I’ll start up irb in the same directory as my card.rb file.
> require 'rubygems'
=> true
> require 'activeresource'
=> true
> require 'card'
=> ["Card"]
> (Card.find 555).version
=> 6
Great, the most recent version of my card is 6. Now all I should have to do is get version 6 of my card and access the comment attribute. Well, not really. For some reason getting the most recent version of a card doesn’t seem to qualify that card as a historical version which means that your card will not have the comment attribute and your code will still fail.
require 'rubygems'
require 'activeresource'
class Card < ActiveResource::Base
Project = "my_project"
MyMingleServer = "mingle.weromans.com"
self.site = "http://#{MyMingleServer}/projects/#{Project}"
end
Card.user = "mroman"
Card.password = "*******"
card = Card.find(555, :params => {:version => 6})
# this will fail with undefined method
card.comment
If I change my code to get the previous version it appears to work.
require 'rubygems'
require 'activeresource'
class Card < ActiveResource::Base
Project = "my_project"
MyMingleServer = "mingle.weromans.com"
self.site = "http://#{MyMingleServer}/projects/#{Project}"
end
Card.user = "mroman"
Card.password = "*******"
card = Card.find 555
historical_card = Card.find(card.number,
:params => {:version => card.version - 1})
card.comment
Success, kind of. The above code returned nil, but I’m taking that as a good sign. This means that my historical_card has a comment attribute, it most likely doesn’t have an actual comment though. If that’s the case all that’s left to do is clean up my code and create a loop to move backward through the card history until I find my comment.
require 'rubygems'
require 'activeresource'
class Card < ActiveResource::Base
Project = "my_project"
MyMingleServer = "mingle.weromans.com"
self.site = "http://#{MyMingleServer}/projects/#{Project}"
def Card.find_comment_for(number, version)
card = Card.find(number, :params => {:version => version})
card.comment
end
def Card.last_comment_for(card)
version = card.version -1
while ((comment = Card.find_comment_for(card.number, version)).nil?
and version > 0)
version -= 1
end
comment || "no comment"
end
end
Card.user = "mroman"
Card.password = "*******"
Card.last_comment_for(Card.find(555))
This solves the requirement, however there is still the open question of what happens if the last comment was created on the last version of the card. Unfortunately as it stands I have not found a way to retrieve a comment on a cards current version.
