Skip to content

Commit

Permalink
feature: introduce semver via composer/semver to better detect PHP …
Browse files Browse the repository at this point in the history
…version ranges

Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed Aug 10, 2022
1 parent 3570ac9 commit 15c66c4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 24 deletions.
14 changes: 13 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ RUN npm ci
COPY ./src ./src
RUN npm run build


FROM ghcr.io/boesing/composer-semver:1.0 as composer-semver
FROM php:8.1.9-cli-alpine as php
FROM node:18-alpine

LABEL "repository"="http://github.com/laminas/laminas-ci-matrix-action"
LABEL "homepage"="http://github.com/laminas/laminas-ci-matrix-action"
LABEL "maintainer"="https://github.com/laminas/technical-steering-committee/"
Expand All @@ -25,6 +27,16 @@ ADD laminas-ci.schema.json /action/
COPY --from=compiler /usr/local/source/dist/main.js /action/
RUN chmod u+x /action/main.js

# Setup PHP
RUN mkdir -p /usr/local/bin /usr/local/etc /usr/local/lib
COPY --from=php /usr/local/bin/php /usr/local/bin
COPY --from=php /usr/local/etc/* /usr/local/etc
COPY --from=php /usr/local/lib/php/* /usr/local/lib/php
COPY --from=php /usr/lib/* /usr/lib
COPY --from=php /lib/* /lib

COPY --from=composer-semver /usr/local/bin/main.phar /usr/local/bin/composer-semver.phar

ADD entrypoint.sh /usr/local/bin/entrypoint.sh

ENTRYPOINT ["entrypoint.sh"]
33 changes: 19 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
},
"type": "commonjs",
"devDependencies": {
"@types/node": "^18.6.4",
"@typescript-eslint/eslint-plugin": "^5.16.0",
"@typescript-eslint/parser": "^5.16.0",
"@types/node": "^18.6.4",
"eslint": "^8.11.0",
"eslint-config-incredible": "^2.4.2",
"eslint-import-resolver-typescript": "^2.7.0",
Expand All @@ -25,7 +25,6 @@
"dependencies": {
"@actions/core": "^1.6.0",
"@cfworker/json-schema": "^1.12.2",
"@types/semver": "^7.3.9",
"semver": "^7.3.5"
"child_process": "^1.0.2"
}
}
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export class App {
this.checkRequirements(filesFromDiff),
this.composerJsonFileName,
this.composerLockJsonFileName,
this.continousIntegrationConfigurationJsonFileName
this.continousIntegrationConfigurationJsonFileName,
this.logger
);
}

Expand Down
21 changes: 16 additions & 5 deletions src/config/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs, {PathLike} from 'fs';
import semver from 'semver';
import parseJsonFile from '../json';
import {Tool, ToolExecutionType} from '../tools';
import {Logger} from '../logging';
import {CURRENT_STABLE, INSTALLABLE_VERSIONS, InstallablePhpVersionType, isInstallableVersion} from './php';
import {ComposerJson} from './composer';
import {ConfigurationFromFile, isAdditionalChecksConfiguration, isAnyComposerDependencySet, isAnyPhpVersionType, isConfigurationContainingJobExclusions, isExplicitChecksConfiguration, isLatestPhpVersionType, isLowestPhpVersionType, JobDefinitionFromFile, JobFromFile, JobToExcludeFromFile} from './input';
import {satisfies} from "../semver";

export const OPERATING_SYSTEM = 'ubuntu-latest';
export const ACTION = 'laminas/laminas-continuous-integration-action@v1';
Expand All @@ -16,19 +16,29 @@ export enum ComposerDependencySet {
LATEST = 'latest',
}

function gatherVersions(composerJson: ComposerJson): InstallablePhpVersionType[] {
function gatherVersions(composerJson: ComposerJson, logger: Logger): InstallablePhpVersionType[] {
if (JSON.stringify(composerJson) === '{}') {
logger.debug('The composer.json file is either empty or does not exist.');
return [];
}

const composerPhpVersion: string = (composerJson.require?.php ?? '').replace(/,\s/, ' ');

if (composerPhpVersion === '') {
logger.debug('`composer.json` does not contain any PHP requirement.');
return [];
}

return INSTALLABLE_VERSIONS
.filter((version) => semver.satisfies(`${version}.0`, composerPhpVersion));
.filter((version) => {
if (satisfies(`${version}.0`, composerPhpVersion)) {
logger.debug(`PHP ${version} is supported by projects \`composer.json\`!`)
return true;
}

logger.debug(`PHP ${version} is NOT supported by projects \`composer.json\`!`)
return false;
});
}

function gatherExtensions(composerJson: ComposerJson): Set<string> {
Expand Down Expand Up @@ -418,12 +428,13 @@ export default function createConfig(
requirements: Requirements,
composerJsonFileName: PathLike,
composerLockJsonFileName: PathLike,
continousIntegrationConfigurationJsonFileName: PathLike
continousIntegrationConfigurationJsonFileName: PathLike,
logger: Logger
): Config {
const composerJson: ComposerJson = parseJsonFile(composerJsonFileName) as ComposerJson;
const configurationFromFile: ConfigurationFromFile =
parseJsonFile(continousIntegrationConfigurationJsonFileName) as ConfigurationFromFile;
const phpVersionsSupportedByProject: InstallablePhpVersionType[] = gatherVersions(composerJson);
const phpVersionsSupportedByProject: InstallablePhpVersionType[] = gatherVersions(composerJson, logger);
let phpExtensions: Set<string> = gatherExtensions(composerJson);
let stablePHPVersion: InstallablePhpVersionType = CURRENT_STABLE;

Expand Down
10 changes: 10 additions & 0 deletions src/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {execSync} from "child_process";

export function satisfies(version: string, constraint: string): boolean {
try {
execSync(`/usr/local/bin/composer-semver.phar semver:match "${version}" "${constraint}" > /dev/null`)
return true;
} catch (error) {
return false;
}
}

0 comments on commit 15c66c4

Please sign in to comment.