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.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     }
 61 
 62     Hash headHash() {
 63         return this.headHash;
 64     }
 65 
 66     String diffUrl() {
 67         return pr.getWebUrl() + ".diff";
 68     }
 69 
 70     String changeUrl() {
 71         return pr.getWebUrl() + "/files";
 72     }
 73 
 74     String changeUrl(Hash base, Hash head) {
 75         return pr.getWebUrl() + "/files/" + base.abbreviate() + ".." + head.abbreviate();
 76     }
 77 
 78     String fetchCommand() {
 79         var repoUrl = pr.repository().getWebUrl();
 80         return "git fetch " + repoUrl + " " + pr.getSourceRef() + ":pull/" + pr.getId();
 81     }
 82 
 83     String stats(Hash base, Hash head) {
 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() {
124         return pr;
125     }
126 }