// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/publishers/GitHubPRAbstractPublisher.java
// public abstract class GitHubPRAbstractPublisher extends Recorder implements SimpleBuildStep {
// 
//     @CheckForNull
//     private StatusVerifier statusVerifier;
// 
//     @CheckForNull
//     private PublisherErrorHandler errorHandler;
// 
//     public GitHubPRAbstractPublisher(StatusVerifier statusVerifier, PublisherErrorHandler errorHandler) {
//         this.statusVerifier = statusVerifier;
//         this.errorHandler = errorHandler;
//     }
// 
//     public StatusVerifier getStatusVerifier() {
//         return statusVerifier;
//     }
// 
//     public PublisherErrorHandler getErrorHandler() {
//         return errorHandler;
//     }
// 
//     protected void handlePublisherError(Run<?, ?> run) {
//         if (errorHandler != null) {
//             errorHandler.markBuildAfterError(run);
//         }
//     }
// 
//     public final BuildStepMonitor getRequiredMonitorService() {
//         return BuildStepMonitor.NONE;
//     }
// 
//     public final GitHubPRTrigger.DescriptorImpl getTriggerDescriptor() {
//         return (GitHubPRTrigger.DescriptorImpl) Jenkins.getInstance().getDescriptor(GitHubPRTrigger.class);
//     }
// 
//     public abstract static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
//         @Override
//         public final boolean isApplicable(final Class<? extends AbstractProject> jobType) {
//             return true;
//         }
// 
//         @Override
//         public abstract String getDisplayName();
//     }
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/PublisherErrorHandler.java
// public class PublisherErrorHandler extends AbstractDescribableImpl<PublisherErrorHandler> {
// 
//     private Result buildStatus;
// 
//     @DataBoundConstructor
//     public PublisherErrorHandler(Result buildStatus) {
//         this.buildStatus = buildStatus;
//     }
// 
//     public Result getBuildStatus() {
//         return buildStatus;
//     }
// 
//     public Result markBuildAfterError(Run<?, ?> run) {
//         run.setResult(buildStatus);
//         return buildStatus;
//     }
// 
//     @Symbol("statusOnPublisherError")
//     @Extension
//     public static class DescriptorImpl extends Descriptor<PublisherErrorHandler> {
//         @NonNull
//         @Override
//         public String getDisplayName() {
//             return "Set build status if publisher failed";
//         }
// 
//         public ListBoxModel doFillBuildStatusItems() {
//             ListBoxModel items = new ListBoxModel();
//             items.add(Result.UNSTABLE.toString());
//             items.add(Result.FAILURE.toString());
//             return items;
//         }
//     }
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/JobHelper.java
// public static GHIssue getGhIssue(final Run<?, ?> run) throws IOException {
//     return getGhRepositoryFromPRTrigger(run).getIssue(getPRNumberFromPRCause(run));
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/JobHelper.java
// public static GHIssue getGhPullRequest(final Run<?, ?> run) throws IOException {
//     return getGhRepositoryFromPRTrigger(run).getPullRequest(getPRNumberFromPRCause(run));
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/JobHelper.java
// public static int getPRNumberFromPRCause(final Run<?, ?> run) throws AbortException {
//     GitHubPRCause cause = ghPRCauseFromRun(run);
//     if (isNull(cause)) {
//         throw new AbortException("Can't get cause from run/build");
//     }
//     return cause.getNumber();
// }


import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Api;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.github.pullrequest.publishers.GitHubPRAbstractPublisher;
import org.jenkinsci.plugins.github.pullrequest.utils.PublisherErrorHandler;
import org.jenkinsci.plugins.github.pullrequest.utils.StatusVerifier;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.stapler.DataBoundConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhIssue;
import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhPullRequest;
import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getPRNumberFromPRCause;
package org.jenkinsci.plugins.github.pullrequest.publishers.impl;




/**
 * Closes pull request after build.
 *
 * @author Alina Karpovich
 */
public class GitHubPRClosePublisher extends GitHubPRAbstractPublisher {
    private static final Logger LOGGER = LoggerFactory.getLogger(GitHubPRClosePublisher.class);

    @DataBoundConstructor
    public GitHubPRClosePublisher(StatusVerifier statusVerifier, PublisherErrorHandler errorHandler) {
        super(statusVerifier, errorHandler);
    }

