<commit_before>/*
 * Copyright (c) 2013 M-Way Solutions GmbH
 * 
 * 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 org.jenkinsci.plugins.relution_publisher.builder;

import hudson.FilePath.FileCallable;
import hudson.Util;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.remoting.VirtualChannel;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.tools.ant.types.FileSet;
import org.jenkinsci.plugins.relution_publisher.configuration.global.Store;
import org.jenkinsci.plugins.relution_publisher.configuration.jobs.Publication;
import org.jenkinsci.plugins.relution_publisher.entities.Application;
import org.jenkinsci.plugins.relution_publisher.entities.Asset;
import org.jenkinsci.plugins.relution_publisher.entities.Version;
import org.jenkinsci.plugins.relution_publisher.logging.Log;
import org.jenkinsci.plugins.relution_publisher.net.Request;
import org.jenkinsci.plugins.relution_publisher.net.RequestFactory;
import org.jenkinsci.plugins.relution_publisher.net.responses.ApiResponse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;


public class ArtifactFileUploader implements FileCallable<Boolean> {

    private final AbstractBuild<?, ?> build;

    private final Publication         publication;
    private final Store               store;
    private final Log                 log;

    /**
     * Initializes a new instance of the {@link ArtifactFileUploader} class.
     * @param build The build that produced the artifact to be published.
     * @param publication The {@link Publication} that describes the artifact to be published.
     * @param store The {@link Store} to which the publication should be published.
     * @param log The {@link Log} to write log messages to.
     */
    public ArtifactFileUploader(final AbstractBuild<?, ?> build, final Publication publication, final Store store, final Log log) {

        this.build = build;

        this.publication = publication;
        this.store = store;
        this.log = log;
    }

    @Override
    public Boolean invoke(final File basePath, final VirtualChannel channel) throws IOException, InterruptedException {

        try {
            this.log.write(this, "Uploading build artifact...");
            final List<ApiResponse<Asset>> responses = this.uploadAsset(basePath, this.publication.getArtifactPath());

            if (this.isEmpty(responses)) {
                this.log.write(this, "No artifact to upload found.");
                this.build.setResult(Result.NOT_BUILT);
                return true;
            }

            for (final ApiResponse<Asset> response : responses) {
                this.uploadVersion(basePath, response);
            }

        } catch (final URISyntaxException e) {
            this.log.write(this, "Publication failed with %s", e.toString());
            this.build.setResult(Result.UNSTABLE);
        }
        return true;
    }

    private void uploadVersion(final File basePath, final ApiResponse<Asset> response)
            throws ClientProtocolException, URISyntaxException, IOException {

        if (!this.verifyAssetResponse(response)) {<commit_msg>Added javadoc for ArtifactFileUploader class.<commit_after>/*
 * Copyright (c) 2013 M-Way Solutions GmbH
 * 
 * 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 org.jenkinsci.plugins.relution_publisher.builder;

import hudson.FilePath.FileCallable;
import hudson.Util;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.remoting.VirtualChannel;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.tools.ant.types.FileSet;
import org.jenkinsci.plugins.relution_publisher.configuration.global.Store;
import org.jenkinsci.plugins.relution_publisher.configuration.jobs.Publication;
import org.jenkinsci.plugins.relution_publisher.entities.Application;
import org.jenkinsci.plugins.relution_publisher.entities.Asset;
import org.jenkinsci.plugins.relution_publisher.entities.Version;
import org.jenkinsci.plugins.relution_publisher.logging.Log;
import org.jenkinsci.plugins.relution_publisher.net.Request;
import org.jenkinsci.plugins.relution_publisher.net.RequestFactory;
import org.jenkinsci.plugins.relution_publisher.net.responses.ApiResponse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;


/**
 * Uploads build artifacts to a {@link Store} that has been specified in a Jenkins project's
 * post-build action, in the form of a {@link Publication}. 
 */
public class ArtifactFileUploader implements FileCallable<Boolean> {

    private static final long         serialVersionUID = 1L;

    private final AbstractBuild<?, ?> build;

    private final Publication         publication;
    private final Store               store;
    private final Log                 log;

    /**
     * Initializes a new instance of the {@link ArtifactFileUploader} class.
     * @param build The build that produced the artifact to be published.
     * @param publication The {@link Publication} that describes the artifact to be published.
     * @param store The {@link Store} to which the publication should be published.
     * @param log The {@link Log} to write log messages to.
     */
    public ArtifactFileUploader(final AbstractBuild<?, ?> build, final Publication publication, final Store store, final Log log) {

        this.build = build;

        this.publication = publication;
        this.store = store;
        this.log = log;
    }

    @Override
    public Boolean invoke(final File basePath, final VirtualChannel channel)
            throws IOException, InterruptedException {

        try {
            this.log.write(this, "Uploading build artifact...");
            final List<ApiResponse<Asset>> responses = this.uploadAsset(basePath, this.publication.getArtifactPath());

            if (this.isEmpty(responses)) {
                this.log.write(this, "No artifact to upload found.");
                this.build.setResult(Result.NOT_BUILT);
                return true;
            }

            for (final ApiResponse<Asset> response : responses) {
                this.uploadVersion(basePath, response);
            }

        } catch (final URISyntaxException e) {
            this.log.write(this, "Publication failed with %s", e.toString());
            this.build.setResult(Result.UNSTABLE);
        }
        return true;
    }

    private void uploadVersion(final File basePath, final ApiResponse<Asset> response)
            throws ClientProtocolException, URISyntaxException, IOException {

        if (!this.verifyAssetResponse(response)) {
{'fraction_non_alphanumeric': 0.08650260999254288, 'fraction_numerical': 0.0021128511061396966, 'mean_word_length': 3.4214285714285713, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '6351820', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2109, 'n_words': 634}