Skip to content

Commit e0458df

Browse files
authored
Add support for ssh (#20, fixes #19)
2 parents 43e702a + bbc06ba commit e0458df

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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+
- Support for remote url `ssh://` ([#19](https://github.com/diffplug/spotless-changelog/issues/19))
810

911
## [2.0.1] - 2021-03-13
1012
### Fixed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ spotlessChangelog { // all defaults
173173
commitMessage 'Published release/{{version}}' // {{version}} will be replaced
174174
remote 'origin'
175175
branch 'main'
176+
// default value is `yes`, but if you set it to `no`, then it will
177+
// disable ssh host key checking (.ssh/known_hosts).
178+
sshStrictHostKeyChecking "yes" // can override with `-PsshStrictHostKeyChecking=no`
176179
}
177180
178181
// last version parsed from changelog

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
import com.diffplug.common.collect.Iterables;
20+
import com.jcraft.jsch.Session;
2021
import java.io.File;
2122
import java.io.IOException;
2223
import java.util.Arrays;
@@ -33,9 +34,12 @@
3334
import org.eclipse.jgit.lib.Repository;
3435
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
3536
import org.eclipse.jgit.transport.CredentialsProvider;
37+
import org.eclipse.jgit.transport.JschConfigSessionFactory;
38+
import org.eclipse.jgit.transport.OpenSshConfig;
3639
import org.eclipse.jgit.transport.PushResult;
3740
import org.eclipse.jgit.transport.RefSpec;
3841
import org.eclipse.jgit.transport.RemoteRefUpdate;
42+
import org.eclipse.jgit.transport.SshTransport;
3943
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
4044

4145
/** API for doing the commit, tag, and push operations. See {@link GitCfg#withChangelog(File, ChangelogAndNext)}. */
@@ -69,7 +73,7 @@ public void checkCanPush() throws GitAPIException, IOException {
6973
}
7074
push(cfg.branch, RemoteRefUpdate.Status.UP_TO_DATE);
7175
} catch (GitAPIException e) {
72-
throw new IllegalArgumentException("You can set user/pass with any of these environment variables: " + envVars(), e);
76+
throw new IllegalArgumentException("You can set user/pass with any of these environment variables: " + envVars() + ", or try -PsshStrictHostKeyChecking=no on ssh remotes", e);
7377
}
7478
}
7579

@@ -118,10 +122,25 @@ private void push(Ref ref, RemoteRefUpdate.Status expected) throws GitAPIExcepti
118122
}
119123

120124
private void push(Consumer<PushCommand> cmd, RemoteRefUpdate.Status expected) throws GitAPIException {
121-
PushCommand push = git.push().setCredentialsProvider(creds()).setRemote(cfg.remote);
125+
String remoteUrl = git.getRepository().getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, cfg.remote, ConfigConstants.CONFIG_KEY_URL);
126+
127+
PushCommand push = git.push().setRemote(cfg.remote);
128+
if (remoteUrl.startsWith("http://") || remoteUrl.startsWith("https://")) {
129+
push = push.setCredentialsProvider(creds());
130+
} else if (remoteUrl.startsWith("ssh://")) {
131+
push.setTransportConfigCallback(transport -> {
132+
SshTransport sshTransport = (SshTransport) transport;
133+
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() {
134+
@Override
135+
protected void configure(OpenSshConfig.Host host, Session session) {
136+
session.setConfig("StrictHostKeyChecking", cfg.sshStrictHostKeyChecking);
137+
}
138+
});
139+
});
140+
}
141+
122142
cmd.accept(push);
123143

