Repository navigation
CI: Split BuildInfoCollector into one class per CI service - #14498
Merged
alies-dev merged 20 commits intoNov 21, 2026
Merged
Conversation
The collector had tests for GitHub Actions and the generic CI only. Add a data provider with the whole input environment and the whole `build` array for Travis CI, CircleCI, AppVeyor, Jenkins, Scrutinizer, GitHub Actions (push, tag, pull request, pull request from a fork and from a deleted fork) and the generic CI, so that the collector can be refactored without changing what Shepherd receives.
CircleCI sent null for every unset repository, branch and pull request value, and Travis CI, AppVeyor and Scrutinizer sent "false" or "" as the number of a build that is not a pull request. Shepherd reads a null, an empty and a missing value alike (`GithubData::getBuildValue()`, `CI_PR_NUMBER` is ignored when empty or "false"), and the generic CI already sends no number for these values. Leave them out, so that the build info can be modelled as optional values. The expectations of the characterization tests drop those keys in this commit.
`BuildInfoCollector` held the environment variable names and the logic of every CI service, and wrote its results into the environment it read. - `Ci\CiProvider` with `detects()` and `collect()`, and one small class per service (GitHub Actions, Travis CI, CircleCI, AppVeyor, Jenkins, Scrutinizer, generic CI), each with its variable names as constants. - `Env`, a read-only view of the variables: first non-empty value, flag, `owner/name` slug. - `BuildInfo`, `Repository` and `PullRequest`: what a service tells, and `BuildInfo::toArray()` as the only place that knows the `CI_*` keys. - `BuildInfoCollector` returns the build info of the first provider that detects its service. `detects()` gets the git info too, as the generic CI needs it to tell the repository. The characterization tests are unchanged. What changes: the key order of the array (every provider used its own), a slug without exactly one "/" sends no repository instead of an undefined offset warning, and when the variables of several services are set, only the one that came last in the old collector (so the one that set CI_NAME) is sent.
The old collector wrote `CI_BUILD_NUMBER` into the environment it read instead of the build info, so the number (`CIRCLE_BUILD_NUM`) was never sent, unlike the one of AppVeyor and Jenkins. The characterization expectations of the two CircleCI cases now include it.
The old collector wrote `CI_JOB_ID` into the environment it read, so it was never sent, and
its value would have been `GITHUB_ACTIONS` ("true"), which identifies nothing. Send
`GITHUB_RUN_ID`, the number that is unique per workflow run in a repository and stays the
same when the run is re-run; `GITHUB_JOB` is only the job key of the workflow file.
Without it no job id is sent. The characterization expectations of the GitHub Actions
cases now include it.
`refs/heads/feature/refs/heads/login` became the branch `feature/login`, because every occurrence of the prefix was removed, not only the one the ref starts with.
GitHub Actions sent `refs/tags/v1.0.0` as the branch `v1.0.0`, and Travis CI sent the tag it builds as `CI_BRANCH` (`TRAVIS_BRANCH` is the name of the tag for a tag build, the docs add that git does not store the branch a commit was tagged from). Shepherd compares `CI_BRANCH` with the default branch of the repository, so a tag called like it was taken for it. Send no branch for a tag; the tag stays in `GITHUB_REF` and `TRAVIS_TAG`, which are sent as they are, and the git info built from the push event of a tag has no branch. The characterization expectations of the two tag cases drop the branch.
`SCRUTINIZER_PROJECT` is "g/owner/name" for a GitHub project and "b/owner/name" for a Bitbucket one (https://scrutinizer-ci.com/docs/build/environment-variables). The owner and the name were read from both, so a Bitbucket project was sent as a GitHub repository, which Shepherd then looked up on GitHub. Send the repository of a "g/" project only.
The old collector split "owner/name" slugs with explode() and read both parts, so a slug without a "/" (Travis CI, AppVeyor, GitHub Actions) raised an undefined offset warning and sent the owner without a name. Every slug now goes through `Env::repository()`, which returns nothing unless it is exactly "owner/name". The new cases fail on the old collector and pass since the split into one class per CI service; this commit only adds the test.
Contributor
✅ Performance: unchanged · Types: unchanged
How to read this table
head 9235ff0 · base f18e993 (merge-base) · workflow run |
`collect()` returns null when the environment is not the one of the CI service, so that the generic CI no longer looks up the GitHub remote twice and needs no exception for a state that cannot happen.
A build that is not a tag build sent `TRAVIS_TAG` as an empty string, although a value that is not known is not sent. Shepherd never reads it. The expectations of the two Travis CI cases that are not tag builds drop it.
CircleCI and the GitHub Actions pull request built a repository from an owner and a name that can be missing in the same way. Both use `Repository::fromParts()` now, which also returns null for an empty owner or name.
The comment told which CI_NAME used to win in the old collector. It now says what the order means: known services first, the generic one last. Remove a leftover section marker.
A pull request build sent only `CI_PR_REPO_OWNER` and `CI_PR_REPO_NAME`, so Shepherd, which needs `CI_REPO_*` to name the repository (`GithubData::getRepositoryForCommitAndPayload()`), could not link the report to the pull request. `SCRUTINIZER_PROJECT` is the inspected project, so `CI_REPO_*` is sent from it on every build, and on a pull request build `CI_PR_REPO_*` is the same repository: Scrutinizer does not expose the repository a pull request comes from. This is what the generic CI sends for a pull request. `SCRUTINIZER_BRANCH` is the target branch of a pull request, which is usually the default one, but Shepherd records a commit in the history of the default branch only when no `CI_PR_REPO_*` is sent (`PsalmData::linkToGithub()`), so a pull request commit does not end up there. The expectation of the Scrutinizer pull request case now includes `CI_REPO_*`.
`CI_BRANCH` of a `pull_request` or `pull_request_target` event was `refs/pull/N/merge`, the ref of the merge commit. Send `GITHUB_HEAD_REF`, the branch the pull request comes from, when it is set. Shepherd compares `CI_BRANCH` with the default branch only when no `CI_PR_REPO_*` is sent (`PsalmData::linkToGithub()`, `GithubData::getRepositoryForCommitAndPayload()`), so for a pull request from a repository it does not change what is recorded. A pull request from a deleted fork has no repository to send, and a head branch called like the default one would be taken for a commit of it, so there the ref stays. Without `GITHUB_HEAD_REF` (or empty) the ref stays too. The expectations of the pull request cases now have the head branch.
A pull request from a deleted fork kept `GITHUB_REF` as `CI_BRANCH`. For a `pull_request_target` event that ref is the base branch, so the build sent the default branch, the pull request number and no `CI_PR_REPO_*`. Shepherd then recorded the head commit of the pull request in the history of the default branch (`PsalmData::linkToGithub()`). Without the repository a pull request comes from, no branch is sent, for both events.
Splitting the collector into providers stopped merging the keys of several CI services, but no test set the variables of two known ones. On the base, Travis CI with Jenkins sent `CI_NAME=jenkins` together with the `CI_JOB_ID`, `CI_REPO_*` and `CI_PR_NUMBER` of Travis CI.
The providers read their flag with `Env::isTruthy()`, so `TRAVIS`, `CIRCLECI`, `APPVEYOR` and `SCRUTINIZER` set to "false" no longer detect the service; on the base, PHP took the string "false" as true. A GitHub Actions environment with only `GITHUB_ACTIONS` sends its name and nothing else, where the base sent an empty branch and repository. Pin both, and that a flag without the build variable the service needs is no CI service.
…nd tests The collector reads `GITHUB_HEAD_REF` and `GITHUB_RUN_ID` now, so on a GitHub Actions pull request run the tests that fake a GitHub Actions environment picked up the real branch and run id.
`AfterAnalysisEvent::getBuildInfo()` gives plugins the keys of one CI service, without the values that are not known.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BuildInfoCollectormixed the environment variable names and logic of every CI service in one 500-line class, which also wrote its results back into the environment it read. That hid several bugs: CircleCI's build number and the GitHub Actions job id were never sent, a tag was sent as the branch, a Bitbucket project on Scrutinizer was sent as a GitHub repository, and when several CI services matched, their keys were merged (Travis CI plus GitHub Actions sentgithub-actionstogether with Travis'sCI_JOB_IDandTRAVIS_*keys).Before: one class with
fillTravisCi(),fillCircleCi(), … all writing into one array.After:
BuildInfoCollectorreturns the build info of the firstCi\CiProviderwhosecollect()recognises the environment: the known services first (GitHub Actions, Scrutinizer, Jenkins, AppVeyor, CircleCI, Travis CI), then any other CI with a GitHub remote. Each provider is a small class with its environment variable names as constants. They read through an immutableEnvand return aBuildInfo, whosetoArray()is the only place that knows theCI_*keys Shepherd reads.The first commit pins the full output for every CI service, and the refactor commit passes those tests unchanged. Each bug fix has a test that fails before it. The last two below come from the split itself, so their tests are separate commits:
CI_BUILD_NUMBER.GITHUB_RUN_IDasCI_JOB_ID.CI_BRANCH.refs/heads/is stripped from a GitHub ref.GITHUB_HEAD_REF) asCI_BRANCHinstead ofrefs/pull/N/merge(pull_request) or the base branch (pull_request_target).CI_BRANCH. Without a head repository, Shepherd recorded the head commit of apull_request_targetbuild in the history of the default branch.g/) projects, and a pull request build now sendsCI_REPO_*too, so Shepherd can link the report to the pull request.owner/namesends no repository.TRAVIS,CIRCLECI,APPVEYORandSCRUTINIZERset to"false"no longer count as that CI service.The build info array is also what
AfterAnalysisEvent::getBuildInfo()gives plugins. Its shape changes: values that are not known (null, an emptyTRAVIS_TAG,""/"false"/"0"pull request numbers) are no longer included, and keys of several CI services are no longer merged. Shepherd already reads missing and empty values the same way.UPGRADING.mdlists the change.