Skip to content

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

MonkeyTester-Plugin-Build-Local

build-local is a MonkeyTester build plugin: it implements BUILD_PLUGIN_API.md (contract monkeytester-build-plugin, version 1, kept in the MonkeyTester repo) with builds that run on the plugin's own host. The viewer's Build & Test page asks it for a repo's branches and test cases and to build a branch; the plugin fetches the code from Gitea or GitHub with a read-only token, runs the app repo's .monkeytester/build-<platform>.sh scripts here (Android and iOS on a Mac, Android only on Linux) and uploads the built apps to the viewer with the viewer's submit.py, so the run attaches to the build. It can also open a pull request that adds a test recorded on the viewer's Devices page (POST /repos/<owner>/<repo>/tests, optional capability write_tests; that one route needs a token that can write, see below).

The code-host URL and token live only in this plugin; the viewer never sees the token. A build that fails is not an HTTP error: GET /builds/<ref> is 200 with a failed conclusion. A platform that failed to build while another one built still ends the build as success: the failed platform is uploaded as a build error with its log, so its devices show every test as Unavailable with View logs. Builds run one at a time (FIFO queue); queued or running builds can be cancelled (POST /builds/<ref>/cancel terminates the script's process group), and the build log is tail-able (GET /builds/<ref>/log?offset= with X-Log-Size / X-Log-Complete).

  • Default port: 8094
  • Python 3.10+, standard library only. Runs on Linux and macOS.

Requirements per host

  • Mac (builds Android and iOS): macOS with Xcode (the plugin detects iOS with xcodebuild -version), the Android SDK (detected via ANDROID_HOME / ANDROID_SDK_ROOT / ~/Library/Android/sdk / ~/Android/Sdk), a JDK for the Android build, and — for the signed .ipa — the code-signing identity in the login keychain of the user the plugin runs as (the launchd agent runs as an ordinary user; keychain access needs the Aqua session type, see the plist). The provisioning profile must cover each test iPhone's UDID.
  • Linux (Android only): the Android SDK plus a JDK; iOS is not offered.

Install

With Nix (Linux or macOS)

git clone <repo-url> MonkeyTester-Plugin-Build-Local
cd MonkeyTester-Plugin-Build-Local
nix run .            # plugin on :8094
nix run .#tests      # unit tests against the fake Gitea/GitHub

The flake builds from git-tracked files only, so git add new files before nix run.

To keep it running:

  • Linux: packaging/systemd/monkeytester-build-local.service (user unit; the install steps are in the file's header; Android only).
  • macOS: packaging/launchd/com.blackjackfruit.monkeytester-build-local.plist (LaunchAgent; replace CHECKOUT_DIR and PYTHON).

Without Nix

Any python3 3.10 or newer works, with no extra packages:

python3 build_plugin.py
python3 -m unittest discover -s tests -v

Configuration

Env var Default Meaning
PLUGIN_HOST 0.0.0.0 listen address. The default is the LAN, because the viewer on the Linux host reaches the Mac over it — so the default needs PLUGIN_TOKEN. 127.0.0.1 listens on this machine only
PLUGIN_PORT 8094 listen port
PLUGIN_TOKEN unset pairing token: every request except GET /health must send it as X-Plugin-Token or Authorization: Bearer. Mandatory when PLUGIN_HOST is not loopback (127.0.0.1, ::1, localhost): without it the plugin refuses to start (exit status 2). Generate one with python3 -c 'import secrets; print(secrets.token_urlsafe(32))'
BUILD_PLUGIN_STATE_DIR ~/.config/monkeytester-build-local/ config.json (code host + token + viewer URL, mode 0600), builds.json, checkouts/, out/ (newest 50 builds keep their files)
BUILD_LOCAL_PLATFORMS auto-detect e.g. android to force Android only
BUILD_LOCAL_TIMEOUT_ANDROID / BUILD_LOCAL_TIMEOUT_IOS 3600 seconds a platform's build script may run
BUILD_LOCAL_IMPORT unset first start only: import url + token + user from this gitea_config.json (the viewer's old Gitea settings file) as a Gitea code host

The token is plaintext at rest in config.json (mode 0600): the plugin reads it without a person present, so encrypting it on the same disk would not add real protection. It is never returned by any endpoint, never written to a build log or to any repo's .git/config, and passed to git only per command via environment.

Register the plugin in the viewer

  1. Set PLUGIN_TOKEN (see SECURITY.md) and start the plugin.
  2. In the viewer: Settings → Build & Test → Add build plugin. Name build-local, address http://<host>:8094, and the same pairing token as PLUGIN_TOKEN.
  3. Click Configure on the plugin and enter:
    • the code host: Gitea or GitHub,

    • its URL: the Gitea URL (e.g. https://gitea.example.com) or the GitHub API base (https://api.github.com, or https://<host>/api/v3 for GitHub Enterprise Server),

    • a read-only token — it only reads the repo and fetches it with git:

      • Gitea: a token with read:repository (plus read:user) under Settings → Applications → Access tokens,
      • GitHub: a fine-grained token with Contents: read + Metadata: read, or a classic token with the repo scope for private repos,

      To let the viewer's Record test open pull requests (POST /repos/<owner>/<repo>/tests), the token must also write: Gitea write:repository; GitHub fine-grained Contents: read and write + Pull requests: read and write (a classic repo token already can). With a read-only token builds work as before and that route answers 403 forbidden.

    • the viewer URL (prefilled with the address the browser is using) — the plugin uploads to it. The plugin checks the token against the code host (who it belongs to) before it saves anything.

  4. Add projects (one repo each) and pick build-local as their build plugin.

The app repo side

Commit one script per platform at the paths the plugin reports in /health recipes: .monkeytester/build-android.sh and .monkeytester/build-ios.sh (GET /template?platform= returns the example scripts — download them from Settings and commit them to your repo). The plugin runs each with bash from the root of a clean checkout of the branch, with:

Variable Value
MT_PLATFORM android or ios
MT_OUTPUT where the script must write the app: a .apk (Android) or a signed .ipa (iOS)
MT_BRANCH the branch being built
MT_CASES comma-separated case names
MT_REQUEST_ID the viewer's build id

Exit status 0 with a non-empty file at MT_OUTPUT is a successful build; anything else is a build error, reported with the script's last error: lines and its log.

Endpoints

The full contract is BUILD_PLUGIN_API.md in the MonkeyTester repo. Every route except GET /health needs the pairing token when PLUGIN_TOKEN is set.

Route Answer
GET /health contract, plugin name/version, code host, platforms, recipes, capabilities; "docs": "/docs" while this README is readable (omitted otherwise)
GET /docs this README rendered as a self-contained HTML page (text/html, strict CSP, no scripts); 404 JSON if README.md is missing. The viewer shows it through /plugins/docs/build-local
GET / POST /config code host kind + URL + user and viewer URL (the token is never returned) / save them
GET /template?platform= example .monkeytester/build-<platform>.sh
GET /repos/<owner>/<repo>[?branch=] check a repo (and branch)
GET /repos/<owner>/<repo>/branches the repo's branches
GET /repos/<owner>/<repo>/cases?ref= test cases on a ref, the build scripts present, and each case's DEVICE lines (devices, see below)
POST /repos/<owner>/<repo>/tests capability write_tests: open a pull request adding a recorded test file (see below)
POST /builds queue a build; GET /builds/<ref> its status
GET /builds/<ref>/log?offset= the build log from offset
POST /builds/<ref>/cancel stop a queued or running build

Cases and their DEVICE lines

GET /repos/<owner>/<repo>/cases?ref=<branch> answers:

{
  "repo": "owner/App",
  "ref": "master",
  "cases": ["login", "onboarding"],
  "recipes": {"android": true, "ios": true},
  "devices": {
    "login": ["ios, physical", "android, virtual = MT_Pixel"],
    "onboarding": []
  }
}

A case is a folder mt-tests/<case>/ holding INSTRUCTION.txt. devices maps each case to the raw text after DEVICE: on every DEVICE line in that file's header, in file order, trimmed. A case without a DEVICE line maps to []. The header is the run of KEY: value lines at the top of the file (MODE, CLEAR_DATA, REINSTALL_APP, DEVICE; the key is case-insensitive; blank lines and # lines are allowed) and ends at the first other line, the same way the MonkeyTester runner reads it. The plugin passes the values through without interpreting them; the viewer parses them.

The plugin reads every INSTRUCTION.txt as a git blob named by the branch's tree, in parallel, and caches the result by blob sha, so a later listing only reads files that changed. On GitHub, when the recursive tree is truncated, the contents-API answers it already makes carry the files, so no extra request is needed. If any file can't be read, devices is left out of the answer, which the contract reads as "unknown" (the cases list still comes back). POST /builds does not read these files.

Recorded tests: POST /repos/<owner>/<repo>/tests

Optional capability write_tests (listed in /health capabilities; the contract stays version 1). The viewer's Devices page Record test calls it to add a recorded steps file to a case. Same pairing token as every other route.

{"base": "master",
 "case": "onboarding",
 "new_case": false,
 "file": "login_recorded.txt",
 "content": "AI: CLICK Login button\nWAIT: 2.5\nAI: INPUT Email field \"a@b.c\"\n",
 "instruction": "MODE: everytime\n",
 "title": "Add recorded test onboarding/login_recorded.txt",
 "body": "Recorded on the MonkeyTester Devices page (device <name>, AI plugin <NAME>)."}

The plugin checks everything itself before contacting the code host:

  • case matches ^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$; file matches ^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}\.txt$ and is not INSTRUCTION.txt. The file is written to mt-tests/<case>/<file>.
  • content is a non-empty UTF-8 string of at most 256 KiB (413 too_large above). title is the commit message and pull request title (default Add recorded test <case>/<file>); body is the pull request text.
  • new_case: false: mt-tests/<case>/INSTRUCTION.txt must exist on base. new_case: true: mt-tests/<case> must not exist, and instruction (required, non-empty) is written as mt-tests/<case>/INSTRUCTION.txt in the same commit.

It then creates the branch mt-record/<case>-<file stem>-<yyyymmddHHMMSS> (UTC) from base with ONE commit holding the file(s) and opens a pull request into base:

{"ok": true, "branch": "mt-record/onboarding-login_recorded-20261202153000",
 "pr_url": "https://github.com/o/r/pull/12", "pr_number": 12}
Status code When
400 bad_request a bad name, type or empty field
401 unauthorized missing or wrong pairing token
403 forbidden the code host token can't write (the code host's message is in error)
404 unknown_branch base doesn't exist
404 unknown_case new_case: false and the case has no INSTRUCTION.txt on base
409 case_exists new_case: true and mt-tests/<case> exists on base
409 file_exists mt-tests/<case>/<file> exists on base (never overwritten)
409 not_configured no code host token saved
413 too_large content over 256 KiB

Errors use the usual {"error": ..., "code": ...} body (plus forge_status when the code host answered). On GitHub the commit is made with the git data API (tree with base_tree, commit, ref) and POST /repos/{o}/{r}/pulls; on Gitea with one POST /repos/{o}/{r}/contents call carrying new_branch (Gitea 1.20 or later) and POST /repos/{o}/{r}/pulls. If opening the pull request fails, the new branch is deleted again. No local clone is used.

/docs reads README.md next to build_plugin.py at request time. The launchd/systemd units run from the checkout and the Nix flake copies every git-tracked file, so README.md is present in all three; an install without it just leaves docs out of /health.

Security

See SECURITY.md for the threat model, token setup (launchd EnvironmentVariables, systemd Environment=) and what is not covered (no TLS, plaintext secrets at rest). In short: on a LAN bind the plugin will not start without PLUGIN_TOKEN; /health is the only unauthenticated route and carries no secret or configuration.

The plugin runs the scripts of the branch it builds — run it as a dedicated, never-root user, and only add trusted repos as projects: a repo's build script runs with that user's environment, keychain and SDKs. The forge token is read-only unless you want POST /repos/<owner>/<repo>/tests (it then needs write access, and can only add files on a new mt-record/... branch plus a pull request; the plugin never pushes to an existing branch) and stays out of every log and git config. Builds are one-at-a-time; use POST /builds/<ref>/cancel to stop a queued or running build (the script's whole process group is terminated), and GET /builds/<ref>/log to watch what it is doing.

Tests

python3 -m unittest discover -s tests -v — fake Gitea, fake GitHub and a fake viewer, all in-process on 127.0.0.1; a real forge or viewer is never contacted. nix run .#tests and CI (.github/workflows/test.yml) run the same command.

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages