asfgit closed pull request #28: improve exception handling
URL: https://github.com/apache/ignite-teamcity-bot/pull/28
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITcHelper.java 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITcHelper.java
index 5c25432..5d68ce9 100644
--- a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITcHelper.java
+++ b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITcHelper.java
@@ -18,13 +18,11 @@
 package org.apache.ignite.ci;
 
 import java.util.List;
-import org.apache.ignite.ci.observer.BuildObserver;
 import org.apache.ignite.ci.issue.IssueDetector;
 import org.apache.ignite.ci.issue.IssuesStorage;
 import org.apache.ignite.ci.user.ICredentialsProv;
 import org.apache.ignite.ci.user.UserAndSessionsStorage;
 
-import javax.annotation.Nullable;
 import java.util.Collection;
 
 /**
@@ -43,7 +41,6 @@
 
     IssueDetector issueDetector();
 
-
     ITcAnalytics tcAnalytics(String serverId);
 
     UserAndSessionsStorage users();
@@ -62,5 +59,5 @@
      * @param ticket JIRA ticket full name.
      * @return {@code True} if JIRA was notified.
      */
-    boolean notifyJira(String srvId, ICredentialsProv prov, String 
buildTypeId, String branchForTc, String ticket);
+    String notifyJira(String srvId, ICredentialsProv prov, String buildTypeId, 
String branchForTc, String ticket);
 }
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITeamcity.java 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITeamcity.java
index 4989b31..e404b70 100644
--- a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITeamcity.java
+++ b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/ITeamcity.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.ci;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Date;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
@@ -37,7 +38,6 @@
 import org.apache.ignite.ci.tcmodel.hist.BuildRef;
 import org.apache.ignite.ci.tcmodel.result.Build;
 import org.apache.ignite.ci.tcmodel.result.issues.IssuesUsagesList;
-import org.apache.ignite.ci.tcmodel.result.problems.ProblemOccurrence;
 import org.apache.ignite.ci.tcmodel.result.problems.ProblemOccurrences;
 import org.apache.ignite.ci.tcmodel.result.stat.Statistics;
 import org.apache.ignite.ci.tcmodel.result.tests.TestOccurrence;
