// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/GhprbCommentAppender.java
// public interface GhprbCommentAppender {
//     String postBuildComment(Run<?, ?> build, TaskListener listener);
// }
// 
// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/GhprbCommitStatusException.java
// public class GhprbCommitStatusException extends Exception {
//     private static final long serialVersionUID = 6220095323686649609L;
// 
//     private final IOException exception;
// 
//     private final String message;
// 
//     private final GHCommitState state;
// 
//     private final int id;
// 
//     public GhprbCommitStatusException(IOException exception, GHCommitState state, String message, int id) {
//         this.exception = exception;
//         this.state = state;
//         this.message = message;
//         this.id = id;
//     }
// 
//     public IOException getException() {
//         return exception;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public GHCommitState getState() {
//         return state;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
// }
// 
// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/GhprbExtension.java
// public abstract class GhprbExtension extends AbstractDescribableImpl<GhprbExtension> {
// 
// }
// 
// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/comments/GhprbBuildStatus.java
// public class GhprbBuildStatus extends GhprbExtension implements GhprbCommentAppender, GhprbGlobalExtension, GhprbProjectExtension {
// 
//     @Extension
//     public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
// 
//     private final List<GhprbBuildResultMessage> messages;
// 
//     @DataBoundConstructor
//     public GhprbBuildStatus(List<GhprbBuildResultMessage> messages) {
//         this.messages = messages;
//     }
// 
//     public List<GhprbBuildResultMessage> getMessages() {
//         return messages == null ? new ArrayList<GhprbBuildResultMessage>(0) : messages;
//     }
// 
//     public String postBuildComment(Run<?, ?> build, TaskListener listener) {
//         StringBuilder msg = new StringBuilder();
// 
//         List<GhprbBuildResultMessage> messages = getDescriptor().getMessagesDefault(this);
// 
//         for (GhprbBuildResultMessage message : messages) {
//             msg.append(message.postBuildComment(build, listener));
//         }
// 
//         return msg.toString();
//     }
// 
//     @Override
//     public DescriptorImpl getDescriptor() {
//         return DESCRIPTOR;
//     }
// 
//     public static class DescriptorImpl extends GhprbExtensionDescriptor implements GhprbGlobalExtension, GhprbProjectExtension {
// 
//         @Override
//         public String getDisplayName() {
//             return "Build Status Messages";
//         }
// 
//         public List<GhprbBuildResultMessage> getMessagesDefault(GhprbBuildStatus local) {
//             return Ghprb.getDefaultValue(local, GhprbBuildStatus.class, "getMessages");
//         }
//     }
// }


import hudson.BulkChange;
import hudson.XmlFile;
import hudson.model.Items;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.Saveable;
import hudson.model.TaskListener;
import hudson.model.listeners.SaveableListener;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommentAppender;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatusException;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension;
import org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildStatus;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHEventPayload.IssueComment;
import org.kohsuke.github.GHEventPayload.PullRequest;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
        try {
            openPulls = repo.getPullRequests(GHIssueState.OPEN);
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, "Could not retrieve open pull requests.", ex);
            return;
        }


        Set<Integer> closedPulls = new HashSet<Integer>(pullRequests.keySet());

        for (GHPullRequest pr : openPulls) {
            if (pr.getHead() == null) { // Not sure if we need this, but leaving it for now.
                try {
                    pr = getActualPullRequest(pr.getNumber());
                } catch (IOException ex) {
                    LOGGER.log(Level.SEVERE, "Could not retrieve pr " + pr.getNumber(), ex);
                    return;
                }
            }
            check(pr);
            closedPulls.remove(pr.getNumber());
        }


        // remove closed pulls so we don't check them again
        for (Integer id : closedPulls) {
            pullRequests.remove(id);
        }
        try {
            this.save();
// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/GhprbCommentAppender.java
// public interface GhprbCommentAppender {
//     String postBuildComment(Run<?, ?> build, TaskListener listener);
// }
// 
// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/GhprbCommitStatusException.java
// public class GhprbCommitStatusException extends Exception {
//     private static final long serialVersionUID = 6220095323686649609L;
// 
//     private final IOException exception;
// 
//     private final String message;
// 
//     private final GHCommitState state;
// 
//     private final int id;
// 
//     public GhprbCommitStatusException(IOException exception, GHCommitState state, String message, int id) {
//         this.exception = exception;
//         this.state = state;
//         this.message = message;
//         this.id = id;
//     }
// 
//     public IOException getException() {
//         return exception;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public GHCommitState getState() {
//         return state;
//     }
// 
//     public int getId() {
//         return id;
//     }
// 
// }
// 
// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/GhprbExtension.java
// public abstract class GhprbExtension extends AbstractDescribableImpl<GhprbExtension> {
// 
// }
// 
// Path: src/main/java/org/jenkinsci/plugins/ghprb/extensions/comments/GhprbBuildStatus.java
// public class GhprbBuildStatus extends GhprbExtension implements GhprbCommentAppender, GhprbGlobalExtension, GhprbProjectExtension {
// 
//     @Extension
//     public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();
// 
//     private final List<GhprbBuildResultMessage> messages;
// 
//     @DataBoundConstructor
//     public GhprbBuildStatus(List<GhprbBuildResultMessage> messages) {
//         this.messages = messages;
//     }
// 
//     public List<GhprbBuildResultMessage> getMessages() {
//         return messages == null ? new ArrayList<GhprbBuildResultMessage>(0) : messages;
//     }
// 
//     public String postBuildComment(Run<?, ?> build, TaskListener listener) {
//         StringBuilder msg = new StringBuilder();
// 
//         List<GhprbBuildResultMessage> messages = getDescriptor().getMessagesDefault(this);
// 
//         for (GhprbBuildResultMessage message : messages) {
//             msg.append(message.postBuildComment(build, listener));
//         }
// 
//         return msg.toString();
//     }
// 
//     @Override
//     public DescriptorImpl getDescriptor() {
//         return DESCRIPTOR;
//     }
// 
//     public static class DescriptorImpl extends GhprbExtensionDescriptor implements GhprbGlobalExtension, GhprbProjectExtension {
// 
//         @Override
//         public String getDisplayName() {
//             return "Build Status Messages";
//         }
// 
//         public List<GhprbBuildResultMessage> getMessagesDefault(GhprbBuildStatus local) {
//             return Ghprb.getDefaultValue(local, GhprbBuildStatus.class, "getMessages");
//         }
//     }
// }


// Path: src/main/java/org/jenkinsci/plugins/ghprb/GhprbRepository.java
import hudson.BulkChange;
import hudson.XmlFile;
import hudson.model.Items;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.Saveable;
import hudson.model.TaskListener;
import hudson.model.listeners.SaveableListener;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommentAppender;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatusException;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension;
import org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildStatus;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHEventPayload.IssueComment;
import org.kohsuke.github.GHEventPayload.PullRequest;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

        try {
            openPulls = repo.getPullRequests(GHIssueState.OPEN);
        } catch (IOException ex) {
            LOGGER.log(Level.SEVERE, "Could not retrieve open pull requests.", ex);
            return;
        }


        Set<Integer> closedPulls = new HashSet<Integer>(pullRequests.keySet());

        for (GHPullRequest pr : openPulls) {
            if (pr.getHead() == null) { // Not sure if we need this, but leaving it for now.
                try {
                    pr = getActualPullRequest(pr.getNumber());
                } catch (IOException ex) {
                    LOGGER.log(Level.SEVERE, "Could not retrieve pr " + pr.getNumber(), ex);
                    return;
                }
            }
            check(pr);
            closedPulls.remove(pr.getNumber());
        }


        // remove closed pulls so we don't check them again
        for (Integer id : closedPulls) {
            pullRequests.remove(id);
        }
        try {
            this.save();
        } catch (IOException e) {