Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
253 changes: 253 additions & 0 deletions .claude/commands/create-pr-current.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
---
name: create-pr-current
displayName: Create PR for Current Branch
description: Creates a pull request for the current branch in java-sdk repository. Must be explicitly invoked with /create-pr-current to avoid confusion with create-prs agent.
version: 1.0.0
disable-model-invocation: true
requiredTools:
- Bash
- Read
- mcp__github__create_pull_request
---

# Create PR for Current Branch

Creates a pull request for the current branch in the java-sdk repository.

## Instructions

When invoked, follow these steps:

**🚨 CRITICAL: Before starting the workflow, use TodoWrite to set up all steps as pending todos.**

**Step Setup (Use TodoWrite tool immediately):**
```
1. Get current branch information
2. Check for merge conflicts with master branch
3. Get repository information
4. Push current branch
5. Generate PR title and description
6. Create pull request
7. Report results
```

**TodoWrite Setup Example:**
```json
{
"todos": [
{
"content": "Get current branch information",
"activeForm": "Getting current branch information",
"status": "pending"
},
{
"content": "Check for merge conflicts with master branch",
"activeForm": "Checking for merge conflicts with master branch",
"status": "pending"
},
{
"content": "Get repository information",
"activeForm": "Getting repository information",
"status": "pending"
},
{
"content": "Push current branch",
"activeForm": "Pushing current branch",
"status": "pending"
},
{
"content": "Generate PR title and description",
"activeForm": "Generating PR title and description",
"status": "pending"
},
{
"content": "Create pull request",
"activeForm": "Creating pull request",
"status": "pending"
},
{
"content": "Report results",
"activeForm": "Reporting results",
"status": "pending"
}
]
}
```

**After completing each step, use TodoWrite to mark it as completed before proceeding to the next step.**

---

### 1. Get Current Branch Information
**Mark this step as in_progress using TodoWrite**
- Use `git branch --show-current` to get the current branch name
- Use `git status` to verify there are no uncommitted changes
- If uncommitted changes exist, warn user and ask if they want to commit first

**Mark Step 1 as completed using TodoWrite before proceeding**

---

### 2. Check for Merge Conflicts with Master Branch
**Mark this step as in_progress using TodoWrite**

#### a. Detect Potential Conflicts (Physical and Logical)
- Fetch latest master: `git fetch origin master`
- Check for physical conflicts: `git merge-tree $(git merge-base HEAD origin/master) HEAD origin/master`
- Check for .md file changes: `git diff --name-only origin/master...HEAD | grep '\.md$'`
- **Decision logic**:
- If .md files changed → ALWAYS proceed to conflict analysis (even if no physical conflicts)
- Reason: Logical conflicts in agent configs/prompts cannot be detected by git
- If only non-.md files changed AND no physical conflicts → Skip to step 3
- If physical conflicts detected → Proceed to conflict analysis

#### b-h. Resolve Conflicts with Semantic Evaluation

**🚨 CRITICAL: Follow the detailed process in [prompts/rules/git-logical-merge.md](../../prompts/rules/git-logical-merge.md)**

This process handles both physical conflicts (detected by git) and logical conflicts (semantic incompatibilities in .md files that git cannot detect).

**The process includes these critical steps:**
- **Step b**: Analyze files with semantic evaluation (understand what EACH side added)
- **Step c**: Categorize conflict severity (CRITICAL/HIGH/MEDIUM/LOW)
- **Step d**: Present critical conflicts to user with impact assessment
- **Step e**: **BLOCKING** - Use AskUserQuestion tool for user decision (MANDATORY)
- **Step f**: Execute resolution strategy based on user choice
- **Step g**: **MANDATORY** - Verify resolution preserves intent using Read tool
- **Step h**: **GATE CHECK** - Pre-commit checklist (all answers must be YES)

**⚠️ You MUST follow every step in git-logical-merge.md. Do NOT skip steps e, g, or h - they contain blocking requirements and verification gates.**

See [git-logical-merge.md](../../prompts/rules/git-logical-merge.md) for complete step-by-step instructions with examples.

**Mark Step 2 as completed using TodoWrite before proceeding**

---

