<commit_before>package net.nemerosa.ontrack.jenkins;

import com.fasterxml.jackson.databind.JsonNode;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import net.nemerosa.ontrack.jenkins.support.client.OntrackResource;
import net.nemerosa.ontrack.jenkins.support.json.JsonUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;

import static net.nemerosa.ontrack.jenkins.OntrackPluginSupport.expand;
import static net.nemerosa.ontrack.jenkins.support.client.OntrackClient.forBuild;

/**
 * Allows to notify for a build.
 */
public class OntrackBuildPropertySetter extends AbstractOntrackNotifier {

    private final String project;
    private final String branch;
    private final String build;
    private final String typeName;
    private final String value;

    @DataBoundConstructor
    public OntrackBuildPropertySetter(String project, String branch, String build, String typeName, String value) {
        this.project = project;
        this.branch = branch;
        this.build = build;
        this.typeName = typeName;
        this.value = value;
    }

    public String getProject() {
        return project;
    }

    public String getBranch() {
        return branch;
    }

    public String getBuild() {
        return build;
    }

    public String getTypeName() {
        return typeName;
    }

    public String getValue() {
        return value;
    }

    @Override
    public boolean perform(final AbstractBuild<?, ?> theBuild, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
        // Only triggers in case of success
        if (theBuild.getResult().isBetterOrEqualTo(Result.SUCCESS)) {
            // Expands the expressions into actual values
            final String projectName = expand(project, theBuild, listener);
            final String branchName = expand(branch, theBuild, listener);
            final String buildName = expand(build, theBuild, listener);
            final String typeName = expand(this.typeName, theBuild, listener);
            final String theValue = expand(value, theBuild, listener);
            // Logging of parameters
            listener.getLogger().format(
                    "Setting property %s = %s on build %s for branch %s of project %s%n",
                    typeName, theValue,
                    buildName, branchName, projectName);
            // For the build
            JsonNode buildProperties = forBuild(listener.getLogger(), projectName, branchName, buildName).on("_properties").get();
            // Looking for property
            JsonNode propertyDef = null;
            for (JsonNode propertyDefCandidate : buildProperties.path("resources")) {
                String candidateTypeName = propertyDefCandidate.path("typeDescriptor").path("typeName").asText();
                if (typeName.equals(candidateTypeName)) {
                    propertyDef = propertyDefCandidate;
                }
            }
            // No property found
            if (propertyDef == null) {
                listener.getLogger().format("Could not find property %s for build %s.%n", typeName, buildName);
                theBuild.setResult(Result.FAILURE);
            }
            // Checks if the property is editable
            else if (!propertyDef.path("editable").asBoolean()) {
                listener.getLogger().format("Property %s on build %s is not editable.%n", typeName, buildName);
                theBuild.setResult(Result.FAILURE);
            }
            // Setting the property
            else {
                new OntrackResource(listener.getLogger(), propertyDef).on("_update").post(
                        // FIXME Need to be a custom JSON, or a list of key/values
                        JsonUtils.object()
                                .with("name", theValue)
                                .end()
                );
            }
        }
        // OK
        return true;
    }<commit_msg>#12 Checks the `_update` link<commit_after>package net.nemerosa.ontrack.jenkins;

import com.fasterxml.jackson.databind.JsonNode;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import net.nemerosa.ontrack.jenkins.support.client.OntrackResource;
import net.nemerosa.ontrack.jenkins.support.json.JsonUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;

import static net.nemerosa.ontrack.jenkins.OntrackPluginSupport.expand;
import static net.nemerosa.ontrack.jenkins.support.client.OntrackClient.forBuild;

/**
 * Allows to notify for a build.
 */
public class OntrackBuildPropertySetter extends AbstractOntrackNotifier {

    private final String project;
    private final String branch;
    private final String build;
    private final String typeName;
    private final String value;

    @DataBoundConstructor
    public OntrackBuildPropertySetter(String project, String branch, String build, String typeName, String value) {
        this.project = project;
        this.branch = branch;
        this.build = build;
        this.typeName = typeName;
        this.value = value;
    }

    public String getProject() {
        return project;
    }

    public String getBranch() {
        return branch;
    }

    public String getBuild() {
        return build;
    }

    public String getTypeName() {
        return typeName;
    }

    public String getValue() {
        return value;
    }

    @Override
    public boolean perform(final AbstractBuild<?, ?> theBuild, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
        // Only triggers in case of success
        if (theBuild.getResult().isBetterOrEqualTo(Result.SUCCESS)) {
            // Expands the expressions into actual values
            final String projectName = expand(project, theBuild, listener);
            final String branchName = expand(branch, theBuild, listener);
            final String buildName = expand(build, theBuild, listener);
            final String typeName = expand(this.typeName, theBuild, listener);
            final String theValue = expand(value, theBuild, listener);
            // Logging of parameters
            listener.getLogger().format(
                    "Setting property %s = %s on build %s for branch %s of project %s%n",
                    typeName, theValue,
                    buildName, branchName, projectName);
            // For the build
            JsonNode buildProperties = forBuild(listener.getLogger(), projectName, branchName, buildName).on("_properties").get();
            // Looking for property
            JsonNode propertyDef = null;
            for (JsonNode propertyDefCandidate : buildProperties.path("resources")) {
                String candidateTypeName = propertyDefCandidate.path("typeDescriptor").path("typeName").asText();
                if (typeName.equals(candidateTypeName)) {
                    propertyDef = propertyDefCandidate;
                }
            }
            // No property found
            if (propertyDef == null) {
                listener.getLogger().format("Could not find property %s for build %s.%n", typeName, buildName);
                theBuild.setResult(Result.FAILURE);
            }
            // Checks if the property is editable
            else if (!propertyDef.has("_update")) {
                listener.getLogger().format("Property %s on build %s is not editable.%n", typeName, buildName);
                theBuild.setResult(Result.FAILURE);
            }
            // Setting the property
            else {
                new OntrackResource(listener.getLogger(), propertyDef).on("_update").post(
                        // FIXME Need to be a custom JSON, or a list of key/values
                        JsonUtils.object()
                                .with("name", theValue)
                                .end()
                );
            }
        }
        // OK
        return true;
    }
{'fraction_non_alphanumeric': 0.08903781713738632, 'fraction_numerical': 0.00023934897079942556, 'mean_word_length': 2.261904761904762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7393948', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2057, 'n_words': 564}