Skip to content

Commit e52c5b9

Browse files
Merge pull request #58 from testingbot/feat/ci-repo-detect
Detect repository, branch, commit and PR from the CI environment
2 parents 36a681e + 2ac6edb commit e52c5b9

7 files changed

Lines changed: 464 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to `@testingbot/cli` are documented here. Releases are published to npm from GitHub releases.
44

5+
## Unreleased
6+
7+
### Added
8+
9+
- `testingbot maestro` detects the repository URL, branch, commit and pull request in GitHub Actions, GitLab CI, Bitbucket Pipelines, Azure Pipelines and CircleCI and sends them as run metadata (`repoUrl` is new), so the dashboard can link to the commit and PR on any Git host. On a GitHub pull request the PR's head commit is used, not the merge commit. Explicit flags win.
10+
511
## 1.4.3 - 2026-10-27
612

713
### Added

‎README.md‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ Values passed with `-e` are stored in plain text: they are returned in the run's
193193
194194
**CI/CD Integration:**
195195

196-
| Option | Description |
197-
| ---------------------------- | -------------------------------------------------------------------------------------------------------------------- |
198-
| `--branch <name>` | Git branch this test run was built from |
199-
| `--commit-sha <sha>` | Git commit SHA associated with this test run |
200-
| `--pull-request-id <id>` | Pull request ID this test run originated from |
201-
| `--pr-url <url>` | Pull request URL this test run originated from |
202-
| `--repo-name <name>` | Repository name (e.g., GitHub repo slug) |
203-
| `--repo-owner <owner>` | Repository owner (e.g., GitHub organization or username) |
204-
| `-m, --metadata <KEY=VALUE>` | Free-form metadata attached to the run and shown in the dashboard (repeatable, e.g. `-m team=mobile -m env=staging`) |
205-
| `--check-name <name>` | Name the GitHub pull request check `TestingBot / <name>` so several runs on one commit (iOS, Android) post separate checks that branch protection can require individually |
196+
| Option | Description |
197+
| ---------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
198+
| `--branch <name>` | Git branch this test run was built from |
199+
| `--commit-sha <sha>` | Git commit SHA associated with this test run |
200+
| `--pull-request-id <id>` | Pull request ID this test run originated from |
201+
| `--pr-url <url>` | Pull request URL this test run originated from |
202+
| `--repo-name <name>` | Repository name (e.g., GitHub repo slug) |
203+
| `--repo-owner <owner>` | Repository owner (e.g., GitHub organization or username) |
204+
| `-m, --metadata <KEY=VALUE>` | Free-form metadata attached to the run and shown in the dashboard (repeatable, e.g. `-m team=mobile -m env=staging`) |
205+
| `--check-name <name>` | Name the GitHub pull request check `TestingBot / <name>` so several runs on one commit (iOS, Android) post separate checks that branch protection can require individually |
206206

207207
**Allure reports:** `--report allure` converts each run's results into Allure result files under `<report-output-dir>/allure-results/`, one JSON per flow with its steps, status and failure details. Render them with `allure serve <report-output-dir>/allure-results` (requires the [Allure CLI](https://allurereport.org/docs/install/)). Results from several runs or shards accumulate in the same directory.
208208

@@ -405,6 +405,8 @@ testingbot maestro --app-url "$URL" ./flows --device "Pixel 9"
405405
testingbot maestro build.tar.gz ./flows
406406
```
407407

408+
Inside GitHub Actions, GitLab CI, Bitbucket Pipelines, Azure Pipelines or CircleCI, `testingbot maestro` reads the repository URL, branch, commit and pull request from the CI environment, so the dashboard links the run to them on whichever host the repository lives (GitHub, GitLab, Bitbucket, Azure Repos or self-hosted). Any of `--branch`, `--commit-sha`, `--pull-request-id`, `--pr-url`, `--repo-owner` and `--repo-name` overrides what was detected.
409+
408410
Inside an EAS Build or EAS Workflows job, the run is tagged automatically with the EAS build id, profile and platform, and `EAS_BUILD_GIT_COMMIT_HASH` is used as the commit SHA unless `--commit-sha` is given. `testingbot upload <url>` accepts URLs as well.
409411

410412
### Status, artifacts and list

‎src/cli.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import MaestroOptions, {
2121
ArtifactDownloadMode,
2222
} from './models/maestro_options';
2323
import Maestro from './providers/maestro';
24+
import { ciRepoMetadata } from './utils/ci_env';
2425
import Login from './providers/login';
2526
import {
2627
CREDENTIALS_FILE,
@@ -931,18 +932,25 @@ program
931932
if (Array.isArray(args.secretEnv)) args.secretEnv.length = 0;
932933

933934
const eas = easBuildMetadata(process.env);
935+
const ci = ciRepoMetadata(process.env);
936+
// The repository is one unit: an explicit --repo-owner / --repo-name
937+
// replaces everything detected about it, and an explicit PR id drops
938+
// the detected PR URL, which would point at a different PR.
939+
const explicitRepo = !!(args.repoOwner || args.repoName);
934940
const custom = {
935941
...eas.custom,
936942
...(parseKeyValues(args.metadata, '--metadata') ?? {}),
937943
...(args.checkName?.trim() && { checkName: args.checkName.trim() }),
938944
};
939945
const metadata = buildRunMetadata({
940-
commitSha: args.commitSha ?? eas.commitSha,
941-
pullRequestId: args.pullRequestId,
942-
pullRequestUrl: args.prUrl,
943-
repoName: args.repoName,
944-
repoOwner: args.repoOwner,
945-
branch: args.branch,
946+
commitSha: args.commitSha ?? eas.commitSha ?? ci.commitSha,
947+
pullRequestId: args.pullRequestId ?? ci.pullRequestId,
948+
pullRequestUrl:
949+
args.prUrl ?? (args.pullRequestId ? undefined : ci.pullRequestUrl),
950+
repoUrl: explicitRepo ? undefined : ci.repoUrl,
951+
repoName: explicitRepo ? args.repoName : ci.repoName,
952+
repoOwner: explicitRepo ? args.repoOwner : ci.repoOwner,
953+
branch: args.branch ?? ci.branch,
946954
custom: Object.keys(custom).length > 0 ? custom : undefined,
947955
});
948956

‎src/models/maestro_options.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export interface RunMetadata {
2424
pullRequestUrl?: string;
2525
repoName?: string;
2626
repoOwner?: string;
27+
/** The repository's web URL on any host; detected from the CI env. */
28+
repoUrl?: string;
2729
branch?: string;
2830
/** Free-form key/value pairs from --metadata, shown on the dashboard. */
2931
custom?: Record<string, string>;

‎src/utils/ci_env.ts‎

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/**
2+
* Repository metadata read from the CI system the CLI runs in, so a Maestro
3+
* run can link back to its repository, branch, commit and pull request
4+
* without the user passing --repo-owner / --repo-name / --commit-sha.
5+
*
6+
* `repoUrl` is the repository's web URL on whatever host it lives on (GitHub,
7+
* GitLab, Bitbucket, Azure Repos, a self-hosted server); the dashboard builds
8+
* the branch / commit / PR links from it. `repoOwner` / `repoName` are only
9+
* filled in for github.com, because the API reads them as a GitHub repository
10+
* (status checks via the TestingBot GitHub App).
11+
*
12+
* Explicit CLI flags always win over anything detected here.
13+
*/
14+
15+
import fs from 'node:fs';
16+
17+
export interface CiRepoMetadata {
18+
repoUrl?: string;
19+
repoOwner?: string;
20+
repoName?: string;
21+
commitSha?: string;
22+
branch?: string;
23+
pullRequestId?: string;
24+
pullRequestUrl?: string;
25+
}
26+
27+
type Env = NodeJS.ProcessEnv;
28+
29+
/**
30+
* Turns a clone URL into the repository's web URL: SSH (`git@host:o/r.git`,
31+
* `ssh://git@host/o/r`) becomes https, credentials and a trailing `.git` are
32+
* dropped. Returns undefined for anything that is not an http(s) URL after that.
33+
*/
34+
export function webRepoUrl(raw: string | undefined): string | undefined {
35+
if (!raw) return undefined;
36+
let value = raw.trim();
37+
const scp = value.match(/^[^@\s/]+@([^:\s/]+):(.+)$/);
38+
if (scp) value = `https://${scp[1]}/${scp[2]}`;
39+
value = value.replace(/^ssh:\/\//, 'https://');
40+
let url: URL;
41+
try {
42+
url = new URL(value);
43+
} catch {
44+
return undefined;
45+
}
46+
if (url.protocol === 'http:') url.protocol = 'https:';
47+
if (url.protocol !== 'https:') return undefined;
48+
url.username = '';
49+
url.password = '';
50+
url.port = '';
51+
url.search = '';
52+
url.hash = '';
53+
const path = url.pathname.replace(/\/+$/, '').replace(/\.git$/, '');
54+
if (path === '' || path === '/') return undefined;
55+
return `${url.origin}${path}`;
56+
}
57+
58+
/** `refs/heads/feature/x` → `feature/x`; other refs are returned as given. */
59+
function stripHeads(ref: string | undefined): string | undefined {
60+
if (!ref) return undefined;
61+
return ref.replace(/^refs\/heads\//, '');
62+
}
63+
64+
function githubOwnerName(
65+
repoUrl: string | undefined,
66+
): Pick<CiRepoMetadata, 'repoOwner' | 'repoName'> {
67+
const m = repoUrl?.match(/^https:\/\/github\.com\/([^/]+)\/([^/]+)$/);
68+
return m ? { repoOwner: m[1], repoName: m[2] } : {};
69+
}
70+
71+
function readJson(path: string | undefined): Record<string, unknown> | null {
72+
if (!path) return null;
73+
try {
74+
return JSON.parse(fs.readFileSync(path, 'utf8'));
75+
} catch {
76+
return null;
77+
}
78+
}
79+
80+
function githubActions(env: Env): CiRepoMetadata {
81+
const server = env.GITHUB_SERVER_URL || 'https://github.com';
82+
const repoUrl = env.GITHUB_REPOSITORY
83+
? webRepoUrl(`${server}/${env.GITHUB_REPOSITORY}`)
84+
: undefined;
85+
// On pull_request events GITHUB_SHA is the throwaway merge commit; the
86+
// event payload carries the PR's head commit, which is what the user pushed.
87+
const event = readJson(env.GITHUB_EVENT_PATH);
88+
const pr = event?.pull_request as
89+
| { number?: number; head?: { sha?: string }; html_url?: string }
90+
| undefined;
91+
const prFromRef = env.GITHUB_REF?.match(/^refs\/pull\/(\d+)\//)?.[1];
92+
return {
93+
repoUrl,
94+
...githubOwnerName(repoUrl),
95+
commitSha: pr?.head?.sha || env.GITHUB_SHA,
96+
branch: env.GITHUB_HEAD_REF || env.GITHUB_REF_NAME,
97+
pullRequestId: pr?.number ? String(pr.number) : prFromRef,
98+
pullRequestUrl: pr?.html_url,
99+
};
100+
}
101+
102+
function gitlab(env: Env): CiRepoMetadata {
103+
const repoUrl = webRepoUrl(env.CI_PROJECT_URL);
104+
return {
105+
repoUrl,
106+
commitSha: env.CI_COMMIT_SHA,
107+
branch:
108+
env.CI_MERGE_REQUEST_SOURCE_BRANCH_NAME ||
109+
env.CI_COMMIT_BRANCH ||
110+
env.CI_COMMIT_REF_NAME,
111+
pullRequestId: env.CI_MERGE_REQUEST_IID,
112+
};
113+
}
114+
115+
function bitbucket(env: Env): CiRepoMetadata {
116+
const repoUrl = webRepoUrl(env.BITBUCKET_GIT_HTTP_ORIGIN);
117+
return {
118+
repoUrl,
119+
commitSha: env.BITBUCKET_COMMIT,
120+
branch: env.BITBUCKET_BRANCH,
121+
pullRequestId: env.BITBUCKET_PR_ID,
122+
};
123+
}
124+
125+
function azurePipelines(env: Env): CiRepoMetadata {
126+
const repoUrl = webRepoUrl(env.BUILD_REPOSITORY_URI);
127+
// PR builds check out a merge commit; for GitHub-hosted repositories Azure
128+
// also exposes the PR's own head commit. Azure Repos PRs use the ID, GitHub
129+
// PRs the number.
130+
const isPr = !!env.SYSTEM_PULLREQUEST_PULLREQUESTID;
131+
const prId =
132+
env.BUILD_REPOSITORY_PROVIDER === 'GitHub'
133+
? env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER
134+
: env.SYSTEM_PULLREQUEST_PULLREQUESTID;
135+
return {
136+
repoUrl,
137+
...githubOwnerName(repoUrl),
138+
commitSha: env.SYSTEM_PULLREQUEST_SOURCECOMMITID || env.BUILD_SOURCEVERSION,
139+
branch: isPr
140+
? stripHeads(env.SYSTEM_PULLREQUEST_SOURCEBRANCH)
141+
: stripHeads(env.BUILD_SOURCEBRANCH) || env.BUILD_SOURCEBRANCHNAME,
142+
pullRequestId: isPr ? prId : undefined,
143+
};
144+
}
145+
146+
function circleci(env: Env): CiRepoMetadata {
147+
const repoUrl = webRepoUrl(env.CIRCLE_REPOSITORY_URL);
148+
const prUrl = env.CIRCLE_PULL_REQUEST;
149+
return {
150+
repoUrl,
151+
...githubOwnerName(repoUrl),
152+
commitSha: env.CIRCLE_SHA1,
153+
branch: env.CIRCLE_BRANCH,
154+
pullRequestId: prUrl?.match(/\/(\d+)$/)?.[1],
155+
pullRequestUrl: prUrl,
156+
};
157+
}
158+
159+
/** Detects the CI system from `env` and returns what it knows; {} outside CI. */
160+
export function ciRepoMetadata(env: Env = process.env): CiRepoMetadata {
161+
let detected: CiRepoMetadata = {};
162+
if (env.GITHUB_ACTIONS === 'true') detected = githubActions(env);
163+
else if (env.GITLAB_CI === 'true') detected = gitlab(env);
164+
else if (env.BITBUCKET_BUILD_NUMBER) detected = bitbucket(env);
165+
else if (env.TF_BUILD?.toLowerCase() === 'true')
166+
detected = azurePipelines(env);
167+
else if (env.CIRCLECI === 'true') detected = circleci(env);
168+
169+
const result: CiRepoMetadata = {};
170+
for (const [key, value] of Object.entries(detected)) {
171+
if (value) result[key as keyof CiRepoMetadata] = value;
172+
}
173+
return result;
174+
}

‎tests/cli.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ import fs from 'node:fs';
88
import { JsonOutput } from './../src/utils/json_output';
99
import * as account from './../src/providers/account';
1010
import Credentials from './../src/models/credentials';
11+
import { ciRepoMetadata } from './../src/utils/ci_env';
1112

1213
jest.mock('./../src/logger');
1314
jest.mock('./../src/auth');
1415
jest.mock('./../src/providers/espresso');
1516
jest.mock('./../src/providers/xcuitest');
1617
jest.mock('./../src/providers/maestro');
18+
// Keep the suite hermetic when it runs inside CI itself (GITHUB_ACTIONS etc.).
19+
jest.mock('./../src/utils/ci_env', () => ({
20+
ciRepoMetadata: jest.fn(() => ({})),
21+
}));
1722
jest.mock('./../src/providers/account', () => ({
1823
...jest.requireActual('./../src/providers/account'),
1924
fetchAccount: jest.fn(),
@@ -1972,6 +1977,57 @@ describe('TestingBotCTL CLI', () => {
19721977
expect(mockUploadOnly).toHaveBeenCalledTimes(1);
19731978
});
19741979

1980+
test('detected CI repository metadata fills in what flags leave out', async () => {
1981+
(ciRepoMetadata as jest.Mock).mockReturnValueOnce({
1982+
repoUrl: 'https://dev.azure.com/acme/mobile/_git/app',
1983+
commitSha: 'b'.repeat(40),
1984+
branch: 'develop',
1985+
pullRequestId: '12',
1986+
pullRequestUrl:
1987+
'https://dev.azure.com/acme/mobile/_git/app/pullrequest/12',
1988+
});
1989+
await program.parseAsync([
1990+
'node',
1991+
'cli',
1992+
'maestro',
1993+
'app.apk',
1994+
'./flows',
1995+
'--branch',
1996+
'release',
1997+
'--pull-request-id',
1998+
'7',
1999+
]);
2000+
expect(lastConstructorOptions<Opts>(Maestro).metadata).toEqual({
2001+
repoUrl: 'https://dev.azure.com/acme/mobile/_git/app',
2002+
commitSha: 'b'.repeat(40),
2003+
branch: 'release',
2004+
pullRequestId: '7',
2005+
});
2006+
});
2007+
2008+
test('an explicit --repo-owner/--repo-name replaces the detected repository', async () => {
2009+
(ciRepoMetadata as jest.Mock).mockReturnValueOnce({
2010+
repoUrl: 'https://github.com/detected/repo',
2011+
repoOwner: 'detected',
2012+
repoName: 'repo',
2013+
});
2014+
await program.parseAsync([
2015+
'node',
2016+
'cli',
2017+
'maestro',
2018+
'app.apk',
2019+
'./flows',
2020+
'--repo-owner',
2021+
'acme',
2022+
'--repo-name',
2023+
'app',
2024+
]);
2025+
expect(lastConstructorOptions<Opts>(Maestro).metadata).toEqual({
2026+
repoOwner: 'acme',
2027+
repoName: 'app',
2028+
});
2029+
});
2030+
19752031
test('EAS Build env fills in the commit and build metadata', async () => {
19762032
process.env.EAS_BUILD = 'true';
19772033
process.env.EAS_BUILD_ID = 'b-123';

0 commit comments

Comments
 (0)