Skip to content

Commit

Permalink
#1251 SafeComments
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 11, 2018
1 parent 7c4af07 commit 68eacb5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/com/rultor/agents/github/Answer.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 1.0
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@Immutable
@ToString
Expand Down Expand Up @@ -111,7 +112,9 @@ public void post(final boolean success, final String msg,
++mine;
}
if (mine < Answer.MAX) {
issue.comments().post(this.msg(success, Logger.format(msg, args)));
new SafeComments(
issue.comments()
).post(this.msg(success, Logger.format(msg, args)));
} else {
Logger.error(
this, "too many (%d) comments from %s already in %s#%d",
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/com/rultor/agents/github/SafeComments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2009-2018, rultor.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the rultor.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.rultor.agents.github;

import com.jcabi.aspects.Immutable;
import com.jcabi.github.Comment;
import com.jcabi.github.Comments;
import com.jcabi.github.Issue;
import com.jcabi.github.mock.MkGithub;
import com.jcabi.log.Logger;
import java.io.IOException;
import java.util.Date;
import lombok.ToString;

/**
* Comments that never crashes.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 1.68
*/
@Immutable
@ToString
public final class SafeComments implements Comments {

/**
* Original comments.
*/
private final transient Comments origin;

/**
* Ctor.
* @param cmt Original comments
*/
public SafeComments(final Comments cmt) {
this.origin = cmt;
}

@Override
public Issue issue() {
throw new UnsupportedOperationException("#issue()");
}

@Override
public Comment get(final int number) {
throw new UnsupportedOperationException("#get()");
}

@Override
public Iterable<Comment> iterate(final Date since) {
throw new UnsupportedOperationException("#iterate()");
}

@Override
public Comment post(final String text) throws IOException {
Comment cmt;
try {
cmt = this.origin.post(text);
} catch (final AssertionError ex) {
Logger.warn(this, "Failed to post to GitHub: %[exception]s", ex);
cmt = new MkGithub().randomRepo()
.issues().create("", "")
.comments().post(text);
}
return cmt;
}
}

0 comments on commit 68eacb5

Please sign in to comment.