aboutsummaryrefslogtreecommitdiff
path: root/buildSrc/src/main/java/deployment/DeploymentRequestProcessTask.java
blob: 57ee6b2f76c188a75f8f05d0558f281138fd191f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package deployment;

import github.DeploymentCreate;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;

public class DeploymentRequestProcessTask extends DefaultTask {

    static class DeploymentCommandLineArgs extends RequestCommandLineArgs {
        static final String PROP_KEY_SHA = "Request.sha";
        final String sha;

        DeploymentCommandLineArgs(Map<String, ?> properties) {
            super(properties);
            this.sha = properties.get(PROP_KEY_SHA).toString();
        }
    }

    private final DeploymentProcessConfiguration mConfiguration;
    private final int mStepIndex;

    @Input
    public String getEnvironmentKey() {
        return getEnvironmentName(mConfiguration, mStepIndex);
    }

    @Inject
    public DeploymentRequestProcessTask(
            DeploymentProcessConfiguration configuration, int stepIndex) {
        mConfiguration = configuration;
        mStepIndex = stepIndex;
        setGroup("Publishing");
        setDescription("Request deployment of " + getEnvironmentName(configuration, stepIndex));
    }

    @TaskAction
    public void deploymentRequestAction() {
        try {
            deploymentRequest(
                    new DeploymentCommandLineArgs(getProject().getProperties()),
                    mConfiguration,
                    mStepIndex);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void deploymentRequest(
            DeploymentCommandLineArgs data,
            DeploymentProcessConfiguration configuration,
            int stepIndex)
            throws Exception {

        DeploymentCreate deploymentCreate =
                new DeploymentCreate(data.apiUsername, data.apiUserToken);
        requestDeploymentAction(deploymentCreate, data, configuration, stepIndex);
    }

    private static void requestDeploymentAction(
            DeploymentCreate deploymentCreate,
            DeploymentCommandLineArgs data,
            DeploymentProcessConfiguration environment,
            int stepIndex)
            throws Exception {
        final String environmentToDeploy = getEnvironmentName(environment, stepIndex);
        final List<String> environmentsToKill =
                environment.environmentSteps.stream()
                        .map(name -> getEnvironmentName(environment.name, name))
                        .filter(env -> !env.equals(environmentToDeploy))
                        .collect(Collectors.toList());

        final DeploymentCreate.Response response =
                deploymentCreate.request(
                        new DeploymentCreate.Request(
                                data.sha,
                                stepIndex == 0 ? "deploy" : "deploy:migration",
                                false,
                                environmentToDeploy,
                                String.format(
                                        Locale.ROOT,
                                        "Deployment for '%s' request by '%s'.",
                                        environmentToDeploy,
                                        data.apiUsername),
                                Collections.singletonList("master-green-requirement"),
                                new DeploymentCreate.RequestPayloadField(environmentsToKill)));

        System.out.println(
                String.format(
                        Locale.ROOT,
                        "Deploy request response: id %s, sha %s, environment %s, task %s.",
                        response.id,
                        response.sha,
                        response.environment,
                        response.task));
    }

    private static String getEnvironmentName(String environmentName, String stepName) {
        return String.format(Locale.ROOT, "%s_%s", environmentName, stepName);
    }

    private static String getEnvironmentName(
            DeploymentProcessConfiguration environment, int index) {
        return getEnvironmentName(environment.name, environment.environmentSteps.get(index));
    }
}