Skip to content

Commit

Permalink
feat!: save/delete methods proxy calls improvement, introduced separa… (
Browse files Browse the repository at this point in the history
#39)

feat!: save/delete methods proxy calls improvement, introduced separate pre&post hook methods
  • Loading branch information
Jumas authored Jun 4, 2024
1 parent ca4ca3b commit 3176de3
Show file tree
Hide file tree
Showing 23 changed files with 529 additions and 205 deletions.
31 changes: 31 additions & 0 deletions hooks/module-save-time-logger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
replay_pid*
.idea/*

# code style config
!.idea/codeStyles
.idea/codeStyles/*
!.idea/codeStyles/Project.xml
!.idea/codeStyles/codeStyleConfig.xml

target/
*.iml
19 changes: 19 additions & 0 deletions hooks/module-save-time-logger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Hook example for Polarion Interceptor extension

This hook writes to log module save time.

## Build

This hook can be produced using maven:
```
mvn clean package
```

## Installation to Polarion

To install this hook to Polarion `ch.sbb.polarion.extension.interceptor-hooks.<hookname>-<version>.jar` should be copied to `<polarion_home>/polarion/extensions/ch.sbb.polarion.extension.interceptor/eclipse/plugins/hooks`.
It can be done manually or automated using maven build:
```
mvn clean install -P install-to-local-polarion
```
For automated installation with maven env variable `POLARION_HOME` should be defined and point to folder where Polarion is installed.
159 changes: 159 additions & 0 deletions hooks/module-save-time-logger/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>

<groupId>ch.sbb.polarion.extensions.interceptor-hooks</groupId>
<artifactId>ch.sbb.polarion.extension.interceptor-hooks.module-save-time-logger</artifactId>
<version>1.1.2-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<ch.sbb.polarion.extension.interceptor.version>2.0.0</ch.sbb.polarion.extension.interceptor.version>

<maven-jar-plugin.Automatic-Module-Name>ch.sbb.polarion.extension.interceptor_hooks.module_save_time_logger</maven-jar-plugin.Automatic-Module-Name>
<maven-jar-plugin.Extension-Context>module-save-time-logger</maven-jar-plugin.Extension-Context>

<interceptor.artifactId>ch.sbb.polarion.extension.interceptor</interceptor.artifactId>
<hooks.folder.name>hooks</hooks.folder.name>

<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<polarion.version>2404</polarion.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>

<!-- Maven plugins -->
<maven-clean-plugin.version>3.3.2</maven-clean-plugin.version>
<maven-jar-plugin.version>3.4.1</maven-jar-plugin.version>
<maven-dependency-plugin.version>3.6.1</maven-dependency-plugin.version>

<!-- Others -->
<jetbrains.api.version>24.0.1</jetbrains.api.version>

<!--suppress UnresolvedMavenProperty -->
<maven-jar-plugin.Bundle-Version>${project.artifact.selectedVersion.majorVersion}.${project.artifact.selectedVersion.minorVersion}.${project.artifact.selectedVersion.incrementalVersion}</maven-jar-plugin.Bundle-Version>
</properties>

<profiles>
<profile>
<id>install-to-local-polarion</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>${maven-clean-plugin.version}</version>
<configuration>
<filesets>
<fileset>
<directory>${env.POLARION_HOME}/polarion/extensions/${interceptor.artifactId}/eclipse/plugins/${hooks.folder.name}</directory>
<includes>
<include>*${maven-jar-plugin.Extension-Context}*.jar</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>copy-to-local-polarion</id>
<phase>install</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<outputDirectory>${env.POLARION_HOME}/polarion/extensions/${interceptor.artifactId}/eclipse/plugins/${hooks.folder.name}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencies>

<dependency>
<groupId>ch.sbb.polarion.extensions</groupId>
<artifactId>ch.sbb.polarion.extension.interceptor</artifactId>
<version>${ch.sbb.polarion.extension.interceptor.version}</version>
<scope>provided</scope>
</dependency>

<!-- Polarion artifacts -->
<dependency>
<groupId>com.polarion.alm.projects</groupId>
<artifactId>projects</artifactId>
<version>${polarion.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.polarion.alm.tracker</groupId>
<artifactId>tracker</artifactId>
<version>${polarion.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.polarion.platform.persistence</groupId>
<artifactId>platform-persistence</artifactId>
<version>${polarion.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.polarion.core.util</groupId>
<artifactId>util</artifactId>
<version>${polarion.version}</version>
<scope>provided</scope>
</dependency>

<!-- Others -->
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>${jetbrains.api.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<pluginManagement>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>

</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ch.sbb.polarion.extension.interceptor_hooks.module_save_time_logger;

import ch.sbb.polarion.extension.interceptor.model.ActionHook;
import ch.sbb.polarion.extension.interceptor.model.HookExecutor;
import ch.sbb.polarion.extension.interceptor.util.PropertiesUtils;
import com.polarion.core.util.logging.Logger;
import com.polarion.platform.persistence.model.IPObject;
import org.jetbrains.annotations.NotNull;

@SuppressWarnings("unused")
public class ModuleSaveTimeHook extends ActionHook implements HookExecutor {

public static final String SETTINGS_LOG_MESSAGE = "logMessage";
public static final String SETTINGS_TIME = "time";
public static final String TIME_VARIABLE = "{%s}".formatted(SETTINGS_TIME);
public static final String DEFAULT_LOG_MESSAGE = "Module saved in " + TIME_VARIABLE + "ms";
public static final String VERSION = "1.0.0";
public static final Logger logger = Logger.getLogger(ModuleSaveTimeHook.class);

public ModuleSaveTimeHook() {
super(ItemType.MODULE, ActionType.SAVE, VERSION, "Logs module save time");
}

@Override
public @NotNull HookExecutor getExecutor() {
return new SaveTimeLoggerExecutor();
}

@Override
public String getDefaultSettings() {
return PropertiesUtils.build(
SETTINGS_LOG_MESSAGE, DEFAULT_LOG_MESSAGE
);
}

public class SaveTimeLoggerExecutor implements HookExecutor {

private long startTime;

@Override
public String preAction(@NotNull IPObject polarionObject) {
startTime = System.currentTimeMillis();
return null;
}

@Override
public void postAction(@NotNull IPObject polarionObject) {
logger.info(getSettingsValue(SETTINGS_LOG_MESSAGE)
.replace(TIME_VARIABLE, String.valueOf(System.currentTimeMillis() - startTime)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name: Title length hook for Polarion ALM Interceptor plugin
Main-Class: ch.sbb.polarion.extension.interceptor_hooks.module_save_time_logger.ModuleSaveTimeHook
2 changes: 1 addition & 1 deletion hooks/only-assignee-can-delete/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<packaging>jar</packaging>

<properties>
<ch.sbb.polarion.extension.interceptor.version>1.1.1</ch.sbb.polarion.extension.interceptor.version>
<ch.sbb.polarion.extension.interceptor.version>2.0.0</ch.sbb.polarion.extension.interceptor.version>

<maven-jar-plugin.Automatic-Module-Name>ch.sbb.polarion.extension.interceptor_hooks.only_assignee_can_delete</maven-jar-plugin.Automatic-Module-Name>
<maven-jar-plugin.Extension-Context>only-assignee-can-delete</maven-jar-plugin.Extension-Context>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ch.sbb.polarion.extension.interceptor_hooks.only_assignee_can_delete;

import ch.sbb.polarion.extension.interceptor.model.ActionHook;
import ch.sbb.polarion.extension.interceptor.model.HookExecutor;
import ch.sbb.polarion.extension.interceptor.util.PropertiesUtils;
import com.polarion.alm.projects.model.IUniqueObject;
import com.polarion.alm.projects.model.IUser;
import com.polarion.alm.tracker.ITrackerService;
import com.polarion.alm.tracker.model.IWorkItem;
Expand All @@ -15,14 +15,14 @@
import java.util.Iterator;

@SuppressWarnings({"unused", "unchecked", "rawtypes"})
public class OnlyAssigneeCanDeleteHook extends ActionHook {
public class OnlyAssigneeCanDeleteHook extends ActionHook implements HookExecutor {

private static final String SETTINGS_PROJECTS = "projects";
private static final String SETTINGS_ERROR_MESSAGE = "errorMessage";
private static final String SETTINGS_DELETE_UNASSIGNED = "deleteUnassigned";
private static final boolean DEFAULT_DELETE_UNASSIGNED = true;
private static final String DEFAULT_ERROR_MESSAGE = "Only assignee user can delete WI!";
private static final String VERSION = "1.0.0";
private static final String VERSION = "2.0.0";
private static final ITrackerService trackerService = PlatformContext.getPlatform().lookupService(ITrackerService.class);
private static final Logger logger = Logger.getLogger(OnlyAssigneeCanDeleteHook.class);

Expand All @@ -32,7 +32,7 @@ public OnlyAssigneeCanDeleteHook() {
}

@Override
public String processAction(@NotNull IUniqueObject object) {
public String preAction(@NotNull IPObject object) {
boolean deleteUnassigned = DEFAULT_DELETE_UNASSIGNED;
String deleteUnassignedStringValue = getSettingsValue(SETTINGS_DELETE_UNASSIGNED);
try {
Expand Down Expand Up @@ -63,6 +63,11 @@ public String processAction(@NotNull IUniqueObject object) {
return returnMessage;
}

@Override
public @NotNull HookExecutor getExecutor() {
return this; //there is no need to create a separate executor instance coz only 'pre' action used
}

@Override
public String getDefaultSettings() {
return PropertiesUtils.build(
Expand Down
2 changes: 1 addition & 1 deletion hooks/plan-save/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<packaging>jar</packaging>

<properties>
<ch.sbb.polarion.extension.interceptor.version>1.1.1</ch.sbb.polarion.extension.interceptor.version>
<ch.sbb.polarion.extension.interceptor.version>2.0.0</ch.sbb.polarion.extension.interceptor.version>

<maven-jar-plugin.Automatic-Module-Name>ch.sbb.polarion.extension.interceptor_hooks.plan_save</maven-jar-plugin.Automatic-Module-Name>
<maven-jar-plugin.Extension-Context>plan-save</maven-jar-plugin.Extension-Context>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package ch.sbb.polarion.extension.interceptor_hooks.plan_save;

import ch.sbb.polarion.extension.interceptor.model.ActionHook;
import ch.sbb.polarion.extension.interceptor.model.HookExecutor;
import ch.sbb.polarion.extension.interceptor.util.PropertiesUtils;
import com.polarion.alm.projects.model.IUniqueObject;
import com.polarion.alm.tracker.model.IPlan;
import com.polarion.alm.tracker.model.IWorkItem;
import com.polarion.core.util.logging.Logger;
import com.polarion.platform.persistence.model.IPObject;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedHashSet;

@SuppressWarnings("unused")
public class PlanSaveHook extends ActionHook {
public class PlanSaveHook extends ActionHook implements HookExecutor {

private static final String SETTINGS_PROJECTS = "projects";
private static final String SETTINGS_TEMPLATES = "templates";
private static final String VERSION = "1.0.0";
private static final String VERSION = "2.0.0";
private static final Logger logger = Logger.getLogger(PlanSaveHook.class);

public PlanSaveHook() {
super(ItemType.PLAN, ActionType.SAVE, VERSION, "Hook intercepting Plan save operation for adding WIs to parent plan (propagating)");
}

@Override
public String processAction(@NotNull IUniqueObject object) {
public void postAction(@NotNull IPObject object) {
IPlan plan = (IPlan) object;

logger.debug("Processing Plan: " + plan.getId());
Expand All @@ -50,8 +51,11 @@ public String processAction(@NotNull IUniqueObject object) {
} else {
logger.debug("Unsupported project: " + plan.getProjectId() + " Supporting: " + getSettingsValue(SETTINGS_PROJECTS));
}
}

return null;
@Override
public @NotNull HookExecutor getExecutor() {
return this; //there is no need to create a separate executor instance coz only 'post' action used
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion hooks/single-assignee/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<packaging>jar</packaging>

<properties>
<ch.sbb.polarion.extension.interceptor.version>1.1.1</ch.sbb.polarion.extension.interceptor.version>
<ch.sbb.polarion.extension.interceptor.version>2.0.0</ch.sbb.polarion.extension.interceptor.version>

<maven-jar-plugin.Automatic-Module-Name>ch.sbb.polarion.extension.interceptor_hooks.single_assignee</maven-jar-plugin.Automatic-Module-Name>
<maven-jar-plugin.Extension-Context>single-assignee</maven-jar-plugin.Extension-Context>
Expand Down
Loading

0 comments on commit 3176de3

Please sign in to comment.