    @Override
    public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNull Launcher launcher,
                        @NonNull TaskListener listener) throws InterruptedException, IOException {
        if (getStatusVerifier() != null && !getStatusVerifier().isRunAllowed(run)) {
            return;
        }

        String publishedURL = getTriggerDescriptor().getPublishedURL();
        if (publishedURL != null && !publishedURL.isEmpty()) {
            try {
                if (getGhIssue(run).getState().equals(GHIssueState.OPEN)) {
                    try {
                        getGhPullRequest(run).close();
                    } catch (IOException ex) {
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/publishers/GitHubPRAbstractPublisher.java
// public abstract class GitHubPRAbstractPublisher extends Recorder implements SimpleBuildStep {
// 
//     @CheckForNull
//     private StatusVerifier statusVerifier;
// 
//     @CheckForNull
//     private PublisherErrorHandler errorHandler;
// 
//     public GitHubPRAbstractPublisher(StatusVerifier statusVerifier, PublisherErrorHandler errorHandler) {
//         this.statusVerifier = statusVerifier;
//         this.errorHandler = errorHandler;
//     }
// 
//     public StatusVerifier getStatusVerifier() {
//         return statusVerifier;
//     }
// 
//     public PublisherErrorHandler getErrorHandler() {
//         return errorHandler;
//     }
// 
//     protected void handlePublisherError(Run<?, ?> run) {
//         if (errorHandler != null) {
//             errorHandler.markBuildAfterError(run);
//         }
//     }
// 
//     public final BuildStepMonitor getRequiredMonitorService() {
//         return BuildStepMonitor.NONE;
//     }
// 
//     public final GitHubPRTrigger.DescriptorImpl getTriggerDescriptor() {
//         return (GitHubPRTrigger.DescriptorImpl) Jenkins.getInstance().getDescriptor(GitHubPRTrigger.class);
//     }
// 
//     public abstract static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
//         @Override
//         public final boolean isApplicable(final Class<? extends AbstractProject> jobType) {
//             return true;
//         }
// 
//         @Override
//         public abstract String getDisplayName();
//     }
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/PublisherErrorHandler.java
// public class PublisherErrorHandler extends AbstractDescribableImpl<PublisherErrorHandler> {
// 
//     private Result buildStatus;
// 
//     @DataBoundConstructor
//     public PublisherErrorHandler(Result buildStatus) {
//         this.buildStatus = buildStatus;
//     }
// 
//     public Result getBuildStatus() {
//         return buildStatus;
//     }
// 
//     public Result markBuildAfterError(Run<?, ?> run) {
//         run.setResult(buildStatus);
//         return buildStatus;
//     }
// 
//     @Symbol("statusOnPublisherError")
//     @Extension
//     public static class DescriptorImpl extends Descriptor<PublisherErrorHandler> {
//         @NonNull
//         @Override
//         public String getDisplayName() {
//             return "Set build status if publisher failed";
//         }
// 
//         public ListBoxModel doFillBuildStatusItems() {
//             ListBoxModel items = new ListBoxModel();
//             items.add(Result.UNSTABLE.toString());
//             items.add(Result.FAILURE.toString());
//             return items;
//         }
//     }
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/JobHelper.java
// public static GHIssue getGhIssue(final Run<?, ?> run) throws IOException {
//     return getGhRepositoryFromPRTrigger(run).getIssue(getPRNumberFromPRCause(run));
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/JobHelper.java
// public static GHIssue getGhPullRequest(final Run<?, ?> run) throws IOException {
//     return getGhRepositoryFromPRTrigger(run).getPullRequest(getPRNumberFromPRCause(run));
// }
// 
// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/utils/JobHelper.java
// public static int getPRNumberFromPRCause(final Run<?, ?> run) throws AbortException {
//     GitHubPRCause cause = ghPRCauseFromRun(run);
//     if (isNull(cause)) {
//         throw new AbortException("Can't get cause from run/build");
//     }
//     return cause.getNumber();
// }


// Path: github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/publishers/impl/GitHubPRClosePublisher.java
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Api;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.github.pullrequest.publishers.GitHubPRAbstractPublisher;
import org.jenkinsci.plugins.github.pullrequest.utils.PublisherErrorHandler;
import org.jenkinsci.plugins.github.pullrequest.utils.StatusVerifier;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.stapler.DataBoundConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhIssue;
import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhPullRequest;
import static org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getPRNumberFromPRCause;

package org.jenkinsci.plugins.github.pullrequest.publishers.impl;




/**
 * Closes pull request after build.
 *
 * @author Alina Karpovich
 */
public class GitHubPRClosePublisher extends GitHubPRAbstractPublisher {
    private static final Logger LOGGER = LoggerFactory.getLogger(GitHubPRClosePublisher.class);

    @DataBoundConstructor
    public GitHubPRClosePublisher(StatusVerifier statusVerifier, PublisherErrorHandler errorHandler) {
        super(statusVerifier, errorHandler);
    }

    @Override
    public void perform(@NonNull Run<?, ?> run, @NonNull FilePath workspace, @NonNull Launcher launcher,
                        @NonNull TaskListener listener) throws InterruptedException, IOException {
        if (getStatusVerifier() != null && !getStatusVerifier().isRunAllowed(run)) {
            return;
        }

        String publishedURL = getTriggerDescriptor().getPublishedURL();
        if (publishedURL != null && !publishedURL.isEmpty()) {
            try {
                if (getGhIssue(run).getState().equals(GHIssueState.OPEN)) {
                    try {
                        getGhPullRequest(run).close();
                    } catch (IOException ex) {
                        LOGGER.error("Couldn't close the pull request #{}:", getPRNumberFromPRCause(run), ex);