|
| 1 | +--- |
| 2 | +description: Analyze divergences from upstream async-profiler, propose grouped PRs, and open draft PRs. |
| 3 | +--- |
| 4 | + |
| 5 | +# Contribute Upstream Workflow |
| 6 | + |
| 7 | +You are orchestrating the contribution of java-profiler divergences back to the upstream async-profiler project. These are all changes in our repo relative to upstream — not just uncommitted local modifications. Follow these steps precisely. |
| 8 | + |
| 9 | +## Configuration |
| 10 | + |
| 11 | +- **Fork repo**: `git@github.com:DataDog/async-profiler.git` |
| 12 | +- **Upstream repo**: `async-profiler/async-profiler` (for PR target) |
| 13 | +- **Upstream branch**: `master` |
| 14 | +- **Analysis script**: `utils/check_contribution_candidates.sh` |
| 15 | +- **Report dir**: `build/contribution-reports/` |
| 16 | + |
| 17 | +## Step 1: Run Analysis |
| 18 | + |
| 19 | +Execute the analysis script to generate reports: |
| 20 | + |
| 21 | +```bash |
| 22 | +./utils/check_contribution_candidates.sh |
| 23 | +``` |
| 24 | + |
| 25 | +If it fails, diagnose and report the error to the user. Do not proceed. |
| 26 | + |
| 27 | +## Step 2: Parse Results |
| 28 | + |
| 29 | +Find the most recent JSON report in `build/contribution-reports/` (highest timestamp). Read it to get the list of files with contributable hunks. |
| 30 | + |
| 31 | +Also read the corresponding markdown report to understand the actual diff hunks for each file. |
| 32 | + |
| 33 | +If there are zero candidates, tell the user and stop. |
| 34 | + |
| 35 | +## Step 3: Group into PR Proposals |
| 36 | + |
| 37 | +Analyze the contributable hunks across all candidate files and group them into logical PR proposals. Guidelines: |
| 38 | + |
| 39 | +- **Related changes go together**: e.g., a bug fix touching `stackWalker.cpp` and `vmStructs.cpp` for the same issue = one PR |
| 40 | +- **Independent changes are separate**: unrelated fixes in different files = separate PRs |
| 41 | +- **Each PR should be self-contained**: it should make sense on its own, compile on its own, and have a clear rationale |
| 42 | +- **Keep PRs small**: prefer multiple small PRs over one large one |
| 43 | + |
| 44 | +For each proposed PR, prepare: |
| 45 | +- A descriptive title (e.g., "Fix null pointer check in stackWalker", "Add bounds validation in VMStruct") |
| 46 | +- The list of files and hunks it covers |
| 47 | +- A brief rationale explaining why this change benefits upstream |
| 48 | + |
| 49 | +## Step 4: Present Proposals to User |
| 50 | + |
| 51 | +Show the user a numbered list of proposed PRs with: |
| 52 | +- Title |
| 53 | +- Files involved |
| 54 | +- Brief description of the change |
| 55 | +- Number of hunks |
| 56 | + |
| 57 | +Use `AskUserQuestion` with `multiSelect: true` to let the user pick which PRs to create. Offer all proposals as options. |
| 58 | + |
| 59 | +If the user selects none, stop. |
| 60 | + |
| 61 | +## Step 5: Create Selected PRs |
| 62 | + |
| 63 | +For each selected PR, perform the following: |
| 64 | + |
| 65 | +### 5a. Clone the Fork (once) |
| 66 | + |
| 67 | +Clone `git@github.com:DataDog/async-profiler.git` to a temp directory. Reuse this clone for all PRs. |
| 68 | + |
| 69 | +```bash |
| 70 | +FORK_DIR=$(mktemp -d "${TMPDIR:-/tmp}/async-profiler-fork.XXXXXX") |
| 71 | +git clone git@github.com:DataDog/async-profiler.git "$FORK_DIR" |
| 72 | +cd "$FORK_DIR" |
| 73 | +git remote add upstream https://github.com/async-profiler/async-profiler.git |
| 74 | +git fetch upstream |
| 75 | +git checkout -b temp upstream/master |
| 76 | +git branch -D master 2>/dev/null || true |
| 77 | +git checkout -b master |
| 78 | +git branch -D temp |
| 79 | +``` |
| 80 | + |
| 81 | +### 5b. Create Feature Branch |
| 82 | + |
| 83 | +For each PR, create a branch from upstream master: |
| 84 | + |
| 85 | +```bash |
| 86 | +cd "$FORK_DIR" |
| 87 | +git checkout master |
| 88 | +BRANCH_NAME="contribute/<slug>-$(date +%Y%m%d)" |
| 89 | +git checkout -b "$BRANCH_NAME" |
| 90 | +``` |
| 91 | + |
| 92 | +Where `<slug>` is a short kebab-case description derived from the PR title. |
| 93 | + |
| 94 | +### 5c. Port Changes |
| 95 | + |
| 96 | +Apply only the relevant contributable hunks for this PR to the upstream files: |
| 97 | + |
| 98 | +1. For each file in the PR, find the corresponding upstream file in `src/` of the fork |
| 99 | +2. Apply the contributable hunks using careful manual editing (use the Edit tool) |
| 100 | +3. **Critical**: Ensure NO Datadog-specific references leak through (DD_, ddprof, Datadog, datadog, DDPROF, context.h, counters.h, tagger, QueueItem) |
| 101 | +4. If a hunk cannot be cleanly applied because the upstream file diverged, skip it and note it for the user |
| 102 | + |
| 103 | +### 5d. Verify Build |
| 104 | + |
| 105 | +Attempt a basic build check: |
| 106 | + |
| 107 | +```bash |
| 108 | +cd "$FORK_DIR" |
| 109 | +make |
| 110 | +``` |
| 111 | + |
| 112 | +If the build fails, analyze the error. If it's a simple fix (e.g., missing include), fix it. If it's complex, note it for the user and proceed anyway (the PR is draft). |
| 113 | + |
| 114 | +### 5e. Commit and Push |
| 115 | + |
| 116 | +```bash |
| 117 | +cd "$FORK_DIR" |
| 118 | +git add -A |
| 119 | +git commit -m "<concise description of the change>" |
| 120 | +git push origin "$BRANCH_NAME" |
| 121 | +``` |
| 122 | + |
| 123 | +### 5f. Open Draft PR |
| 124 | + |
| 125 | +Use `gh` to create a draft PR against upstream. **Important**: Before creating the first PR, fetch the target project's PR template from the upstream repo (`gh api repos/async-profiler/async-profiler/contents/.github/PULL_REQUEST_TEMPLATE.md` and decode the base64 content). The PR body **must** follow that template exactly — use all its sections, checkboxes, and footer verbatim. Fill in each section with the relevant content for this change. |
| 126 | + |
| 127 | +```bash |
| 128 | +gh pr create \ |
| 129 | + --repo async-profiler/async-profiler \ |
| 130 | + --base master \ |
| 131 | + --head "DataDog:$BRANCH_NAME" \ |
| 132 | + --draft \ |
| 133 | + --title "<PR title>" \ |
| 134 | + --body "<body following the upstream PR template>" |
| 135 | +``` |
| 136 | + |
| 137 | +## Step 6: Report |
| 138 | + |
| 139 | +After all selected PRs are created, show the user: |
| 140 | +- A summary table of created PRs with their URLs |
| 141 | +- Any hunks that could not be applied |
| 142 | +- Any build issues encountered |
| 143 | +- The temp directory path in case manual follow-up is needed |
| 144 | + |
| 145 | +## Error Handling |
| 146 | + |
| 147 | +- If `gh` is not authenticated, tell the user to run `gh auth login` and stop |
| 148 | +- If the fork clone fails, check SSH key setup and report |
| 149 | +- If a branch already exists on the fork, append a counter suffix (e.g., `-2`) |
| 150 | +- Always clean up on fatal errors (remove temp directory) |
0 commit comments