|
| 1 | +""" |
| 2 | +.github/scripts/upgrade_readme.py |
| 3 | +
|
| 4 | +Reads the current README.md and the full list of exported functions (from |
| 5 | +EXPORTS_JSON), then asks the AI to make one focused, cohesive improvement |
| 6 | +to the README based on the week's section focus. |
| 7 | +
|
| 8 | +Required environment variables: |
| 9 | + OPENAI_API_KEY - your OpenAI secret key |
| 10 | + EXPORTS_JSON - JSON string produced by extract_exports.py |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import json |
| 16 | +from openai import OpenAI |
| 17 | + |
| 18 | +README_PATH = "README.md" |
| 19 | + |
| 20 | +PROMPT_TEMPLATE = """\ |
| 21 | +You are a technical writer helping improve the README for an R package. |
| 22 | +
|
| 23 | +## Current README |
| 24 | +
|
| 25 | +```markdown |
| 26 | +{readme_content} |
| 27 | +``` |
| 28 | +
|
| 29 | +## Exported Functions |
| 30 | +
|
| 31 | +Below are all the exported functions in this package, along with their \ |
| 32 | +current roxygen2 documentation: |
| 33 | +
|
| 34 | +{exports_block} |
| 35 | +
|
| 36 | +## Your Task |
| 37 | +
|
| 38 | +Make a single, cohesive improvement to the README focused on: **{section_focus}** |
| 39 | +
|
| 40 | +Guidelines: |
| 41 | +- Improve or add the "{section_focus}" section using the exported functions above as your source of truth. |
| 42 | +- Write clean, idiomatic R code examples that actually work. |
| 43 | +- Keep the tone friendly and practical — help someone get up and running quickly. |
| 44 | +- Do not remove or substantially alter any existing sections outside your focus area. |
| 45 | +- Do not add placeholder text like "coming soon" or "TODO". |
| 46 | +- Return ONLY the full updated README.md contents, with no explanation or markdown fences around it. |
| 47 | +""" |
| 48 | + |
| 49 | + |
| 50 | +def format_exports_block(exports: list[dict]) -> str: |
| 51 | + blocks = [] |
| 52 | + for exp in exports: |
| 53 | + blocks.append(f"### `{exp['name']}` ({exp['file']})\n\n{exp['roxygen']}") |
| 54 | + return "\n\n---\n\n".join(blocks) |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + api_key = os.environ.get("OPENAI_API_KEY") |
| 59 | + exports_json = os.environ.get("EXPORTS_JSON") |
| 60 | + |
| 61 | + missing = [k for k, v in { |
| 62 | + "OPENAI_API_KEY": api_key, |
| 63 | + "EXPORTS_JSON": exports_json, |
| 64 | + }.items() if not v] |
| 65 | + |
| 66 | + if missing: |
| 67 | + print(f"❌ Missing required environment variables: {', '.join(missing)}", file=sys.stderr) |
| 68 | + sys.exit(1) |
| 69 | + |
| 70 | + exports = json.loads(exports_json) |
| 71 | + section_focus = os.environ.get("SECTION_FOCUS", "Usage Examples") |
| 72 | + |
| 73 | + if not os.path.exists(README_PATH): |
| 74 | + print(f"❌ {README_PATH} not found in repo root.", file=sys.stderr) |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | + with open(README_PATH, "r") as f: |
| 78 | + readme_content = f.read() |
| 79 | + |
| 80 | + exports_block = format_exports_block(exports) |
| 81 | + |
| 82 | + prompt = PROMPT_TEMPLATE.format( |
| 83 | + readme_content=readme_content, |
| 84 | + exports_block=exports_block, |
| 85 | + section_focus=section_focus, |
| 86 | + ) |
| 87 | + |
| 88 | + client = OpenAI(api_key=api_key) |
| 89 | + print(f"⏳ Calling OpenAI to improve README (focus: {section_focus}) ...") |
| 90 | + |
| 91 | + response = client.chat.completions.create( |
| 92 | + model="gpt-4o", |
| 93 | + messages=[{"role": "user", "content": prompt}], |
| 94 | + temperature=0.4, |
| 95 | + ) |
| 96 | + |
| 97 | + updated_readme = response.choices[0].message.content.strip() |
| 98 | + |
| 99 | + with open(README_PATH, "w") as f: |
| 100 | + f.write(updated_readme) |
| 101 | + |
| 102 | + print(f"✅ README updated successfully.") |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments