# tricky

* [git clone](#git-clone)
  * [setup to fetch `refs/changes` only](#setup-to-fetch-refschanges-only)
  * [setup to fetch only `refs/heads/sandbox/marslo/*`](#setup-to-fetch-only-refsheadssandboxmarslo)
  * [fetch gerrit changes](#fetch-gerrit-changes)
* [downgrade](#downgrade)
  * [jsch](#jsch)
  * [grgit](#grgit)

### git clone

> \[!NOTE|label:references:]
>
> * [Git - What is "Refspec"](https://stackoverflow.com/q/44333437/2940319)
> * [Jenkins Git Refspec not correctly substituted for branch specifier](https://stackoverflow.com/q/50640603/2940319)
> * [Why does "git clone" not take a refspec?](https://stackoverflow.com/a/43759576/2940319)
> * [Jenkins gerrit trigger not fetching my change when building](https://stackoverflow.com/a/45976830/2940319)
> * [How to execute Jenkinsfile with gerrit trigger plugin for changeset in jenkins?](https://stackoverflow.com/a/46205264/2940319)

#### setup to fetch `refs/changes` only

```bash
# Refspec
refs/changes/*:refs/remotes/origin/changes/*
```

#### setup to fetch only `refs/heads/sandbox/marslo/*`

```bash
# Refspec
refs/heads/sandbox/marslo/*:refs/remotes/origin/heads/sandbox/marslo/*
```

#### fetch gerrit changes

```bash
# Refspec
$GERRIT_REFSPEC:$GERRIT_REFSPEC

# branch
$GERRIT_REFSPEC
# or
FETCH_HEAD
```

### downgrade

#### jsch

> \[!NOTE]
>
> * [JENKINS-71273: Gerrit Trigger fails to connect Gerrit 2.14 - com.jcraft.jsch.JSchException: verify: false](https://issues.jenkins.io/browse/JENKINS-71273)
> * [Nicola Beghin](https://stackoverflow.com/questions/72743823/public-key-authentication-fails-with-jsch-but-work-with-openssh-with-the-same-ke#comment133364530_72771323)
> * [Public key authentication fails with JSch but work with OpenSSH with the same key](https://stackoverflow.com/questions/72743823/public-key-authentication-fails-with-jsch-but-work-with-openssh-with-the-same-ke/72771323#comment133364530_72771323)
> * api
>   * [org.jenkinsci.plugins.jsch.JSchConnector](https://javadoc.jenkins.io/plugin/jsch/org/jenkinsci/plugins/jsch/JSchConnector.html)
>   * [Package com.jcraft.jsch](https://epaul.github.io/jsch-documentation/javadoc/)
>   * [com.jcraft.jsch.Session](https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/Session.html)
>   * [com.jcraft.jsch.KeyExchange](https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/KeyExchange.html)
>   * [com.jcraft.jsch.OpenSSHConfig](https://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/OpenSSHConfig.html)
> * references:
>   * [Java JSch Example to run Shell Commands on SSH Unix Server](https://www.digitalocean.com/community/tutorials/jsch-example-java-ssh-unix-server)
>   * [Best Java code snippets using com.jcraft.jsch.JSch](https://www.tabnine.com/code/java/classes/com.jcraft.jsch.JSch)
>   * [Java SSH connect with key using JSch - Auth Failed](https://stackoverflow.com/a/68919510/2940319)
>   * [JSCH: SFTP. Hangs at session.connect() using the port 21](https://stackoverflow.com/a/36693526/2940319)
>   * [Creating JSch HostKey instance from a public key in .pub format](https://stackoverflow.com/a/29605457/2940319)

```
$ curl -O https://repo1.maven.org/maven2/com/github/mwiede/jsch/0.2.7/jsch-0.2.7.jar
$ docker cp jsch-0.2.7.jar e6cd5d7516cd:/var/jenkins_home/plugins/jsch/WEB-INF/lib/jsch-0.1.55.jar
#                               ^
#                        Docker Container ID
```

```groovy
import org.jenkinsci.plugins.jsch.JSchConnector
import java.io.InputStream

import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session
import com.jcraft.jsch.LocalIdentityRepository
import com.jcraft.jsch.Channel
import com.jcraft.jsch.ChannelExec

String user = 'srv-ssd-fw-devops'
String host = 'sample.gerrit.com'
int port    = 29418
String path = System.getProperty( 'user.home' ) + '/.ssh/ed25519/devops@ssdfw'
// Byte[] privateKey = ''.getBytes()

JSch jsch = new JSchConnector( user, host, port ).getJSch()
jsch.addIdentity( path )

Session session = jsch.getSession( user, host, port )
session.setConfig( 'StrictHostKeyChecking', 'no' )
session.setConfig( 'HostkeyAlgorithms', '+ssh-rsa' )
session.setConfig( 'PreferredAuthentications', 'publickey' )

println """
  ${session.getClass()}:
    session client version : ${session.getClientVersion()} : ${session.clientVersion}
    session host           : ${session.getHost()} : ${session.host}
    session port           : ${session.getPort()} : ${session.port}
    session hostkey        : ${session.getHostKey()} : ${session.hostKey}
    session hostkey alias  : ${session.getHostKeyAlias()} : ${session.hostKeyAlias}
    session isConnected    : ${session.isConnected()}
"""

def knowHosts = session.getHostKeyRepository()
println """
  ${knowHosts.getClass()}:
    knownHosts host key     : ${knowHosts.getHostKey( host, 'ssh-rsa' )}
    knowHosts repository id : ${knowHosts.getKnownHostsRepositoryID()}
"""

def idRepo = jsch.getIdentityRepository()
println """
  ${idRepo.getClass()} :
      identityRepository name       : ${idRepo.getName()} : ${idRepo.name}
      identityRepository status     : ${idRepo.getStatus()} : ${idRepo.status}
      identityRepository identities : ${idRepo.getIdentities()} : ${idRepo.identities}
"""

session.connect()
```

*   revert

    ```bash
    sudo cp -rp /jenkins/DEVOPS-JENKINS/plugins/jsch* .
    sudo cp -rp /jenkins/DEVOPS-JENKINS/plugins/gerrit* .
    sudo cp -rp /jenkins/DEVOPS-JENKINS/plugins/maven-plugin* .
    sudo cp -rp /jenkins/DEVOPS-JENKINS/plugins/mercuri* .
    sudo cp -rp /jenkins/DEVOPS-JENKINS/plugins/ivy* .
    sudo cp -rp /jenkins/DEVOPS-JENKINS/plugins/artifactory* .
    sudo cp -rp /jenkins/DEVOPS-JENKINS/plugins/bitbucket*
    ```

    ![jsch plugin issue](https://1999659500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHqOSW6on5cHbtwcSH6%2Fuploads%2Fgit-blob-4cbb3dd8935b467ae2193c41332e979cd857a0b3%2Fjsch-plugin-rsa-issue.png?alt=media)

#### grgit

> \[!NOTE]
>
> * [grgit-authentication](https://ajoberstar.org/grgit/main/grgit-authentication-2.html)
>   * This support must be explicitly enabled with the system property `org.ajoberstar.grgit.auth.command.allow=true`.

*   grgit-clone

    ```groovy
    Grgit.clone {
      dir = <path>
      uri = <path or uri>
      remote = <name>
      all = <boolean>
      bare = <boolean>
      branches = <full refs>
      checkout = <boolean>
      refToCheckout = <name>
      depth = <integer>
      credentials = <credentials>
    }
    ```

    *   [sample](https://ajoberstar.org/grgit/main/grgit-authentication-2.html#_hardcoded_credentials)

        ```groovy
        import org.ajoberstar.grgit.Grgit
        import org.ajoberstar.grgit.Credentials

        def grgit = Grgit.clone(dir: '...', url: '...', credentials: new Credentials(username, password))
        ```
