Snippets

Adaptavist Files not under Git LFS hook condition

Created by Adam Markham

File FilesNotUnderGitLfsHookCondition.groovy Added

  • Ignore whitespace
  • Hide word diff
+import com.atlassian.bitbucket.content.AbstractChangeCallback
+import com.atlassian.bitbucket.content.Change
+import com.atlassian.bitbucket.content.ChangeType
+import com.atlassian.bitbucket.content.ChangesRequest
+import com.atlassian.bitbucket.repository.RefChange
+import com.atlassian.bitbucket.repository.Repository
+import com.atlassian.bitbucket.scm.CommandOutputHandler
+import com.atlassian.utils.process.StringOutputHandler
+import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScript
+import groovy.transform.BaseScript
+
+import javax.annotation.Nonnull
+
+@BaseScript BitbucketBaseScript bitbucketBaseScript
+
+Collection<RefChange> refChanges = refChanges
+Repository repository = repository
+
+def commits = refChanges.getCommits(repository)
+
+commits.any { commit ->
+    def underLfs = false
+    commitService.streamChanges(new ChangesRequest.Builder(repository, commit.id).build(), new AbstractChangeCallback() {
+
+        @Override
+        boolean onChange(@Nonnull Change change) throws IOException {
+
+            // ignore deletes
+            if (change.type == ChangeType.DELETE) {
+                return true
+            }
+
+            def path = change.path.toString()
+
+            def content = gitCommandBuilderFactory.builder(repository).command("cat-file")
+                .argument("-p")
+                .argument(change.contentId)
+                .build(new StringCommandOutputHandler())
+                .call()
+
+            if (!content) {
+                underLfs = true
+            } else {
+                underLfs = content.split("\n").any {
+                    it.contains("version https://git-lfs.github.com")
+                }
+            }
+
+            if (! underLfs) {
+                log.debug "$path not under LFS!"
+            }
+
+            underLfs
+        }
+    })
+
+    ! underLfs
+}
+
+class StringCommandOutputHandler extends StringOutputHandler implements CommandOutputHandler<String> {
+
+    StringCommandOutputHandler() {
+        super()
+    }
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.