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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Max, Robert - thank you for your answers.
I have used Compare service as suggested but it all turns out quite tricky to use.
To meet the requirement of my pre commit hook I need to be able to take a file that is being pushed (added or edited) and then validate every line of this file as it would be seen in the repository if the push was successful.
The problem with using CompareService and looking at changes instead of the whole file is that I don't actually get every line in the streamed diff. Lines that haven't changed are not included. Changes include added and deleted lines, deleted files also need special handling etc.. It's doable but makes it complicated to use for my need.
After struggling with CompareService I came back to this post and saw response from Robert, this looks promising but I have a question
When you do this:
contentService.streamFile(repository, filePaths[filePath], filePath) { fileStream }
fileStream.toString()
Isn't it the case that contentService.streamFile will only look at the state of the file as it is currently in the repository, hence ignoring all changes brought by the commits being pushed? Which is my main problem and what Max also mentioned at the beginning of his response.
Thanks in advance for looking into this, I find ScriptRunner very powerful, apart from some beginner's issues like this.
Regards,
Mark
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Mark,
The library script that uses ContentService does work.
When I tested it previously, I passed:
contentService.streamFile(repository, branchRefId, filePath)
So, it only retrieve the current content.
With the new library script, filePaths[filePath] returns the commit id. So, it will retrieve the pushed content.
Sorry for the mixed up earlier.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.