Skip to content

Commit

Permalink
feat: initial implementation (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoriev authored Jan 16, 2024
1 parent 6a5c5e1 commit e5f0955
Show file tree
Hide file tree
Showing 84 changed files with 5,093 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ repos:
- id: check-git-config-user-email
args: [--templates, ^\S+\.\S+@sbb\.ch$]
- repo: https://github.com/commitizen-tools/commitizen
rev: 3.12.0
rev: v3.13.0
hooks:
- id: commitizen
27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Polarion ALM extension to <...>
# Polarion ALM extension to execute validation during save/delete actions

This Polarion extension provides possibility to run custom Java code (via hooks), before Polarion saves/deletes a Work Item, Document or Test Run.

This Polarion extension provides possibility to <...>
## Build

This extension can be produced using maven:
Expand All @@ -10,27 +11,19 @@ mvn clean package

## Installation to Polarion

To install the extension to Polarion `ch.sbb.polarion.extension.<extension_name>-<version>.jar`
should be copied to `<polarion_home>/polarion/extensions/ch.sbb.polarion.extension.<extension_name>/eclipse/plugins`
To install extension to Polarion `ch.sbb.polarion.extension.interceptor-<version>.jar`
should be copied to `<polarion_home>/polarion/extensions/interceptor/eclipse/plugins`
It can be done manually or automated using maven build:
```
mvn clean install -P polarion2304,install-to-local-polarion
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.

Changes only take effect after restart of Polarion.

## Polarion configuration

<...>


## Extension Configuration

<...>


## Usage
## Interceptor settings/customization
Use Administration -> Interceptor -> Settings page to enable/disable or set specific properties for each existing hook.

<...>

## Hooks installation
Copy hook jar to hooks folder (`<polarion_home>/polarion/extensions/interceptor/eclipse/plugins/hooks`) and enforce hooks reloading from the settings page or restart Polarion.
165 changes: 165 additions & 0 deletions hooks/only-assignee-can-delete/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?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.only-assignee-can-delete</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<ch.sbb.polarion.extension.interceptor.version>1.0.0-SNAPSHOT</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>

<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>2310</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.3.0</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</groupId>
<artifactId>platform</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,74 @@
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.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;
import com.polarion.core.util.logging.Logger;
import com.polarion.platform.core.PlatformContext;
import com.polarion.platform.persistence.model.IPObject;
import com.polarion.platform.persistence.model.IPObjectList;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

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

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 ITrackerService trackerService = PlatformContext.getPlatform().lookupService(ITrackerService.class);
private static final Logger logger = Logger.getLogger(OnlyAssigneeCanDeleteHook.class);

public OnlyAssigneeCanDeleteHook() {
super(ItemType.WORKITEM, ActionType.DELETE, VERSION, "Control that only assignee user can delete WI." +
" If Wi is unassigned then it can be removed or not dependent on value property " + SETTINGS_DELETE_UNASSIGNED);
}

@Override
public String processAction(@NotNull IUniqueObject object) {
boolean deleteUnassigned = DEFAULT_DELETE_UNASSIGNED;
String deleteUnassignedStringValue = getSettingsValue(SETTINGS_DELETE_UNASSIGNED);
try {
deleteUnassigned = Boolean.parseBoolean(deleteUnassignedStringValue);
} catch (NumberFormatException e) {
logger.error("Cannot parse boolean value '%s'".formatted(deleteUnassignedStringValue), e);
}

IWorkItem workItem = (IWorkItem) object;
String returnMessage = null;
if (workItem.getType() != null && workItem.getId() != null && isCommaSeparatedSettingsHasItem(workItem.getProjectId(), SETTINGS_PROJECTS)) {
boolean allowToDelete = false;
String currentUser = trackerService.getDataService().getSecurityService().getCurrentUser();
IPObjectList assignees = workItem.getAssignees();
if (assignees.isEmpty()) {
allowToDelete = deleteUnassigned;
} else {
for (Iterator<IPObject> i = assignees.iterator(); i.hasNext() && !allowToDelete; ) {
IPObject user = i.next();
allowToDelete = currentUser.equals(((IUser) user).getId());
}
}
if (!allowToDelete) {
returnMessage = getSettingsValue(SETTINGS_ERROR_MESSAGE);
}
}

return returnMessage;
}

@Override
public String getDefaultSettings() {
return PropertiesUtils.build(
SETTINGS_PROJECTS, ALL_WILDCARD,
SETTINGS_ERROR_MESSAGE, DEFAULT_ERROR_MESSAGE,
SETTINGS_DELETE_UNASSIGNED, String.valueOf(DEFAULT_DELETE_UNASSIGNED)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name: WorkItem deletion hook for Polarion ALM Interceptor plugin
Main-Class: ch.sbb.polarion.extension.interceptor_hooks.only_assignee_can_delete.OnlyAssigneeCanDeleteHook
Loading

0 comments on commit e5f0955

Please sign in to comment.