1 /*
  2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.skara.bots.mlbridge;
 24 
 25 import org.openjdk.skara.host.PullRequest;
 26 import org.openjdk.skara.host.network.URIBuilder;
 27 import org.openjdk.skara.vcs.*;
 28 import org.openjdk.skara.vcs.openjdk.Issue;
 29 
 30 import java.io.*;
 31 import java.net.URI;
 32 import java.nio.file.Path;
 33 import java.util.Optional;
 34 import java.util.stream.Collectors;
 35 
 36 class PullRequestInstance {
 37     private final PullRequest pr;
 38     private final Repository localRepo;
 39     private final Hash targetHash;
 40     private final Hash headHash;
 41     private final Hash baseHash;
 42     private final URI issueTracker;
 43     private final String projectPrefix;
 44 
 45     PullRequestInstance(Path localRepoPath, PullRequest pr, URI issueTracker, String projectPrefix) {
 46         this.pr = pr;
 47         this.issueTracker = issueTracker;
 48         this.projectPrefix = projectPrefix;
 49 
 50         // Materialize the PR's target ref
 51         try {
 52             var repository = pr.repository();
 53             localRepo = Repository.materialize(localRepoPath, repository.getUrl(), pr.getTargetRef());
 54             targetHash = localRepo.fetch(repository.getUrl(), pr.getTargetRef());
 55             headHash = localRepo.fetch(repository.getUrl(), pr.getHeadHash().hex());
 56             baseHash = localRepo.mergeBase(targetHash, headHash);
 57         } catch (IOException e) {
 58             throw new UncheckedIOException(e);
 59         }
 60     }
 61 
 62     Repository localRepo() {
 63         return this.localRepo;
 64     }
 65 
 66     Hash baseHash() {
 67         return this.baseHash;
 68     }
 69 
 70     Hash headHash() {
 71         return this.headHash;
 72     }
 73 
 74     String diffUrl() {
 75         return pr.getWebUrl() + ".diff";
 76     }
 77 
 78     String changeUrl() {
 79         return pr.getWebUrl() + "/files";
 80     }
 81 
 82     String changeUrl(Hash base, Hash head) {
 83         return pr.getWebUrl() + "/files/" + base.abbreviate() + ".." + head.abbreviate();
 84     }
 85 
 86     String fetchCommand() {
 87         var repoUrl = pr.repository().getWebUrl();
 88         return "git fetch " + repoUrl + " " + pr.getSourceRef() + ":pull/" + pr.getId();
 89     }
 90 
 91     String stats(Hash base, Hash head) {
 92         try {
 93             var diff = localRepo.diff(base, head);
 94             var inserted = diff.added();
 95             var deleted = diff.removed();
 96             var modified = diff.modified();
 97             var linesChanged = inserted + deleted + modified;
 98             var filesChanged = diff.patches().size();
 99             return String.format("%d line%s in %d file%s changed: %d ins; %d del; %d mod",
100                                  linesChanged,
101                                  linesChanged == 1 ? "" : "s",
102                                  filesChanged,
103                                  filesChanged == 1 ? "" : "s",
104                                  inserted,
105                                  deleted,
106                                  modified);
107         } catch (IOException e) {
108             throw new UncheckedIOException(e);
109         }
110     }
111 
112     Optional<String> issueUrl() {
113         var issue = Issue.fromString(pr.getTitle());
114         return issue.map(value -> URIBuilder.base(issueTracker).appendPath(projectPrefix + "-" + value.id()).build().toString());
115     }
116 
117     @FunctionalInterface
118     interface CommitFormatter {
119         String format(Commit commit);
120     }
121 
122     String formatCommitMessages(Hash first, Hash last, CommitFormatter formatter) {
123         try (var commits = localRepo().commits(first.hex() + ".." + last.hex())) {
124             return commits.stream()
125                           .map(formatter::format)
126                           .collect(Collectors.joining("\n"));
127         } catch (IOException e) {
128             throw new UncheckedIOException(e);
129         }
130     }
131 
132     String id() {
133         return pr.getId();
134     }
135 
136     PullRequest pr() {
137         return pr;
138     }
139 }