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.forge.PullRequest; 26 import org.openjdk.skara.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.url(), 54 "+" + pr.targetRef() + ":mlbridge_prinstance_" + repository.name()); 55 targetHash = localRepo.fetch(repository.url(), pr.targetRef()); 56 headHash = localRepo.fetch(repository.url(), pr.headHash().hex()); 57 baseHash = localRepo.mergeBase(targetHash, headHash); 58 } catch (IOException e) { 59 throw new UncheckedIOException(e); 60 } 61 } 62 63 Repository localRepo() { 64 return this.localRepo; 65 } 66 67 Hash baseHash() { 68 return this.baseHash; 69 } 70 71 Hash headHash() { 72 return this.headHash; 73 } 74 75 String diffUrl() { 76 return pr.webUrl() + ".diff"; 77 } 78 79 String fetchCommand() { 80 var repoUrl = pr.repository().webUrl(); 81 return "git fetch " + repoUrl + " " + pr.sourceRef() + ":pull/" + pr.id(); 82 } 83 84 String stats(Hash base, Hash head) { 85 try { 86 var diff = localRepo.diff(base, head); 87 var inserted = diff.added(); 88 var deleted = diff.removed(); 89 var modified = diff.modified(); 90 var linesChanged = inserted + deleted + modified; 91 var filesChanged = diff.patches().size(); 92 return String.format("%d line%s in %d file%s changed: %d ins; %d del; %d mod", 93 linesChanged, 94 linesChanged == 1 ? "" : "s", 95 filesChanged, 96 filesChanged == 1 ? "" : "s", 97 inserted, 98 deleted, 99 modified); 100 } catch (IOException e) { 101 throw new UncheckedIOException(e); 102 } 103 } 104 105 Optional<String> issueUrl() { 106 var issue = Issue.fromString(pr.title()); 107 return issue.map(value -> URIBuilder.base(issueTracker).appendPath(projectPrefix + "-" + value.id()).build().toString()); 108 } 109 110 @FunctionalInterface 111 interface CommitFormatter { 112 String format(Commit commit); 113 } 114 115 String formatCommitMessages(Hash first, Hash last, CommitFormatter formatter) { 116 try (var commits = localRepo().commits(first.hex() + ".." + last.hex())) { 117 return commits.stream() 118 .map(formatter::format) 119 .collect(Collectors.joining("\n")); 120 } catch (IOException e) { 121 throw new UncheckedIOException(e); 122 } 123 } 124 125 String id() { 126 return pr.id(); 127 } 128 129 PullRequest pr() { 130 return pr; 131 } 132 }