001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.release.plugin.slsa.v1_2;
018
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022import java.util.Objects;
023
024import com.fasterxml.jackson.annotation.JsonProperty;
025
026/**
027 * Inputs that define the build: the build type, external and internal parameters, and resolved dependencies.
028 *
029 * <p>
030 * Specifies everything that influenced the build output. Together with {@link RunDetails}, it forms the complete
031 * {@link Provenance} record.
032 * </p>
033 *
034 * @see <a href="https://slsa.dev/spec/v1.2">SLSA v1.2 Specification</a>
035 * @since 1.10.0
036 */
037public class BuildDefinition {
038
039    /**
040     * URI indicating what type of build was performed.
041     */
042    @JsonProperty("buildType")
043    private String buildType = "https://commons.apache.org/proper/commons-release-plugin/slsa/v0.1.0";
044
045    /**
046     * Inputs passed to the build.
047     */
048    @JsonProperty("externalParameters")
049    private Map<String, Object> externalParameters = new HashMap<>();
050
051    /**
052     * Parameters set by the build platform.
053     */
054    @JsonProperty("internalParameters")
055    private Map<String, Object> internalParameters = new HashMap<>();
056
057    /**
058     * Artifacts the build depends on, specified by URI and digest.
059     */
060    @JsonProperty("resolvedDependencies")
061    private List<ResourceDescriptor> resolvedDependencies;
062
063    /**
064     * Creates a new BuildDefinition instance with the default build type.
065     */
066    public BuildDefinition() {
067    }
068
069    /**
070     * Creates a new BuildDefinition with the given build type and external parameters.
071     *
072     * @param buildType          URI indicating what type of build was performed.
073     * @param externalParameters inputs passed to the build.
074     */
075    public BuildDefinition(final String buildType, final Map<String, Object> externalParameters) {
076        this.buildType = buildType;
077        this.externalParameters = externalParameters;
078    }
079
080    @Override
081    public boolean equals(final Object o) {
082        if (this == o) {
083            return true;
084        }
085        if (o == null || getClass() != o.getClass()) {
086            return false;
087        }
088        final BuildDefinition that = (BuildDefinition) o;
089        return Objects.equals(buildType, that.buildType) && Objects.equals(externalParameters, that.externalParameters) && Objects.equals(internalParameters,
090                that.internalParameters) && Objects.equals(resolvedDependencies, that.resolvedDependencies);
091    }
092
093    /**
094     * Gets the URI indicating what type of build was performed.
095     *
096     * <p>
097     * Determines the meaning of {@code externalParameters} and {@code internalParameters}.
098     * </p>
099     *
100     * @return The build type URI.
101     */
102    public String getBuildType() {
103        return buildType;
104    }
105
106    /**
107     * Gets the inputs passed to the build, such as command-line arguments or environment variables.
108     *
109     * @return The external parameters map, or {@code null} if not set.
110     */
111    public Map<String, Object> getExternalParameters() {
112        return externalParameters;
113    }
114
115    /**
116     * Gets the parameters set by the build platform.
117     *
118     * @return The internal parameters map, or {@code null} if not set.
119     */
120    public Map<String, Object> getInternalParameters() {
121        return internalParameters;
122    }
123
124    /**
125     * Gets the materials that influenced the build.
126     *
127     * <p>
128     * The dependency list is considered incomplete unless resolved materials are present.
129     * </p>
130     *
131     * @return The list of resolved dependencies, or {@code null} if not set.
132     */
133    public List<ResourceDescriptor> getResolvedDependencies() {
134        return resolvedDependencies;
135    }
136
137    @Override
138    public int hashCode() {
139        return Objects.hash(buildType, externalParameters, internalParameters, resolvedDependencies);
140    }
141
142    /**
143     * Sets the URI indicating what type of build was performed.
144     *
145     * @param buildType The build type URI.
146     * @return this for chaining.
147     */
148    public BuildDefinition setBuildType(final String buildType) {
149        this.buildType = buildType;
150        return this;
151    }
152
153    /**
154     * Sets the inputs passed to the build.
155     *
156     * @param externalParameters The external parameters map.
157     * @return this for chaining.
158     */
159    public BuildDefinition setExternalParameters(final Map<String, Object> externalParameters) {
160        this.externalParameters = externalParameters;
161        return this;
162    }
163
164    /**
165     * Sets the parameters set by the build platform.
166     *
167     * @param internalParameters The internal parameters map.
168     * @return this for chaining.
169     */
170    public BuildDefinition setInternalParameters(final Map<String, Object> internalParameters) {
171        this.internalParameters = internalParameters;
172        return this;
173    }
174
175    /**
176     * Sets the materials that influenced the build.
177     *
178     * @param resolvedDependencies The list of resolved dependencies.
179     * @return this for chaining.
180     */
181    public BuildDefinition setResolvedDependencies(final List<ResourceDescriptor> resolvedDependencies) {
182        this.resolvedDependencies = resolvedDependencies;
183        return this;
184    }
185
186    @Override
187    public String toString() {
188        return "BuildDefinition{buildType='" + buildType
189                + "', externalParameters=" + externalParameters
190                + ", internalParameters=" + internalParameters
191                + ", resolvedDependencies=" + resolvedDependencies + '}';
192    }
193}