-
-
Save BenBergman/57c2caefaf670193d0a7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.example.plugin.myhook; | |
import com.atlassian.stash.hook.repository.*; | |
import com.atlassian.stash.repository.*; | |
import com.atlassian.stash.setting.*; | |
import com.atlassian.stash.server.*; | |
import com.atlassian.stash.history.*; | |
import com.atlassian.stash.content.*; | |
import com.atlassian.stash.content.ChangesetsBetweenRequest; | |
//import com.atlassian.stash.content.ChangesetsBetweenRequest.Builder; | |
import com.atlassian.stash.util.*; | |
import java.net.URL; | |
import java.util.Collection; | |
import com.atlassian.stash.repository.RepositoryMetadataService; | |
/** | |
* Note that hooks can implement RepositorySettingsValidator directly. | |
*/ | |
public class MyRepositoryHook implements AsyncPostReceiveRepositoryHook, RepositorySettingsValidator { | |
private final RepositoryMetadataService repositoryMetadataService; | |
private final ApplicationPropertiesService applicationPropertiesService; | |
private final HistoryService historyService; | |
public MyRepositoryHook( | |
RepositoryMetadataService repositoryMetadataService, | |
ApplicationPropertiesService applicationPropertiesService, | |
HistoryService historyService | |
) { | |
this.repositoryMetadataService = repositoryMetadataService; | |
this.applicationPropertiesService = applicationPropertiesService; | |
this.historyService = historyService; | |
} | |
private boolean refWasUpdated(Ref ref, Collection<RefChange> refChanges) { | |
for (RefChange refChange : refChanges) { | |
if (refChange.getRefId().equals(ref.getId())) { | |
return true; | |
} | |
} | |
return false; | |
} | |
/** | |
* Connects to a configured URL to notify of all changes. | |
*/ | |
@Override | |
public void postReceive(RepositoryHookContext context, Collection<RefChange> refChanges) { | |
String refId = context.getSettings().getString("ref-id"); | |
Ref found = repositoryMetadataService.resolveRef(context.getRepository(), refId); | |
if (!refWasUpdated(found, refChanges)) | |
return; | |
Repository repository = context.getRepository(); | |
System.out.println("\n\n\nYou are here\n\n\n"); | |
System.out.println("Working Directory = " + System.getProperty("user.dir")); | |
System.out.println("repository " + repository); | |
System.out.println("repository dir " + applicationPropertiesService.getRepositoryDir(repository)); | |
ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder(repository) | |
.exclude("0a943a2") | |
.include("master") | |
.build(); | |
// Page<Changeset> page = historyService.getChangesetBetween(request, PageUtils.newRequest(0, 100)); | |
//// Page<Changeset> page = historyService.getChangesetBetween(repository, "0a943a2", "master", PageUtils.newRequest(0, 100)); | |
// system.out.println("repository changes since 0a943a2 " + page); | |
System.out.println("\n\n\nYou are here\n\n\n"); | |
String url = context.getSettings().getString("url"); | |
if (url != null) { | |
try { | |
new URL(url).openConnection().getInputStream().close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public void validate(Settings settings, SettingsValidationErrors errors, Repository repository) { | |
if (settings.getString("url", "").isEmpty()) { | |
errors.addFieldError("url", "Url field is blank, please supply one"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment