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}" } 
  1. getcommits() returns collection of com.atlassian.bitbucket.commit.commit.
  2. object.each(closure) added groovy gdk (yes, object class) , calls closure repeatedly; each time element of collection.
  3. although it's not apparent in example code, line within each(closure) gstring. expressions within ${...} evaluated , whole thing concatenated string.

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -