Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Support for remote url `ssh://` ([#19](https://github.com/diffplug/spotless-changelog/issues/19))

## [2.0.1] - 2021-03-13
### Fixed
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ spotlessChangelog { // all defaults
commitMessage 'Published release/{{version}}' // {{version}} will be replaced
remote 'origin'
branch 'main'
// default value is `yes`, but if you set it to `no`, then it will
// disable ssh host key checking (.ssh/known_hosts).
sshStrictHostKeyChecking "yes" // can override with `-PsshStrictHostKeyChecking=no`
}

// last version parsed from changelog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@


import com.diffplug.common.collect.Iterables;
import com.jcraft.jsch.Session;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
Expand All @@ -33,9 +34,12 @@
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

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

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

private void push(Consumer<PushCommand> cmd, RemoteRefUpdate.Status expected) throws GitAPIException {
PushCommand push = git.push().setCredentialsProvider(creds()).setRemote(cfg.remote);
String remoteUrl = git.getRepository().getConfig().getString(ConfigConstants.CONFIG_REMOTE_SECTION, cfg.remote, ConfigConstants.CONFIG_KEY_URL);

PushCommand push = git.push().setRemote(cfg.remote);
if (remoteUrl.startsWith("http://") || remoteUrl.startsWith("https://")) {
push = push.setCredentialsProvider(creds());
} else if (remoteUrl.startsWith("ssh://")) {
push.setTransportConfigCallback(transport -> {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
session.setConfig("StrictHostKeyChecking", cfg.sshStrictHostKeyChecking);
}
});
});
}

cmd.accept(push);

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 DiffPlug
* Copyright (C) 2019-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@ public class GitCfg {
public String commitMessage = "Published release/" + COMMIT_MESSAGE_VERSION;
public String remote = "origin";
public String branch = "main";
public String sshStrictHostKeyChecking = "yes";

/** Returns an api configured with this config. */
public GitActions withChangelog(File changelogFile, ChangelogAndNext model) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 DiffPlug
* Copyright (C) 2019-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,7 +54,7 @@ public ChangelogExtension(Project project) {
/**
* Parses the changelog and calculates the next version. Once this
* has been done, the user can't change the configuration at all.
* Use {@link #assertNotCalculatedYet()} on every mutation to check for this.
* Use {@link #assertNotCalculatedYet()} on every mutation to check for this.
*/
ChangelogAndNext model() {
if (model == null) {
Expand Down Expand Up @@ -123,7 +123,7 @@ public void versionSchema(Class<? extends NextVersionFunction> functionClass) th
* If any of these strings are found in the `## [Unreleased]` section, then the
* next version will bump the `added` place in `breaking.added.fixed` (unless
* overruled by `ifFoundBumpBreaking`).
*
*
* Default value is `['### Added']`
*/
public void ifFoundBumpAdded(List<String> toFind) {
Expand All @@ -139,7 +139,7 @@ public void ifFoundBumpAdded(String... toFind) {
/**
* If any of these strings are found in the `## [Unreleased]` section, then the
* next version will bump the `breaking` place in `breaking.added.fixed`.
*
*
* Default value is `['**BREAKING**']`.
*/
public void ifFoundBumpBreaking(List<String> toFind) {
Expand All @@ -163,7 +163,7 @@ public void forceNextVersion(String forceNextVersion) {
* appended to the end, unless you add `-Prelease=true` to the gradle command line.
* Essentially, it asks like a gun safety where all versions are nerfed to `-SNAPSHOT`,
* until you allow a release by adding `-Prelease`.
*
*
* Enabling this mode should look like this in your buildscript: `appendDashSnapshotUnless_dashPrelease=true`
*/
public void setAppendDashSnapshotUnless_dashPrelease(boolean appendSnapshot) {
Expand All @@ -172,6 +172,17 @@ public void setAppendDashSnapshotUnless_dashPrelease(boolean appendSnapshot) {
}
}

/**
* If you set this to `no`, then the ssh host key checking over ssh:// remotes will be disabled.
* By default strict host key checking is `yes`. Make sure that there is an entry
* in know_hosts file for given ssh remote.
* You can also add `-PsshStrictHostKeyChecking=no` to the gradle command.
* In your buildscript you can disable checking with `sshStrictHostKeyChecking = "no"`
*/
public void setSshStrictHostKeyChecking(String sshStrictHostKeyChecking) {
gitCfg.sshStrictHostKeyChecking = sshStrictHostKeyChecking;
}

// tag and push
/** Default value is `release/` */
public void tagPrefix(String tagPrefix) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019-2020 DiffPlug
* Copyright (C) 2019-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -46,6 +46,9 @@ public void apply(Project project) {

ChangelogExtension extension = project.getExtensions().create(ChangelogExtension.NAME, ChangelogExtension.class, project);
project.getTasks().register(PrintTask.NAME, PrintTask.class, extension);
if (project.getRootProject().hasProperty("sshStrictHostKeyChecking")) {
extension.gitCfg.sshStrictHostKeyChecking = (String) project.getRootProject().findProperty("sshStrictHostKeyChecking");
}

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