Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env :: support ssh protocol for github repos. #40451

Merged
merged 9 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

## 4.2.1 (2022-05-07)

### Enhancement
- Added support for ssh protocol.
n3f marked this conversation as resolved.
Show resolved Hide resolved

## 4.2.0 (2022-01-27)

### Enhancement
Expand Down
13 changes: 7 additions & 6 deletions packages/env/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,13 @@ _Note: the port number environment variables (`WP_ENV_PORT` and `WP_ENV_TESTS_PO

Several types of strings can be passed into the `core`, `plugins`, `themes`, and `mappings` fields.

| Type | Format | Example(s) |
| ----------------- | ----------------------------- | -------------------------------------------------------- |
| Relative path | `.<path>\|~<path>` | `"./a/directory"`, `"../a/directory"`, `"~/a/directory"` |
| Absolute path | `/<path>\|<letter>:\<path>` | `"/a/directory"`, `"C:\\a\\directory"` |
| GitHub repository | `<owner>/<repo>[#<ref>]` | `"WordPress/WordPress"`, `"WordPress/gutenberg#trunk"` |
| ZIP File | `http[s]://<host>/<path>.zip` | `"https://wordpress.org/wordpress-5.4-beta2.zip"` |
| Type | Format | Example(s) |
| ----------------- | -------------------------------------------- | -------------------------------------------------------- |
| Relative path | `.<path>\|~<path>` | `"./a/directory"`, `"../a/directory"`, `"~/a/directory"` |
| Absolute path | `/<path>\|<letter>:\<path>` | `"/a/directory"`, `"C:\\a\\directory"` |
| GitHub repository | `<owner>/<repo>[#<ref>]` | `"WordPress/WordPress"`, `"WordPress/gutenberg#trunk"` |
| SSH repository | `ssh://user@host/<owner>/<repo>.git[#<ref>]` | `"ssh://git@github.com/WordPress/WordPress.git"` |
| ZIP File | `http[s]://<host>/<path>.zip` | `"https://wordpress.org/wordpress-5.4-beta2.zip"` |

Remote sources will be downloaded into a temporary directory located in `~/.wp-env`.

Expand Down
27 changes: 27 additions & 0 deletions packages/env/lib/config/parse-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,33 @@ function parseSourceString( sourceString, { workDirectoryPath } ) {
};
}

// SSH URLs (git)
const supportedProtocols = [ 'ssh:', 'git+ssh:' ];
try {
const sshUrl = new URL( sourceString );
if ( supportedProtocols.includes( sshUrl.protocol ) ) {
const pathElements = sshUrl.pathname
.split( '/' )
.filter( ( e ) => !! e );
const basename = pathElements
.slice( -1 )[ 0 ]
.replace( /\.git/, '' );
const workingPath = path.resolve(
workDirectoryPath,
...pathElements.slice( 0, -1 ),
basename
);
return {
type: 'git',
url: sshUrl.href.split( '#' )[ 0 ],
ref: sshUrl.hash.slice( 1 ) || 'master',
noahtallen marked this conversation as resolved.
Show resolved Hide resolved
path: workingPath,
clonePath: workingPath,
basename,
};
}
} catch ( err ) {}

const gitHubFields = sourceString.match(
/^([^\/]+)\/([^#\/]+)(\/([^#]+))?(?:#(.+))?$/
);
Expand Down
61 changes: 61 additions & 0 deletions packages/env/test/parse-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* External dependencies
*/
const _path = require( 'path' );

/**
* Internal dependencies
*/
const { parseSourceString } = require( '../lib/config/parse-config' );

const parseSourceStringOptions = { workDirectoryPath: '.' };
const currentDirectory = _path.resolve( '.' );

describe( 'parseSourceString', () => {
it( 'returns null for null source', () => {
const wpSource = parseSourceString( null, {} );
expect( wpSource ).toBeNull();
} );
} );

const gitTests = [
{
sourceString: 'ssh://git@github.com/short.git',
url: 'ssh://git@github.com/short.git',
ref: 'master',
path: currentDirectory + '/short',
clonePath: currentDirectory + '/short',
basename: 'short',
},
{
sourceString: 'ssh://git@github.com/owner/long/path/repo.git',
url: 'ssh://git@github.com/owner/long/path/repo.git',
ref: 'master',
path: currentDirectory + '/owner/long/path/repo',
clonePath: currentDirectory + '/owner/long/path/repo',
basename: 'repo',
},
{
sourceString: 'git+ssh://git@github.com/owner/repo.git#kitchen-sink',
url: 'git+ssh://git@github.com/owner/repo.git',
ref: 'kitchen-sink',
path: currentDirectory + '/owner/repo',
clonePath: currentDirectory + '/owner/repo',
basename: 'repo',
},
];

describe.each( gitTests )( 'parseSourceString', ( source ) => {
it( `parses ${ source.sourceString }`, () => {
const { type, url, ref, path, clonePath, basename } = parseSourceString(
source.sourceString,
parseSourceStringOptions
);
expect( type ).toBe( 'git' );
expect( url ).toBe( source.url );
expect( ref ).toBe( source.ref );
expect( path ).toBe( source.path );
expect( clonePath ).toBe( source.clonePath );
expect( basename ).toBe( source.basename );
} );
} );