<commit_before>/*
 * Copyright 2015 Netflix, Inc.
 *
 * 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 com.netflix.spinnaker.gradle.ospackage

import com.jfrog.bintray.gradle.BintrayExtension
import com.jfrog.bintray.gradle.BintrayHttpClientFactory
import com.jfrog.bintray.gradle.BintrayPlugin
import com.jfrog.bintray.gradle.BintrayUploadTask
import com.netflix.gradle.plugins.deb.Deb
import groovy.transform.Canonical
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.tasks.Upload

import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.POST

/**
 * This is a workaround for:
 * https://github.com/bintray/gradle-bintray-plugin/issues/84
 *
 * and as such should die-in-a-fire if that issue gets resolved
 */
class OspackageBintrayPublishPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.plugins.apply(BintrayPlugin)

        def packageExtension = project.extensions.create('bintrayPackage', OspackageBintrayExtension)

        project.tasks.withType(Deb) { Deb deb ->
            def spinnakerDebians = project.configurations.maybeCreate('spinnakerDebians')
            project.artifacts.add('spinnakerDebians', deb)
            def extension = (BintrayExtension) project.extensions.getByName('bintray')
            String publishName = 'publish' + deb.name.charAt(0).toUpperCase() + deb.name.substring(1)
            def buildDebPublish = project.tasks.create(publishName, BintrayUploadTask) { BintrayUploadTask task ->
                task.with {
                    apiUrl = extension.apiUrl
                    user = extension.user
                    apiKey = extension.key
                    configurations = ['spinnakerDebians']
                    publish = extension.publish
                    dryRun = extension.dryRun
                    userOrg = extension.pkg.userOrg ?: extension.user
                    repoName = packageExtension.packageRepo ?: extension.pkg.repo
                    packageName = extension.pkg.name
                    packageDesc = extension.pkg.desc
                    packageWebsiteUrl = extension.pkg.websiteUrl
                    packageIssueTrackerUrl = extension.pkg.issueTrackerUrl
                    packageVcsUrl = extension.pkg.vcsUrl
                    packageLicenses = extension.pkg.licenses
                    packageLabels = extension.pkg.labels
                    packageAttributes = extension.pkg.attributes
                    packagePublicDownloadNumbers = extension.pkg.publicDownloadNumbers
                    versionName = extension.pkg.version.name ?: project.version
                    versionDesc = extension.pkg.version.desc
                    versionReleased = extension.pkg.version.released
                    versionVcsTag = extension.pkg.version.vcsTag ?: project.version
                    versionAttributes = extension.pkg.version.attributes
                    signVersion = extension.pkg.version.gpg.sign
                    gpgPassphrase = extension.pkg.version.gpg.passphrase
                    syncToMavenCentral = false
                }
            }

            buildDebPublish.mustRunAfter('build')
            buildDebPublish.dependsOn(deb)
            buildDebPublish.dependsOn(spinnakerDebians.allArtifacts)
            Upload installTask = project.tasks.withType(Upload)?.findByName('install')
            if (installTask) {
                buildDebPublish.dependsOn(installTask)
            }
            buildDebPublish.group = BintrayUploadTask.GROUP
            project.rootProject.tasks.release.dependsOn(buildDebPublish)
            def publishAllVersions = project.rootProject.tasks.maybeCreate('publishAllBintrayVersions')
            publishAllVersions.doFirst {
                def publishes = project.tasks.findAll { it instanceof BintrayUploadTask }.collect { BintrayUploadTask task ->
                    new PubVer(task.userOrg, task.repoName, task.packageName, task.versionName, task.apiUrl, task.user, task.apiKey)
                }.unique()

                for (PubVer pubVer : publishes) {
                    def http = BintrayHttpClientFactory.create(pubVer.apiUrl, pubVer.apiUser, pubVer.apiKey)
                    String pubUri = "/content/$pubVer.org/$pubVer.repoName/$pubVer.packageName/$pubVer.version/publish"
                    http.request(POST, JSON) {
                        uri.path = pubUri
                        response.success = { resp ->
                            logger.info("Published '$pubUri'.")
                        }
                        response.failure = { resp, reader ->
                            throw new GradleException("Could not publish '$pkgPath/$versionName': $resp.statusLine $reader")
                        }
                    }

                }
            }
            publishAllVersions.dependsOn(buildDebPublish)
            project.rootProject.tasks.release.dependsOn(publishAllVersions)
            project.gradle.taskGraph.whenReady { TaskExecutionGraph graph ->
                buildDebPublish.onlyIf {
                    (project.hasProperty('force' + publishName) && (project.property('force' + publishName) as Boolean)) || graph.hasTask(':final') || graph.hasTask(':candidate')
                }
                publishAllVersions.onlyIf {
                    graph.allTasks.find { it instanceof BintrayUploadTask && it.enabled } &&
                       ((project.hasProperty('force' + publishName) && (project.property('force' + publishName) as Boolean)) || graph.hasTask(':final') || graph.hasTask(':candidate'))
                }
            }

        }
    }

    @Canonical
    private static class PubVer {
        String org
        String repoName
        String packageName
        String version
        String apiUrl
        String apiUser
        String apiKey
    }
}<commit_msg>Revert "allow force deb publish"<commit_after>/*
 * Copyright 2015 Netflix, Inc.
 *
 * 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 com.netflix.spinnaker.gradle.ospackage

import com.jfrog.bintray.gradle.BintrayExtension
import com.jfrog.bintray.gradle.BintrayHttpClientFactory
import com.jfrog.bintray.gradle.BintrayPlugin
import com.jfrog.bintray.gradle.BintrayUploadTask
import com.netflix.gradle.plugins.deb.Deb
import groovy.transform.Canonical
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.tasks.Upload

import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.POST

/**
 * This is a workaround for:
 * https://github.com/bintray/gradle-bintray-plugin/issues/84
 *
 * and as such should die-in-a-fire if that issue gets resolved
 */
class OspackageBintrayPublishPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.plugins.apply(BintrayPlugin)

        def packageExtension = project.extensions.create('bintrayPackage', OspackageBintrayExtension)

        project.tasks.withType(Deb) { Deb deb ->
            def spinnakerDebians = project.configurations.maybeCreate('spinnakerDebians')
            project.artifacts.add('spinnakerDebians', deb)
            def extension = (BintrayExtension) project.extensions.getByName('bintray')
            def buildDebPublish = project.tasks.create("publish${deb.name}", BintrayUploadTask) { BintrayUploadTask task ->
                task.with {
                    apiUrl = extension.apiUrl
                    user = extension.user
                    apiKey = extension.key
                    configurations = ['spinnakerDebians']
                    publish = extension.publish
                    dryRun = extension.dryRun
                    userOrg = extension.pkg.userOrg ?: extension.user
                    repoName = packageExtension.packageRepo ?: extension.pkg.repo
                    packageName = extension.pkg.name
                    packageDesc = extension.pkg.desc
                    packageWebsiteUrl = extension.pkg.websiteUrl
                    packageIssueTrackerUrl = extension.pkg.issueTrackerUrl
                    packageVcsUrl = extension.pkg.vcsUrl
                    packageLicenses = extension.pkg.licenses
                    packageLabels = extension.pkg.labels
                    packageAttributes = extension.pkg.attributes
                    packagePublicDownloadNumbers = extension.pkg.publicDownloadNumbers
                    versionName = extension.pkg.version.name ?: project.version
                    versionDesc = extension.pkg.version.desc
                    versionReleased = extension.pkg.version.released
                    versionVcsTag = extension.pkg.version.vcsTag ?: project.version
                    versionAttributes = extension.pkg.version.attributes
                    signVersion = extension.pkg.version.gpg.sign
                    gpgPassphrase = extension.pkg.version.gpg.passphrase
                    syncToMavenCentral = false
                }
            }

            buildDebPublish.mustRunAfter('build')
            buildDebPublish.dependsOn(deb)
            buildDebPublish.dependsOn(spinnakerDebians.allArtifacts)
            Upload installTask = project.tasks.withType(Upload)?.findByName('install')
            if (installTask) {
                buildDebPublish.dependsOn(installTask)
            }
            buildDebPublish.group = BintrayUploadTask.GROUP
            project.rootProject.tasks.release.dependsOn(buildDebPublish)
            def publishAllVersions = project.rootProject.tasks.maybeCreate('publishAllBintrayVersions')
            publishAllVersions.doFirst {
                def publishes = project.tasks.findAll { it instanceof BintrayUploadTask }.collect { BintrayUploadTask task ->
                    new PubVer(task.userOrg, task.repoName, task.packageName, task.versionName, task.apiUrl, task.user, task.apiKey)
                }.unique()

                for (PubVer pubVer : publishes) {
                    def http = BintrayHttpClientFactory.create(pubVer.apiUrl, pubVer.apiUser, pubVer.apiKey)
                    String pubUri = "/content/$pubVer.org/$pubVer.repoName/$pubVer.packageName/$pubVer.version/publish"
                    http.request(POST, JSON) {
                        uri.path = pubUri
                        response.success = { resp ->
                            logger.info("Published '$pubUri'.")
                        }
                        response.failure = { resp, reader ->
                            throw new GradleException("Could not publish '$pkgPath/$versionName': $resp.statusLine $reader")
                        }
                    }

                }
            }
            publishAllVersions.dependsOn(buildDebPublish)
            project.rootProject.tasks.release.dependsOn(publishAllVersions)
            project.gradle.taskGraph.whenReady { TaskExecutionGraph graph ->
                buildDebPublish.onlyIf {
                    graph.hasTask(':final') || graph.hasTask(':candidate')
                }
                publishAllVersions.onlyIf {
                    graph.allTasks.find { it instanceof BintrayUploadTask && it.enabled } &&
                       (graph.hasTask(':final') || graph.hasTask(':candidate'))
                }
            }

        }
    }

    @Canonical
    private static class PubVer {
        String org
        String repoName
        String packageName
        String version
        String apiUrl
        String apiUser
        String apiKey
    }
}
{'fraction_non_alphanumeric': 0.08249528598365807, 'fraction_numerical': 0.0017284726587052169, 'mean_word_length': 2.070928829915561, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3877682', 'n_tokens_mistral': 3362, 'n_tokens_neox': 3131, 'n_words': 651}