-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithdrawApprovalsExceptForSquash.groovy
More file actions
72 lines (57 loc) · 2.27 KB
/
WithdrawApprovalsExceptForSquash.groovy
File metadata and controls
72 lines (57 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import com.atlassian.bitbucket.commit.CommitService
import com.atlassian.bitbucket.content.AbstractChangeCallback
import com.atlassian.bitbucket.content.Change
import com.atlassian.bitbucket.content.ChangesRequest
import com.atlassian.bitbucket.content.ContentService
import com.atlassian.bitbucket.repository.Repository
import com.atlassian.sal.api.component.ComponentLocator
if (!isSquashCommit()) {
return true
}
String nextCommitId = event.addedCommits.commits.first().id
String prevCommitId = event.previousFromHash
Repository repository = event.pullRequest.commits.first().repository
List<String> paths = collectPaths(repository, nextCommitId)
FileLoader fileLoader = new FileLoader(repository: repository)
return paths.any { path ->
Properties prevFile = fileLoader.load(path, prevCommitId)
Properties nextFile = fileLoader.load(path, nextCommitId)
return prevFile.any {
if (nextFile.get(it.key) != it.value) {
return true
}
false
}
}
// utils
boolean isSquashCommit() {
event.addedCommits.commits.size() == 1 && event.removedCommits.commits.size() > 0
}
class FileLoader {
Repository repository
ContentService contentService = ComponentLocator.getComponent(ContentService)
Properties load(String path, String commitId) {
def outputStream = new ByteArrayOutputStream()
contentService.streamFile(repository, commitId, path, { outputStream })
def properties = new Properties()
return properties.load(new ByteArrayInputStream(outputStream.toByteArray()))
}
}
List<String> collectPaths(Repository repository, String nextCommitId) {
ChangesRequest changesRequest = new ChangesRequest.Builder(repository, nextCommitId).build()
def commitService = ComponentLocator.getComponent(CommitService)
ChangedPathsCollector changedPathsCollector = new ChangedPathsCollector()
commitService.streamChanges(changesRequest, changedPathsCollector)
return changedPathsCollector.getChangedPaths()
}
class ChangedPathsCollector extends AbstractChangeCallback {
List<String> changedPaths = []
@Override
boolean onChange(Change change) {
changedPaths.add(change.getPath().toString());
return true
}
List<String> getChangedPaths() {
return changedPaths
}
}