Mark property as released version mark-property-as-released-version is a new type of CustomAction triggered by a project event that allows to mark a JIRA Project version as released. mark-property-as-released-version can be used to mark a version as released in the JIRA project when a Tag is created in the Gerrit project. Change-Id: I3567e4200a162d2c6a47616fa93010eae440a480
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java index cf14882..e76e66c 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraClient.java
@@ -26,14 +26,17 @@ import com.googlesource.gerrit.plugins.its.jira.restapi.JiraComment; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraIssue; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraIssueUpdate; +import com.googlesource.gerrit.plugins.its.jira.restapi.JiraPageRequest; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraProject; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraRestApi; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraRestApiProvider; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraServerInfo; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraTransition; import com.googlesource.gerrit.plugins.its.jira.restapi.JiraVersion; +import com.googlesource.gerrit.plugins.its.jira.restapi.JiraVersionsPage; import java.io.IOException; import java.util.Arrays; +import java.util.Date; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -111,6 +114,46 @@ log.debug("Version {} created on project {}", version, projectKey); } + public void markVersionAsReleased(JiraItsServerInfo server, String projectKey, String version) + throws IOException { + JiraVersion jiraVersion = findVersion(server, projectKey, version); + if (jiraVersion == null) { + log.error( + "Version {} of project {} does not exist or no access permission", version, projectKey); + return; + } + + log.debug( + "Trying to mark version {} with id {} of project {} as released", + version, + jiraVersion.getId(), + projectKey); + + JiraVersion markAsReleased = + JiraVersion.builder().released(true).releaseDate(new Date()).build(); + apiBuilder.getVersions(server).doPut(jiraVersion.getId(), gson.toJson(markAsReleased), HTTP_OK); + + log.debug("Version {} of project {} was marked as released", version, projectKey); + } + + private JiraVersion findVersion(JiraItsServerInfo server, String projectKey, String version) + throws IOException { + JiraRestApi<JiraVersionsPage> api = apiBuilder.getProjectVersions(server, projectKey); + + JiraPageRequest pageRequest = JiraPageRequest.builder().orderBy("-sequence").build(); + JiraVersion jiraVersion = null; + while (pageRequest != null) { + JiraVersionsPage versionsPage = api.doGet(pageRequest.toSpec(), HTTP_OK); + jiraVersion = versionsPage.findByName(version); + if (jiraVersion != null) { + break; + } + pageRequest = versionsPage.nextPageRequest(pageRequest); + } + + return jiraVersion; + } + public void addValueToField( JiraItsServerInfo server, String issueKey, String value, String fieldId) throws IOException { if (!issueExists(server, issueKey)) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServer.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServer.java index 0a698aa..7666239 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServer.java +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServer.java
@@ -24,16 +24,13 @@ * to its-base to perform the its-actions. */ public class JiraItsServer implements ItsFacadeFactory { - private final JiraConfig jiraConfig; + private final JiraItsServerInfoProvider serverInfoProvider; private final JiraItsFacade itsFacade; - private final JiraItsServerCache serverCache; @Inject - public JiraItsServer( - JiraConfig jiraConfig, JiraItsFacade itsFacade, JiraItsServerCache serverCache) { - this.jiraConfig = jiraConfig; + public JiraItsServer(JiraItsServerInfoProvider serverInfoProvider, JiraItsFacade itsFacade) { + this.serverInfoProvider = serverInfoProvider; this.itsFacade = itsFacade; - this.serverCache = serverCache; } /** @@ -48,25 +45,7 @@ */ @Override public JiraItsFacade getFacade(Project.NameKey projectName) { - JiraItsServerInfo jiraItsServerInfo = serverCache.get(projectName.get()); - if (jiraItsServerInfo.isValid()) { - jiraConfig.addCommentLinksSection(projectName, jiraItsServerInfo); - } else { - jiraItsServerInfo = jiraConfig.getDefaultServerInfo(); - } - - if (!jiraItsServerInfo.isValid()) { - throw new RuntimeException( - String.format( - "No valid Jira server configuration was found for project '%s' %n." - + "Missing one or more configuration values: url: %s, username: %s, password: %s", - projectName.get(), - jiraItsServerInfo.getUrl(), - jiraItsServerInfo.getUsername(), - jiraItsServerInfo.getPassword())); - } - - itsFacade.setJiraServerInstance(jiraItsServerInfo); + itsFacade.setJiraServerInstance(serverInfoProvider.get(projectName)); return itsFacade; } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfoProvider.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfoProvider.java new file mode 100644 index 0000000..b5e0128 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfoProvider.java
@@ -0,0 +1,52 @@ +// Copyright (C) 2019 Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"), +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.googlesource.gerrit.plugins.its.jira; + +import com.google.gerrit.reviewdb.client.Project; +import com.google.inject.Inject; + +public class JiraItsServerInfoProvider { + + private final JiraConfig jiraConfig; + private final JiraItsServerCache serverCache; + + @Inject + public JiraItsServerInfoProvider(JiraConfig jiraConfig, JiraItsServerCache serverCache) { + this.jiraConfig = jiraConfig; + this.serverCache = serverCache; + } + + public JiraItsServerInfo get(Project.NameKey projectName) { + JiraItsServerInfo jiraItsServerInfo = serverCache.get(projectName.get()); + if (jiraItsServerInfo.isValid()) { + jiraConfig.addCommentLinksSection(projectName, jiraItsServerInfo); + } else { + jiraItsServerInfo = jiraConfig.getDefaultServerInfo(); + } + + if (!jiraItsServerInfo.isValid()) { + throw new RuntimeException( + String.format( + "No valid Jira server configuration was found for project '%s' %n." + + "Missing one or more configuration values: url: %s, username: %s, password: %s", + projectName.get(), + jiraItsServerInfo.getUrl(), + jiraItsServerInfo.getUsername(), + jiraItsServerInfo.getPassword())); + } + + return jiraItsServerInfo; + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraModule.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraModule.java index b9e98b3..6fff5d7 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraModule.java +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/JiraModule.java
@@ -28,6 +28,8 @@ import com.googlesource.gerrit.plugins.its.base.its.ItsConfig; import com.googlesource.gerrit.plugins.its.base.its.ItsFacade; import com.googlesource.gerrit.plugins.its.base.its.ItsFacadeFactory; +import com.googlesource.gerrit.plugins.its.base.workflow.CustomAction; +import com.googlesource.gerrit.plugins.its.jira.workflow.MarkPropertyAsReleasedVersion; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,6 +60,10 @@ .annotatedWith(Exports.named(PROJECT_CONFIG_PASSWORD_KEY)) .toInstance(new ProjectConfigEntry("JIRA password", "")); bind(ItsConfig.class); + bind(JiraItsServerInfoProvider.class); + bind(CustomAction.class) + .annotatedWith(Exports.named(MarkPropertyAsReleasedVersion.ACTION_NAME)) + .to(MarkPropertyAsReleasedVersion.class); install(new ItsHookModule(pluginName, pluginCfgFactory)); install(JiraItsServerCacheImpl.module()); LOG.info("JIRA is configured as ITS");
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraPage.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraPage.java new file mode 100644 index 0000000..3de05f4 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraPage.java
@@ -0,0 +1,80 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira.restapi; + +import java.util.List; + +public class JiraPage<T> { + + private final String self; + private final String nextPage; + private final long maxResults; + private final long startAt; + private final long total; + private final boolean isLast; + private final List<T> values; + + public JiraPage( + String self, + String nextPage, + long maxResults, + long startAt, + long total, + boolean isLast, + List<T> values) { + this.self = self; + this.nextPage = nextPage; + this.maxResults = maxResults; + this.startAt = startAt; + this.total = total; + this.isLast = isLast; + this.values = values; + } + + public JiraPageRequest nextPageRequest(JiraPageRequest currentPageRequest) { + if (isLast) { + return null; + } + return currentPageRequest.nextPageRequest(); + } + + public String getSelf() { + return self; + } + + public String getNextPage() { + return nextPage; + } + + public long getMaxResults() { + return maxResults; + } + + public long getStartAt() { + return startAt; + } + + public long getTotal() { + return total; + } + + public boolean isLast() { + return isLast; + } + + public List<T> getValues() { + return values; + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraPageRequest.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraPageRequest.java new file mode 100644 index 0000000..5e87ec3 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraPageRequest.java
@@ -0,0 +1,88 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira.restapi; + +import com.google.common.base.Strings; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +public class JiraPageRequest { + + private final Long startAt; + private final Long maxResults; + private final String orderBy; + + private JiraPageRequest(Long startAt, Long maxResults, String orderBy) { + this.startAt = startAt; + this.maxResults = maxResults; + this.orderBy = orderBy; + } + + public JiraPageRequest nextPageRequest() { + return new JiraPageRequest(startAt + 1, maxResults, orderBy); + } + + public String toSpec() { + Map<String, Object> parameters = new HashMap<>(); + if (startAt != null) { + parameters.put("startAt", startAt); + } + if (maxResults != null) { + parameters.put("maxResults", maxResults); + } + if (!Strings.isNullOrEmpty(orderBy)) { + parameters.put("orderBy", orderBy); + } + String requestParameters = + parameters + .entrySet() + .stream() + .map(parameter -> parameter.getKey() + "=" + parameter.getValue()) + .collect(Collectors.joining("&")); + return "?" + requestParameters; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private Long startAt; + private Long maxResults; + private String orderBy; + + private Builder() {} + + public Builder startAt(Long startAt) { + this.startAt = startAt; + return this; + } + + public Builder maxResults(Long maxResults) { + this.maxResults = maxResults; + return this; + } + + public Builder orderBy(String orderBy) { + this.orderBy = orderBy; + return this; + } + + public JiraPageRequest build() { + return new JiraPageRequest(startAt, maxResults, orderBy); + } + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java index 7c59e09..9159068 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraRestApiProvider.java
@@ -43,4 +43,9 @@ public JiraRestApi<JiraVersion[]> getVersions(JiraItsServerInfo serverInfo) { return get(serverInfo, JiraVersion[].class, "/version"); } + + public JiraRestApi<JiraVersionsPage> getProjectVersions( + JiraItsServerInfo serverInfo, String projectKey) { + return get(serverInfo, JiraVersionsPage.class, "/project/" + projectKey + "/version"); + } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java index 60a0b67..36022be 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersion.java
@@ -20,6 +20,7 @@ /** Represents a version in JIRA. */ public class JiraVersion { + private final String id; private final String description; private final String name; private final boolean archived; @@ -29,6 +30,7 @@ private final Long projectId; private JiraVersion( + String id, String description, String name, boolean archived, @@ -36,6 +38,7 @@ Date releaseDate, String project, Long projectId) { + this.id = id; this.description = description; this.name = name; this.archived = archived; @@ -49,6 +52,10 @@ this.projectId = projectId; } + public String getId() { + return id; + } + public String getDescription() { return description; } @@ -82,6 +89,7 @@ } public static class Builder { + private String id; private String description; private String name; private boolean archived; @@ -92,6 +100,11 @@ private Builder() {} + public Builder id(String id) { + this.id = id; + return this; + } + public Builder description(String description) { this.description = description; return this; @@ -129,7 +142,7 @@ public JiraVersion build() { return new JiraVersion( - description, name, archived, released, releaseDate, project, projectId); + id, description, name, archived, released, releaseDate, project, projectId); } } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersionsPage.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersionsPage.java new file mode 100644 index 0000000..5fa684f --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/restapi/JiraVersionsPage.java
@@ -0,0 +1,25 @@ +package com.googlesource.gerrit.plugins.its.jira.restapi; + +import java.util.List; + +/** + * Created on 03/06/18. + * + * @author Reda.Housni-Alaoui + */ +public class JiraVersionsPage extends JiraPage<JiraVersion> { + public JiraVersionsPage( + String self, + String nextPage, + int maxResults, + int startAt, + int total, + boolean isLast, + List<JiraVersion> values) { + super(self, nextPage, maxResults, startAt, total, isLast, values); + } + + public JiraVersion findByName(String name) { + return this.getValues().stream().filter(v -> name.equals(v.getName())).findFirst().orElse(null); + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersion.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersion.java new file mode 100644 index 0000000..db6de54 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersion.java
@@ -0,0 +1,67 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira.workflow; + +import com.google.gerrit.reviewdb.client.Project; +import com.google.inject.Inject; +import com.googlesource.gerrit.plugins.its.base.its.ItsFacade; +import com.googlesource.gerrit.plugins.its.base.workflow.ActionRequest; +import com.googlesource.gerrit.plugins.its.base.workflow.ActionType; +import com.googlesource.gerrit.plugins.its.base.workflow.CustomAction; +import com.googlesource.gerrit.plugins.its.jira.JiraClient; +import com.googlesource.gerrit.plugins.its.jira.JiraItsServerInfo; +import com.googlesource.gerrit.plugins.its.jira.JiraItsServerInfoProvider; +import java.io.IOException; +import java.util.Map; +import java.util.Optional; + +public class MarkPropertyAsReleasedVersion implements CustomAction { + + public static final String ACTION_NAME = "mark-property-as-released-version"; + + private final JiraItsServerInfoProvider serverInfoProvider; + private final JiraClient jiraClient; + private final MarkPropertyAsReleasedVersionParametersExtractor parametersExtractor; + + @Inject + public MarkPropertyAsReleasedVersion( + JiraItsServerInfoProvider serverInfoProvider, + JiraClient jiraClient, + MarkPropertyAsReleasedVersionParametersExtractor parametersExtractor) { + this.serverInfoProvider = serverInfoProvider; + this.jiraClient = jiraClient; + this.parametersExtractor = parametersExtractor; + } + + @Override + public void execute( + ItsFacade its, String itsProject, ActionRequest actionRequest, Map<String, String> properties) + throws IOException { + Optional<MarkPropertyAsReleasedVersionParameters> parameters = + parametersExtractor.extract(actionRequest, properties); + if (!parameters.isPresent()) { + return; + } + Project.NameKey projectName = new Project.NameKey(properties.get("project")); + JiraItsServerInfo jiraItsServerInfo = serverInfoProvider.get(projectName); + jiraClient.markVersionAsReleased( + jiraItsServerInfo, itsProject, parameters.get().getPropertyValue()); + } + + @Override + public ActionType getType() { + return ActionType.PROJECT; + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParameters.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParameters.java new file mode 100644 index 0000000..43b1a9a --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParameters.java
@@ -0,0 +1,32 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira.workflow; + +/** Parameters needed by {@link MarkPropertyAsReleasedVersion} action */ +public class MarkPropertyAsReleasedVersionParameters { + + private final String propertyValue; + + public MarkPropertyAsReleasedVersionParameters(String propertyValue) { + this.propertyValue = propertyValue; + } + + /** + * @return The extracted property value that will be used as the version value to mark as released + */ + public String getPropertyValue() { + return propertyValue; + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParametersExtractor.java b/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParametersExtractor.java new file mode 100644 index 0000000..d55e550 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParametersExtractor.java
@@ -0,0 +1,58 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira.workflow; + +import com.google.common.base.Strings; +import com.googlesource.gerrit.plugins.its.base.workflow.ActionRequest; +import java.util.Arrays; +import java.util.Map; +import java.util.Optional; +import javax.inject.Inject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class MarkPropertyAsReleasedVersionParametersExtractor { + + private static final Logger log = + LoggerFactory.getLogger(MarkPropertyAsReleasedVersionParametersExtractor.class); + + @Inject + public MarkPropertyAsReleasedVersionParametersExtractor() {} + + public Optional<MarkPropertyAsReleasedVersionParameters> extract( + ActionRequest actionRequest, Map<String, String> properties) { + String[] parameters = actionRequest.getParameters(); + if (parameters.length != 1) { + log.error( + "Wrong number of received parameters. Received parameters are {}. Only one parameter is expected, the property id.", + Arrays.toString(parameters)); + return Optional.empty(); + } + + String propertyId = parameters[0]; + if (Strings.isNullOrEmpty(propertyId)) { + log.error("Received property id is blank"); + return Optional.empty(); + } + + if (!properties.containsKey(propertyId)) { + log.error("No event property found for id {}", propertyId); + return Optional.empty(); + } + + String propertyValue = properties.get(propertyId); + return Optional.of(new MarkPropertyAsReleasedVersionParameters(propertyValue)); + } +}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md index db66b9b..62a30df 100644 --- a/src/main/resources/Documentation/config.md +++ b/src/main/resources/Documentation/config.md
@@ -240,3 +240,21 @@ limitation and the reason why this feature is marked as experimental, i.e., not production ready. Additional work is needed in order to offer a secure level of encryption for this information. + +Specific actions +---------------- + +### mark-property-as-released-version + +The `mark-property-as-released-version` action marks a version as released in +JIRA. +The version to mark as released is identified by an event property value. + +This is useful when you want to mark a version as released in JIRA when a +tag is created in the Gerrit project. + +Example with the event property `ref`: + +``` + action = mark-property-as-released-version ref +``` \ No newline at end of file
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfoProviderTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfoProviderTest.java new file mode 100644 index 0000000..c8fabc7 --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerInfoProviderTest.java
@@ -0,0 +1,69 @@ +// Copyright (C) 2018 Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"), +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira; + +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; + +import com.google.gerrit.reviewdb.client.Project; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class JiraItsServerInfoProviderTest { + + private static final Project.NameKey PROJECT_NAMEKEY = new Project.NameKey("project"); + + @Mock private JiraConfig jiraConfig; + @Mock private JiraItsServerCache serverCache; + @Mock private JiraItsServerInfo jiraItsServerInfo; + + @Rule public ExpectedException expectedException = ExpectedException.none(); + + private JiraItsServerInfoProvider jiraItsServerInfoProvider; + + @Test + public void testValidServerInfoIsreturnedFromTheCache() { + when(jiraItsServerInfo.isValid()).thenReturn(true); + when(serverCache.get(PROJECT_NAMEKEY.get())).thenReturn(jiraItsServerInfo); + jiraItsServerInfoProvider = new JiraItsServerInfoProvider(jiraConfig, serverCache); + jiraItsServerInfoProvider.get(PROJECT_NAMEKEY); + verify(jiraConfig).addCommentLinksSection(PROJECT_NAMEKEY, jiraItsServerInfo); + } + + @Test + public void testGetDefaultServerInfo() { + when(jiraItsServerInfo.isValid()).thenReturn(false).thenReturn(true); + when(serverCache.get(PROJECT_NAMEKEY.get())).thenReturn(jiraItsServerInfo); + when(jiraConfig.getDefaultServerInfo()).thenReturn(jiraItsServerInfo); + jiraItsServerInfoProvider = new JiraItsServerInfoProvider(jiraConfig, serverCache); + jiraItsServerInfoProvider.get(PROJECT_NAMEKEY); + verify(jiraConfig, never()).addCommentLinksSection(PROJECT_NAMEKEY, jiraItsServerInfo); + } + + @Test + public void testNoConfiguredServerInfo() { + when(serverCache.get(PROJECT_NAMEKEY.get())).thenReturn(jiraItsServerInfo); + when(jiraItsServerInfo.isValid()).thenReturn(false).thenReturn(false); + when(jiraConfig.getDefaultServerInfo()).thenReturn(jiraItsServerInfo); + jiraItsServerInfoProvider = new JiraItsServerInfoProvider(jiraConfig, serverCache); + expectedException.expect(RuntimeException.class); + jiraItsServerInfoProvider.get(PROJECT_NAMEKEY); + } +}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerTest.java index 06daa15..78cb0fb 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/JiraItsServerTest.java
@@ -14,7 +14,6 @@ package com.googlesource.gerrit.plugins.its.jira; -import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -30,43 +29,17 @@ public class JiraItsServerTest { private static final Project.NameKey PROJECT_NAMEKEY = new Project.NameKey("project"); - @Mock private JiraConfig jiraConfig; + @Mock private JiraItsServerInfoProvider jiraItsserverInfoProvider; @Mock private JiraItsFacade itsFacade; - @Mock private JiraItsServerCache serverCache; @Mock private JiraItsServerInfo jiraItsServerInfo; @Rule public ExpectedException expectedException = ExpectedException.none(); - private JiraItsServer jiraItsServer; - @Test - public void testValidServerInfoIsreturnedFromTheCache() throws Exception { - when(jiraItsServerInfo.isValid()).thenReturn(true); - when(serverCache.get(PROJECT_NAMEKEY.get())).thenReturn(jiraItsServerInfo); - jiraItsServer = new JiraItsServer(jiraConfig, itsFacade, serverCache); + public void testGetFacade() { + when(jiraItsserverInfoProvider.get(PROJECT_NAMEKEY)).thenReturn(jiraItsServerInfo); + JiraItsServer jiraItsServer = new JiraItsServer(jiraItsserverInfoProvider, itsFacade); jiraItsServer.getFacade(PROJECT_NAMEKEY); - verify(jiraConfig).addCommentLinksSection(PROJECT_NAMEKEY, jiraItsServerInfo); verify(itsFacade).setJiraServerInstance(jiraItsServerInfo); } - - @Test - public void testGetDefaultServerInfo() throws Exception { - when(jiraItsServerInfo.isValid()).thenReturn(false).thenReturn(true); - when(serverCache.get(PROJECT_NAMEKEY.get())).thenReturn(jiraItsServerInfo); - when(jiraConfig.getDefaultServerInfo()).thenReturn(jiraItsServerInfo); - jiraItsServer = new JiraItsServer(jiraConfig, itsFacade, serverCache); - jiraItsServer.getFacade(PROJECT_NAMEKEY); - verify(jiraConfig, never()).addCommentLinksSection(PROJECT_NAMEKEY, jiraItsServerInfo); - verify(itsFacade).setJiraServerInstance(jiraItsServerInfo); - } - - @Test - public void testNoConfiguredServerInfo() throws Exception { - when(serverCache.get(PROJECT_NAMEKEY.get())).thenReturn(jiraItsServerInfo); - when(jiraItsServerInfo.isValid()).thenReturn(false).thenReturn(false); - when(jiraConfig.getDefaultServerInfo()).thenReturn(jiraItsServerInfo); - jiraItsServer = new JiraItsServer(jiraConfig, itsFacade, serverCache); - expectedException.expect(RuntimeException.class); - jiraItsServer.getFacade(PROJECT_NAMEKEY); - } }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParametersExtractorTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParametersExtractorTest.java new file mode 100644 index 0000000..81dc49a --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionParametersExtractorTest.java
@@ -0,0 +1,90 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira.workflow; + +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.googlesource.gerrit.plugins.its.base.workflow.ActionRequest; +import java.util.Collections; +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; + +public class MarkPropertyAsReleasedVersionParametersExtractorTest { + + private static final String PROPERTY_ID = "propertyId"; + private static final String PROPERTY_VALUE = "propertyValue"; + + private MarkPropertyAsReleasedVersionParametersExtractor extractor; + + @Before + public void before() { + extractor = new MarkPropertyAsReleasedVersionParametersExtractor(); + } + + @Test + public void testNoParameter() { + testWrongNumberOfReceivedParameters(new String[] {}); + } + + @Test + public void testTwoParameters() { + testWrongNumberOfReceivedParameters(new String[] {PROPERTY_ID, PROPERTY_ID}); + } + + private void testWrongNumberOfReceivedParameters(String[] parameters) { + ActionRequest actionRequest = mock(ActionRequest.class); + when(actionRequest.getParameters()).thenReturn(parameters); + + Optional<MarkPropertyAsReleasedVersionParameters> extractedParameters = + extractor.extract(actionRequest, Collections.emptyMap()); + assertFalse(extractedParameters.isPresent()); + } + + @Test + public void testBlankPropertyId() { + ActionRequest actionRequest = mock(ActionRequest.class); + when(actionRequest.getParameters()).thenReturn(new String[] {""}); + + Optional<MarkPropertyAsReleasedVersionParameters> extractedParameters = + extractor.extract(actionRequest, Collections.emptyMap()); + assertFalse(extractedParameters.isPresent()); + } + + @Test + public void testUnknownPropertyId() { + ActionRequest actionRequest = mock(ActionRequest.class); + when(actionRequest.getParameters()).thenReturn(new String[] {PROPERTY_ID}); + + Optional<MarkPropertyAsReleasedVersionParameters> extractedParameters = + extractor.extract(actionRequest, Collections.emptyMap()); + assertFalse(extractedParameters.isPresent()); + } + + @Test + public void testHappyPath() { + ActionRequest actionRequest = mock(ActionRequest.class); + when(actionRequest.getParameters()).thenReturn(new String[] {PROPERTY_ID}); + + Optional<MarkPropertyAsReleasedVersionParameters> extractedParameters = + extractor.extract(actionRequest, Collections.singletonMap(PROPERTY_ID, PROPERTY_VALUE)); + assertTrue(extractedParameters.isPresent()); + assertEquals(PROPERTY_VALUE, extractedParameters.get().getPropertyValue()); + } +}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionTest.java b/src/test/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionTest.java new file mode 100644 index 0000000..ad29c2a --- /dev/null +++ b/src/test/java/com/googlesource/gerrit/plugins/its/jira/workflow/MarkPropertyAsReleasedVersionTest.java
@@ -0,0 +1,80 @@ +// Copyright (C) 2018 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.googlesource.gerrit.plugins.its.jira.workflow; + +import static org.mockito.Mockito.*; + +import com.google.gerrit.reviewdb.client.Project; +import com.googlesource.gerrit.plugins.its.base.its.ItsFacade; +import com.googlesource.gerrit.plugins.its.base.workflow.ActionRequest; +import com.googlesource.gerrit.plugins.its.jira.JiraClient; +import com.googlesource.gerrit.plugins.its.jira.JiraItsServerInfo; +import com.googlesource.gerrit.plugins.its.jira.JiraItsServerInfoProvider; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.junit.Before; +import org.junit.Test; + +public class MarkPropertyAsReleasedVersionTest { + + private static final String ITS_PROJECT = "test-project"; + private static final String PROJECT_KEY = "project"; + private static final String PROJECT_NAME = "projectName"; + private static final String PROPERTY_ID = "propertyId"; + private static final String PROPERTY_VALUE = "propertyValue"; + + private ItsFacade its; + private JiraItsServerInfo serverInfo; + private JiraClient jiraClient; + private MarkPropertyAsReleasedVersionParametersExtractor parametersExtractor; + private MarkPropertyAsReleasedVersion markPropertyAsReleasedVersion; + + @Before + public void before() { + its = mock(ItsFacade.class); + JiraItsServerInfoProvider serverInfoProvider = mock(JiraItsServerInfoProvider.class); + serverInfo = mock(JiraItsServerInfo.class); + when(serverInfoProvider.get(new Project.NameKey(PROJECT_NAME))).thenReturn(serverInfo); + jiraClient = mock(JiraClient.class); + parametersExtractor = mock(MarkPropertyAsReleasedVersionParametersExtractor.class); + markPropertyAsReleasedVersion = + new MarkPropertyAsReleasedVersion(serverInfoProvider, jiraClient, parametersExtractor); + } + + @Test + public void testHappyPath() throws IOException { + MarkPropertyAsReleasedVersionParameters extractedParameters = + mock(MarkPropertyAsReleasedVersionParameters.class); + when(extractedParameters.getPropertyValue()).thenReturn(PROPERTY_VALUE); + + ActionRequest actionRequest = mock(ActionRequest.class); + Map<String, String> properties = buildProperties(); + when(parametersExtractor.extract(actionRequest, properties)) + .thenReturn(Optional.of(extractedParameters)); + + markPropertyAsReleasedVersion.execute(its, ITS_PROJECT, actionRequest, properties); + + verify(jiraClient).markVersionAsReleased(serverInfo, ITS_PROJECT, PROPERTY_VALUE); + } + + private Map<String, String> buildProperties() { + Map<String, String> properties = new HashMap<>(); + properties.put(PROPERTY_ID, PROJECT_NAME); + properties.put(PROJECT_KEY, PROJECT_NAME); + return properties; + } +}