Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Pre-receive hooks

devops_account October 3, 2019

Hi, I'm using ScriptRunner to write a Pre-receive hook. In this customized script, I used the matchingChangsCollector. But there is an error show - 

[Static type checking] - Cannot call java.util.Collection #pathsMatch(com.atlassian.bitbucket.repository.Repository, java.lang.String, groovy.lang.Closure ) with arguments [com.atlassian.bitbucket.repository.Repository, java.lang.String, groovy.lang.Closure ] @ line 24, column 5.

 

Basically, I just used the sample code from - https://scriptrunner.adaptavist.com/5.6.3/bitbucket/

 

import com.atlassian.bitbucket.content.Change

import com.atlassian.bitbucket.repository.RefChange

import com.atlassian.bitbucket.repository.Repository

 

def repository = repository as Repository
def refChanges = refChanges as Collection<RefChange>
def hookMsg = ""

def matchingChangesCollector = { Iterable<Change> matchingChanges ->
hookMsg << "The following changes containing private ssh keys were blocked:\n"

matchingChanges.each { change ->
def message = "Id: ${shortId(change.contentId)}, Path: ${change.path.toString()}\n"

hookMsg << message
}
}

 

refChanges.pathsMatch(repository, "glob:id_rsa", matchingChangesCollector)

private String shortId(String contentId) {
contentId.substring(0, 11)
}

 

Thank you,

Jason

3 answers

1 accepted

1 vote
Answer accepted
Mark Ryan January 7, 2020

Hi 

 

Just came across your post whilst looking for something else and it struck me that you might be trying to implement a 'Custom Script Hook' whereby the example you quote in your original post was actually a script/condition for a 'Protect git refs' Pre Hook.

 

If you want to implement your script above as a 'Custom' Hook, then you'll need to explicitly return true/false plus a veto message. I have modified your script in order to make it work as a 'Custom' Hook:

 

import com.atlassian.bitbucket.content.Change
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils

def repository = repository as Repository
def refChanges = refChanges as Collection<RefChange>

def matchingChangesCollector = { Iterable<Change> matchingChanges ->
  hookMessage << "The following changes containing private ssh keys were blocked:\n"
  matchingChanges.each { change ->
    hookMessage << "Id: ${shortId(change.contentId)}, Path: ${change.path.toString()}\n"
  }
}

if ( refChanges.pathsMatch(repository, "glob:id_rsa", matchingChangesCollector) ) {
  hookResponse.out().print(BitbucketCannedScriptUtils.wrapHookResponse(hookMessage))
  return false
}

private String shortId(String contentId) {
  contentId.substring(0, 11)

 

The changes I have made here:

* Use the available binding variable for 'hookMessage' (just saves defining your own message as a StringBuilder).

* Wrap the hookMessage with 'wrapHookResponse' for prettier output.

* If there are any changes that need to be blocked, then write the message to the 'hookResponse.out()' method and return false.

 

To implement the above script:

Bitbucket -> Administration -> Pre Hooks -> Add New Item -> Custom Script Hook ... 

 

 

Hope this helps.

Regards

Mark

 

PS. I see the 'Static type checking' quite often and in this case I believe it is a bit of a red-herring and can be ignored .. as annoying as it is .. :) 

devops_account January 9, 2020

Hi, Mark. Thank you for the response. I find another workaround in my case. But still thank you for the effort to reply and hope this answer can help others with the same issue.

Best,

Jason

0 votes
Levente Szabo _Midori_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
November 15, 2019

As an alternative, you can also give Better Commit Policy for Bitbucket a try! It also allows you to install local hooks, so you can be more efficient by verifing commits right on the developers' computer at commit time. 

(Please note that the app relies on Better Commit Policy for Jira! I'm member of the team behind this app.) 

0 votes
devops_account October 3, 2019

Any suggestions will be helpful. Thanks

dineshav
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 7, 2019

Hi,

  You just need to remove the repository from pathsMatch, that should help

 

refChanges.pathsMatch("glob:id_rsa", matchingChangesCollector)

 

Thanks,

Dinesh

devops_account October 7, 2019

hi, Dinesh.

I've tried the above method. But the error changed to:


[Static type checking] - Cannot find matching method java.util.Collection#pathsMatch(java.lang.String, groovy.lang.Closure ). Please check if the declared type is correct and if the method exists. Possible solutions: pathsMatch(com.atlassian.bitbucket.repository.Repository, java.lang.String), pathsMatch(com.atlassian.bitbucket.repository.Repository, java.lang.String, groovy.lang.Closure) @ line 23, column 5.

The possible solutions suggest having the repository as one of the parameters.

Thanks,

Jason

Egor Kovetskiy October 7, 2019

Hi, it looks like you could also use External Hooks Add-on for your purposes (I'm co-maintainer of the add-on), give it a try and let me know if it is useful for you, I can issue 50% discount for you as part of community support.

Like devops_account likes this
Sanpo March 24, 2024


Hi @Mark Ryan @Egor Kovetskiy 

 

Is it possible to have custom script to validate syntax of yaml and json code in bitbucket datacenter

 

Whenever the user having minor modifications they just modifying the code and commit

While  modifying they are missing some braces colons and  commiting leading to pipeline failure.

 

Is there any way to insert syntax validation.for bitbucket UI commit .

I also explored atlassian marketplace apps such as 

External hook 

Yaml syntax validator

Json viewers and validator

but result is these plugins/applications no longer available for bitbucket datacenter or incompitable having bugs

 

so trying to have custom pre- commit script for syntax  validation

is it possible ? If any suggestions that would be helpful 

 

Thank you : 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events