com.atlassian.bitbucket.repository.RefChange Java Examples

The following examples show how to use com.atlassian.bitbucket.repository.RefChange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChangesetService.java    From TeamcityTriggerHook with GNU Lesser General Public License v3.0 7 votes vote down vote up
public static ArrayList<String> GetChangedFiles(final CommitService scmService, final Repository repository, RefChange refChange) {
  final ArrayList<String> changedfilespath = new ArrayList<>();
 
  ChangesRequest changesRequest = new ChangesRequest.Builder(repository, refChange.getToHash()).sinceId(refChange.getFromHash()).build();
               
  scmService.streamChanges(changesRequest, new ChangeCallback() {
      @Override
      public boolean onChange(Change change) throws IOException {
        changedfilespath.add(change.getPath().toString());
        return true;
      }

      @Override
      public void onEnd(ChangeSummary cs) throws IOException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
      }

      @Override
      public void onStart(ChangeContext cc) throws IOException {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
      }
    });
  
  return changedfilespath;
}
 
Example #2
Source File: CommitBlockerHook.java    From pr-harmony with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onReceive(Repository repository, Collection<RefChange> collection, HookResponse hookResponse) {
  Config config = configDao.getConfigForRepo(repository.getProject().getKey(), repository.getSlug());

  UserProfile user = userManager.getRemoteUser();
  for(RefChange ch : collection) {
    String branch = regexUtils.formatBranchName(ch.getRef().getId());
    Set<String> excluded = newHashSet(concat(config.getExcludedUsers(), userUtils.dereferenceGroups(config.getExcludedGroups())));
    if(regexUtils.match(config.getBlockedCommits(), branch) && !excluded.contains(user.getUsername())) {
      hookResponse.err().write("\n" +
              "******************************\n" +
              "*    !! Commit Rejected !!   *\n" +
              "******************************\n\n" +
              "Direct commits are not allowed\n" +
              "to branch [" + branch + "].\n\n"
      );
      return false;
    }
  }
  return true;
}
 
Example #3
Source File: FileNameHook.java    From stash-filehooks-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onReceive(@Nonnull RepositoryHookContext context, @Nonnull Collection<RefChange> refChanges, @Nonnull HookResponse hookResponse) {
    Repository repository = context.getRepository();
    FileNameHookSetting setting = getSettings(context.getSettings());
    Optional<Pattern> branchesPattern = setting.getBranchesPattern();

    Collection<RefChange> filteredRefChanges = refChanges.stream().filter(isNotDeleteRefChange).filter(isNotTagRefChange).collect(Collectors.toList());

    if(branchesPattern.isPresent()) {
        filteredRefChanges = filteredRefChanges.stream().filter(matchesBranchPattern(branchesPattern.get())).collect(Collectors.toList());
    }

    Iterable<Change> changes = changesetService.getChanges(filteredRefChanges, repository);

    Collection<String> filteredPaths = StreamSupport.stream(changes.spliterator(), false).filter(isNotDeleteChange).map(Functions.CHANGE_TO_PATH).filter(setting.getIncludePattern().asPredicate()).collect(Collectors.toList());

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

    if (filteredPaths.size() > 0) {
        hookResponse.out().println("=================================");
        for (String path : filteredPaths) {
            String msg;
            if(branchesPattern.isPresent()) {
                msg = String.format("File [%s] violates file name pattern [%s] for branch [%s].", path, setting.getIncludePattern().pattern(), branchesPattern.get());
            } else {
                msg = String.format("File [%s] violates file name pattern [%s].", path, setting.getIncludePattern().pattern());
            }
            hookResponse.out().println(msg);
        }
        hookResponse.out().println("=================================");
        return false;
    }
    return true;
}
 
Example #4
Source File: ChangesetServiceImpl.java    From stash-filehooks-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Change> getChanges(Iterable<RefChange> refChanges, final Repository repository) {
    List<Change> changes = new ArrayList<>();

    Iterable<Commit> commits = getCommitsBetween(repository, refChanges);
    for (Iterable<Change> values : getChanges(repository, commits).values()) {
        Iterables.addAll(changes, values);
    }

    return changes;
}
 
Example #5
Source File: ChangesetServiceImpl.java    From stash-filehooks-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Set<Commit> getCommitsBetween(final Repository repository, Iterable<RefChange> refChanges) {
    Set<Commit> commits = new HashSet<>();
    CommitsCommandParameters.Builder builder = new CommitsCommandParameters.Builder().withMessages(false);

    for (RefChange refChange : refChanges) {
        switch (refChange.getType()) {
            case UPDATE:
                builder = builder.include(refChange.getToHash());
                builder = builder.exclude(refChange.getFromHash());
                break;
            case ADD:
                builder = builder.include(refChange.getToHash());
                break;
            case DELETE:
                // Deleting branch means that its commits were already in repository,
                // excluding them may reduce amount of commits to inspect if other ref changes exist.
                builder = builder.exclude(refChange.getFromHash());
                break;
        }
    }

    Set<String> existingHeads = getExistingRefs(repository).stream()
            .map(Ref::getLatestCommit)
            .collect(Collectors.toSet());

    builder = builder.exclude(existingHeads);

    CommitsCommandParameters parameters = builder.build();
    if (parameters.hasIncludes()) {
        scmService.getCommandFactory(repository).commits(parameters, commits::add).call();
    }
    return commits;
}
 
Example #6
Source File: Predicates.java    From stash-filehooks-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Predicate to check if the RefChange is matched by the @param branchesPattern
 */
static Predicate<RefChange> matchesBranchPattern (final Pattern branchesPattern) {
    return refChange -> branchesPattern.matcher(refChange.getRef().getDisplayId()).matches();
}
 
Example #7
Source File: ChangesetService.java    From stash-filehooks-plugin with Apache License 2.0 votes vote down vote up
Iterable<Change> getChanges(Iterable<RefChange> refChanges, final Repository repository); 
Example #8
Source File: ChangesetService.java    From stash-filehooks-plugin with Apache License 2.0 votes vote down vote up
Set<Commit> getCommitsBetween(final Repository repository, Iterable<RefChange> refChanges);