<commit_before>package net.nemerosa.ontrack.dsl

class Config {

    private final Ontrack ontrack

    Config(Ontrack ontrack) {
        this.ontrack = ontrack
    }

    /**
     * Gets the security settings
     */
    boolean getGrantProjectViewToAll() {
        def settings = ontrack.get('settings/general-security')
        return settings.grantProjectViewToAll
    }

    /**
     * Updates security settings
     */
    def setGrantProjectViewToAll(boolean grantProjectViewToAll) {
        ontrack.put(
                'settings/general-security',
                [
                        grantProjectViewToAll: grantProjectViewToAll
                ]
        )
    }

    /**
     * Creates or update a GitHub configuration.
     */

    def gitHub(String name) {
        gitHub([:], name)
    }

    def gitHub(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/github/configurations/create',
                params
        )
    }

    /**
     * Gets the list of all GitHub configuration names
     */
    List<String> getGitHub() {
        ontrack.get('extension/github/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Stash configurations.
     */

    def stash(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/stash/configurations/create',
                params
        )
    }

    List<String> getStash() {
        ontrack.get('extension/stash/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Creates or update a Git configuration
     */
    def git(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/git/configurations/create',
                params
        )
    }

    /**
     * Gets the list of all Git configuration names
     */
    List<String> getGit() {
        ontrack.get('extension/git/configurations/descriptors').resources.collect { it.id }
    }

    def svn(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/svn/configurations/create',
                params
        )
    }

    def getSvn() {
        ontrack.get('extension/svn/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Jenkins configuration
     */

    def jenkins(String name, String url, String user = '', String password = '') {
        ontrack.post(
                'extension/jenkins/configurations/create', [
                name    : name,
                url     : url,
                user    : user,
                password: password
        ])
    }

    List<String> getJenkins() {
        ontrack.get('extension/jenkins/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * JIRA configuration
     */

    def jira(String name, String url, String user = '', String password = '') {
        ontrack.post(
                'extension/jira/configurations/create', [
                name    : name,
                url     : url,
                user    : user,
                password: password
        ])
    }

    List<String> getJira() {
        ontrack.get('extension/jira/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Artifactory configuration
     */

    def artifactory(String name, String url, String user = '', String password = '') {
        ontrack.post(
                'extension/artifactory/configurations/create', [
                name    : name,
                url     : url,
                user    : user,
                password: password
        ])
    }

    List<String> getArtifactory() {
        ontrack.get('extension/artifactory/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Predefined validation stamps
     */

    List<PredefinedValidationStamp> getPredefinedValidationStamps() {
        ontrack.get('admin/predefinedValidationStamps').resources.collect {
            new PredefinedValidationStamp(
                    ontrack,
                    it
            )
        }
    }

    PredefinedValidationStamp predefinedValidationStamp(String name, String description = '', boolean getIfExists = false) {
        def node = predefinedValidationStamps.find { it.name == name }
        if (node) {
            if (getIfExists) {
                new PredefinedValidationStamp(
                        ontrack,
                        ontrack.get(node._self)
                )
            } else {
                throw new ObjectAlreadyExistsException("Predefined validation stamp ${name} already exists.")
            }
        } else {
            new PredefinedValidationStamp(
                    ontrack,
                    ontrack.post(ontrack.get('admin/predefinedValidationStamps')._create, [
                            name       : name,
                            description: description
                    ])
            )<commit_msg>#441 Predefined validation stamp correction<commit_after>package net.nemerosa.ontrack.dsl

class Config {

    private final Ontrack ontrack

    Config(Ontrack ontrack) {
        this.ontrack = ontrack
    }

    /**
     * Gets the security settings
     */
    boolean getGrantProjectViewToAll() {
        def settings = ontrack.get('settings/general-security')
        return settings.grantProjectViewToAll
    }

    /**
     * Updates security settings
     */
    def setGrantProjectViewToAll(boolean grantProjectViewToAll) {
        ontrack.put(
                'settings/general-security',
                [
                        grantProjectViewToAll: grantProjectViewToAll
                ]
        )
    }

    /**
     * Creates or update a GitHub configuration.
     */

    def gitHub(String name) {
        gitHub([:], name)
    }

    def gitHub(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/github/configurations/create',
                params
        )
    }

    /**
     * Gets the list of all GitHub configuration names
     */
    List<String> getGitHub() {
        ontrack.get('extension/github/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Stash configurations.
     */

    def stash(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/stash/configurations/create',
                params
        )
    }

    List<String> getStash() {
        ontrack.get('extension/stash/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Creates or update a Git configuration
     */
    def git(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/git/configurations/create',
                params
        )
    }

    /**
     * Gets the list of all Git configuration names
     */
    List<String> getGit() {
        ontrack.get('extension/git/configurations/descriptors').resources.collect { it.id }
    }

    def svn(Map<String, ?> parameters, String name) {
        def params = parameters + [name: name]
        ontrack.post(
                'extension/svn/configurations/create',
                params
        )
    }

    def getSvn() {
        ontrack.get('extension/svn/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Jenkins configuration
     */

    def jenkins(String name, String url, String user = '', String password = '') {
        ontrack.post(
                'extension/jenkins/configurations/create', [
                name    : name,
                url     : url,
                user    : user,
                password: password
        ])
    }

    List<String> getJenkins() {
        ontrack.get('extension/jenkins/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * JIRA configuration
     */

    def jira(String name, String url, String user = '', String password = '') {
        ontrack.post(
                'extension/jira/configurations/create', [
                name    : name,
                url     : url,
                user    : user,
                password: password
        ])
    }

    List<String> getJira() {
        ontrack.get('extension/jira/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Artifactory configuration
     */

    def artifactory(String name, String url, String user = '', String password = '') {
        ontrack.post(
                'extension/artifactory/configurations/create', [
                name    : name,
                url     : url,
                user    : user,
                password: password
        ])
    }

    List<String> getArtifactory() {
        ontrack.get('extension/artifactory/configurations/descriptors').resources.collect { it.id }
    }

    /**
     * Predefined validation stamps
     */

    List<PredefinedValidationStamp> getPredefinedValidationStamps() {
        ontrack.get('admin/predefinedValidationStamps').resources.collect {
            new PredefinedValidationStamp(
                    ontrack,
                    it
            )
        }
    }

    PredefinedValidationStamp predefinedValidationStamp(String name, String description = '', boolean getIfExists = false) {
        def vs = predefinedValidationStamps.find { it.name == name }
        if (vs) {
            if (getIfExists) {
                new PredefinedValidationStamp(
                        ontrack,
                        ontrack.get(vs.link('self'))
                )
            } else {
                throw new ObjectAlreadyExistsException("Predefined validation stamp ${name} already exists.")
            }
        } else {
            new PredefinedValidationStamp(
                    ontrack,
                    ontrack.post(ontrack.get('admin/predefinedValidationStamps')._create, [
                            name       : name,
                            description: description
                    ])
            )
{'fraction_non_alphanumeric': 0.1006319883325231, 'fraction_numerical': 0.0002916869227029655, 'mean_word_length': 1.5902795265676153, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3228066', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2625, 'n_words': 524}