Improve composer setup

This commit is contained in:
Shivam Mathur 2020-07-07 16:36:32 +05:30
parent 38731c6386
commit 5753a00ef5
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
9 changed files with 1086 additions and 83 deletions

View File

@ -1,5 +1,6 @@
name: Main workflow
on:
workflow_dispatch:
pull_request:
branches:
- master

View File

@ -1,5 +1,21 @@
import * as httpm from '@actions/http-client';
import * as tools from '../src/tools';
httpm.HttpClient.prototype.get = jest.fn().mockImplementation(() => {
return {
message: null,
readBody: jest.fn().mockImplementation(() => {
return JSON.stringify({
stable: [{path: '/composer-stable.phar'}],
preview: [{path: '/composer-preview.phar'}],
snapshot: [{path: '/composer.phar'}],
'1': [{path: '/composer-1.phar'}],
'2': [{path: '/composer-2.phar'}]
});
})
};
});
describe('Tools tests', () => {
it('checking getCommand', async () => {
expect(await tools.getCommand('linux', 'tool')).toBe('add_tool ');
@ -239,20 +255,24 @@ describe('Tools tests', () => {
).toStrictEqual(['composer:2', 'a', 'b', 'c']);
});
it('checking updateComposer', async () => {
expect(await tools.updateComposer('latest', 'linux')).toContain('');
expect(await tools.updateComposer('stable', 'win32')).toContain('');
expect(await tools.updateComposer('snapshot', 'darwin')).toContain(
'\ncomposer self-update --snapshot'
it('checking getComposerUrl', async () => {
expect(await tools.getComposerUrl('latest')).toContain(
'https://getcomposer.org/composer-stable.phar'
);
expect(await tools.updateComposer('preview', 'linux')).toContain(
'\ncomposer self-update --preview'
expect(await tools.getComposerUrl('stable')).toContain(
'https://getcomposer.org/composer-stable.phar'
);
expect(await tools.updateComposer('1', 'win32')).toContain(
'\ncomposer self-update --1'
expect(await tools.getComposerUrl('snapshot')).toContain(
'https://getcomposer.org/composer.phar'
);
expect(await tools.updateComposer('2', 'darwin')).toContain(
'\ncomposer self-update --2'
expect(await tools.getComposerUrl('preview')).toContain(
'https://getcomposer.org/composer-preview.phar'
);
expect(await tools.getComposerUrl('1')).toContain(
'https://getcomposer.org/composer-1.phar'
);
expect(await tools.getComposerUrl('2')).toContain(
'https://getcomposer.org/composer-2.phar'
);
});
@ -547,17 +567,20 @@ describe('Tools tests', () => {
'linux'
);
expect(script).toContain(
'add_tool https://getcomposer.org/composer-stable.phar composer'
'add_tool https://getcomposer.org/composer-1.phar composer'
);
expect(script).toContain('composer self-update --1');
script = await tools.addTools('composer:preview', '7.4', 'linux');
expect(script).toContain('composer self-update --preview');
expect(script).toContain(
'add_tool https://getcomposer.org/composer-preview.phar composer'
);
script = await tools.addTools(
'composer:v1, composer:preview, composer:snapshot',
'7.4',
'linux'
);
expect(script).toContain('composer self-update --snapshot');
expect(script).toContain(
'add_tool https://getcomposer.org/composer.phar composer'
);
});
});

949
dist/index.js vendored

File diff suppressed because it is too large Load Diff

37
package-lock.json generated
View File

@ -17,6 +17,14 @@
"@actions/io": "^1.0.1"
}
},
"@actions/http-client": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/@actions/http-client/-/http-client-1.0.8.tgz",
"integrity": "sha512-G4JjJ6f9Hb3Zvejj+ewLLKLf99ZC+9v+yCxoYf9vSyH+WkzPLB2LuUtRMGNkooMqdugGBFStIKXOuvH1W+EctA==",
"requires": {
"tunnel": "0.0.6"
}
},
"@actions/io": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.2.tgz",
@ -970,9 +978,9 @@
}
},
"@types/babel__traverse": {
"version": "7.0.12",
"resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.0.12.tgz",
"integrity": "sha512-t4CoEokHTfcyfb4hUaF9oOHu9RmmNWnm1CP0YmMqOOfClKascOmvlEM736vlqeScuGvBDsHkf8R2INd4DWreQA==",
"version": "7.0.13",
"resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.0.13.tgz",
"integrity": "sha512-i+zS7t6/s9cdQvbqKDARrcbrPvtJGlbYsMkazo03nTAK3RX9FNrLllXys22uiTGJapPOTZTQ35nHh4ISph4SLQ==",
"dev": true,
"requires": {
"@babel/types": "^7.3.0"
@ -1025,9 +1033,9 @@
}
},
"@types/jest": {
"version": "26.0.3",
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.3.tgz",
"integrity": "sha512-v89ga1clpVL/Y1+YI0eIu1VMW+KU7Xl8PhylVtDKVWaSUHBHYPLXMQGBdrpHewaKoTvlXkksbYqPgz8b4cmRZg==",
"version": "26.0.4",
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.4.tgz",
"integrity": "sha512-4fQNItvelbNA9+sFgU+fhJo8ZFF+AS4Egk3GWwCW2jFtViukXbnztccafAdLhzE/0EiCogljtQQXP8aQ9J7sFg==",
"dev": true,
"requires": {
"jest-diff": "^25.2.1",
@ -1047,9 +1055,9 @@
"dev": true
},
"@types/node": {
"version": "14.0.14",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.14.tgz",
"integrity": "sha512-syUgf67ZQpaJj01/tRTknkMNoBBLWJOBODF0Zm4NrXmiSuxjymFrxnTu1QVYRubhVkRcZLYZG8STTwJRdVm/WQ==",
"version": "14.0.18",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.18.tgz",
"integrity": "sha512-0Z3nS5acM0cIV4JPzrj9g/GH0Et5vmADWtip3YOXOp1NpOLU8V3KoZDc8ny9c1pe/YSYYzQkAWob6dyV/EWg4g==",
"dev": true
},
"@types/normalize-package-data": {
@ -1065,9 +1073,9 @@
"dev": true
},
"@types/prettier": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.0.1.tgz",
"integrity": "sha512-boy4xPNEtiw6N3abRhBi/e7hNvy3Tt8E9ZRAQrwAGzoCGZS/1wjo9KY7JHhnfnEsG5wSjDbymCozUM9a3ea7OQ==",
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.0.2.tgz",
"integrity": "sha512-IkVfat549ggtkZUthUzEX49562eGikhSYeVGX97SkMFn+sTZrgRewXjQ4tPKFPCykZHkX1Zfd9OoELGqKU2jJA==",
"dev": true
},
"@types/stack-utils": {
@ -6713,6 +6721,11 @@
"tslib": "^1.8.1"
}
},
"tunnel": {
"version": "0.0.6",
"resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz",
"integrity": "sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg=="
},
"tunnel-agent": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",

View File

@ -26,6 +26,7 @@
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.4",
"@actions/http-client": "^1.0.8",
"@actions/io": "^1.0.2",
"fs": "0.0.1-security"
},

View File

@ -127,6 +127,26 @@ add_unstable_extension() {
add_pecl_extension "$extension" "$pecl_version" "$prefix"
}
# Function to configure composer
configure_composer() {
tool_path=$1
sudo ln -sf "$tool_path" "$tool_path.phar"
php -r "try {\$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}"
if [ $? -eq 1 ]; then
add_log "$cross" "composer" "Could not download composer"
exit 1;
fi
composer -q global config process-timeout 0
echo "::add-path::/Users/$USER/.composer/vendor/bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q global config github-oauth.github.com "$COMPOSER_TOKEN"
fi
# TODO: Remove after composer 2.0 update, fixes peer fingerprint error
if [[ "$version" =~ $old_versions ]]; then
composer -q global config repos.packagist composer https://repo-ca-bhs-1.packagist.org
fi
}
# Function to setup a remote tool.
add_tool() {
url=$1
@ -140,15 +160,7 @@ add_tool() {
if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path"
if [ "$tool" = "composer" ]; then
composer -q global config process-timeout 0
echo "::add-path::/Users/$USER/.composer/vendor/bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q global config github-oauth.github.com "$COMPOSER_TOKEN"
fi
# TODO: Remove after composer 2.0 update, fixes peer fingerprint error
if [[ "$version" =~ $old_versions ]]; then
composer -q global config repos.packagist composer https://repo-ca-bhs-1.packagist.org
fi
configure_composer "$tool_path"
elif [ "$tool" = "phan" ]; then
add_extension fileinfo "sudo pecl install -f fileinfo" extension >/dev/null 2>&1
add_extension ast "sudo pecl install -f ast" extension >/dev/null 2>&1

View File

@ -228,6 +228,26 @@ add_extension_from_source() {
) || add_log "$cross" "$extension" "Could not install $extension-$release on PHP $semver"
}
# Function to configure composer
configure_composer() {
tool_path=$1
sudo ln -sf "$tool_path" "$tool_path.phar"
php -r "try {\$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}"
if [ $? -eq 1 ]; then
add_log "$cross" "composer" "Could not download composer"
exit 1;
fi
composer -q global config process-timeout 0
echo "::add-path::/home/$USER/.composer/vendor/bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q global config github-oauth.github.com "$COMPOSER_TOKEN"
fi
# TODO: Remove after composer 2.0 update, fixes peer fingerprint error
if [[ "$version" =~ $old_versions ]]; then
composer -q global config repos.packagist composer https://repo-ca-bhs-1.packagist.org
fi
}
# Function to setup a remote tool.
add_tool() {
url=$1
@ -240,15 +260,7 @@ add_tool() {
if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path"
if [ "$tool" = "composer" ]; then
composer -q global config process-timeout 0
echo "::add-path::/home/$USER/.composer/vendor/bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q global config github-oauth.github.com "$COMPOSER_TOKEN"
fi
# TODO: Remove after composer 2.0 update, fixes peer fingerprint error
if [[ "$version" =~ $old_versions ]]; then
composer -q global config repos.packagist composer https://repo-ca-bhs-1.packagist.org
fi
configure_composer "$tool_path"
elif [ "$tool" = "cs2pr" ]; then
sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path"
elif [ "$tool" = "phan" ]; then

View File

@ -161,6 +161,31 @@ Function Remove-Extension() {
}
}
Function Edit-ComposerConfig() {
Param(
[Parameter(Position = 0, Mandatory = $true)]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string]
$tool_path
)
Copy-Item $tool_path -Destination "$tool_path.phar"
php -r "try {`$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception `$e) {exit(1);}"
if ($? -eq $False) {
Add-Log "$cross" "composer" "Could not download composer"
exit 1;
}
composer -q global config process-timeout 0
Write-Output "::add-path::$env:APPDATA\Composer\vendor\bin"
if (Test-Path env:COMPOSER_TOKEN) {
composer -q global config github-oauth.github.com $env:COMPOSER_TOKEN
}
# TODO: Remove after composer 2.0 update, fixes peer fingerprint error
if ($version -lt 5.6) {
composer -q global config repos.packagist composer https://repo-ca-bhs-1.packagist.org
}
}
# Function to add tools.
Function Add-Tool() {
Param (
@ -201,15 +226,7 @@ Function Add-Tool() {
} elseif($tool -eq "cs2pr") {
(Get-Content $bin_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $bin_dir/cs2pr
} elseif($tool -eq "composer") {
composer -q global config process-timeout 0
Write-Output "::add-path::$env:APPDATA\Composer\vendor\bin"
if (Test-Path env:COMPOSER_TOKEN) {
composer -q global config github-oauth.github.com $env:COMPOSER_TOKEN
}
# TODO: Remove after composer 2.0 update, fixes peer fingerprint error
if ($version -lt 5.6) {
composer -q global config repos.packagist composer https://repo-ca-bhs-1.packagist.org
}
Edit-ComposerConfig $bin_dir\$tool
} elseif($tool -eq "wp-cli") {
Copy-Item $bin_dir\wp-cli.bat -Destination $bin_dir\wp.bat
}

View File

@ -1,4 +1,6 @@
import * as utils from './utils';
import * as httpm from '@actions/http-client';
import {IHttpClientResponse as hcr} from '@actions/http-client/interfaces';
/**
* Function to get command to setup tools
@ -319,27 +321,28 @@ export async function addComposer(tools_list: string[]): Promise<string[]> {
}
/**
* Function to get script to update composer
* Function to get composer URL for a given version
*
* @param version
* @param os_version
*/
export async function updateComposer(
version: string,
os_version: string
): Promise<string> {
export async function getComposerUrl(version: string): Promise<string> {
const getComposerUrlHelper = async function (
version: string
): Promise<string> {
const client: httpm.HttpClient = new httpm.HttpClient('setup-php');
const response: hcr = await client.get('https://getcomposer.org/versions');
const data = JSON.parse(await response.readBody());
return 'https://getcomposer.org' + data[version][0]['path'];
};
switch (version) {
case 'snapshot':
return 'https://getcomposer.org/composer.phar';
case 'preview':
case '1':
case '2':
return (
'\ncomposer self-update --' +
version +
(await utils.suppressOutput(os_version))
);
return await getComposerUrlHelper(version);
default:
return '';
return 'https://getcomposer.org/composer-stable.phar';
}
}
@ -513,10 +516,8 @@ export async function addTools(
script += await addArchive(tool, version, url, os_version);
break;
case 'composer':
url = 'https://getcomposer.org/composer-stable.phar';
script +=
(await addArchive('composer', version, url, os_version)) +
(await updateComposer(version, os_version));
url = await getComposerUrl(version);
script += await addArchive('composer', version, url, os_version);
break;
case 'codeception':
url =