|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.gradle.internal.release; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 13 | + |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 16 | +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; |
| 17 | +import org.gradle.api.DefaultTask; |
| 18 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 19 | +import org.gradle.api.file.Directory; |
| 20 | +import org.gradle.api.file.DirectoryProperty; |
| 21 | +import org.gradle.api.file.FileCollection; |
| 22 | +import org.gradle.api.file.RegularFile; |
| 23 | +import org.gradle.api.file.RegularFileProperty; |
| 24 | +import org.gradle.api.logging.Logger; |
| 25 | +import org.gradle.api.logging.Logging; |
| 26 | +import org.gradle.api.model.ObjectFactory; |
| 27 | +import org.gradle.api.tasks.InputDirectory; |
| 28 | +import org.gradle.api.tasks.InputFiles; |
| 29 | +import org.gradle.api.tasks.OutputFile; |
| 30 | +import org.gradle.api.tasks.TaskAction; |
| 31 | +import org.gradle.process.ExecOperations; |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.StringReader; |
| 36 | +import java.time.Instant; |
| 37 | +import java.util.Comparator; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Properties; |
| 40 | + |
| 41 | +import javax.inject.Inject; |
| 42 | + |
| 43 | +import static java.util.stream.Collectors.toList; |
| 44 | + |
| 45 | +public class BundleChangelogsTask extends DefaultTask { |
| 46 | + private static final Logger LOGGER = Logging.getLogger(BundleChangelogsTask.class); |
| 47 | + |
| 48 | + private final ConfigurableFileCollection changelogs; |
| 49 | + |
| 50 | + private final RegularFileProperty bundleFile; |
| 51 | + private final DirectoryProperty changelogDirectory; |
| 52 | + |
| 53 | + private final GitWrapper gitWrapper; |
| 54 | + |
| 55 | + private static final ObjectMapper yamlMapper = new ObjectMapper( |
| 56 | + new YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES) |
| 57 | + .disable(YAMLGenerator.Feature.SPLIT_LINES) |
| 58 | + .enable(YAMLGenerator.Feature.INDENT_ARRAYS_WITH_INDICATOR) |
| 59 | + .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER) |
| 60 | + .enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE) |
| 61 | + ).setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 62 | + |
| 63 | + @Inject |
| 64 | + public BundleChangelogsTask(ObjectFactory objectFactory, ExecOperations execOperations) { |
| 65 | + changelogs = objectFactory.fileCollection(); |
| 66 | + |
| 67 | + bundleFile = objectFactory.fileProperty(); |
| 68 | + changelogDirectory = objectFactory.directoryProperty(); |
| 69 | + |
| 70 | + gitWrapper = new GitWrapper(execOperations); |
| 71 | + } |
| 72 | + |
| 73 | + @TaskAction |
| 74 | + public void executeTask() throws IOException { |
| 75 | + final String upstreamRemote = gitWrapper.getUpstream(); |
| 76 | + |
| 77 | + String ref = "main"; |
| 78 | + try { |
| 79 | + checkoutChangelogs(gitWrapper, upstreamRemote, ref); |
| 80 | + Properties props = new Properties(); |
| 81 | + props.load(new StringReader(gitWrapper.runCommand("git", "show", ref + ":build-tools-internal/version.properties"))); |
| 82 | + String version = props.getProperty("elasticsearch"); |
| 83 | + |
| 84 | + LOGGER.info("Finding changelog files..."); |
| 85 | + |
| 86 | + List<ChangelogEntry> entries = this.changelogDirectory.getAsFileTree() |
| 87 | + .getFiles() |
| 88 | + .stream() |
| 89 | + .map(ChangelogEntry::parse) |
| 90 | + .sorted(Comparator.comparing(ChangelogEntry::getPr)) |
| 91 | + .collect(toList()); |
| 92 | + |
| 93 | + ChangelogBundle bundle = new ChangelogBundle(version, Instant.now().toString(), entries); |
| 94 | + |
| 95 | + yamlMapper.writeValue(new File("docs/release-notes/changelog-bundles/" + version + ".yml"), bundle); |
| 96 | + } finally { |
| 97 | + gitWrapper.runCommand("git", "restore", "-s@", "-SW", "--", "docs/changelog"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static void checkoutChangelogs(GitWrapper gitWrapper, String upstream, String ref) { |
| 102 | + gitWrapper.updateRemote(upstream); |
| 103 | + // TODO check for changes first |
| 104 | + gitWrapper.runCommand("git", "checkout", ref, "--", "docs/changelog"); |
| 105 | + } |
| 106 | + |
| 107 | + @InputDirectory |
| 108 | + public DirectoryProperty getChangelogDirectory() { |
| 109 | + return changelogDirectory; |
| 110 | + } |
| 111 | + |
| 112 | + public void setChangelogDirectory(Directory dir) { |
| 113 | + this.changelogDirectory.set(dir); |
| 114 | + } |
| 115 | + |
| 116 | + @InputFiles |
| 117 | + public FileCollection getChangelogs() { |
| 118 | + return changelogs; |
| 119 | + } |
| 120 | + |
| 121 | + public void setChangelogs(FileCollection files) { |
| 122 | + this.changelogs.setFrom(files); |
| 123 | + } |
| 124 | + |
| 125 | + @OutputFile |
| 126 | + public RegularFileProperty getBundleFile() { |
| 127 | + return bundleFile; |
| 128 | + } |
| 129 | + |
| 130 | + public void setBundleFile(RegularFile file) { |
| 131 | + this.bundleFile.set(file); |
| 132 | + } |
| 133 | +} |
0 commit comments