Skip to content

Commit

Permalink
feat: Baseline support (#310)
Browse files Browse the repository at this point in the history
Refs: #305
  • Loading branch information
grigoriev authored Dec 10, 2024
1 parent 6c7ccf4 commit da6c9c0
Show file tree
Hide file tree
Showing 14 changed files with 296 additions and 153 deletions.
12 changes: 12 additions & 0 deletions docs/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,14 @@
"schema": {
"type": "string"
}
},
{
"description": "The specific revision of the provided collection",
"in": "query",
"name": "revision",
"schema": {
"type": "string"
}
}
],
"responses": {
Expand Down Expand Up @@ -1672,6 +1680,10 @@
"description": "Filter for attachments to be downloaded, example: '*.pdf'",
"type": "string"
},
"baselineRevision": {
"description": "The specific revision of the provided baseline",
"type": "string"
},
"chapters": {
"description": "Specific higher level chapters",
"items": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public class PdfExporterFormExtension implements IFormExtension {
@Override
@Nullable
public String render(@NotNull IFormExtensionContext context) {
return TransactionalExecutor.executeSafelyInReadOnlyTransaction(
transaction -> renderForm(transaction.context(), context.object().getOldApi()));
return TransactionalExecutor.executeSafelyInReadOnlyTransaction(transaction -> {
String baselineRevision = transaction.context().baselineRevision();
return polarionService.executeInBaseline(baselineRevision, transaction, () -> renderForm(transaction.context(), context.object().getOldApi()));
});
}

public String renderForm(@NotNull SharedContext context, @NotNull IPObject object) {
Expand Down Expand Up @@ -101,7 +103,7 @@ public String renderForm(@NotNull SharedContext context, @NotNull IPObject objec
form = adjustLocalizeEnums(form, selectedStylePackage, module.getCustomField(DOC_LANGUAGE_FIELD));
form = adjustLinkRoles(form, EnumValuesProvider.getAllLinkRoleNames(module.getProject()), selectedStylePackage);
form = adjustFilename(form, module);
form = adjustButtons(form, module, selectedStylePackage);
form = adjustButtons(form, module, selectedStylePackage, context.baselineRevision());

builder.html(form);
}
Expand Down Expand Up @@ -302,10 +304,10 @@ private String adjustFilename(@NotNull String form, @NotNull IModule module) {
return form.replace("{FILENAME}", filename).replace("{DATA_FILENAME}", filename);
}

private String adjustButtons(@NotNull String form, @NotNull IModule module, @NotNull StylePackageModel stylePackage) {
private String adjustButtons(@NotNull String form, @NotNull IModule module, @NotNull StylePackageModel stylePackage, @Nullable String baselineRevision) {
IProject project = module.getProject();
String moduleLocationPath = module.getModuleLocation().getLocationPath();
String params = fillParams(project.getId(), moduleLocationPath, module.getRevision());
String params = fillParams(project.getId(), moduleLocationPath, baselineRevision, module.getRevision());
form = form.replace("{LOAD_PDF_PARAMS}", params);

form = form.replace("{VALIDATE_PDF_PARAMS}", params);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ch.sbb.polarion.extension.pdf_exporter.exception;

public class BaselineExecutionException extends RuntimeException {
public BaselineExecutionException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CollectionApiController extends CollectionInternalController {
private static final PolarionService polarionService = new PolarionService();

@Override
public List<DocumentCollectionEntry> getDocumentsFromCollection(String projectId, String collectionId) {
return polarionService.callPrivileged(() -> super.getDocumentsFromCollection(projectId, collectionId));
public List<DocumentCollectionEntry> getDocumentsFromCollection(String projectId, String collectionId, String revision) {
return polarionService.callPrivileged(() -> super.getDocumentsFromCollection(projectId, collectionId, revision));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.List;

Expand All @@ -40,8 +41,11 @@ public CollectionInternalController() {
)
}
)
public List<DocumentCollectionEntry> getDocumentsFromCollection(@Parameter(description = "Project ID", required = true) @PathParam("projectId") String projectId,
@Parameter(description = "Collection ID", required = true) @PathParam("collectionId") String collectionId) {
return TransactionalExecutor.executeSafelyInReadOnlyTransaction(transaction -> pdfExporterPolarionService.getDocumentsFromCollection(projectId, collectionId, transaction));
public List<DocumentCollectionEntry> getDocumentsFromCollection(
@Parameter(description = "Project ID", required = true) @PathParam("projectId") String projectId,
@Parameter(description = "Collection ID", required = true) @PathParam("collectionId") String collectionId,
@Parameter(description = "The specific revision of the provided collection") @QueryParam("revision") String revision
) {
return TransactionalExecutor.executeSafelyInReadOnlyTransaction(transaction -> pdfExporterPolarionService.getDocumentsFromCollection(projectId, collectionId, revision, transaction));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class ExportParams {
@Schema(description = "Document path for export", example = "Specification/Product Specification")
private @Nullable String locationPath;

@Schema(description = "The specific revision of the provided baseline")
private @Nullable String baselineRevision;

@Schema(description = "The specific revision of the document")
private @Nullable String revision;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import ch.sbb.polarion.extension.generic.settings.SettingId;
import ch.sbb.polarion.extension.generic.settings.SettingName;
import ch.sbb.polarion.extension.generic.util.ScopeUtils;
import ch.sbb.polarion.extension.pdf_exporter.exception.BaselineExecutionException;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.attachments.TestRunAttachment;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.collections.DocumentCollectionEntry;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.stylepackage.StylePackageModel;
import ch.sbb.polarion.extension.pdf_exporter.rest.model.settings.stylepackage.StylePackageWeightInfo;
import ch.sbb.polarion.extension.pdf_exporter.settings.StylePackageSettings;
import ch.sbb.polarion.extension.pdf_exporter.util.WildcardUtils;
import com.polarion.alm.projects.IProjectService;
import com.polarion.alm.shared.api.model.baselinecollection.BaselineCollection;
import com.polarion.alm.shared.api.model.baselinecollection.BaselineCollectionReference;
import com.polarion.alm.shared.api.transaction.ReadOnlyTransaction;
import com.polarion.alm.tracker.ITestManagementService;
Expand All @@ -38,6 +40,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;

public class PdfExporterPolarionService extends PolarionService {

Expand Down Expand Up @@ -171,10 +174,16 @@ private boolean sameDocument(@Nullable String projectId, @NotNull String spaceId
return testRunAttachment;
}

public @NotNull List<DocumentCollectionEntry> getDocumentsFromCollection(@NotNull String projectId, @NotNull String collectionId, @NotNull ReadOnlyTransaction transaction) {
public @NotNull List<DocumentCollectionEntry> getDocumentsFromCollection(@NotNull String projectId, @NotNull String collectionId, @Nullable String revision, @NotNull ReadOnlyTransaction transaction) {
List<DocumentCollectionEntry> result = new ArrayList<>();

IBaselineCollection collection = new BaselineCollectionReference(projectId, collectionId).get(transaction).getOldApi();
BaselineCollectionReference baselineCollectionReference = new BaselineCollectionReference(projectId, collectionId);
if (revision != null) {
baselineCollectionReference = baselineCollectionReference.getWithRevision(revision);
}

BaselineCollection baselineCollection = baselineCollectionReference.get(transaction);
IBaselineCollection collection = baselineCollection.getOldApi();

List<IModule> modules = collection.getElements().stream()
.map(IBaselineCollectionElement::getObjectWithRevision)
Expand All @@ -196,4 +205,21 @@ private boolean sameDocument(@Nullable String projectId, @NotNull String spaceId

return result;
}

public <T> T executeInBaseline(@Nullable String baselineRevision, @NotNull ReadOnlyTransaction transaction, @NotNull Callable<T> callable) {
if (baselineRevision == null) {
return callCallable(callable);
} else {
return transaction.utils().executeInBaseline(baselineRevision, () -> callCallable(callable));
}
}

private static <T> T callCallable(@NotNull Callable<T> callable) {
try {
return callable.call();
} catch (Exception e) {
throw new BaselineExecutionException("Error during callable execution", e);
}
}

}
Loading

0 comments on commit da6c9c0

Please sign in to comment.