Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Script runner pre commit hook reading change content

Edited
Mark I'm New Here Dec 08, 2021

Hi, I am writing a script runner pre commit hook and need to validate that the file being committed contains certain string. and reject the push if it does not. 

I know how to stream a file using ContentService if the file has already been committed into the repository but it doesn’t seem to work with newly added files. 

When I add new file to but bucket I don’t see how to read the content of newly added file. 

// When iterating through changes as below, I get to the change where new file that needs to be validated got added 


def collector = { Iterable<Change> changes -> changes.each { change -> validate(change.path.toString()) } }

pathsMatchExcludingDeletes(“glob:**.txt”, collector)

// and them validate in that file 

def validate(path) {


   contentService.streamFile(repo, contentId, path, supplier) // this will throw “The path xxx does not exist at revision yyy

}


If I attempt to read the file using path provided by the change.path it will throw the exception “The path xxx does not exist at revision yyy” 

I assume this is because file is only getting added and does not yet exist in the repository at the time pre hook runs.

How can I read content of the change for the newly added files?

1 answer

I think, as I have tested previously, ContentService only read the current content of the file, instead of the new changes being pushed.

So, you can use CompareService for checking the changes (diff's). Following snippet will also work for newly added file:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.compare.CompareService
import com.atlassian.bitbucket.compare.CompareDiffRequest
import com.atlassian.bitbucket.compare.CompareRef
import com.atlassian.bitbucket.content.AbstractDiffContentCallback
import com.atlassian.bitbucket.content.DiffSegmentType
import com.atlassian.bitbucket.content.ConflictMarker

def compareService = ComponentLocator.getComponent(CompareService)

refChanges.each { refChange ->
    compareService.streamDiff(
        new CompareDiffRequest.Builder()
            .fromRef(new CompareRef(refChange.toHash, repository))
            .toRef(new CompareRef(refChange.fromHash, repository))
            .build(),
        new AbstractDiffContentCallback() {
            boolean isLineChanged = false
            void onSegmentStart(DiffSegmentType type) {
                isLineChanged = type == DiffSegmentType.ADDED
            }
            void onSegmentLine(String line, ConflictMarker marker, boolean truncated) {
                if (isLineChanged) {
//check the contents of the line log.warn "changed or new line is: " + line } } } ) }

This snippet will also soon be available in our library, be sure to check out for more useful example there 😀.

Hi @Mark ,

Further to @Max Lim _Adaptavist_ previous answer, we have now published a Script to our Adaptavist Library that shows an example of blocking commits based on file content.

The Script can be found here: https://library.adaptavist.com/entity/block-commits-for-files-containing-content-that-doesnt-match-regex

Kind regards,

Robert Giddings,

Product Manager,

ScriptRunner for Bitbucket

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

📣 Calling Bitbucket Data Center customers to participate in research

Hi everyone, Are you Bitbucket DC customer? If so, we'd love to talk to you! Our team wants to dive deep to understand your long-term plans regarding Bitbucket DC and Atlassian Cloud. Do you plan...

117 views 2 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you