< prev index next >

bots/mlbridge/src/main/java/org/openjdk/skara/bots/mlbridge/PullRequestInstance.java

Print this page

  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.vcs.*;

 27 
 28 import java.io.*;

 29 import java.nio.file.Path;

 30 import java.util.stream.Collectors;
 31 
 32 class PullRequestInstance {
 33     private final PullRequest pr;
 34     private final Repository localRepo;
 35     private final Hash targetHash;
 36     private final Hash headHash;
 37     private final Hash baseHash;


 38 
 39     PullRequestInstance(Path localRepoPath, PullRequest pr) {
 40         this.pr = pr;


 41 
 42         // Materialize the PR's target ref
 43         try {
 44             var repository = pr.repository();
 45             localRepo = Repository.materialize(localRepoPath, repository.getUrl(), pr.getTargetRef());
 46             targetHash = localRepo.fetch(repository.getUrl(), pr.getTargetRef());
 47             headHash = localRepo.fetch(repository.getUrl(), pr.getHeadHash().hex());
 48             baseHash = localRepo.mergeBase(targetHash, headHash);
 49         } catch (IOException e) {
 50             throw new UncheckedIOException(e);
 51         }
 52     }
 53 
 54     Repository localRepo() {
 55         return this.localRepo;
 56     }
 57 
 58     Hash baseHash() {
 59         return this.baseHash;
 60     }

 84         try {
 85             var diff = localRepo.diff(base, head);
 86             var inserted = diff.added();
 87             var deleted = diff.removed();
 88             var modified = diff.modified();
 89             var linesChanged = inserted + deleted + modified;
 90             var filesChanged = diff.patches().size();
 91             return String.format("%d line%s in %d file%s changed: %d ins; %d del; %d mod",
 92                                  linesChanged,
 93                                  linesChanged == 1 ? "" : "s",
 94                                  filesChanged,
 95                                  filesChanged == 1 ? "" : "s",
 96                                  inserted,
 97                                  deleted,
 98                                  modified);
 99         } catch (IOException e) {
100             throw new UncheckedIOException(e);
101         }
102     }
103 





104     @FunctionalInterface
105     interface CommitFormatter {
106         String format(Commit commit);
107     }
108 
109     String formatCommitMessages(Hash first, Hash last, CommitFormatter formatter) {
110         try (var commits = localRepo().commits(first.hex() + ".." + last.hex())) {
111             return commits.stream()
112                           .map(formatter::format)
113                           .collect(Collectors.joining("\n"));
114         } catch (IOException e) {
115             throw new UncheckedIOException(e);
116         }
117     }
118 
119     String id() {
120         return pr.getId();
121     }
122 
123     PullRequest pr() {

  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     }

 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() {
< prev index next >