Skip to content

Commit 7a9d0f2

Browse files
authored
fix(coverage): detect branch on Travis CI push builds (#2753)
Fixes qltysh/cloud#8231. ## Summary Fix `qlty coverage publish` failing on Travis CI push builds (merge commits, direct pushes to a branch) with: ``` ERROR > A branch, tag, or pull request must be specified. > Please provide it using a supported CI provider or with one of > --override-branch, --override-git-tag, or --override-pr-number ``` PR builds were unaffected — only push builds hit this. ## The bug `TravisCI::branch()` in `qlty-coverage/src/ci/travisci.rs` reads: ```rust self.env .var("TRAVIS_PULL_REQUEST_BRANCH") .or_else(|| self.env.var("TRAVIS_BRANCH")) .unwrap_or_default() ``` On a Travis **push build**, `TRAVIS_PULL_REQUEST_BRANCH` is exported as an *empty string*, not unset. `SystemEnv::var()` is `std::env::var(name).ok()`, which returns `Some("")` for set-but-empty vars — so the `Option` is `Some("")`, `.or_else(...)` never fires, the `TRAVIS_BRANCH` fallback is skipped, and `branch()` returns `""`. With an empty branch, empty `pull_number()` (it's `"false"` on push builds), and empty `git_tag()`, `reference_type` resolves to `Unspecified` and validation at `qlty-cli/src/commands/coverage/utils.rs` aborts the upload. PR builds work because `TRAVIS_PULL_REQUEST_BRANCH` is populated with the source branch, so the `Some("<branch>")` path returns a valid value. Reference: [Travis default environment variables](https://docs.travis-ci.com/user/environment-variables/#default-environment-variables). ## The fix One-line change: filter out the empty string before falling through to `TRAVIS_BRANCH`. ```diff fn branch(&self) -> String { self.env .var("TRAVIS_PULL_REQUEST_BRANCH") + .filter(|b| !b.is_empty()) .or_else(|| self.env.var("TRAVIS_BRANCH")) .unwrap_or_default() } ``` This mirrors the pattern the same file already uses in `git_tag()` for `TRAVIS_TAG`. PR builds keep working: `TRAVIS_PULL_REQUEST_BRANCH` is non-empty, the filter passes it through unchanged. ## Testing - Added `branch_push_build_with_empty_pr_branch` — sets `TRAVIS_BRANCH=main`, `TRAVIS_PULL_REQUEST_BRANCH=""`, `TRAVIS_PULL_REQUEST=false` (exactly what Travis exports on a push build) and asserts `branch() == "main"`. - **Reproduced the bug first**: ran the new test against the *unfixed* code and confirmed it failed with `left: "" right: "main"` — matching the real-world error. - Applied the fix, new test passes. - Full `qlty-coverage` suite passes: **255/255 tests, 0 failures**. - Existing `branch` and `branch_pull_request` tests continue to pass, confirming no regression in the push-without-PR-var and PR-build codepaths.
1 parent 869e2fd commit 7a9d0f2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

qlty-coverage/src/ci/travisci.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ impl CI for TravisCI {
3030
fn branch(&self) -> String {
3131
self.env
3232
.var("TRAVIS_PULL_REQUEST_BRANCH")
33+
.filter(|b| !b.is_empty())
3334
.or_else(|| self.env.var("TRAVIS_BRANCH"))
3435
.unwrap_or_default()
3536
}
@@ -151,6 +152,19 @@ mod test {
151152
assert_eq!(&ci.branch(), "feature-branch");
152153
}
153154

155+
#[test]
156+
fn branch_push_build_with_empty_pr_branch() {
157+
let mut env: HashMap<String, String> = HashMap::default();
158+
env.insert("TRAVIS_BRANCH".to_string(), "main".to_string());
159+
env.insert("TRAVIS_PULL_REQUEST_BRANCH".to_string(), "".to_string());
160+
env.insert("TRAVIS_PULL_REQUEST".to_string(), "false".to_string());
161+
162+
let ci = TravisCI {
163+
env: Box::new(HashMapEnv::new(env)),
164+
};
165+
assert_eq!(&ci.branch(), "main");
166+
}
167+
154168
#[test]
155169
fn workflow() {
156170
let env: HashMap<String, String> = HashMap::default();

0 commit comments

Comments
 (0)