This repository was archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Allow to specify git branch instead of pull request id, Allow to break the build #93
Closed
ghenzler
wants to merge
1
commit into
AmadeusITGroup:master
from
ghenzler:feature/#91-#92-automatic-PR-id-from-branch-and-fail-build
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -261,6 +261,17 @@ public String getStashRepository() throws StashConfigurationException { | |
| */ | ||
| public String getStashPullRequestId() throws StashConfigurationException { | ||
| String result = config.getPullRequestId(); | ||
|
|
||
| if(StringUtils.isNotBlank(config.getPullRequestBranch())) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the main part for #92 - doing this in StashIssueReportingPostJob directly was not that easy (obscure junit test failures) but it would probably cleaner to move it there. |
||
| try (StashClient stashClient = new StashClient(getStashURL(), getCredentials(), config.getStashTimeout(), config.getSonarQubeVersion())) { | ||
| StashPullRequest pullRequest = stashClient.getPullRequestByBranchName(config.getStashProject(), config.getStashRepository(), config.getPullRequestBranch()); | ||
| LOGGER.debug("Found PR for branch {}: {}", config.getPullRequestBranch(), pullRequest); | ||
| return pullRequest.getId(); | ||
| } catch (StashClientException e) { | ||
| throw new StashConfigurationException(MessageFormat.format(EXCEPTION_STASH_CONF, StashPlugin.STASH_PULL_REQUEST_ID)); | ||
| } | ||
| } | ||
|
|
||
| if (result == null){ | ||
| throw new StashConfigurationException(MessageFormat.format(EXCEPTION_STASH_CONF, StashPlugin.STASH_PULL_REQUEST_ID)); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| import org.sonar.plugins.stash.StashPlugin; | ||
| import org.sonar.plugins.stash.exceptions.StashClientException; | ||
| import org.sonar.plugins.stash.exceptions.StashReportExtractionException; | ||
| import org.sonar.plugins.stash.exceptions.GitBranchNotFoundOrNotUniqueException; | ||
| import org.sonar.plugins.stash.issue.StashComment; | ||
| import org.sonar.plugins.stash.issue.StashCommentReport; | ||
| import org.sonar.plugins.stash.issue.StashDiffReport; | ||
|
|
@@ -26,6 +27,9 @@ | |
| import org.sonar.plugins.stash.issue.StashUser; | ||
| import org.sonar.plugins.stash.issue.collector.StashCollector; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.text.MessageFormat; | ||
|
|
@@ -37,8 +41,10 @@ | |
|
|
||
| import static java.net.HttpURLConnection.HTTP_CREATED; | ||
|
|
||
| public class StashClient implements AutoCloseable { | ||
|
|
||
| public class StashClient implements AutoCloseable { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(StashClient.class); | ||
|
|
||
| private final String baseUrl; | ||
| private final StashCredentials credentials; | ||
| private final int stashTimeout; | ||
|
|
@@ -61,6 +67,7 @@ public class StashClient implements AutoCloseable { | |
|
|
||
| private static final String PULL_REQUEST_APPROVAL_POST_ERROR_MESSAGE = "Unable to change status of pull-request {0} #{1}."; | ||
| private static final String PULL_REQUEST_GET_ERROR_MESSAGE = "Unable to retrieve pull-request {0} #{1}."; | ||
| private static final String PULL_REQUEST_GET_FOR_BRANCH_ERROR_MESSAGE = "Unable to retrieve pull-request in repo {0} for branch {1}."; | ||
| private static final String PULL_REQUEST_PUT_ERROR_MESSAGE = "Unable to update pull-request {0} #{1}."; | ||
| private static final String USER_GET_ERROR_MESSAGE = "Unable to retrieve user {0}."; | ||
| private static final String COMMENT_POST_ERROR_MESSAGE = "Unable to post a comment to {0} #{1}."; | ||
|
|
@@ -178,6 +185,25 @@ public StashPullRequest getPullRequest(String project, String repository, String | |
| return StashCollector.extractPullRequest(project, repository, pullRequestId, response); | ||
| } | ||
|
|
||
| public StashPullRequest getPullRequestByBranchName(String project, String repository, String branchName) | ||
| throws StashClientException { | ||
| String request = MessageFormat.format(REPO_API + "pull-requests?at=refs/heads/{3}&direction=OUTGOING", baseUrl, project, repository, branchName); | ||
|
|
||
| JSONObject response = get(request, MessageFormat.format(PULL_REQUEST_GET_FOR_BRANCH_ERROR_MESSAGE, repository, branchName)); | ||
|
|
||
| Long size = (Long) response.get("size"); | ||
| if(size==0) { | ||
| throw new GitBranchNotFoundOrNotUniqueException("Found no PR for branch "+branchName + " in repo "+repository); | ||
| } else if(size==1) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if you run a git-flow branching scheme and make a hotfix commit with PR towards both develop and master (if they happen to be in sync). That case would likely trigger size == 2 and still be valid, right? |
||
| // the expected case | ||
| JSONObject prJsonObject = (JSONObject) ((JSONArray) response.get("values")).get(0); | ||
| Long prId = (Long) prJsonObject.get("id"); | ||
| return StashCollector.extractPullRequest(project, repository, String.valueOf(prId), prJsonObject); | ||
| } else { | ||
| throw new GitBranchNotFoundOrNotUniqueException("Found "+size+" PRs for branch "+branchName + " in repo "+repository); | ||
| } | ||
| } | ||
|
|
||
| public void addPullRequestReviewer(String project, String repository, String pullRequestId, long pullRequestVersion, ArrayList<StashUser> reviewers) | ||
| throws StashClientException { | ||
| String request = MessageFormat.format(API_ONE_PR, baseUrl, project, repository, pullRequestId); | ||
|
|
||
9 changes: 9 additions & 0 deletions
9
src/main/java/org/sonar/plugins/stash/exceptions/GitBranchNotFoundOrNotUniqueException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package org.sonar.plugins.stash.exceptions; | ||
|
|
||
| public class GitBranchNotFoundOrNotUniqueException extends RuntimeException { | ||
|
|
||
| public GitBranchNotFoundOrNotUniqueException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/sonar/plugins/stash/exceptions/StashFailBuildException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package org.sonar.plugins.stash.exceptions; | ||
|
|
||
| public class StashFailBuildException extends RuntimeException { | ||
|
|
||
| public StashFailBuildException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Main part for #91 - needs to be outside of try... catch since the fail build should even work, if there is no pull request (yet)