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.pr;
 24 
 25 import org.openjdk.skara.census.*;
 26 import org.openjdk.skara.forge.*;
 27 import org.openjdk.skara.jcheck.JCheckConfiguration;
 28 import org.openjdk.skara.vcs.*;
 29 
 30 import java.io.*;
 31 import java.net.URLEncoder;
 32 import java.nio.charset.StandardCharsets;
 33 import java.nio.file.Path;
 34 import java.util.Optional;
 35 import java.util.stream.Collectors;
 36 
 37 class CensusInstance {
 38     private final Census census;
 39     private final JCheckConfiguration configuration;
 40     private final Project project;
 41     private final Namespace namespace;
 42 
 43     private CensusInstance(Census census, JCheckConfiguration configuration, Project project, Namespace namespace) {
 44         this.census = census;
 45         this.configuration = configuration;
 46         this.project = project;
 47         this.namespace = namespace;
 48     }
 49 
 50     private static Repository initialize(HostedRepository repo, String ref, Path folder) {
 51         try {
 52             return Repository.materialize(folder, repo.url(), "+" + ref + ":pr_census_" + repo.name());
 53         } catch (IOException e) {
 54             throw new RuntimeException("Failed to retrieve census to " + folder, e);
 55         }
 56     }
 57 
 58     private static Project project(JCheckConfiguration configuration, Census census) {
 59         var project = census.project(configuration.general().project());
 60 
 61         if (project == null) {
 62             throw new RuntimeException("Project not found in census: " + configuration.general().project());
 63         }
 64 
 65         return project;
 66     }
 67 
 68     private static Namespace namespace(Census census, String hostNamespace) {
 69         //var namespace = census.namespace(pr.repository().getNamespace());
 70         var namespace = census.namespace(hostNamespace);
 71         if (namespace == null) {
 72             throw new RuntimeException("Namespace not found in census: " + hostNamespace);
 73         }
 74 
 75         return namespace;
 76     }
 77 
 78     private static JCheckConfiguration configuration(HostedRepository remoteRepo, String ref) {
 79         var confFile = remoteRepo.fileContents(".jcheck/conf", ref);
 80         return JCheckConfiguration.parse(confFile.lines().collect(Collectors.toList()));
 81     }
 82 
 83     static CensusInstance create(HostedRepository censusRepo, String censusRef, Path folder, PullRequest pr) {
 84         var repoName = censusRepo.url().getHost() + "/" + censusRepo.name();
 85         var repoFolder = folder.resolve(URLEncoder.encode(repoName, StandardCharsets.UTF_8));
 86         try {
 87             var localRepo = Repository.get(repoFolder)
 88                                       .or(() -> Optional.of(initialize(censusRepo, censusRef, repoFolder)))
 89                                       .orElseThrow();
 90             var hash = localRepo.fetch(censusRepo.url(), censusRef);
 91             localRepo.checkout(hash, true);
 92         } catch (IOException e) {
 93             initialize(censusRepo, censusRef, repoFolder);
 94         }
 95 
 96         try {
 97             var configuration = configuration(pr.repository(), pr.targetRef());
 98             var census = Census.parse(repoFolder);
 99             var project = project(configuration, census);
100             var namespace = namespace(census, pr.repository().namespace());
101             return new CensusInstance(census, configuration, project, namespace);
102         } catch (IOException e) {
103             throw new UncheckedIOException("Cannot parse census at " + repoFolder, e);
104         }
105     }
106 
107     Census census() {
108         return census;
109     }
110 
111     JCheckConfiguration configuration() {
112         return configuration;
113     }
114 
115     Project project() {
116         return project;
117     }
118 
119     Namespace namespace() {
120         return namespace;
121     }
122 }