Improve composer setup

This commit is contained in:
Shivam Mathur 2020-07-07 16:36:32 +05:30
parent e763fddf01
commit a3d5500a6a
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
9 changed files with 1096 additions and 73 deletions

View File

@ -1,5 +1,6 @@
name: Main workflow name: Main workflow
on: on:
workflow_dispatch:
pull_request: pull_request:
branches: branches:
- releases/v1 - releases/v1

View File

@ -1,5 +1,21 @@
import * as httpm from '@actions/http-client';
import * as tools from '../src/tools'; 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', () => { describe('Tools tests', () => {
it('checking getCommand', async () => { it('checking getCommand', async () => {
expect(await tools.getCommand('linux', 'tool')).toBe('add_tool '); expect(await tools.getCommand('linux', 'tool')).toBe('add_tool ');
@ -263,20 +279,24 @@ describe('Tools tests', () => {
).toStrictEqual(['composer:2', 'a', 'b', 'c']); ).toStrictEqual(['composer:2', 'a', 'b', 'c']);
}); });
it('checking updateComposer', async () => { it('checking getComposerUrl', async () => {
expect(await tools.updateComposer('latest', 'linux')).toContain(''); expect(await tools.getComposerUrl('latest')).toContain(
expect(await tools.updateComposer('stable', 'win32')).toContain(''); 'https://getcomposer.org/composer-stable.phar'
expect(await tools.updateComposer('snapshot', 'darwin')).toContain(
'\ncomposer self-update --snapshot'
); );
expect(await tools.updateComposer('preview', 'linux')).toContain( expect(await tools.getComposerUrl('stable')).toContain(
'\ncomposer self-update --preview' 'https://getcomposer.org/composer-stable.phar'
); );
expect(await tools.updateComposer('1', 'win32')).toContain( expect(await tools.getComposerUrl('snapshot')).toContain(
'\ncomposer self-update --1' 'https://getcomposer.org/composer.phar'
); );
expect(await tools.updateComposer('2', 'darwin')).toContain( expect(await tools.getComposerUrl('preview')).toContain(
'\ncomposer self-update --2' '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'
); );
}); });
@ -527,17 +547,20 @@ describe('Tools tests', () => {
'linux' 'linux'
); );
expect(script).toContain( 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'); 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( script = await tools.addTools(
'composer:v1, composer:preview, composer:snapshot', 'composer:v1, composer:preview, composer:snapshot',
'7.4', '7.4',
'linux' '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/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": { "@actions/io": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.2.tgz", "resolved": "https://registry.npmjs.org/@actions/io/-/io-1.0.2.tgz",
@ -970,9 +978,9 @@
} }
}, },
"@types/babel__traverse": { "@types/babel__traverse": {
"version": "7.0.12", "version": "7.0.13",
"resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.0.12.tgz", "resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.0.13.tgz",
"integrity": "sha512-t4CoEokHTfcyfb4hUaF9oOHu9RmmNWnm1CP0YmMqOOfClKascOmvlEM736vlqeScuGvBDsHkf8R2INd4DWreQA==", "integrity": "sha512-i+zS7t6/s9cdQvbqKDARrcbrPvtJGlbYsMkazo03nTAK3RX9FNrLllXys22uiTGJapPOTZTQ35nHh4ISph4SLQ==",
"dev": true, "dev": true,
"requires": { "requires": {
"@babel/types": "^7.3.0" "@babel/types": "^7.3.0"
@ -1025,9 +1033,9 @@
} }
}, },
"@types/jest": { "@types/jest": {
"version": "26.0.3", "version": "26.0.4",
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.3.tgz", "resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.4.tgz",
"integrity": "sha512-v89ga1clpVL/Y1+YI0eIu1VMW+KU7Xl8PhylVtDKVWaSUHBHYPLXMQGBdrpHewaKoTvlXkksbYqPgz8b4cmRZg==", "integrity": "sha512-4fQNItvelbNA9+sFgU+fhJo8ZFF+AS4Egk3GWwCW2jFtViukXbnztccafAdLhzE/0EiCogljtQQXP8aQ9J7sFg==",
"dev": true, "dev": true,
"requires": { "requires": {
"jest-diff": "^25.2.1", "jest-diff": "^25.2.1",
@ -1047,9 +1055,9 @@
"dev": true "dev": true
}, },
"@types/node": { "@types/node": {
"version": "14.0.14", "version": "14.0.18",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.14.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.18.tgz",
"integrity": "sha512-syUgf67ZQpaJj01/tRTknkMNoBBLWJOBODF0Zm4NrXmiSuxjymFrxnTu1QVYRubhVkRcZLYZG8STTwJRdVm/WQ==", "integrity": "sha512-0Z3nS5acM0cIV4JPzrj9g/GH0Et5vmADWtip3YOXOp1NpOLU8V3KoZDc8ny9c1pe/YSYYzQkAWob6dyV/EWg4g==",
"dev": true "dev": true
}, },
"@types/normalize-package-data": { "@types/normalize-package-data": {
@ -1065,9 +1073,9 @@
"dev": true "dev": true
}, },
"@types/prettier": { "@types/prettier": {
"version": "2.0.1", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.0.2.tgz",
"integrity": "sha512-boy4xPNEtiw6N3abRhBi/e7hNvy3Tt8E9ZRAQrwAGzoCGZS/1wjo9KY7JHhnfnEsG5wSjDbymCozUM9a3ea7OQ==", "integrity": "sha512-IkVfat549ggtkZUthUzEX49562eGikhSYeVGX97SkMFn+sTZrgRewXjQ4tPKFPCykZHkX1Zfd9OoELGqKU2jJA==",
"dev": true "dev": true
}, },
"@types/stack-utils": { "@types/stack-utils": {
@ -6713,6 +6721,11 @@
"tslib": "^1.8.1" "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": { "tunnel-agent": {
"version": "0.6.0", "version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",

View File

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

View File

@ -105,7 +105,23 @@ add_unstable_extension() {
add_pecl_extension "$extension" "$pecl_version" "$prefix" add_pecl_extension "$extension" "$pecl_version" "$prefix"
} }
# Function to setup a remote tool # 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
}
# Function to setup a remote tool.
add_tool() { add_tool() {
url=$1 url=$1
tool=$2 tool=$2
@ -118,11 +134,10 @@ add_tool() {
if [ "$status_code" = "200" ]; then if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path" sudo chmod a+x "$tool_path"
if [ "$tool" = "composer" ]; then if [ "$tool" = "composer" ]; then
composer -q global config process-timeout 0 configure_composer "$tool_path"
echo "::add-path::/Users/$USER/.composer/vendor/bin" elif [ "$tool" = "phan" ]; then
if [ -n "$COMPOSER_TOKEN" ]; then add_extension fileinfo "sudo pecl install -f fileinfo" extension >/dev/null 2>&1
composer -q global config github-oauth.github.com "$COMPOSER_TOKEN" add_extension ast "sudo pecl install -f ast" extension >/dev/null 2>&1
fi
elif [ "$tool" = "phive" ]; then elif [ "$tool" = "phive" ]; then
add_extension curl "sudo pecl install -f curl" extension >/dev/null 2>&1 add_extension curl "sudo pecl install -f curl" extension >/dev/null 2>&1
add_extension mbstring "sudo pecl install -f mbstring" extension >/dev/null 2>&1 add_extension mbstring "sudo pecl install -f mbstring" extension >/dev/null 2>&1

View File

@ -16,6 +16,16 @@ add_log() {
fi fi
} }
# Function to backup and cleanup package lists.
cleanup_lists() {
if [ ! -e /etc/apt/sources.list.d.save ]; then
sudo mv /etc/apt/sources.list.d /etc/apt/sources.list.d.save
sudo mkdir /etc/apt/sources.list.d
sudo mv /etc/apt/sources.list.d.save/*ondrej*.list /etc/apt/sources.list.d/
trap "sudo mv /etc/apt/sources.list.d.save/*.list /etc/apt/sources.list.d/" exit
fi
}
# Function to update php ppa # Function to update php ppa
update_lists() { update_lists() {
if [ "$lists_updated" = "false" ]; then if [ "$lists_updated" = "false" ]; then
@ -140,7 +150,23 @@ update_extension() {
fi fi
} }
# Function to setup a remote tool # 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
}
# Function to setup a remote tool.
add_tool() { add_tool() {
url=$1 url=$1
tool=$2 tool=$2
@ -152,11 +178,7 @@ add_tool() {
if [ "$status_code" = "200" ]; then if [ "$status_code" = "200" ]; then
sudo chmod a+x "$tool_path" sudo chmod a+x "$tool_path"
if [ "$tool" = "composer" ]; then if [ "$tool" = "composer" ]; then
composer -q global config process-timeout 0 configure_composer "$tool_path"
echo "::add-path::/home/$USER/.composer/vendor/bin"
if [ -n "$COMPOSER_TOKEN" ]; then
composer -q global config github-oauth.github.com "$COMPOSER_TOKEN"
fi
elif [ "$tool" = "cs2pr" ]; then elif [ "$tool" = "cs2pr" ]; then
sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path" sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path"
elif [ "$tool" = "phive" ]; then elif [ "$tool" = "phive" ]; then

View File

@ -109,6 +109,32 @@ 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 Add-Tool() { Function Add-Tool() {
Param ( Param (
[Parameter(Position = 0, Mandatory = $true)] [Parameter(Position = 0, Mandatory = $true)]
@ -147,11 +173,9 @@ Function Add-Tool() {
} elseif($tool -eq "cs2pr") { } elseif($tool -eq "cs2pr") {
(Get-Content $php_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $php_dir/cs2pr (Get-Content $php_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $php_dir/cs2pr
} elseif($tool -eq "composer") { } elseif($tool -eq "composer") {
composer -q global config process-timeout 0 Edit-ComposerConfig $php_dir\$tool
Write-Output "::add-path::$env:APPDATA\Composer\vendor\bin" } elseif($tool -eq "wp-cli") {
if (Test-Path env:COMPOSER_TOKEN) { Copy-Item $php_dir\wp-cli.bat -Destination $php_dir\wp.bat
composer -q global config github-oauth.github.com $env:COMPOSER_TOKEN
}
} }
if (((Get-ChildItem -Path $php_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) { if (((Get-ChildItem -Path $php_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) {

View File

@ -1,4 +1,6 @@
import * as utils from './utils'; 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 * Function to get command to setup tools
@ -298,27 +300,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 version
* @param os_version
*/ */
export async function updateComposer( export async function getComposerUrl(version: string): Promise<string> {
version: string, const getComposerUrlHelper = async function (
os_version: string version: string
): Promise<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) { switch (version) {
case 'snapshot': case 'snapshot':
return 'https://getcomposer.org/composer.phar';
case 'preview': case 'preview':
case '1': case '1':
case '2': case '2':
return ( return await getComposerUrlHelper(version);
'\ncomposer self-update --' +
version +
(await utils.suppressOutput(os_version))
);
default: default:
return ''; return 'https://getcomposer.org/composer-stable.phar';
} }
} }
@ -473,10 +476,8 @@ export async function addTools(
script += await addArchive(tool, version, url, os_version); script += await addArchive(tool, version, url, os_version);
break; break;
case 'composer': case 'composer':
url = 'https://getcomposer.org/composer-stable.phar'; url = await getComposerUrl(version);
script += script += await addArchive('composer', version, url, os_version);
(await addArchive('composer', version, url, os_version)) +
(await updateComposer(version, os_version));
break; break;
case 'codeception': case 'codeception':
url = url =