Repository navigation
126 lines (107 loc) · 4.56 KB
/
Copy pathauto-docs.yml
File metadata and controls
126 lines (107 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
name: Auto-docs intake
# Trigger when PRs are merged to main, or manually with a PR number for testing.
# IMPORTANT: This workflow does NOT check out PR code and does NOT execute any
# code from the PR branch. It only reads PR metadata and forwards it to a webhook.
on:
push:
branches: [main]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to process manually'
required: true
type: number
# Minimal permissions: read repo contents and PR metadata only.
permissions:
contents: read
pull-requests: read
# One run per push or manual dispatch at a time. Does not cancel in progress.
concurrency:
group: ${{ github.event_name == 'push' && format('push-{0}', github.sha) || format('manual-{0}', inputs.pr_number) }}
cancel-in-progress: false
jobs:
call-valtown:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Determine PR number and check branch prefix or title
id: pr
env:
GH_TOKEN: ${{ github.token }}
run: |
set -euo pipefail
trap 'rm -f /tmp/gh_err.txt' EXIT
if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
PR_NUMBER="${{ inputs.pr_number }}"
else
PR_NUMBER=$(gh api "repos/${{ github.repository }}/commits/${{ github.sha }}/pulls" --jq '.[0].number // empty' 2>/tmp/gh_err.txt) || {
echo "Failed to fetch PR for commit: $(cat /tmp/gh_err.txt)"
echo "should_send=false" >> "$GITHUB_OUTPUT"
exit 0
}
if [[ ! "$PR_NUMBER" =~ ^[0-9]+$ ]]; then
echo "No PR found for commit, skipping."
echo "should_send=false" >> "$GITHUB_OUTPUT"
exit 0
fi
fi
PR_META=$(gh api "repos/${{ github.repository }}/pulls/$PR_NUMBER" --jq '{ref: .head.ref, title: .title}' 2>/tmp/gh_err.txt) || {
echo "Failed to fetch PR #$PR_NUMBER info: $(cat /tmp/gh_err.txt), skipping."
echo "should_send=false" >> "$GITHUB_OUTPUT"
exit 0
}
HEAD_BRANCH=$(echo "$PR_META" | jq -r '.ref')
PR_TITLE=$(echo "$PR_META" | jq -r '.title')
if [[ ! "$HEAD_BRANCH" =~ ^feat(ure)?[/-] ]] && [[ ! "${PR_TITLE,,}" =~ feat(ure)? ]]; then
echo "Skipping PR #$PR_NUMBER: branch '$HEAD_BRANCH' does not start with feat/ or feature/, and title '$PR_TITLE' does not contain feat or feature"
echo "should_send=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
echo "should_send=true" >> "$GITHUB_OUTPUT"
echo "Found PR #$PR_NUMBER with branch: $HEAD_BRANCH, title: $PR_TITLE"
- name: Build and send webhook payload
if: steps.pr.outputs.should_send == 'true'
env:
GH_TOKEN: ${{ github.token }}
DOC_WEBHOOK_URL: ${{ secrets.DOC_WEBHOOK_URL }}
DOC_WEBHOOK_SECRET: ${{ secrets.DOC_WEBHOOK_SECRET }}
run: |
set -euo pipefail
trap 'rm -f pr.json /tmp/gh_err.txt' EXIT
if [ -z "${DOC_WEBHOOK_URL:-}" ]; then
echo "DOC_WEBHOOK_URL secret is not set"
exit 1
fi
if [ -z "${DOC_WEBHOOK_SECRET:-}" ]; then
echo "DOC_WEBHOOK_SECRET secret is not set"
exit 1
fi
PR_NUMBER="${{ steps.pr.outputs.pr_number }}"
gh api "repos/${{ github.repository }}/pulls/$PR_NUMBER" > pr.json 2>/tmp/gh_err.txt || {
echo "Failed to fetch PR #$PR_NUMBER details: $(cat /tmp/gh_err.txt)"
exit 1
}
PAYLOAD=$(jq -c -n \
--arg event_name "${{ github.event_name }}" \
--arg repo "${{ github.repository }}" \
--arg pr_number "${{ steps.pr.outputs.pr_number }}" \
--slurpfile pr pr.json \
'{
event_name: $event_name,
repo: $repo,
pr_number: $pr_number,
title: $pr[0].title,
description: ($pr[0].body // ""),
author: $pr[0].user.login,
merged_at: ($pr[0].merged_at // ""),
pr_url: $pr[0].html_url,
base_branch: $pr[0].base.ref,
head_branch: $pr[0].head.ref
}')
echo "Payload created:"
echo "$PAYLOAD" | jq .
curl --fail-with-body -sS --connect-timeout 10 --max-time 30 -X POST "$DOC_WEBHOOK_URL" \
-H "Content-Type: application/json" \
-H "X-Docs-Webhook-Secret: $DOC_WEBHOOK_SECRET" \
--data-binary "$PAYLOAD"