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         config.add("jbs=JDK");
 74 
 75         config.add("[checks]");
 76         var error = "error=blacklist,author,committer,reviewers,merge,hg-tag,message,issues,executable";
 77         var shouldCheckWhitespace = false;
 78         var checkWhitespace = old.get("whitespace");
 79         if (checkWhitespace == null || !checkWhitespace.asString().equals("lax")) {
 80             error += ",whitespace";
 81             shouldCheckWhitespace = true;
 82         }
 83         config.add(error);
 84 
 85         if (project.startsWith("jdk")) {
 86             config.add("[repository]");
 87 
 88             var tags = "tags=";
 89             var checkTags = old.get("tags");
 90             if (checkTags == null || !checkTags.asString().equals("lax")) {
 91                 var jdkTag = "(?:jdk-(?:[1-9]([0-9]*)(?:\\.(?:0|[1-9][0-9]*)){0,4})(?:\\+(?:(?:[0-9]+))|(?:-ga)))";
 92                 var jdkuTag = "(?:jdk[4-9](?:u\\d{1,3})?-(?:(?:b\\d{2,3})|(?:ga)))";
 93                 var hsTag = "(?:hs\\d\\d(?:\\.\\d{1,2})?-b\\d\\d)";
 94                 tags += jdkTag + "|" + jdkuTag + "|" + hsTag;
 95             } else {
 96                 tags += ".*";
 97             }
 98             config.add(tags);
 99 
100             var branches = "branches=";
101             var checkBranches = old.get("branches");
102             if (checkBranches != null && checkBranches.asString().equals("lax")) {
103                 branches += ".*\n";
104             }
105             config.add(branches);
106         }
107 
108         config.add("[census]");
109         config.add("version=0");
110         config.add("domain=openjdk.org");
111 
112         if (shouldCheckWhitespace) {
113             config.add("[checks \"whitespace\"]");
114             config.add("files=.*\\.cpp|.*\\.hpp|.*\\.c|.*\\.h|.*\\.java");
115         }
116 
117         config.add("[checks \"merge\"]");
118         config.add("message=Merge");
119 
120         config.add("[checks \"reviewers\"]");
121         config.add("minimum=1");
122         config.add("role=contributor");
123         config.add("ignore=duke");
124 
125         config.add("[checks \"committer\"]");
126         config.add("role=contributor");
127 
128         return INI.parse(config);
129     }
130 
131     public static JCheckConfiguration parse(List<String> lines) {
132         var ini = INI.parse(lines);
133         if (ini.sections().size() == 0) {
134             // This is an old-style jcheck conf with only a global section -
135             // translate to new configuration style before parsing.
136             return new JCheckConfiguration(convert(ini));
137         }
138         return new JCheckConfiguration(ini);
139     }
140 
141     public static JCheckConfiguration from(Repository r, Hash h, Path p) throws IOException {
142         return parse(r.lines(p, h).orElse(Collections.emptyList()));
143     }
144 
145     public static JCheckConfiguration from(Repository r, Hash h) throws IOException {
146         return from(r, h, Path.of(".jcheck", "conf"));
147     }
148 }