<commit_before>/*

    Copyright 2015-2016 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
package com.github.terma.jenkins.githubprcoveragestatus;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 class CoberturaParser implements CoverageReportParser {

    private static String find(String string, String pattern) {
        Matcher matcher = Pattern.compile(pattern).matcher(string);
        if (matcher.find()) {
            return matcher.group(1);
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public float get(String coberturaFilePath) {
        try {
            String content = FileUtils.readFileToString(new File(coberturaFilePath));
            float lineRate = Float.parseFloat(find(content, "line-rate=\"([0-9.]+)\""));
            float branchRate = Float.parseFloat(find(content, "branch-rate=\"([0-9.]+)\""));
            return (lineRate / 2 + branchRate / 2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}<commit_msg>Check if Cobertura line-rate and branch-rate are valid.<commit_after>/*

    Copyright 2015-2016 <NAME>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
package com.github.terma.jenkins.githubprcoveragestatus;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 class CoberturaParser implements CoverageReportParser {

    private static String find(String string, String pattern) {
        Matcher matcher = Pattern.compile(pattern).matcher(string);
        if (matcher.find()) {
            return matcher.group(1);
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public float get(String coberturaFilePath) {
        try {
            String content = FileUtils.readFileToString(new File(coberturaFilePath));
            float lineRate = Float.parseFloat(find(content, "line-rate=\"([0-9.]+)\""));
            float branchRate = Float.parseFloat(find(content, "branch-rate=\"([0-9.]+)\""));
            float linesValid = Float.parseFloat(find(content, "lines-valid=\"([0-9.]+)\""));
            float branchesValid = Float.parseFloat(find(content, "branches-valid=\"([0-9.]+)\""));
            if (linesValid > 0 && branchesValid > 0) {
                return (lineRate / 2 + branchRate / 2);
            } else if (linesValid > 0) {
                return lineRate;
            } else if (branchesValid > 0) {
                return branchRate;
            } else {
                return 0;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
{'fraction_non_alphanumeric': 0.10512344040350412, 'fraction_numerical': 0.01247677196708256, 'mean_word_length': 2.768, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819955', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1002, 'n_words': 325}