|
| 1 | +package au.com.dius.pact.provider.gradle |
| 2 | + |
| 3 | +import au.com.dius.pact.core.pactbroker.Latest |
| 4 | +import au.com.dius.pact.core.pactbroker.PactBrokerClient |
| 5 | +import org.apache.commons.lang3.StringUtils |
| 6 | +import org.fusesource.jansi.Ansi |
| 7 | +import org.fusesource.jansi.AnsiConsole |
| 8 | +import org.gradle.api.DefaultTask |
| 9 | +import org.gradle.api.GradleScriptException |
| 10 | +import org.gradle.api.tasks.TaskAction |
| 11 | + |
| 12 | +/** |
| 13 | + * Task to push pact files to a pact broker |
| 14 | + */ |
| 15 | +@SuppressWarnings('Println') |
| 16 | +class PactCanIDeployTask extends DefaultTask { |
| 17 | + |
| 18 | + @TaskAction |
| 19 | + void canIDeploy() { |
| 20 | + AnsiConsole.systemInstall() |
| 21 | + if (!project.pact.broker) { |
| 22 | + throw new GradleScriptException('You must add a pact broker configuration to your build before you can ' + |
| 23 | + 'use the CanIDeploy task', null) |
| 24 | + } |
| 25 | + |
| 26 | + Broker config = project.pact.broker |
| 27 | + def options = [:] |
| 28 | + if (StringUtils.isNotEmpty(config.pactBrokerToken)) { |
| 29 | + options.authentication = [config.pactBrokerAuthenticationScheme ?: 'bearer', |
| 30 | + config.pactBrokerToken] |
| 31 | + } else if (StringUtils.isNotEmpty(config.pactBrokerUsername)) { |
| 32 | + options.authentication = [config.pactBrokerAuthenticationScheme ?: 'basic', |
| 33 | + config.pactBrokerUsername, config.pactBrokerPassword] |
| 34 | + } |
| 35 | + def brokerClient = new PactBrokerClient(config.pactBrokerUrl, options) |
| 36 | + if (!project.hasProperty('pacticipant')) { |
| 37 | + throw new GradleScriptException('The CanIDeploy task requires -Ppacticipant=...', null) |
| 38 | + } |
| 39 | + String pacticipant = project.property('pacticipant') |
| 40 | + if (!project.hasProperty('pacticipantVersion')) { |
| 41 | + throw new GradleScriptException('The CanIDeploy task requires -PpacticipantVersion=...', null) |
| 42 | + } |
| 43 | + String pacticipantVersion = project.property('pacticipantVersion') |
| 44 | + Latest latest = new Latest.UseLatest(false) |
| 45 | + if (project.hasProperty('latest')) { |
| 46 | + String latestProp = project.property('latest') |
| 47 | + if (latestProp == 'true') { |
| 48 | + latest = new Latest.UseLatest(true) |
| 49 | + } else if (latestProp == 'false') { |
| 50 | + latest = new Latest.UseLatest(false) |
| 51 | + } else { |
| 52 | + latest = new Latest.UseLatestTag(latestProp) |
| 53 | + } |
| 54 | + } |
| 55 | + String to = null |
| 56 | + if (project.hasProperty('to')) { |
| 57 | + to = project.property('to') |
| 58 | + } |
| 59 | + def result = brokerClient.canIDeploy(pacticipant, pacticipantVersion, latest, to) |
| 60 | + if (result.ok) { |
| 61 | + AnsiConsole.out().println(Ansi.ansi().a('Computer says yes \\o/ ').a(result.message).a('\n\n') |
| 62 | + .fg(Ansi.Color.GREEN).a(result.reason).reset()) |
| 63 | + } else { |
| 64 | + AnsiConsole.out().println(Ansi.ansi().a('Computer says no ¯\\_(ツ)_/¯ ').a(result.message).a('\n\n') |
| 65 | + .fg(Ansi.Color.RED).a(result.reason).reset()) |
| 66 | + } |
| 67 | + |
| 68 | + AnsiConsole.systemUninstall() |
| 69 | + |
| 70 | + if (!result.ok) { |
| 71 | + throw new GradleScriptException("Can you deploy? Computer says no ¯\\_(ツ)_/¯ ${result.message}", null) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments