Skip to content

Commit

Permalink
Merge 258293f into 4e7f5fc
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed May 7, 2020
2 parents 4e7f5fc + 258293f commit b87dbfb
Show file tree
Hide file tree
Showing 31 changed files with 746 additions and 793 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Unreleased
- Add AWS CodeBuild detection support.
- Allow late static binding in `CiDetector::fromEnvironment()` when inheriting the class.
- Fix branch detection in PR builds on AppVeyor, Buddy and GitHub Actions (target branch of the PR was returned instead).

## 3.3.0 - 2020-03-06
- Allow injecting instance of `Env` using `CiDetector::fromEnvironment()` (useful for environment mocking in unit tests).
Expand Down
9 changes: 7 additions & 2 deletions src/Ci/AppVeyor.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ public function getGitCommit(): string

public function getGitBranch(): string
{
// For PR builds this is the base branch (not the PR branch)
return $this->env->getString('APPVEYOR_REPO_BRANCH');
$prBranch = $this->env->getString('APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH');

if ($this->isPullRequest()->no() || empty($prBranch)) {
return $this->env->getString('APPVEYOR_REPO_BRANCH');
}

return $prBranch;
}

public function getRepositoryName(): string
Expand Down
4 changes: 4 additions & 0 deletions src/Ci/Bamboo.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function getGitCommit(): string

public function getGitBranch(): string
{
// TODO: While the PR source branch is in `bamboo.repository_pr_sourceBranch`, it cannot be reliably
// used, probably because it is environment variables with dot in name.
// As a result, returned branch in case of PR build is the target branch (eg. master) not the PR branch.

return $this->env->getString('bamboo_planRepository_branch');
}

Expand Down
8 changes: 7 additions & 1 deletion src/Ci/Buddy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ public function getGitCommit(): string

public function getGitBranch(): string
{
return $this->env->getString('BUDDY_EXECUTION_BRANCH');
$prBranch = $this->env->getString('BUDDY_EXECUTION_PULL_REQUEST_HEAD_BRANCH');

if ($this->isPullRequest()->no() || empty($prBranch)) {
return $this->env->getString('BUDDY_EXECUTION_BRANCH');
}

return $prBranch;
}

public function getRepositoryName(): string
Expand Down
10 changes: 8 additions & 2 deletions src/Ci/GitHubActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ public function getGitCommit(): string

public function getGitBranch(): string
{
$gitReference = $this->env->getString('GITHUB_REF');
$prBranch = $this->env->getString('GITHUB_HEAD_REF');

return preg_replace('~^refs/heads/~', '', $gitReference);
if ($this->isPullRequest()->no() || empty($prBranch)) {
$gitReference = $this->env->getString('GITHUB_REF');

return preg_replace('~^refs/heads/~', '', $gitReference);
}

return $prBranch;
}

public function getRepositoryName(): string
Expand Down
8 changes: 4 additions & 4 deletions tests/phpt/Ci/CiDetector_AppVeyor.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
AppVeyor: Detect properties

--ENV--
APPVEYOR=True
APPVEYOR_ACCOUNT_NAME=OndraM
APPVEYOR_API_URL=http://localhost:1563/
APPVEYOR_BUILD_FOLDER=C:\projects\ci-detector
Expand All @@ -16,25 +15,26 @@ APPVEYOR_PROJECT_ID=321955
APPVEYOR_PROJECT_NAME=ci-detector
APPVEYOR_PROJECT_SLUG=ci-detector
APPVEYOR_REPO_BRANCH=branchname
APPVEYOR_REPO_COMMIT=11cc783de14cf438a41a60af7cd148a43da74cce
APPVEYOR_REPO_COMMIT_AUTHOR=Ondrej Machulda
APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL=ondrej.machulda@gmail.com
APPVEYOR_REPO_COMMIT_AUTHOR=Ondrej Machulda
APPVEYOR_REPO_COMMIT_MESSAGE=Foo bar message
APPVEYOR_REPO_COMMIT_TIMESTAMP=2017-05-26T14:48:13.0000000Z
APPVEYOR_REPO_COMMIT=11cc783de14cf438a41a60af7cd148a43da74cce
APPVEYOR_REPO_NAME=OndraM/ci-detector
APPVEYOR_REPO_PROVIDER=gitHub
APPVEYOR_REPO_SCM=git
APPVEYOR_REPO_TAG=false
APPVEYOR_URL=https://ci.appveyor.com
APPVEYOR=True
CI=True
HOMEDRIVE=C:
HOMEPATH=\Users\appveyor
OS=Windows_NT
Platform=x64
SystemDrive=C:
SystemRoot=C:\Windows
USERDOMAIN=APPVYR-WIN
USERDOMAIN_ROAMINGPROFILE=APPVYR-WIN
USERDOMAIN=APPVYR-WIN
USERNAME=appveyor
windir=C:\Windows

Expand Down
16 changes: 10 additions & 6 deletions tests/phpt/Ci/CiDetector_AppVeyorPullRequest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
AppVeyor: Detect properties of PR build

--ENV--
APPVEYOR=True
APPVEYOR_ACCOUNT_NAME=OndraM
APPVEYOR_API_URL=http://localhost:1563/
APPVEYOR_BUILD_FOLDER=C:\projects\ci-detector
Expand All @@ -16,30 +15,31 @@ APPVEYOR_PROJECT_ID=321955
APPVEYOR_PROJECT_NAME=ci-detector
APPVEYOR_PROJECT_SLUG=ci-detector
APPVEYOR_PULL_REQUEST_HEAD_COMMIT=4adb3ce7ab78a1aeec59271f81d15fdec9c3d899
APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH=repo_name
APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH=feature/pr-branch
APPVEYOR_PULL_REQUEST_HEAD_REPO_NAME=foo/ci-detector
APPVEYOR_PULL_REQUEST_NUMBER=42
APPVEYOR_PULL_REQUEST_TITLE=Foo bar PR title
APPVEYOR_REPO_BRANCH=branchname
APPVEYOR_REPO_COMMIT=08750c66ed93c27a192d1b6bd5614788f1e5f1e9
APPVEYOR_REPO_COMMIT_AUTHOR=foo
APPVEYOR_REPO_BRANCH=master
APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL=foo@example.com
APPVEYOR_REPO_COMMIT_AUTHOR=foo
APPVEYOR_REPO_COMMIT_MESSAGE=Foo bar PR commit message
APPVEYOR_REPO_COMMIT_TIMESTAMP=2020-02-11T11:48:29.0000000Z
APPVEYOR_REPO_COMMIT=08750c66ed93c27a192d1b6bd5614788f1e5f1e9
APPVEYOR_REPO_NAME=OndraM/ci-detector
APPVEYOR_REPO_PROVIDER=gitHub
APPVEYOR_REPO_SCM=git
APPVEYOR_REPO_TAG=false
APPVEYOR_URL=https://ci.appveyor.com
APPVEYOR=True
CI=True
HOMEDRIVE=C:
HOMEPATH=\Users\appveyor
OS=Windows_NT
Platform=x64
SystemDrive=C:
SystemRoot=C:\Windows
USERDOMAIN=APPVYR-WIN
USERDOMAIN_ROAMINGPROFILE=APPVYR-WIN
USERDOMAIN=APPVYR-WIN
USERNAME=appveyor
windir=C:\Windows

Expand All @@ -51,7 +51,11 @@ require __DIR__ . '/../../../vendor/autoload.php';
$ci = (new OndraM\CiDetector\CiDetector())->detect();
echo "Is pull request:\n";
var_dump($ci->isPullRequest()->describe());
echo "Git branch:\n";
var_dump($ci->getGitBranch());

--EXPECT--
Is pull request:
string(3) "Yes"
Git branch:
string(17) "feature/pr-branch"
125 changes: 35 additions & 90 deletions tests/phpt/Ci/CiDetector_AwsCodeBuild.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,50 @@
AWS CodeBuild: Detect properties

