Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for Bitbucket 8.x #53

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
target
13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 51 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.christiangalsterer</groupId>
<artifactId>stash-filehooks-plugin</artifactId>
<version>3.3.2</version>
<artifactId>bitbucket-filehooks-plugin</artifactId>
<version>4.0</version>
<organization>
<name>Christian Galsterer</name>
<url>https://github.com/christiangalsterer/stash-filehooks-plugin</url>
Expand All @@ -13,6 +13,10 @@
<name>Krzysztof Malinowski</name>
<organization>Motorola Solutions, Inc.</organization>
</contributor>
<contributor>
<name>John Lawlor</name>
<organization>Openet Telecom Ltd.</organization>
</contributor>
</contributors>
<licenses>
<license>
Expand All @@ -25,7 +29,7 @@
<packaging>atlassian-plugin</packaging>

<properties>
<bitbucket.version>5.4.1</bitbucket.version>
<bitbucket.version>6.2.0</bitbucket.version>
<bitbucket.data.version>${bitbucket.version}</bitbucket.data.version>
<amps.version>6.3.0</amps.version>
<plugin.testrunner.version>1.2.3</plugin.testrunner.version>
Expand Down Expand Up @@ -61,6 +65,30 @@
<artifactId>bitbucket-spi</artifactId>
<version>${bitbucket.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.bitbucket.server</groupId>
<artifactId>bitbucket-scm-common</artifactId>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's a tiny mis-indentation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

<version>${bitbucket.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.bitbucket.server</groupId>
<artifactId>bitbucket-util</artifactId>
<version>${bitbucket.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.bitbucket.server</groupId>
<artifactId>bitbucket-page-objects</artifactId>
<version>${bitbucket.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.sal</groupId>
<artifactId>sal-api</artifactId>
<version>2.10.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
Expand All @@ -71,6 +99,11 @@
<artifactId>sal-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.bitbucket.server</groupId>
<artifactId>bitbucket-util</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.bitbucket.server</groupId>
<artifactId>bitbucket-api</artifactId>
Expand Down Expand Up @@ -113,6 +146,21 @@
<version>${commons-lang.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.atlassian.utils</groupId>
<artifactId>atlassian-processutils</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>r05</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void complete() {
@Override
protected void processReader(LineReader reader) throws IOException {
String line;
while ((line = resetWatchdogAndReadLine(reader)) != null) {
while ((line = reader.readLine()) != null) {
String[] split = line.split(" ");
// Only process blobs (ie files), ignore folders
if (split.length == 3 && split[1].equals("blob")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@
import com.atlassian.bitbucket.content.ChangeType;
import com.atlassian.bitbucket.content.ChangesRequest;
import com.atlassian.bitbucket.hook.HookResponse;
import com.atlassian.bitbucket.hook.repository.PreReceiveRepositoryHook;
import com.atlassian.bitbucket.hook.repository.RepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.RepositoryMergeRequestCheck;
import com.atlassian.bitbucket.hook.repository.RepositoryMergeRequestCheckContext;
import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.pull.PullRequest;
import com.atlassian.bitbucket.pull.PullRequestRef;
import com.atlassian.bitbucket.repository.RefChange;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.git.GitScmConfig;
import com.atlassian.bitbucket.scm.git.command.GitCommandBuilderFactory;
import com.atlassian.bitbucket.scm.pull.MergeRequest;
import com.atlassian.bitbucket.setting.RepositorySettingsValidator;
import com.atlassian.bitbucket.setting.Settings;
import com.atlassian.bitbucket.setting.SettingsValidationErrors;
import com.google.common.base.Strings;
import com.google.common.collect.*;

import javax.annotation.Nonnull;

Expand All @@ -43,6 +37,8 @@
/**
* Checks the name and path of a file in the pre-receive phase and rejects the push when the changeset contains files which match the configured file name pattern.
*/

/**
public class FileNameHook implements PreReceiveRepositoryHook, RepositorySettingsValidator, RepositoryMergeRequestCheck {

private static final String SETTINGS_INCLUDE_PATTERN = "pattern";
Expand All @@ -51,14 +47,14 @@ public class FileNameHook implements PreReceiveRepositoryHook, RepositorySetting

private final ChangesetService changesetService;
private final I18nService i18n;
private final CommitService commitService;
private final CommitService commitService;
private final MergeBaseResolver mergeBaseResolver;

public FileNameHook(GitCommandBuilderFactory builderFactory, CommitService commitService, ChangesetService changesetService, I18nService i18n, GitScmConfig gitScmConfig) {
this.changesetService = changesetService;
this.i18n = i18n;
this.commitService = commitService;
this.mergeBaseResolver = new MergeBaseResolver(builderFactory, gitScmConfig, commitService);
this.mergeBaseResolver = new MergeBaseResolver(builderFactory, gitScmConfig, commitService);
}

@Override
Expand Down Expand Up @@ -136,28 +132,24 @@ public void validate(Settings settings, SettingsValidationErrors errors, Reposit
}
}
}

/**
* Callback, collecting all the paths, changed in the requested change
* range.
*/

private static class ChangedPathsCollector extends AbstractChangeCallback {
private final Collection<String> changedPaths = new HashSet<>();
@Override

@Override
public boolean onChange(Change change) throws IOException {
if (change.getType() != ChangeType.DELETE) {
if (change.getType() != ChangeType.DELETE) {
changedPaths.add(change.getPath().toString());
}
return true;
}
}
return true;
}

Collection<String> getChangedPaths() {
return changedPaths;
}
}
return changedPaths;
}

}

private String getPullRequestError(Collection<String> filteredFiles) {
final StringBuilder sb = new StringBuilder();
final Iterator<String> iter = filteredFiles.iterator();
Expand All @@ -169,38 +161,40 @@ private String getPullRequestError(Collection<String> filteredFiles) {
}
return sb.toString();
}
@Override
public void check(RepositoryMergeRequestCheckContext context) {
final MergeRequest request = context.getMergeRequest();
final PullRequest pr = request.getPullRequest();
final Commit prFrom = getChangeSet(pr.getFromRef());
final Commit prTo = getChangeSet(pr.getToRef());
final Commit base = mergeBaseResolver.findMergeBase(prFrom, prTo);
final FileNameHookSetting setting = getSettings(context.getSettings());
final ChangesRequest.Builder builder = new ChangesRequest.Builder(prFrom.getRepository(), prFrom.getId());
if (base.getId() != null) {
builder.sinceId(base.getId());
}
final ChangesRequest pathsRequest = builder.build();

@Override
public void check(RepositoryMergeRequestCheckContext context) {
final MergeRequest request = context.getMergeRequest();
final PullRequest pr = request.getPullRequest();
final Commit prFrom = getChangeSet(pr.getFromRef());
final Commit prTo = getChangeSet(pr.getToRef());
final Commit base = mergeBaseResolver.findMergeBase(prFrom, prTo);
final FileNameHookSetting setting = getSettings(context.getSettings());

final ChangesRequest.Builder builder = new ChangesRequest.Builder(prFrom.getRepository(), prFrom.getId());
if (base.getId() != null) {
builder.sinceId(base.getId());
}
final ChangesRequest pathsRequest = builder.build();
final ChangedPathsCollector pathsCallback = new ChangedPathsCollector();
commitService.streamChanges(pathsRequest, pathsCallback);
Collection<String> filteredFiles = pathsCallback.getChangedPaths();
Collection<String> filteredFiles = pathsCallback.getChangedPaths();
filteredFiles = filteredFiles.stream().filter(setting.getIncludePattern().asPredicate()).collect(Collectors.toList());
if(setting.getExcludePattern().isPresent()) {
Pattern excludePattern = setting.getExcludePattern().get();
filteredFiles = filteredFiles.stream().filter(excludePattern.asPredicate().negate()).collect(Collectors.toList());
}
if (filteredFiles.size() > 0) {
request.veto(i18n.getText("filename-hook.mergecheck.veto", "File Name Hook: The following files violate the file name pattern [{0}]:", setting.getIncludePattern().pattern()), getPullRequestError(filteredFiles));
}
}

if(setting.getExcludePattern().isPresent()) {
Pattern excludePattern = setting.getExcludePattern().get();
filteredFiles = filteredFiles.stream().filter(excludePattern.asPredicate().negate()).collect(Collectors.toList());
}

if (filteredFiles.size() > 0) {
request.veto(i18n.getText("filename-hook.mergecheck.veto", "File Name Hook: The following files violate the file name pattern [{0}]:", setting.getIncludePattern().pattern()), getPullRequestError(filteredFiles));
}
}

private Commit getChangeSet(PullRequestRef prRef) {
final CommitRequest.Builder builder = new CommitRequest.Builder(prRef.getRepository(), prRef.getLatestCommit());
return commitService.getCommit(builder.build());
}
}

*/
Loading