This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e5b6db
commit b84e8c2
Showing
12 changed files
with
546 additions
and
45 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,20 @@ | ||
package se.bjurr.prnfs.admin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
@XmlRootElement | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public final class AdminFormValues extends ArrayList<Map<String, String>> { | ||
public static final String NAME = "name"; | ||
private static final long serialVersionUID = 9084184120202816120L; | ||
public static final String VALUE = "value"; | ||
|
||
@VisibleForTesting | ||
public void setSetting(String name, String value) { | ||
Map<String, String> map = new HashMap<String, String>(); | ||
map.put(NAME, name); | ||
map.put(VALUE, value); | ||
add(map); | ||
public enum FIELDS { | ||
user, password, events, FORM_IDENTIFIER, url | ||
} | ||
} |
30 changes: 26 additions & 4 deletions
30
src/main/java/se/bjurr/prnfs/listener/PrnfsPullRequestEventListener.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
package se.bjurr.prnfs.listener; | ||
|
||
import static se.bjurr.prnfs.settings.SettingsStorage.getPrnfsSettings; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import se.bjurr.prnfs.settings.PrnfsNotification; | ||
import se.bjurr.prnfs.settings.PrnfsSettings; | ||
import se.bjurr.prnfs.settings.ValidationException; | ||
|
||
import com.atlassian.event.api.EventListener; | ||
import com.atlassian.sal.api.ApplicationProperties; | ||
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; | ||
import com.atlassian.stash.event.pull.PullRequestEvent; | ||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
public class PrnfsPullRequestEventListener { | ||
|
||
private UrlInvoker urlInvoker = new UrlInvoker(); | ||
private final PluginSettingsFactory pluginSettingsFactory; | ||
private static final Logger logger = LoggerFactory.getLogger(PrnfsPullRequestEventListener.class); | ||
|
||
public PrnfsPullRequestEventListener(ApplicationProperties applicationProperties) { | ||
public PrnfsPullRequestEventListener(PluginSettingsFactory pluginSettingsFactory) { | ||
this.pluginSettingsFactory = pluginSettingsFactory; | ||
} | ||
|
||
@VisibleForTesting | ||
public void setUrlInvoker(UrlInvoker urlInvoker) { | ||
this.urlInvoker = urlInvoker; | ||
} | ||
|
||
@EventListener | ||
public void anEvent(PullRequestEvent o) { | ||
logger.info(o.getAction().name()); | ||
logger.info(o.getPullRequest().getTitle()); | ||
try { | ||
final PrnfsSettings settings = getPrnfsSettings(pluginSettingsFactory.createGlobalSettings()); | ||
for (final PrnfsNotification n : settings.getNotifications()) { | ||
urlInvoker.ivoke(new PrnfsRenderer(o).render(n.getUrl()), n.getUser(), n.getPassword()); | ||
} | ||
} catch (final ValidationException e) { | ||
logger.error("", e); | ||
} | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/se/bjurr/prnfs/listener/PrnfsRenderer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package se.bjurr.prnfs.listener; | ||
|
||
import com.atlassian.stash.event.pull.PullRequestEvent; | ||
|
||
public class PrnfsRenderer { | ||
|
||
public interface Resolver { | ||
public String resolve(PullRequestEvent pullRequestEvent); | ||
} | ||
|
||
public enum PrnfsVariable { | ||
PULL_REQUEST_ID(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getId() + ""; | ||
} | ||
}), PULL_REQUEST_FROM_HASH(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getFromRef().getLatestCommit(); | ||
} | ||
}), PULL_REQUEST_FROM_ID(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getFromRef().getId(); | ||
} | ||
}), PULL_REQUEST_FROM_REPO_PROJECT_ID(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getId() + ""; | ||
} | ||
}), PULL_REQUEST_TO_REPO_PROJECT_ID(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getId() + ""; | ||
} | ||
}), PULL_REQUEST_FROM_REPO_ID(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getId() + ""; | ||
} | ||
}), PULL_REQUEST_TO_REPO_ID(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getToRef().getRepository().getId() + ""; | ||
} | ||
}), PULL_REQUEST_FROM_REPO_NAME(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getName() + ""; | ||
} | ||
}), PULL_REQUEST_TO_REPO_NAME(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getToRef().getRepository().getName() + ""; | ||
} | ||
}), PULL_REQUEST_FROM_REPO_SLUG(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getSlug() + ""; | ||
} | ||
}), PULL_REQUEST_TO_REPO_SLUG(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getToRef().getRepository().getSlug() + ""; | ||
} | ||
}), PULL_REQUEST_FROM_REPO_PROJECT_KEY(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getFromRef().getRepository().getProject().getKey(); | ||
} | ||
}), PULL_REQUEST_TO_REPO_PROJECT_KEY(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getToRef().getRepository().getProject().getKey(); | ||
} | ||
}), PULL_REQUEST_TO_ID(new Resolver() { | ||
@Override | ||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return pullRequestEvent.getPullRequest().getToRef().getId(); | ||
} | ||
}); | ||
|
||
private Resolver resolver; | ||
|
||
private PrnfsVariable(Resolver resolver) { | ||
this.resolver = resolver; | ||
} | ||
|
||
public String resolve(PullRequestEvent pullRequestEvent) { | ||
return resolver.resolve(pullRequestEvent); | ||
} | ||
} | ||
|
||
private final PullRequestEvent pullRequestEvent; | ||
|
||
public PrnfsRenderer(PullRequestEvent pullRequestEvent) { | ||
this.pullRequestEvent = pullRequestEvent; | ||
} | ||
|
||
public String render(String string) { | ||
for (final PrnfsVariable variable : PrnfsVariable.values()) { | ||
final String regExpStr = "\\$\\{" + variable.name() + "\\}"; | ||
if (string.contains(regExpStr.replaceAll("\\\\", ""))) { | ||
string = string.replaceAll(regExpStr, variable.resolve(pullRequestEvent)); | ||
} | ||
} | ||
return string; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package se.bjurr.prnfs.listener; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.base.Optional; | ||
|
||
public class UrlInvoker { | ||
private static final Logger logger = LoggerFactory.getLogger(UrlInvoker.class); | ||
|
||
public void ivoke(String url, Optional<String> user, Optional<String> password) { | ||
logger.info("Url: \"" + url + "\" user: \"" + user.or("") + "\" password: \"" + password.or("") + "\""); | ||
} | ||
} |
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
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
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
Oops, something went wrong.