--ENV--
NODE_10_VERSION=10.19.0
ANDROID_SDK_MANAGER_VER=4333796
MAVEN_DOWNLOAD_SHA512=c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0
MAVEN_OPTS=-Dmaven.wagon.httpconnectionManager.maxPerRoute=2
CODEBUILD_LAST_EXIT=0
CODEBUILD_START_TIME=1585485986820
ANT_VERSION=1.10.6
CODEBUILD_BMR_URL=https://CODEBUILD_AGENT:3000
NODE_12_VERSION=12.16.1
JRE_8_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto/jre
DOCKER_BUCKET=download.docker.com
CODEBUILD_SOURCE_VERSION=test/evn
ANDROID_SDK_PLATFORM_TOOLS_28=platforms;android-28
AWS_ACCOUNT_ID=911074024513
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=/v2/credentials/9ab844f8-d292-41ec-b0b9-3895d1901405
AWS_DEFAULT_OUTPUT=json
AWS_DEFAULT_REGION=eu-west-1
AWS_ECR_CODEBUILD_URI=911074024513.dkr.ecr.eu-west-1.amazonaws.com/project-api-codebuild
AWS_EXECUTION_ENV=AWS_ECS_EC2
AWS_REGION=eu-west-1
CODEBUILD_AGENT_ENDPOINT=http://127.0.0.1:7831
HOSTNAME=202d63ee90d4
CODEBUILD_AUTH_TOKEN=ce79b1c8-ee3b-4981-bb63-86162d6548b2
CODEBUILD_BMR_URL=https://CODEBUILD_AGENT:3000
CODEBUILD_BUILD_ARN=arn:aws:codebuild:eu-west-1:911074024513:build/project-api-pr:449b6959-c0f7-41ac-aac8-7fec365d3004
CODEBUILD_BUILD_ID=project-api-pr:449b6959-c0f7-41ac-aac8-7fec365d3004
CODEBUILD_KMS_KEY_ID=arn:aws:kms:eu-west-1:911074024513:alias/aws/s3
JDK_DOWNLOAD_TAR=java-11-amazon-corretto-jdk.tar
POWERSHELL_DOWNLOAD_URL=https://github.com/PowerShell/PowerShell/releases/download/v6.2.1/powershell-6.2.1-linux-x64.tar.gz
PYTHON_PIP_VERSION=19.3.1
JRE_11_HOME=/opt/jvm/amazon-corretto-11
SRC_DIR=/usr/src
HOME=/root
OLDPWD=/codebuild/readonly
POWERSHELL_VERSION=6.2.1
JRE_HOME=/opt/jvm/amazon-corretto-11
CODEBUILD_GOPATH=/codebuild/output/src341076892
CODEBUILD_CI=true
GOENV_DISABLE_GOPATH=1
CODEBUILD_RESOLVED_SOURCE_VERSION=c150e3a3d2d89acd1b1222bf6f7a50b45a4218c0
CODEBUILD_BUILD_IMAGE=aws/codebuild/standard:4.0
CODEBUILD_BUILD_NUMBER=559
CODEBUILD_BUILD_SUCCEEDING=1
GRADLE_DOWNLOADS_SHA256=14cd15fc8cc8705bd69dcfa3c8fefb27eb7027f5de4b47a8b279218f76895a91 5.4.1\n336b6898b491f6334502d8074a6b8c2d73ed83b92123106bd4bf837f04111043 4.10.3
CODEBUILD_BUILD_ARN=arn:aws:codebuild:eu-west-1:911074024513:build/project-api-pr:449b6959-c0f7-41ac-aac8-7fec365d3004
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI=/v2/credentials/9ab844f8-d292-41ec-b0b9-3895d1901405
MAVEN_HOME=/opt/maven
AWS_EXECUTION_ENV=AWS_ECS_EC2
ANT_DOWNLOAD_SHA512=c1a9694c3018e248000ff6f46d48af85f537ef3935e0d5256543c58a240084c0aff5289fd9e94cbc40d5442f3cc43592398047f2548fded40d9882be2b40750d
DOCKER_SHA256=c3c8833e227b61fe6ce0bc5c17f97fa547035bef4ef17cf6601f30b0f20f4ce5
RUBY_BUILD_SRC_DIR=/usr/local/rbenv/plugins/ruby-build
CODEBUILD_INITIATOR=radim
INSTALLED_GRADLE_VERSIONS=4.10.3 5.4.1
GRADLE_PATH=/usr/src/gradle
PROJECT_NAME=project-api
DIND_COMMIT=3b5fac462d21ca164b3778647420016315289034
AWS_DEFAULT_REGION=eu-west-1
AWS_ECR_CODEBUILD_URI=911074024513.dkr.ecr.eu-west-1.amazonaws.com/project-api-codebuild
PHP_73_VERSION=7.3.13
ECS_CONTAINER_METADATA_URI=http://169.254.170.2/v3/788d7be3-4280-454e-8f3e-ab9ee035ee04
PHP_74_VERSION=7.4.1
CODEBUILD_BUILD_URL=https://eu-west-1.console.aws.amazon.com/codebuild/home?region=eu-west-1#/builds/project-api-pr:449b6959-c0f7-41ac-aac8-7fec365d3004/view/new
CODEBUILD_CI=true
CODEBUILD_CONTAINER_NAME=default
CODEBUILD_EXECUTION_ROLE_BUILD=
DOTNET_31_SDK_VERSION=3.1.102
PATH=/root/.phpenv/shims:/root/.phpenv/bin:/root/.goenv/shims:/root/.goenv/bin:/go/bin:/root/.phpenv/shims:/root/.phpenv/bin:/root/.pyenv/shims:/root/.pyenv/bin:/root/.rbenv/shims:/usr/local/rbenv/bin:/usr/local/rbenv/shims:/root/.dotnet/:/root/.dotnet/tools/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/tools:/usr/local/android-sdk-linux/tools:/usr/local/android-sdk-linux/tools/bin:/usr/local/android-sdk-linux/platform-tools
N_SRC_DIR=/usr/src/n
POWERSHELL_DOWNLOAD_SHA=E8287687C99162BF70FEFCC2E492F3B54F80BE880D86B9A0EC92C71B05C40013
RUBY_26_VERSION=2.6.5
CODEBUILD_FE_REPORT_ENDPOINT=https://codebuild.eu-west-1.amazonaws.com/
CODEBUILD_INITIATOR=radim
CODEBUILD_KMS_KEY_ID=arn:aws:kms:eu-west-1:911074024513:alias/aws/s3
CODEBUILD_LAST_EXIT=0
CODEBUILD_LOG_PATH=449b6959-c0f7-41ac-aac8-7fec365d3004
RUBY_27_VERSION=2.7.0
SBT_VERSION=1.2.8
ANDROID_SDK_EXTRAS=extras;android;m2repository extras;google;m2repository extras;google;google_play_services
DOCKER_CHANNEL=stable
AWS_DEFAULT_OUTPUT=json
CODEBUILD_BUILD_IMAGE=aws/codebuild/standard:4.0
CODEBUILD_PROJECT_UUID=83afc255-1a5a-4ded-a73e-105110a67956
CODEBUILD_RESOLVED_SOURCE_VERSION=c150e3a3d2d89acd1b1222bf6f7a50b45a4218c0
CODEBUILD_SOURCE_REPO_URL=https://github.com/project-app/project-api.git
ANDROID_SDK_MANAGER_SHA256=92ffee5a1d98d856634e8b71132e8a95d96c83a63fde1099be3d86df3106def9
GITVERSION_VERSION=5.1.2
ANDROID_SDK_BUILD_TOOLS=build-tools;29.0.2
AWS_ACCOUNT_ID=911074024513
DEBIAN_FRONTEND=noninteractive
GOPATH=/go:/codebuild/output/src341076892
AWS_REGION=eu-west-1
CODEBUILD_BUILD_URL=https://eu-west-1.console.aws.amazon.com/codebuild/home?region=eu-west-1#/builds/project-api-pr:449b6959-c0f7-41ac-aac8-7fec365d3004/view/new
JAVA_8_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto
GRADLE_VERSION=5.4.1
DOCKER_VERSION=19.03.3
CODEBUILD_SOURCE_VERSION=test/evn
CODEBUILD_SRC_DIR=/codebuild/output/src341076892/src/github.com/project-app/project-api
CODEBUILD_PROJECT_UUID=83afc255-1a5a-4ded-a73e-105110a67956
MAVEN_VERSION=3.6.3
CODEBUILD_AUTH_TOKEN=ce79b1c8-ee3b-4981-bb63-86162d6548b2
CODEBUILD_CONTAINER_NAME=default
JAVA_11_HOME=/opt/jvm/amazon-corretto-11
ANDROID_SDK_BUILD_TOOLS_28=build-tools;28.0.3
JDK_8_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto
JDK_DOWNLOAD_SHA256=4cc9e65e6e3d036b18cfd5fd6c7843d48244e44a60350f7e45036f4825bd3812
JAVA_HOME=/opt/jvm/amazon-corretto-11
DOCKER_COMPOSE_VERSION=1.24.0
PWD=/codebuild/output/src341076892/src/github.com/project-app/project-api
CODEBUILD_FE_REPORT_ENDPOINT=https://codebuild.eu-west-1.amazonaws.com/
PYTHON_37_VERSION=3.7.6
ANDROID_HOME=/usr/local/android-sdk-linux
PYTHON_38_VERSION=3.8.1
GOLANG_12_VERSION=1.12.17
JDK_11_HOME=/opt/jvm/amazon-corretto-11
ANDROID_SDK_PLATFORM_TOOLS=platforms;android-29
RBENV_SRC_DIR=/usr/local/rbenv
GOLANG_13_VERSION=1.13.8
CODEBUILD_START_TIME=1585485986820
DEBIAN_FRONTEND=noninteractive
DIND_COMMIT=3b5fac462d21ca164b3778647420016315289034
DOTNET_31_SDK_VERSION=3.1.102
ECS_CONTAINER_METADATA_URI=http://169.254.170.2/v3/788d7be3-4280-454e-8f3e-ab9ee035ee04
GITVERSION_VERSION=5.1.2
HOME=/root
HOSTNAME=202d63ee90d4
NUGET_XMLDOC_MODE=skip
JDK_HOME=/opt/jvm/amazon-corretto-11
GOLANG_14_VERSION=1.14.0
OLDPWD=/codebuild/readonly
PHP_73_VERSION=7.3.13
PHP_74_VERSION=7.4.1
PROJECT_NAME=project-api
PWD=/codebuild/output/src341076892/src/github.com/project-app/project-api
SRC_DIR=/usr/src

--FILE--
<?php
Expand Down
Loading

0 comments on commit b87dbfb

Please sign in to comment.