124-
String remoteUrl = git.getRepository().getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, cfg.remote, ConfigConstants.CONFIG_KEY_URL);
125144
RefSpec spec = Iterables.getOnlyElement(push.getRefSpecs());
126145
System.out.println("push " + spec.getSource() + " to " + cfg.remote + " " + remoteUrl);
127146

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 DiffPlug
2+
* Copyright (C) 2019-2021 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.
@@ -29,6 +29,7 @@ public class GitCfg {
2929
public String commitMessage = "Published release/" + COMMIT_MESSAGE_VERSION;
3030
public String remote = "origin";
3131
public String branch = "main";
32+
public String sshStrictHostKeyChecking = "yes";
3233

3334
/** Returns an api configured with this config. */
3435
public GitActions withChangelog(File changelogFile, ChangelogAndNext model) throws IOException {

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 DiffPlug
2+
* Copyright (C) 2019-2021 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.
@@ -54,7 +54,7 @@ public ChangelogExtension(Project project) {
5454
/**
5555
* Parses the changelog and calculates the next version. Once this
5656
* has been done, the user can't change the configuration at all.
57-
* Use {@link #assertNotCalculatedYet()} on every mutation to check for this.
57+
* Use {@link #assertNotCalculatedYet()} on every mutation to check for this.
5858
*/
5959
ChangelogAndNext model() {
6060
if (model == null) {
@@ -123,7 +123,7 @@ public void versionSchema(Class<? extends NextVersionFunction> functionClass) th
123123
* If any of these strings are found in the `## [Unreleased]` section, then the
124124
* next version will bump the `added` place in `breaking.added.fixed` (unless
125125
* overruled by `ifFoundBumpBreaking`).
126-
*
126+
*
127127
* Default value is `['### Added']`
128128
*/
129129
public void ifFoundBumpAdded(List<String> toFind) {
@@ -139,7 +139,7 @@ public void ifFoundBumpAdded(String... toFind) {
139139
/**
140140
* If any of these strings are found in the `## [Unreleased]` section, then the
141141
* next version will bump the `breaking` place in `breaking.added.fixed`.
142-
*
142+
*
143143
* Default value is `['**BREAKING**']`.
144144
*/
145145
public void ifFoundBumpBreaking(List<String> toFind) {
@@ -163,7 +163,7 @@ public void forceNextVersion(String forceNextVersion) {
163163
* appended to the end, unless you add `-Prelease=true` to the gradle command line.
164164
* Essentially, it asks like a gun safety where all versions are nerfed to `-SNAPSHOT`,
165165
* until you allow a release by adding `-Prelease`.
166-
*
166+
*
167167
* Enabling this mode should look like this in your buildscript: `appendDashSnapshotUnless_dashPrelease=true`
168168
*/
169169
public void setAppendDashSnapshotUnless_dashPrelease(boolean appendSnapshot) {
@@ -172,6 +172,17 @@ public void setAppendDashSnapshotUnless_dashPrelease(boolean appendSnapshot) {
172172
}
173173
}
174174

175+
/**
176+
* If you set this to `no`, then the ssh host key checking over ssh:// remotes will be disabled.
177+
* By default strict host key checking is `yes`. Make sure that there is an entry
178+
* in know_hosts file for given ssh remote.
179+
* You can also add `-PsshStrictHostKeyChecking=no` to the gradle command.
180+
* In your buildscript you can disable checking with `sshStrictHostKeyChecking = "no"`
181+
*/
182+
public void setSshStrictHostKeyChecking(String sshStrictHostKeyChecking) {
183+
gitCfg.sshStrictHostKeyChecking = sshStrictHostKeyChecking;
184+
}
185+
175186
// tag and push
176187
/** Default value is `release/` */
177188
public void tagPrefix(String tagPrefix) {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2020 DiffPlug
2+
* Copyright (C) 2019-2021 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.
@@ -46,6 +46,9 @@ public void apply(Project project) {
4646

4747
ChangelogExtension extension = project.getExtensions().create(ChangelogExtension.NAME, ChangelogExtension.class, project);
4848
project.getTasks().register(PrintTask.NAME, PrintTask.class, extension);
49+
if (project.getRootProject().hasProperty("sshStrictHostKeyChecking")) {
50+
extension.gitCfg.sshStrictHostKeyChecking = (String) project.getRootProject().findProperty("sshStrictHostKeyChecking");
51+
}
4952

5053
TaskProvider<CheckTask> check = project.getTasks().register(CheckTask.NAME, CheckTask.class, extension);
5154
TaskProvider<BumpTask> bump = project.getTasks().register(BumpTask.NAME, BumpTask.class, extension);

0 commit comments

Comments
 (0)