### 3. Get Repository Information
**Mark this step as in_progress using TodoWrite**
- Repository owner: Extract from git remote (e.g., "optimizely")
- Repository name: "java-sdk"
- Base branch: Typically "master" (verify with `git remote show origin | grep "HEAD branch"`)

**Mark Step 3 as completed using TodoWrite before proceeding**

---

### 4. Push Current Branch
**Mark this step as in_progress using TodoWrite**
- Push branch to remote: `git push -u origin <branch-name>`
- Verify push succeeded

**Mark Step 4 as completed using TodoWrite before proceeding**

---

### 5. Generate PR Title and Description
**Mark this step as in_progress using TodoWrite**

#### PR Title (Local Rule - java-sdk Repository)
- **Format:** `[TICKET-ID] Brief description of changes`
- **Ticket ID:** Use uppercase format (e.g., `[FSSDK-12345]`) or `[FSSDK-0000]` if no ticket
- **Example:** `[FSSDK-12345] Add feature rollout support`

#### PR Body/Description (Follow pr-format.md)

**🚨 CRITICAL WORKFLOW - Follow these steps exactly:**

**Step 1: Read the Template**
- ALWAYS read `prompts/rules/pr-format.md` first (lines 55-70 for template)
- The template has ONLY these sections:
- ## Summary
- ## Changes
- ## Jira Ticket
- ## Notes (Optional - only for breaking changes)

**Step 2: Read Forbidden Sections**
- Read pr-format.md lines 48-53 for what NOT to include
- NEVER add: Testing, Test Coverage, Quality Assurance, Files Modified, Implementation Details
- NEVER add: Commits section (similar to Files Modified)

**Step 3: Generate PR Description**
- Use ONLY the template sections from pr-format.md
- Summary: 2-3 sentences max
- Changes: 3-5 bullet points, high-level only
- Jira Ticket: `[FSSDK-0000](https://optimizely-ext.atlassian.net/browse/FSSDK-0000)` or actual ticket
- Notes: Only if breaking changes exist

**Step 4: Verify Before Sending**
- Check: Does output have exactly Summary, Changes, Jira Ticket (and optionally Notes)?
- Check: Does output have ANY sections not in the template (Commits, Files, Tests, etc.)?
- If ANY extra sections exist → REMOVE THEM
- Only proceed when output matches template exactly

**Mark Step 5 as completed using TodoWrite before proceeding**

---

### 6. Create or Update Pull Request
**Mark this step as in_progress using TodoWrite**

#### a. Check if PR already exists
- Use `mcp__github__list_pull_requests` with `head` parameter to check for existing PR
- Search for PRs with head branch matching current branch

#### b. If PR exists - Update it
- Use `mcp__github__update_pull_request` with PR number
- Parameters:
- `owner`: Repository owner
- `repo`: "java-sdk"
- `pullNumber`: Existing PR number
- `title`: New PR title
- `body`: New PR description
- Report: "Updated existing PR #X"

#### c. If PR does not exist - Create it
- **CRITICAL:** Use GitHub MCP tool `mcp__github__create_pull_request`
- **NEVER** use `gh pr create` via Bash
- Parameters:
- `owner`: Repository owner
- `repo`: "java-sdk"
- `title`: PR title
- `head`: Current branch name
- `base`: Base branch (usually "master")
- `body`: PR description
- Report: "Created new PR #X"

**Mark Step 6 as completed using TodoWrite before proceeding**

---

### 7. Report Results
**Mark this step as in_progress using TodoWrite**
- Display PR URL
- Show PR title and base branch
- Confirm PR was created successfully

**Mark Step 7 as completed using TodoWrite - PR creation complete!**

---

## Example Usage

User: "/create-pr-current"

Assistant executes:
1. Gets current branch: `jae/add-feature`
2. Checks for merge conflicts with master (if any, resolves interactively)
3. Gets repository information
4. Pushes: `git push -u origin jae/add-feature`
5. Generates PR title and description
6. Creates PR using `mcp__github__create_pull_request`
7. Returns PR URL

**Note:** Triggers are disabled to avoid confusion with create-prs agent.
Must be explicitly invoked with `/create-pr-current` command.

## Error Handling

- If no git repository: Report error
- If on master/main branch: Warn and ask for confirmation
- If uncommitted changes: Offer to show status and ask to commit first
- If push fails: Report error and abort
- If GitHub MCP fails: Report error (do NOT fall back to gh CLI)
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public DatafileProjectConfig(String accountId,
List<Experiment> allExperiments = new ArrayList<Experiment>();
allExperiments.addAll(experiments);
allExperiments.addAll(aggregateGroupExperiments(groups));

// Inject "everyone else" variation into feature_rollout experiments
allExperiments = injectFeatureRolloutVariations(allExperiments, this.featureFlags, this.rollouts);

this.experiments = Collections.unmodifiableList(allExperiments);

if (holdouts == null) {
Expand Down Expand Up @@ -357,6 +361,110 @@ public Experiment getExperimentForVariationId(String variationId) {
return this.variationIdToExperimentMapping.get(variationId);
}

/**
* Injects the "everyone else" variation from the flag's rollout into any experiment
* with type "feature_rollout". This enables Feature Rollout experiments to fall back
* to the everyone else variation when users are outside the rollout percentage.
*/
private List<Experiment> injectFeatureRolloutVariations(
List<Experiment> allExperiments,
List<FeatureFlag> featureFlags,
List<Rollout> rollouts
) {
if (featureFlags == null || featureFlags.isEmpty()) {
return allExperiments;
}

// Build rollout ID to Rollout mapping
Map<String, Rollout> rolloutMap = new HashMap<>();
if (rollouts != null) {
for (Rollout rollout : rollouts) {
rolloutMap.put(rollout.getId(), rollout);
}
}

// Build experiment ID to index mapping for quick lookup
Map<String, Integer> experimentIndexMap = new HashMap<>();
for (int i = 0; i < allExperiments.size(); i++) {
experimentIndexMap.put(allExperiments.get(i).getId(), i);
}

List<Experiment> result = new ArrayList<>(allExperiments);

for (FeatureFlag flag : featureFlags) {
Variation everyoneElseVariation = getEveryoneElseVariation(flag, rolloutMap);
if (everyoneElseVariation == null) {
continue;
}

for (String experimentId : flag.getExperimentIds()) {
Integer index = experimentIndexMap.get(experimentId);
if (index == null) {
continue;
}
Experiment experiment = result.get(index);
if (!Experiment.TYPE_FR.equals(experiment.getType())) {
continue;
}

// Create new experiment with injected variation and traffic allocation
List<Variation> newVariations = new ArrayList<>(experiment.getVariations());
newVariations.add(everyoneElseVariation);

List<TrafficAllocation> newTrafficAllocation = new ArrayList<>(experiment.getTrafficAllocation());
newTrafficAllocation.add(new TrafficAllocation(everyoneElseVariation.getId(), 10000));

Experiment updatedExperiment = new Experiment(
experiment.getId(),
experiment.getKey(),
experiment.getStatus(),
experiment.getLayerId(),
experiment.getAudienceIds(),
experiment.getAudienceConditions(),
newVariations,
experiment.getUserIdToVariationKeyMap(),
newTrafficAllocation,
experiment.getGroupId(),
experiment.getCmab(),
experiment.getType()
);

result.set(index, updatedExperiment);
}
}

return result;
}

/**
* Gets the "everyone else" variation from the flag's rollout.
* The everyone else rule is the last experiment in the rollout,
* and the variation is the first variation of that rule.
*
* @return the everyone else variation, or null if it cannot be resolved
*/
@Nullable
private Variation getEveryoneElseVariation(FeatureFlag flag, Map<String, Rollout> rolloutMap) {
String rolloutId = flag.getRolloutId();
if (rolloutId == null || rolloutId.isEmpty()) {
return null;
}
Rollout rollout = rolloutMap.get(rolloutId);
if (rollout == null) {
return null;
}
List<Experiment> rolloutExperiments = rollout.getExperiments();
if (rolloutExperiments == null || rolloutExperiments.isEmpty()) {
return null;
}
Experiment everyoneElseRule = rolloutExperiments.get(rolloutExperiments.size() - 1);
List<Variation> variations = everyoneElseRule.getVariations();
if (variations == null || variations.isEmpty()) {
return null;
}
return variations.get(0);
}

private List<Experiment> aggregateGroupExperiments(List<Group> groups) {
List<Experiment> groupExperiments = new ArrayList<Experiment>();
for (Group group : groups) {
Expand Down
Loading
Loading