<commit_before>/*
 * Copyright 2013 the original author or authors.
 *
 * 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.gradle.plugin.resolve.internal;

import com.jfrog.bintray.client.api.model.Pkg;
import com.jfrog.bintray.client.impl.BintrayClient;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.dsl.DependencyHandler;

import java.util.List;

import static java.util.Collections.sort;

public class JCenterPluginMapper implements ModuleMappingPluginResolver.Mapper {
    private static final String GRADLE_PLUGINS_ORG = "gradle-plugins-development";
    private static final String GRADLE_PLUGINS_REPO = "gradle-plugins";
    private static final String PLUGIN_ID_ATTRIBUTE_NAME = "gradle-plugin-id";
    public static final String BINTRAY_USER_TODO_REMOVE="bintray-user";
    public static final String BINTRAY_APIKEY_TODO_REMOVE="bintray-key";

    public Dependency map(PluginRequest request, DependencyHandler dependencyHandler) {
        String pluginId = request.getId();
        //TODO should work anonymously, ATM attribute search requires user
        List<Pkg> results = BintrayClient.create(System.getProperty(BINTRAY_USER_TODO_REMOVE), System.getProperty(BINTRAY_APIKEY_TODO_REMOVE)).subject(GRADLE_PLUGINS_ORG).repository(GRADLE_PLUGINS_REPO).searchForPackage().byAttributeName(PLUGIN_ID_ATTRIBUTE_NAME).equals(pluginId).search();
        if(results.isEmpty()){
            throw new InvalidPluginRequest("No plugins found for plugin id "+pluginId);
        }
        if(results.size() > 1) {
            throw new InvalidPluginRequest("Found more than one plugin for plugin id "+pluginId);
        }
        Pkg pluginPackage = results.get(0);
        List<String> systemIds = pluginPackage.systemIds();
        if(systemIds.isEmpty()){
            throw new InvalidPluginRequest("No artifacts in maven layout found for plugin id"+pluginId);
        }
        String version = request.getVersion();
        if(version == null){
            List<String> versions = pluginPackage.versions();
            if(versions.isEmpty()) {
                throw new InvalidPluginRequest("No versions found for plugin id "+pluginId);
            }
            sort(versions);
            version = versions.get(versions.size() -1);
        }
        return dependencyHandler.create(systemIds.get(0) + ":" + version);
    }<commit_msg>take latest version from package information instead of relying on natural sorting<commit_after>/*
 * Copyright 2013 the original author or authors.
 *
 * 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.gradle.plugin.resolve.internal;

import com.jfrog.bintray.client.api.model.Pkg;
import com.jfrog.bintray.client.impl.BintrayClient;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.dsl.DependencyHandler;

import java.util.List;

public class JCenterPluginMapper implements ModuleMappingPluginResolver.Mapper {
    private static final String GRADLE_PLUGINS_ORG = "gradle-plugins-development";
    private static final String GRADLE_PLUGINS_REPO = "gradle-plugins";
    private static final String PLUGIN_ID_ATTRIBUTE_NAME = "gradle-plugin-id";
    public static final String BINTRAY_USER_TODO_REMOVE = "bintray-user";
    public static final String BINTRAY_APIKEY_TODO_REMOVE = "bintray-key";

    public Dependency map(PluginRequest request, DependencyHandler dependencyHandler) {
        String pluginId = request.getId();
        //TODO should work anonymously, ATM attribute search requires user
        List<Pkg> results = BintrayClient.create(System.getProperty(BINTRAY_USER_TODO_REMOVE), System.getProperty(BINTRAY_APIKEY_TODO_REMOVE)).subject(GRADLE_PLUGINS_ORG).repository(GRADLE_PLUGINS_REPO).searchForPackage().byAttributeName(PLUGIN_ID_ATTRIBUTE_NAME).equals(pluginId).search();
        if (results.isEmpty()) {
            throw new InvalidPluginRequest("No plugins found for plugin id " + pluginId);
        }
        if (results.size() > 1) {
            throw new InvalidPluginRequest("Found more than one plugin for plugin id " + pluginId);
        }
        Pkg pluginPackage = results.get(0);
        List<String> systemIds = pluginPackage.systemIds();
        if (systemIds.isEmpty()) {
            throw new InvalidPluginRequest("No artifacts in maven layout found for plugin id" + pluginId);
        }
        String version = request.getVersion();
        if (version == null) {
            version = pluginPackage.latestVersion();
        }
        return dependencyHandler.create(systemIds.get(0) + ":" + version);
    }
{'fraction_non_alphanumeric': 0.08822476735862562, 'fraction_numerical': 0.0041159627773801, 'mean_word_length': 3.736440677966102, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 10, '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': '7064324', 'n_tokens_mistral': 1547, 'n_tokens_neox': 1510, 'n_words': 444}