/**
 * Copyright (c) 2009-2014, 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.qtn;

import com.google.common.collect.ImmutableMap;
import com.jcabi.aspects.Immutable;
import com.jcabi.github.Comment;
import com.jcabi.github.Issue;
import com.jcabi.github.JsonReadable;
import com.jcabi.github.Pull;
import com.jcabi.log.Logger;
import com.rultor.agents.github.Answer;
import com.rultor.agents.github.Question;
import com.rultor.agents.github.Req;
import java.io.IOException;
import java.net.URI;
import javax.json.JsonObject;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
 * Merge request.
 *
 * @author Yegor Bugayenko (yegor@tpc2.com)
 * @version $Id$
 * @since 1.3
 * @checkstyle MultipleStringLiteralsCheck (500 lines)
 */
@Immutable
@ToString
@EqualsAndHashCode
public final class QnMerge implements Question {

    @Override
    public Req understand(final Comment.Smart comment,
        final URI home) throws IOException {
        final Issue issue = comment.issue();
        final JsonReadable pull = new Pull.Smart(
            issue.repo().pulls().get(issue.number())
        );
        final JsonObject head = pull.json().getJsonObject("head");
        final JsonObject base = pull.json().getJsonObject("base");
        new Answer(comment).post(
            String.format(
                "OK, I'm on it. You can track me [here](%s)",
                home.toASCIIString()
            )
        );
        Logger.info(this, "merge request found in #%d", issue.number());
        return new Req.Simple(
            "merge",
            new ImmutableMap.Builder<String, String>()
                .put("head-branch", head.getString("ref"))
                .put("base-branch", base.getString("ref"))
                .put(
                    "base",
                    String.format(
                        "git@github.com:%s.git",
                        base.getJsonObject("repo").getString("full_name")
                    )
                )
                .put(
                    "head",
                    String.format(
                        "git@github.com:%s.git",
                        head.getJsonObject("repo").getString("full_name")
                    )
                )
                .build()
        );
    }

}

@@ -2482,282 +2482,114 @@
 ssue
+.Smart
  issue
- = comment.issue();%0A        final JsonReadable pull
  = new 
-Pull
+Issue
 .Smart(
-%0A            issue.repo().pulls().get(issue.number())%0A        );%0A        final JsonObject head = pull.json().getJsonObject(%22head%22);%0A        final JsonObject base = pull.json().getJsonObject(%22base%22);%0A
+comment.issue());%0A        final Req req;%0A        if (issue.isPull()) %7B%0A    
     
@@ -2618,16 +2618,20 @@
 ).post(%0A
+    
         
@@ -2665,16 +2665,20 @@
         
+    
 %22OK, I'm
@@ -2731,16 +2731,20 @@
         
+    
 home.toA
@@ -2768,26 +2768,33 @@
         
-)%0A
+    )%0A   
         
 );%0A     
@@ -2781,27 +2781,32 @@
 %0A           
+ 
 );%0A
+    
         Logg
@@ -2862,24 +2862,709 @@
 .number());%0A
+            req = QnMerge.pack(%0A                new Pull.Smart(%0A                    issue.repo().pulls().get(issue.number())%0A                )%0A            );%0A        %7D else %7B%0A            new Answer(comment).post(%0A                %22it's not a pull request, I can't merge it%22%0A            );%0A            req = Req.EMPTY;%0A        %7D%0A        return req;%0A    %7D%0A%0A    /**%0A     * Pack a pull request.%0A     * @param pull Pull%0A     * @return Req%0A     * @throws IOException If fails%0A     */%0A    private static Req pack(final JsonReadable pull) throws IOException %7B%0A        final JsonObject head = pull.json().getJsonObject(%22head%22);%0A        final JsonObject base = pull.json().getJsonObject(%22base%22);%0A
         retu

check for PR