Skip to content

Commit

Permalink
BitConDec-11: Enable unit testing with Jacoco (#4)
Browse files Browse the repository at this point in the history
* Add interface for ApplicationLinkService class to make its responsibility clearer

* Get CommitService via ComponentLocator instead of injecting it for better testability

* Add interface for MergeCheckHandler and rename merge check classes, separate MergChecking bean from completeness checking logic

* Add Jacoco maven plugin to generate coverage reports and send them to Codecov GitHub plugin in .travis.yml

* Test completion checking in JSON String

* Refactor and test method to retrieve project key from text

* Add mock package and class for a pull request

* Add test for getCommitsOfPullRequest method
  • Loading branch information
kleebaum authored Oct 6, 2019
1 parent 553e6de commit 00d9d02
Show file tree
Hide file tree
Showing 16 changed files with 674 additions and 354 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ before_install:
- export PATH=opt/atlassian-plugin-sdk/bin:opt/atlassian-plugin-sdk/apache-maven-*/bin:$PATH
install: true
script:
- atlas-package
- atlas-package -P jacoco
notifications:
email: false
deploy:
Expand All @@ -28,4 +28,4 @@ deploy:
tags: true
repo: cures-hub/cures-condec-bitbucket
after_success:
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash) -f $TRAVIS_BUILD_DIR/target/site/jacoco/jacoco.xml
45 changes: 34 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,26 +188,49 @@
<verbose>false</verbose>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<!-- Creates coverage reports in target/site/jacoco/ -->
<profile>
<id>jacoco</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<check />
</configuration>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco-version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</build>
</profile>
</profiles>

<properties>
<bitbucket.version>6.6.2</bitbucket.version>
<bitbucket.data.version>${bitbucket.version}</bitbucket.data.version>
<amps.version>8.0.2</amps.version>
<atlassian.spring.scanner.version>2.1.12</atlassian.spring.scanner.version>
<jacoco-version>0.8.4</jacoco-version>
<!-- This key is used to keep the consistency between the key in atlassian-plugin.xml and the key to
generate bundle. -->
<atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.uhd.ifi.se.decision.management.bitbucket.merge.checks;

import java.util.Locale;
import java.util.Set;

/**
* Interface to check the completeness of the documentation of decision
* knowledge related to a pull request.
*/
public interface CompletenessCheckHandler {

/**
* Checks whether the documentation is complete.
*
* @return true if the documentation is complete.
*/
boolean isDocumentationComplete();

/**
* Returns a set of Jira issues with an incomplete documentation.
*
* @return set of Jira issue keys.
*/
Set<String> getJiraIssuesWithIncompleteDocumentation();

/**
* Looks for the first Jira issue key (e.g. CONDEC-1) in a String and returns the
* Jira project key (e.g. CONDEC).
*
* @param text
* that might contain a project key, e.g., a commit message, branch
* name oder pull request title.
* @return potential project key in upper case.
*/
static String retrieveProjectKey(String text) {
if (text.indexOf("-") == -1) {
// there is no Jira issue key (e.g. CONDEC-1) in the text
return "";
}
String splitted = text.split("-")[0];
String[] words = splitted.split(" ");
String projectKey = words[words.length - 1];
return projectKey.toUpperCase(Locale.ENGLISH);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package de.uhd.ifi.se.decision.management.bitbucket.merge.checks;

import javax.annotation.Nonnull;

import org.springframework.stereotype.Component;

import com.atlassian.bitbucket.hook.repository.PreRepositoryHookContext;
import com.atlassian.bitbucket.hook.repository.PullRequestMergeHookRequest;
import com.atlassian.bitbucket.hook.repository.RepositoryHookResult;
import com.atlassian.bitbucket.hook.repository.RepositoryMergeCheck;
import com.atlassian.bitbucket.pull.PullRequest;

import de.uhd.ifi.se.decision.management.bitbucket.merge.checks.impl.CompletenessCheckHandlerImpl;

/**
* Enforces that pull requests can only be accepted, i.e., the respective branch
* can only be merged back to the maineline if the decision knowledge
* documentation is complete. The completeness is determined in the
* {@link CompletenessCheckHandler}.
*
*/
@Component("completenessMergeCheck")
public class CompletenessMergeCheck implements RepositoryMergeCheck {

@Nonnull
@Override
public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context,
@Nonnull PullRequestMergeHookRequest request) {
PullRequest pullRequest = request.getPullRequest();

CompletenessCheckHandler completenessCheckHandler = new CompletenessCheckHandlerImpl(pullRequest);
if (completenessCheckHandler.isDocumentationComplete()) {
return RepositoryHookResult.accepted();
}
String summaryMessage = "There are not enough decision knowledge elements linked to the pull request.";
String detailMessage = "Every Jira ticket has to have at least one decision problem (=issue) and one decision linked. "
+ "Jira tickets with an incomplete documentation are: ";
for (String jiraIssueKey : completenessCheckHandler.getJiraIssuesWithIncompleteDocumentation()) {
detailMessage += jiraIssueKey + " ";
}

// TODO
// @issue Should the developers be allowed to merge the branch with an
// incomplete decision knowledge documentation?
// @decision It should be configurable on a setting page whether the incomplete
// documentation enforces that the branch cannot be merged or not.
return RepositoryHookResult.rejected(summaryMessage, detailMessage);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 00d9d02

Please sign in to comment.