|
| 1 | +name: Add Guest Article |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [labeled] |
| 6 | + |
| 7 | +jobs: |
| 8 | + add-article: |
| 9 | + if: | |
| 10 | + github.event.label.name == 'approved' && |
| 11 | + contains(github.event.issue.labels.*.name, 'article') |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + contents: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Parse issue and create article content file |
| 22 | + id: parse |
| 23 | + env: |
| 24 | + ISSUE_BODY: ${{ github.event.issue.body }} |
| 25 | + ISSUE_NUMBER: ${{ github.event.issue.number }} |
| 26 | + ISSUE_TITLE: ${{ github.event.issue.title }} |
| 27 | + run: | |
| 28 | + python3 - <<'PYEOF' |
| 29 | + import os, re, datetime, yaml |
| 30 | +
|
| 31 | + body = os.environ['ISSUE_BODY'] |
| 32 | +
|
| 33 | + def field(label): |
| 34 | + m = re.search(rf'### {re.escape(label)}\s+(.+?)(?=\n###|\Z)', body, re.DOTALL) |
| 35 | + if not m: |
| 36 | + return '' |
| 37 | + val = m.group(1).strip() |
| 38 | + return '' if val in ('_No response_', '') else val |
| 39 | +
|
| 40 | + title = field('Article Title') |
| 41 | + author = field('Author Name') |
| 42 | + description = field('Description') |
| 43 | + category = field('Category') |
| 44 | + tags_raw = field('Tags (optional)') |
| 45 | + content = field('Article Content (Markdown)') |
| 46 | +
|
| 47 | + # The Article Content field is rendered as a ```markdown fenced block — unwrap it. |
| 48 | + content = re.sub(r'^```[a-zA-Z]*\n', '', content) |
| 49 | + content = re.sub(r'\n```$', '', content).strip() |
| 50 | +
|
| 51 | + if not title: |
| 52 | + raise SystemExit('No article title found; aborting.') |
| 53 | + if not content: |
| 54 | + # A pitch with no draft — nothing to publish yet. |
| 55 | + print('No article content provided (pitch only); skipping file creation.') |
| 56 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as out: |
| 57 | + out.write('skip=true\n') |
| 58 | + raise SystemExit(0) |
| 59 | +
|
| 60 | + tags = [t.strip() for t in re.split(r'[,\n]', tags_raw) if t.strip()] |
| 61 | +
|
| 62 | + today = datetime.datetime.now(datetime.timezone.utc) |
| 63 | + date_str = today.strftime('%Y-%m-%dT%H:%M:%S+00:00') |
| 64 | +
|
| 65 | + slug = re.sub(r'[^a-z0-9]+', '-', title.lower()).strip('-') |
| 66 | + filename = f"{today.strftime('%Y-%m-%d')}-{slug}.md" |
| 67 | +
|
| 68 | + fm = { |
| 69 | + 'title': title, |
| 70 | + 'author': author, |
| 71 | + 'authors': [author] if author else [], |
| 72 | + 'date': date_str, |
| 73 | + 'description': description, |
| 74 | + } |
| 75 | + if category: |
| 76 | + fm['categories'] = [category] |
| 77 | + if tags: |
| 78 | + fm['tags'] = tags |
| 79 | +
|
| 80 | + front = yaml.safe_dump(fm, default_flow_style=False, allow_unicode=True, sort_keys=False) |
| 81 | + document = '---\n' + front + '---\n\n' + content + '\n' |
| 82 | +
|
| 83 | + filepath = f'content/articles/{filename}' |
| 84 | + os.makedirs('content/articles', exist_ok=True) |
| 85 | + with open(filepath, 'w', encoding='utf-8') as f: |
| 86 | + f.write(document) |
| 87 | +
|
| 88 | + with open(os.environ['GITHUB_OUTPUT'], 'a') as out: |
| 89 | + out.write(f'skip=false\n') |
| 90 | + out.write(f'slug={slug}\n') |
| 91 | + out.write(f'filepath={filepath}\n') |
| 92 | + out.write(f'article_title={title}\n') |
| 93 | +
|
| 94 | + print(f'Created {filepath}') |
| 95 | + PYEOF |
| 96 | +
|
| 97 | + - name: Open PR |
| 98 | + if: steps.parse.outputs.skip == 'false' |
| 99 | + env: |
| 100 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 101 | + SLUG: ${{ steps.parse.outputs.slug }} |
| 102 | + FILEPATH: ${{ steps.parse.outputs.filepath }} |
| 103 | + ARTICLE_TITLE: ${{ steps.parse.outputs.article_title }} |
| 104 | + ISSUE_NUMBER: ${{ github.event.issue.number }} |
| 105 | + run: | |
| 106 | + BRANCH="article/issue-${ISSUE_NUMBER}-${SLUG}" |
| 107 | + git config user.name "github-actions[bot]" |
| 108 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 109 | + git checkout -b "$BRANCH" |
| 110 | + git add "$FILEPATH" |
| 111 | + git commit -m "Add article: ${ARTICLE_TITLE} (closes #${ISSUE_NUMBER})" |
| 112 | + git push origin "$BRANCH" |
| 113 | + gh pr create \ |
| 114 | + --title "Add article: ${ARTICLE_TITLE}" \ |
| 115 | + --body "Closes #${ISSUE_NUMBER} |
| 116 | +
|
| 117 | + Auto-generated from guest blog post submission. Please review front matter and content before merging." \ |
| 118 | + --base main \ |
| 119 | + --head "$BRANCH" |
0 commit comments