forked from opendatahub-io/workload-variant-autoscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
140 lines (122 loc) · 5.83 KB
/
assign-docs-pr.yml
File metadata and controls
140 lines (122 loc) · 5.83 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Assigns documentation PRs created by the Update Docs workflow
# to the AUTHOR (not merger) of the original PR that triggered the docs update
name: Assign Docs PR to Original Author
on:
pull_request:
types: [opened]
workflow_dispatch:
inputs:
docs_pr_number:
description: 'The docs PR number to assign (will extract source PR from body/branch)'
required: true
type: number
permissions:
pull-requests: write
contents: read
jobs:
assign-to-original-author:
# Run for: 1) PRs created by github-actions bot with docs: title, or 2) manual workflow_dispatch
if: >
github.event_name == 'workflow_dispatch' ||
(github.actor == 'github-actions[bot]' && startsWith(github.event.pull_request.title, 'docs:'))
runs-on: ubuntu-latest
steps:
- name: Get original PR author
id: get_author
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
EVENT_PR_BODY: ${{ github.event.pull_request.body }}
EVENT_PR_HEAD_REF: ${{ github.event.pull_request.head.ref }}
INPUT_DOCS_PR: ${{ inputs.docs_pr_number }}
run: |
echo "Finding original PR author..."
# For workflow_dispatch, fetch the docs PR details
if [ -n "$INPUT_DOCS_PR" ]; then
echo "Manual trigger - fetching docs PR #$INPUT_DOCS_PR details..."
if ! PR_BODY=$(gh pr view "$INPUT_DOCS_PR" --repo "$REPO" --json body --jq '.body'); then
echo "Error: Unable to fetch body for docs PR #$INPUT_DOCS_PR. Check that the PR number is valid and accessible."
exit 1
fi
if ! PR_HEAD_REF=$(gh pr view "$INPUT_DOCS_PR" --repo "$REPO" --json headRefName --jq '.headRefName'); then
echo "Error: Unable to fetch head ref for docs PR #$INPUT_DOCS_PR. Check that the PR number is valid and accessible."
exit 1
fi
else
PR_BODY="$EVENT_PR_BODY"
PR_HEAD_REF="$EVENT_PR_HEAD_REF"
fi
ORIGINAL_PR_NUMBER=""
# Try to extract original PR number from the docs PR body (e.g., 'Source PR: #123' or just '#123')
if echo "$PR_BODY" | grep -Eq '#[0-9]+'; then
ORIGINAL_PR_NUMBER="$(echo "$PR_BODY" | grep -oE '#[0-9]+' | head -n1 | tr -d '#')"
echo "Detected original PR number from body: #$ORIGINAL_PR_NUMBER"
# Fallback: try to extract PR number from the head branch name (e.g., 'docs/update-123')
elif echo "$PR_HEAD_REF" | grep -Eq '[0-9]+'; then
ORIGINAL_PR_NUMBER="$(echo "$PR_HEAD_REF" | grep -oE '[0-9]+' | head -n1)"
echo "Detected original PR number from head ref: #$ORIGINAL_PR_NUMBER"
fi
PR_AUTHOR=""
if [ -n "$ORIGINAL_PR_NUMBER" ]; then
echo "Fetching author for original PR #$ORIGINAL_PR_NUMBER..."
PR_AUTHOR=$(gh pr view "$ORIGINAL_PR_NUMBER" \
--repo "$REPO" \
--json author \
--jq '.author.login' 2>/dev/null || echo "")
fi
# Fallback: use the most recently merged PR to main (within last hour)
if [ -z "$PR_AUTHOR" ] || [ "$PR_AUTHOR" = "null" ]; then
echo "Could not determine original PR from metadata; falling back to most recently merged PR..."
ONE_HOUR_AGO=$(date -u -d '1 hour ago' +'%Y-%m-%dT%H:%M:%SZ' 2>/dev/null || date -u -v-1H +'%Y-%m-%dT%H:%M:%SZ')
PR_AUTHOR=$(gh pr list \
--repo "$REPO" \
--base main \
--state merged \
--limit 5 \
--search "merged:>=$ONE_HOUR_AGO" \
--json author,mergedAt \
--jq '[.[] | select(.mergedAt != null)] | sort_by(.mergedAt) | reverse | .[0].author.login' 2>/dev/null || echo "")
fi
if [ -n "$PR_AUTHOR" ] && [ "$PR_AUTHOR" != "null" ]; then
echo "Found original PR author: $PR_AUTHOR"
echo "author=$PR_AUTHOR" >> $GITHUB_OUTPUT
else
echo "No suitable original PR found, skipping assignment"
echo "author=" >> $GITHUB_OUTPUT
fi
- name: Assign PR to original author
if: steps.get_author.outputs.author != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_DOCS_PR: ${{ inputs.docs_pr_number }}
EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
AUTHOR="${{ steps.get_author.outputs.author }}"
# Use input docs_pr_number if provided (workflow_dispatch), otherwise use event PR number
PR_NUMBER="${INPUT_DOCS_PR:-$EVENT_PR_NUMBER}"
if [ -z "$PR_NUMBER" ]; then
echo "Error: PR_NUMBER is empty. Cannot assign PR."
exit 1
fi
echo "Assigning PR #$PR_NUMBER to $AUTHOR (original PR author)"
gh pr edit "$PR_NUMBER" \
--repo "${{ github.repository }}" \
--add-assignee "$AUTHOR"
echo "Successfully assigned PR #$PR_NUMBER to $AUTHOR"
- name: Add comment with attribution
if: steps.get_author.outputs.author != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_DOCS_PR: ${{ inputs.docs_pr_number }}
EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
AUTHOR="${{ steps.get_author.outputs.author }}"
# Use input docs_pr_number if provided (workflow_dispatch), otherwise use event PR number
PR_NUMBER="${INPUT_DOCS_PR:-$EVENT_PR_NUMBER}"
if [ -z "$PR_NUMBER" ]; then
echo "Error: PR_NUMBER is empty. Cannot add comment."
exit 1
fi
gh pr comment "$PR_NUMBER" \
--repo "${{ github.repository }}" \
--body "This documentation update was triggered by changes from @$AUTHOR. Assigning for review."