Pull Request Automation
Relevant source files
The following files were used as context for generating this wiki page:
Pull Request (PR) Automation in Morty is a core feature that bridges the gap between autonomous AI code implementation and standard Git collaboration workflows. When enabled, the system automatically handles the lifecycle of a code change: from task-specific branch creation and code commitment to pushing changes to a remote repository and opening a PR via the GitHub CLI.
This system supports both sequential and parallel execution modes. In parallel mode, it leverages Git worktrees to manage multiple concurrent agents, each eventually contributing its work through automated PR creation or intelligent merging.
Branch and PR Management Logic
The automation logic is governed by specific CLI flags and configuration settings that determine how branches are named and when PRs are triggered.
Sequential Workflow
In the standard sequential loop, Morty identifies a task, creates a slugified branch name prefixed with morty/, implements the changes, and optionally creates a PR if the --create-pr flag is active.
Sources: morty.sh:809-844, cli/src/git/pr.ts:25-58
Parallel PR Workflow
When running in parallel, Morty creates isolated worktrees for each agent. Each agent works on a unique branch to avoid conflicts. Upon completion, if PR creation is enabled, each agent pushes its specific branch and opens a PR.
Key Components and Functions
The implementation relies on simple-git for repository operations and the GitHub CLI (gh) for PR creation.
PR Creation Functions
The following table summarizes the primary functions used to automate PRs:
| Function | File | Description |
|---|---|---|
createPullRequest | cli/src/git/pr.ts | Orchestrates pushing the branch and executing the gh pr create command. |
pushBranch | cli/src/git/pr.ts | Pushes a branch to origin and sets the upstream tracking. |
create_pull_request | morty.sh | Bash implementation of the PR creation logic using gh pr create. |
isGhAvailable | cli/src/git/pr.ts | Validates if the GitHub CLI is installed and authenticated via gh auth status. |
Sources: cli/src/git/pr.ts:1-60, morty.sh:845-877
Configuration Options
Users can control the behavior of the PR automation through various CLI arguments:
| Flag | Option | Default | Description |
|---|---|---|---|
--branch-per-task | branchPerTask | false | Creates a new Git branch for every task processed. |
--create-pr | createPr | false | Automatically creates a PR after task completion. |
--draft-pr | draftPr | false | Creates the PR as a draft. |
--base-branch | baseBranch | Current Branch | The target branch for the PR (e.g., main). |
--no-merge | skipMerge | false | Skips auto-merging branches in parallel mode, keeping them for PRs. |
Sources: cli/src/cli/args.ts:108-124, morty.sh:472-498
Parallel Integration and Merging
When PRs are not requested in parallel mode, Morty utilizes an integration branch strategy to ensure subsequent tasks have access to previously completed work.
- Integration Branches: For YAML task sources with
parallel_groupdefinitions, Morty createsmorty/integration-group-Nbranches. - Merging: Branches from a completed group are merged into this integration branch before the next group starts.
- Conflict Resolution: If a merge conflict occurs during this automation, Morty can invoke the AI engine with a specific
resolve_promptto combine changes and remove conflict markers.
Technical Implementation Snippets
Automated PR Execution
The system builds the PR command arguments dynamically, supporting titles, bodies, and draft status.
// From cli/src/git/pr.ts:40-54
const args = [
"pr",
"create",
"--base",
baseBranch,
"--head",
branch,
"--title",
title,
"--body",
body,
];
if (draft) {
args.push("--draft");
}
const { stdout, exitCode } = await execCommand("gh", args, workDir);
Sources: cli/src/git/pr.ts:40-54
Branch Slugification
To ensure valid Git branch names, the system "slugifies" task descriptions by converting to lowercase and replacing non-alphanumeric characters with hyphens.
# From morty.sh:85-87
slugify() {
echo "$1" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g' | sed -E 's/^-|-$//g' | cut -c1-50
}
Sources: morty.sh:85-87
Conclusion
Pull Request Automation in Morty transforms an autonomous AI agent into a collaborative team member. By automating branch management, remote synchronization, and the PR lifecycle, it ensures that AI-generated code follows established engineering workflows while providing safety mechanisms like draft PRs and AI-assisted merge conflict resolution.
Sources: README.md:144-155, morty.sh:1375-1390