1 /*
  2  * Copyright (c) 2018, 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.jcheck;
 24 
 25 import org.openjdk.skara.ini.INI;
 26 import org.openjdk.skara.vcs.*;
 27 
 28 import java.io.IOException;
 29 import java.nio.file.Path;
 30 import java.util.*;
 31 
 32 public class JCheckConfiguration {
 33     private GeneralConfiguration general;
 34     private RepositoryConfiguration repository;
 35     private CensusConfiguration census;
 36     private ChecksConfiguration checks;
 37 
 38     private JCheckConfiguration(INI ini) {
 39         general = GeneralConfiguration.parse(ini.section(GeneralConfiguration.name()));
 40         if (general.project() == null) {
 41             throw new IllegalArgumentException("general.project must be specified");
 42         }
 43         repository = RepositoryConfiguration.parse(ini.section(RepositoryConfiguration.name()));
 44         census = CensusConfiguration.parse(ini.section(CensusConfiguration.name()));
 45         checks = ChecksConfiguration.parse(ini.section(ChecksConfiguration.name()));
 46     }
 47 
 48     public GeneralConfiguration general() {
 49         return general;
 50     }
 51 
 52     public RepositoryConfiguration repository() {
 53         return repository;
 54     }
 55 
 56     public CensusConfiguration census() {
 57         return census;
 58     }
 59 
 60     public ChecksConfiguration checks() {
 61         return checks;
 62     }
 63 
 64     private static INI convert(INI old) {
 65         var project = old.get("project").asString();
 66         if (project == null) {
 67             throw new IllegalArgumentException("'project' must be specified");
 68         }
 69 
 70         var config = new ArrayList<String>();
 71         config.add("[general]");
 72         config.add("project=" + project);
 73 
 74         config.add("[checks]");
 75         var error = "error=blacklist,author,committer,reviewers,merge,hg-tag,message,issues,executable";
 76         var shouldCheckWhitespace = false;
 77         var checkWhitespace = old.get("whitespace");
 78         if (checkWhitespace == null || !checkWhitespace.equals("lax")) {
 79             error += ",whitespace";
 80             shouldCheckWhitespace = true;
 81         }
 82         config.add(error);
 83 
 84         if (project.startsWith("jdk")) {
 85             config.add("[repository]");
 86 
 87             var tags = "tags=";
 88             var checkTags = old.get("tags");
 89             if (checkTags == null || !checkTags.equals("lax")) {
 90                 var jdkTag = "(?:jdk-(?:[1-9]([0-9]*)(?:\\.(?:0|[1-9][0-9]*)){0,4})(?:\\+(?:(?:[0-9]+))|(?:-ga)))";
 91                 var jdkuTag = "(?:jdk[4-9](?:u\\d{1,3})?-(?:(?:b\\d{2,3})|(?:ga)))";
 92                 var hsTag = "(?:hs\\d\\d(?:\\.\\d{1,2})?-b\\d\\d)";
 93                 tags += jdkTag + "|" + jdkuTag + "|" + hsTag;
 94             } else {
 95                 tags += ".*";
 96             }
 97             config.add(tags);
 98 
 99             var branches = "branches=";
100             var checkBranches = old.get("branches");
101             if (checkBranches != null && checkBranches.equals("lax")) {
102                 branches += ".*\n";
103             }
104             config.add(branches);
105         }
106 
107         config.add("[census]");
108         config.add("version=0");
109         config.add("domain=openjdk.org");
110 
111         if (shouldCheckWhitespace) {
112             config.add("[checks \"whitespace\"]");
113             config.add("files=.*\\.cpp|.*\\.hpp|.*\\.c|.*\\.h|.*\\.java");
114         }
115 
116         config.add("[checks \"merge\"]");
117         config.add("message=Merge");
118 
119         config.add("[checks \"reviewers\"]");
120         config.add("minimum=1");
121         config.add("role=contributor");
122         config.add("ignore=duke");
123 
124         config.add("[checks \"committer\"]");
125         config.add("role=contributor");
126 
127         return INI.parse(config);
128     }
129 
130     public static JCheckConfiguration parse(List<String> lines) {
131         var ini = INI.parse(lines);
132         if (ini.sections().size() == 0) {
133             // This is an old-style jcheck conf with only a global section -
134             // translate to new configuration style before parsing.
135             return new JCheckConfiguration(convert(ini));
136         }
137         return new JCheckConfiguration(ini);
138     }
139 
140     public static JCheckConfiguration from(Repository r, Hash h, Path p) throws IOException {
141         return parse(r.lines(p, h).orElse(Collections.emptyList()));
142     }
143 
144     public static JCheckConfiguration from(Repository r, Hash h) throws IOException {
145         return from(r, h, Path.of(".jcheck", "conf"));
146     }
147 }