@@ -314,8 +314,11 @@ default SingleBuildRunCtx loadTestsAndProblems(@Nonnull 
Build build, @Deprecated
      * @param ticket JIRA ticket full name.
      * @param comment Comment to be placed in the ticket conversation.
      * @return {@code True} if ticket was succesfully commented. Otherwise - 
{@code false}.
+     *
+     * @throws IOException If failed to comment JIRA ticket.
+     * @throws IllegalStateException If can't find URL to the JIRA.
      */
-    boolean sendJiraComment(String ticket, String comment);
+    String sendJiraComment(String ticket, String comment) throws IOException;
 
     /**
      * @param url URL for git integration.
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgnitePersistentTeamcity.java
 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgnitePersistentTeamcity.java
index 82bea6d..35e5d63 100644
--- 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgnitePersistentTeamcity.java
+++ 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgnitePersistentTeamcity.java
@@ -21,6 +21,7 @@
 import com.google.common.base.Throwables;
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
@@ -1162,7 +1163,7 @@ private void 
migrateTestOneOcurrToAddToLatest(TestOccurrence next) {
     }
 
     /** {@inheritDoc} */
-    @Override public boolean sendJiraComment(String ticket, String comment) {
+    @Override public String sendJiraComment(String ticket, String comment) 
throws IOException {
         return teamcity.sendJiraComment(ticket, comment);
     }
 
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgniteTeamcityConnection.java
 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgniteTeamcityConnection.java
index 2b2d365..aa0e76c 100644
--- 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgniteTeamcityConnection.java
+++ 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/IgniteTeamcityConnection.java
@@ -194,25 +194,13 @@ public void init(@Nullable String tcName) {
 
     /** {@inheritDoc} */
     @AutoProfiling
-    @Override public boolean sendJiraComment(String ticket, String comment) {
-        if (isNullOrEmpty(jiraApiUrl)) {
-            logger.error("Failed to notify JIRA [errMsg=JIRA API URL is not 
configured for this server.]");
+    @Override public String sendJiraComment(String ticket, String comment) 
throws IOException {
+        if (isNullOrEmpty(jiraApiUrl))
+            throw new IllegalStateException("JIRA API URL is not configured 
for this server.");
 
-            return false;
-        }
-
-        try {
-            String url = jiraApiUrl + "issue/" + ticket + "/comment";
-
-            HttpUtil.sendPostAsStringToJira(jiraBasicAuthTok, url, "{\"body\": 
\"" + comment + "\"}");
+        String url = jiraApiUrl + "issue/" + ticket + "/comment";
 
-            return true;
-        }
-        catch (IOException e) {
-            logger.error("Failed to notify JIRA [errMsg="+e.getMessage()+']');
-
-            return false;
-        }
+        return HttpUtil.sendPostAsStringToJira(jiraBasicAuthTok, url, 
"{\"body\": \"" + comment + "\"}");
     }
 
     /** {@inheritDoc} */
@@ -238,7 +226,7 @@ public void init(@Nullable String tcName) {
     /** {@inheritDoc} */
     @AutoProfiling
     @Override public PullRequest getPullRequest(String branchForTc) {
-        if (!isNullOrEmpty(gitApiUrl))
+        if (isNullOrEmpty(gitApiUrl))
             throw new IllegalStateException("Git API URL is not configured for 
this server.");
 
         String id = null;
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/TcHelper.java 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/TcHelper.java
index a0e0efc..51484c6 100644
--- a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/TcHelper.java
+++ b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/TcHelper.java
@@ -112,7 +112,7 @@ private BranchesTracked getTrackedBranches() {
     }
 
     /** {@inheritDoc} */
-    @Override public boolean notifyJira(
+    @Override public String notifyJira(
         String srvId,
         ICredentialsProv prov,
         String buildTypeId,
@@ -124,22 +124,26 @@ private BranchesTracked getTrackedBranches() {
         List<BuildRef> builds = 
teamcity.getFinishedBuildsIncludeSnDepFailed(buildTypeId, branchForTc);
 
         if (builds.isEmpty())
-            return false;
+            return "JIRA wasn't commented - no finished builds to analyze.";
 
         BuildRef build = builds.get(builds.size() - 1);
         String comment;
 
         try {
             comment = generateJiraComment(buildTypeId, build.branchName, 
srvId, prov, build.webUrl);
+
+            teamcity.sendJiraComment(ticket, comment);
         }
-        catch (RuntimeException e) {
-            logger.error("Exception happened during generating comment for 
JIRA " +
-                "[build=" + build.getId() + ", errMsg=" + e.getMessage() + 
']');
+        catch (Exception e) {
+            String errMsg = "Exception happened during commenting JIRA ticket 
" +
+                "[build=" + build.getId() + ", errMsg=" + e.getMessage() + ']';
+
+            logger.error(errMsg);
 
-            return false;
+            return "JIRA wasn't commented - " + errMsg;
         }
 
-        return teamcity.sendJiraComment(ticket, comment);
+        return JIRA_COMMENTED;
     }
 
     /**
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/di/IgniteTcBotModule.java
 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/di/IgniteTcBotModule.java
index 5ee9e62..b09ace1 100644
--- 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/di/IgniteTcBotModule.java
+++ 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/di/IgniteTcBotModule.java
@@ -85,7 +85,7 @@
     private static class Jira implements IJiraIntegration {
         @Inject ITcHelper helper;
 
-        @Override public boolean notifyJira(String srvId, ICredentialsProv 
prov, String buildTypeId, String branchForTc,
+        @Override public String notifyJira(String srvId, ICredentialsProv 
prov, String buildTypeId, String branchForTc,
             String ticket) {
             return helper.notifyJira(srvId, prov, buildTypeId, branchForTc, 
ticket);
         }
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/jira/IJiraIntegration.java
 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/jira/IJiraIntegration.java
index 919a679..633ab3a 100644
--- 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/jira/IJiraIntegration.java
+++ 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/jira/IJiraIntegration.java
@@ -23,6 +23,9 @@
  *
  */
 public interface IJiraIntegration {
+    /** Message to show user when JIRA ticket was successfully commented by 
the Bot. */
+    public static String JIRA_COMMENTED = "JIRA commented.";
+
     /**
      * @param srvId TC Server ID to take information about token from.
      * @param prov Credentials.
@@ -31,6 +34,6 @@
      * @param ticket JIRA ticket full name.
      * @return {@code True} if JIRA was notified.
      */
-    public boolean notifyJira(String srvId, ICredentialsProv prov, String 
buildTypeId, String branchForTc,
+    public String notifyJira(String srvId, ICredentialsProv prov, String 
buildTypeId, String branchForTc,
         String ticket);
 }
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/observer/ObserverTask.java
 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/observer/ObserverTask.java
index 823cc07..374099b 100644
--- 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/observer/ObserverTask.java
+++ 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/observer/ObserverTask.java
@@ -32,6 +32,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.ignite.ci.jira.IJiraIntegration.JIRA_COMMENTED;
+
 /**
  * Checks observed builds for finished status and comments JIRA ticket.
  */
@@ -86,7 +88,10 @@ protected String runObserverTask() {
                 continue;
             }
 
-            if (jiraIntegration.notifyJira(info.srvId, info.prov, 
info.build.buildTypeId, info.build.branchName, info.ticket)) {
+            String jiraRes = jiraIntegration.notifyJira(info.srvId, info.prov, 
info.build.buildTypeId,
+                info.build.branchName, info.ticket);
+
+            if (JIRA_COMMENTED.equals(jiraRes)) {
                 ticketsNotified.add(info.ticket);
 
                 builds.remove(info);
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/TriggerBuild.java
 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/TriggerBuild.java
index 6a0ce3a..83e7911 100644
--- 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/TriggerBuild.java
+++ 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/TriggerBuild.java
@@ -77,7 +77,7 @@ public SimpleResult triggerBuild(
         Build build = teamcity.triggerBuild(suiteId, branchForTc, false, top 
!= null && top);
 
         if (observe != null && observe)
-            jiraRes = observeJira(srvId, branchForTc, ticketId, helper, 
teamcity, build, prov);
+            jiraRes = observeJira(srvId, branchForTc, ticketId, teamcity, 
build, prov);
 
         return new SimpleResult("Tests started." + (!jiraRes.isEmpty() ? 
"<br>" + jiraRes : ""));
     }
@@ -102,7 +102,12 @@ public SimpleResult commentJira(
     }
 
     @NotNull
-    public SimpleResult commentJiraEx(@QueryParam("serverId") @Nullable String 
srvId, @QueryParam("branchName") @Nullable String branchForTc, 
@QueryParam("suiteId") @Nullable String suiteId, @QueryParam("ticketId") 
@Nullable String ticketId) {
+    public SimpleResult commentJiraEx(
+        @QueryParam("serverId") @Nullable String srvId,
+        @QueryParam("branchName") @Nullable String branchForTc,
+        @QueryParam("suiteId") @Nullable String suiteId,
+        @QueryParam("ticketId") @Nullable String ticketId
+    ) {
         final ICredentialsProv prov = ICredentialsProv.get(req);
 
         if (!prov.hasAccess(srvId))
@@ -114,25 +119,31 @@ public SimpleResult commentJiraEx(@QueryParam("serverId") 
@Nullable String srvId
         final ITeamcity teamcity = helper.server(srvId, prov);
 
         if (Strings.isNullOrEmpty(ticketId)) {
-            PullRequest pr = teamcity.getPullRequest(branchForTc);
-
-            ticketId = getTicketId(pr);
-
-            if (ticketId.isEmpty()) {
-                jiraRes = "JIRA ticket can't be commented - " +
-                    "PR title \"" + pr.getTitle() + "\" should starts with 
\"IGNITE-XXXX\"." +
-                    " Please, rename PR according to the" +
-                    " <a 
href='https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute"; +
-                    "#HowtoContribute-1.CreateGitHubpull-request'>contributing 
guide</a>" +
-                    " or enter ticket id in the form.";
+            try {
+                PullRequest pr = teamcity.getPullRequest(branchForTc);
+
+                ticketId = getTicketId(pr);
+
+                if (ticketId.isEmpty()) {
+                    jiraRes = "JIRA ticket can't be commented - " +
+                        "PR title \"" + pr.getTitle() + "\" should starts with 
\"IGNITE-XXXX\"." +
+                        " Please, rename PR according to the" +
+                        " <a 
href='https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute"; +
+                        
"#HowtoContribute-1.CreateGitHubpull-request'>contributing guide</a>" +
+                        " or enter ticket id in the form.";
+                }
+            }
+            catch (RuntimeException e) {
+                jiraRes = "Exception happened when server tried to get ticket 
ID from Pull Request - " + e.getMessage();
             }
-
         }
 
-        if (helper.notifyJira(srvId, prov, suiteId, branchForTc, "ignite-" + 
ticketId))
-            return new SimpleResult("JIRA commented." + (!jiraRes.isEmpty() ? 
jiraRes : ""));
+        if (!Strings.isNullOrEmpty(ticketId)) {
+            jiraRes = helper.notifyJira(srvId, prov, suiteId, branchForTc, 
"ignite-" + ticketId);
+
+            return new SimpleResult(jiraRes);
+        }
         else
-            // TODO Write catched exceptions to the response.
             return new SimpleResult("JIRA wasn't commented." + 
(!jiraRes.isEmpty() ? "<br>" + jiraRes : ""));
     }
 
@@ -140,7 +151,6 @@ public SimpleResult commentJiraEx(@QueryParam("serverId") 
@Nullable String srvId
      * @param srvId Server id.
      * @param branchForTc Branch for TeamCity.
      * @param ticketId JIRA ticket number.
-     * @param helper Helper.
      * @param teamcity TeamCity.
      * @param build Build.
      * @param prov Credentials.
@@ -150,22 +160,28 @@ private String observeJira(
         String srvId,
         String branchForTc,
         @Nullable String ticketId,
-        ITcHelper helper,
         ITeamcity teamcity,
         Build build,
         ICredentialsProv prov
     ) {
         if (F.isEmpty(ticketId)) {
-            PullRequest pr = teamcity.getPullRequest(branchForTc);
-
-            ticketId = getTicketId(pr);
-
-            if (ticketId.isEmpty()) {
+            try {
+                PullRequest pr = teamcity.getPullRequest(branchForTc);
+
+                ticketId = getTicketId(pr);
+
+                if (ticketId.isEmpty()) {
+                    return "JIRA ticket will not be notified after the tests 
are completed - " +
+                        "PR title \"" + pr.getTitle() + "\" should starts with 
\"IGNITE-XXXX\"." +
+                        " Please, rename PR according to the" +
+                        " <a 
href='https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute"; +
+                        
"#HowtoContribute-1.CreateGitHubpull-request'>contributing guide</a>.";
+                }
+            }
+            catch (Exception e) {
                 return "JIRA ticket will not be notified after the tests are 
completed - " +
-                    "PR title \"" + pr.getTitle() + "\" should starts with 
\"IGNITE-XXXX\"." +
-                    " Please, rename PR according to the" +
-                    " <a 
href='https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute"; +
-                    "#HowtoContribute-1.CreateGitHubpull-request'>contributing 
guide</a>.";
+                    "exception happened when server tried to get ticket ID 
from Pull Request [errMsg=" +
+                    e.getMessage() + ']';
             }
         }
 
diff --git 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/pr/GetPrTestFailures.java
 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/pr/GetPrTestFailures.java
index e05a6c5..e113b6a 100644
--- 
a/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/pr/GetPrTestFailures.java
+++ 
b/ignite-tc-helper-web/src/main/java/org/apache/ignite/ci/web/rest/pr/GetPrTestFailures.java
@@ -134,7 +134,15 @@ public String getNotifyGit(
 
         IAnalyticsEnabledTeamcity teamcity = CtxListener.server(srvId, ctx, 
req);
 
-        PullRequest pr = teamcity.getPullRequest(branchForTc);
+        PullRequest pr;
+
+        try {
+            pr = teamcity.getPullRequest(branchForTc);
+        }
+        catch (RuntimeException e) {
+            return "Exception happened - " + e.getMessage();
+        }
+
         String statusesUrl = pr.getStatusesUrl();
 
         teamcity.notifyGit(statusesUrl, msg);


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to