Skip to content

Commit 7bbb884

Browse files
authored
Add a way to run a command after a push (#51)
2 parents c17fdc9 + 1322c93 commit 7bbb884

File tree

7 files changed

+519
-4
lines changed

7 files changed

+519
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Added
9+
- A `runAfterPush` command to run a CLI command after the push is complete. ([#51](https://github.com/diffplug/spotless-changelog/pull/51))
810

911
## [3.0.2] - 2023-04-06
1012
### Fixed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ spotlessChangelog { // all defaults
171171
tagPrefix 'release/'
172172
commitMessage 'Published release/{{version}}' // {{version}} will be replaced
173173
tagMessage null // default is null (creates lightweight tag); {{changes}} and {{version}} will be replaced
174+
runAfterPush null // runs a CLI command after the push; {{changes}} and {{version}} will be replaced
174175
remote 'origin'
175176
branch 'main'
176177
// default value is `yes`, but if you set it to `no`, then it will

spotless-changelog-lib/src/main/java/com/diffplug/spotless/changelog/GitCfg.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2021 DiffPlug
2+
* Copyright (C) 2019-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.changelog;
1717

18-
1918
import java.io.File;
2019
import java.io.IOException;
2120
import pl.tlinkowski.annotation.basic.NullOr;
@@ -31,6 +30,8 @@ public class GitCfg {
3130
public String commitMessage = "Published release/" + COMMIT_MESSAGE_VERSION;
3231
/** Message used in tag, null means lightweight tag. */
3332
public @NullOr String tagMessage = null;
33+
/** Runs a CLI command after the push if not null. */
34+
public @NullOr String runAfterPush = null;
3435
public String remote = "origin";
3536
public String branch = "main";
3637
public String sshStrictHostKeyChecking = "yes";

spotless-changelog-plugin-gradle/src/main/java/com/diffplug/spotless/changelog/gradle/ChangelogExtension.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 DiffPlug
2+
* Copyright (C) 2019-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -238,6 +238,11 @@ public void tagMessage(String tagMessage) {
238238
data.gitCfg.tagMessage = tagMessage;
239239
}
240240

241+
/** Runs a CLI command after the push if not null - {{changes}} and {{version}} will be replaced. */
242+
public void runAfterPush(String runAfterPush) {
243+
data.gitCfg.runAfterPush = runAfterPush;
244+
}
245+
241246
/** Default value is 'origin' */
242247
public void remote(String remote) {
243248
data.gitCfg.remote = remote;

spotless-changelog-plugin-gradle/src/main/java/com/diffplug/spotless/changelog/gradle/ChangelogPlugin.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2023 DiffPlug
2+
* Copyright (C) 2019-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -247,6 +247,17 @@ public void push() throws IOException, GitAPIException {
247247
GitActions git = data.gitCfg.withChangelog(data.changelogFile, data.model());
248248
git.addAndCommit();
249249
git.tagBranchPush();
250+
if (data.gitCfg.runAfterPush != null) {
251+
try (var runner = new ProcessRunner()) {
252+
var result = runner.shell(data.gitCfg.runAfterPush);
253+
System.out.write(result.stdOut());
254+
System.out.flush();
255+
System.err.write(result.stdErr());
256+
System.err.flush();
257+
} catch (IOException | InterruptedException e) {
258+
throw new GradleException("runAfterPush failed: " + data.gitCfg.runAfterPush, e);
259+
}
260+
}
250261
}
251262
}
252263
}

0 commit comments

Comments
 (0)