Bitbucket - Groovy Pre-receive Hook -
i trying understand following code snippet following site.
<% refchanges.getcommits(repository).each { commit -> %> - ${commit.author.name} | ${commit.displayid} | ${commit.message} | ${commit.authortimestamp} <% } %> the script using getcommits method, when @ the documentation refchange interface not see such method.
i consider myself expert java developer, have no workable knowledge in groovy, assume misunderstanding groovy or bitbucket documentation (or both).
in groovy it's possible add methods class or interface @ run-time via meta-programming. since refchange interface not include getcommits(), must method being added after-the-fact. based on example code, looks they're using meta-class.
where getcommits()?
for example, in groovy collection interface gets method findall() (along many other methods). can confirm follows:
assert collection.metaclass.metamethods*.name.contains('findall') == true the code above grabs names of meta methods , uses contains() see if match found. can confirm same getcommits() in similar way:
assert collection.metaclass.metamethods*.name.contains('getcommits') == true note specified collection rather refchange because refchanges collection of refchange. , think atlasssian stuck getcommits() collection convenience method.
how work?
to understand what's going, i'll remove templating code:
refchanges.getcommits(repository).each { commit -> "${commit.author.name} | ${commit.displayid} | ${commit.message} | ${commit.authortimestamp}" } getcommits()returnscollectionofcom.atlassian.bitbucket.commit.commit.- object.each(closure) added groovy gdk (yes, object class) , calls
closurerepeatedly; each time element ofcollection. - although it's not apparent in example code, line within
each(closure)gstring. expressions within${...}evaluated , whole thing concatenatedstring.
Comments
Post a Comment