1
+ name : Add Docs Preview Link
2
+
3
+ on :
4
+ pull_request :
5
+ types : [opened, synchronize]
6
+ paths :
7
+ - ' docs/**'
8
+ - ' **.md'
9
+ issue_comment :
10
+ types : [created]
11
+
12
+ permissions :
13
+ contents : read
14
+ pull-requests : write
15
+
16
+ jobs :
17
+ add-preview-link :
18
+ runs-on : ubuntu-latest
19
+ if : |
20
+ (github.event_name == 'pull_request') ||
21
+ (github.event_name == 'issue_comment' && github.event.issue.pull_request && contains(github.event.comment.body, '/docs-preview'))
22
+ steps :
23
+ - name : Checkout
24
+ uses : actions/checkout@v4
25
+
26
+ - name : Get PR Details
27
+ id : pr_details
28
+ run : |
29
+ if [[ "${{ github.event_name }}" == "pull_request" ]]; then
30
+ echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
31
+ echo "branch=${{ github.head_ref }}" >> $GITHUB_OUTPUT
32
+ else
33
+ # For comments, we need to fetch the PR information
34
+ PR_NUMBER="${{ github.event.issue.number }}"
35
+ echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
36
+ BRANCH=$(gh pr view $PR_NUMBER --json headRefName -q .headRefName)
37
+ echo "branch=$BRANCH" >> $GITHUB_OUTPUT
38
+ fi
39
+ env :
40
+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
41
+
42
+ - name : Find files with most additions (when requested via comment)
43
+ id : find_changed_files
44
+ if : github.event_name == 'issue_comment'
45
+ run : |
46
+ # Get the list of changed files in the docs directory
47
+ PR_NUMBER="${{ steps.pr_details.outputs.pr_number }}"
48
+ CHANGED_FILES=$(gh pr diff $PR_NUMBER --name-only | grep -E "^docs/|\.md$" || echo "")
49
+
50
+ if [[ -z "$CHANGED_FILES" ]]; then
51
+ echo "No documentation files changed in this PR."
52
+ echo "has_changes=false" >> $GITHUB_OUTPUT
53
+ exit 0
54
+ else
55
+ echo "has_changes=true" >> $GITHUB_OUTPUT
56
+ fi
57
+
58
+ # Find the file with the most additions
59
+ MOST_CHANGED=""
60
+ MAX_ADDITIONS=0
61
+
62
+ while IFS= read -r file; do
63
+ if [[ -n "$file" ]]; then
64
+ # Get additions count for this file
65
+ ADDITIONS=$(gh pr diff $PR_NUMBER --patch | grep "^+++ b/$file" -A 1000 | grep -c "^+" || echo "0")
66
+
67
+ if (( ADDITIONS > MAX_ADDITIONS )); then
68
+ MAX_ADDITIONS=$ADDITIONS
69
+ MOST_CHANGED=$file
70
+ fi
71
+ fi
72
+ done <<< "$CHANGED_FILES"
73
+
74
+ if [[ -n "$MOST_CHANGED" ]]; then
75
+ # Convert path to URL path by removing the file extension and default index files
76
+ URL_PATH=$(echo $MOST_CHANGED | sed -E 's/\.md$//' | sed -E 's/\/index$//')
77
+ echo "most_changed_file=$MOST_CHANGED" >> $GITHUB_OUTPUT
78
+ echo "most_changed_url_path=$URL_PATH" >> $GITHUB_OUTPUT
79
+ fi
80
+ env :
81
+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
82
+
83
+ - name : Update PR Description
84
+ if : github.event_name == 'pull_request'
85
+ run : |
86
+ PR_NUMBER="${{ steps.pr_details.outputs.pr_number }}"
87
+ BRANCH="${{ steps.pr_details.outputs.branch }}"
88
+ PREVIEW_URL="https://coder.com/docs/@$BRANCH"
89
+
90
+ # Get current PR description
91
+ PR_BODY=$(gh pr view $PR_NUMBER --json body -q .body)
92
+
93
+ # Check if preview link already exists
94
+ if [[ "$PR_BODY" == *"[preview]"*"$PREVIEW_URL"* ]]; then
95
+ echo "Preview link already exists in PR description."
96
+ else
97
+ # Add preview link to the end of the PR description
98
+ NEW_BODY="${PR_BODY}
99
+
100
+ [preview](${PREVIEW_URL})"
101
+
102
+ # Update PR description
103
+ gh pr edit $PR_NUMBER --body "$NEW_BODY"
104
+ echo "Added preview link to PR description : $PREVIEW_URL"
105
+ fi
106
+ env :
107
+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
108
+
109
+ - name : Comment on PR with Preview Link
110
+ if : github.event_name == 'issue_comment' && steps.find_changed_files.outputs.has_changes == 'true'
111
+ run : |
112
+ PR_NUMBER="${{ steps.pr_details.outputs.pr_number }}"
113
+ BRANCH="${{ steps.pr_details.outputs.branch }}"
114
+ MOST_CHANGED="${{ steps.find_changed_files.outputs.most_changed_file }}"
115
+ URL_PATH="${{ steps.find_changed_files.outputs.most_changed_url_path }}"
116
+
117
+ BASE_PREVIEW_URL="https://coder.com/docs/@$BRANCH"
118
+
119
+ if [[ -n "$URL_PATH" ]]; then
120
+ # If we have a specific file that changed the most, link directly to it
121
+ FILE_PREVIEW_URL="${BASE_PREVIEW_URL}/${URL_PATH}"
122
+ COMMENT="📚 Documentation preview is available:
123
+ - Full docs : [${BASE_PREVIEW_URL}](${BASE_PREVIEW_URL})
124
+ - Most changed file (\`${MOST_CHANGED}\`) : [${FILE_PREVIEW_URL}](${FILE_PREVIEW_URL})"
125
+ else
126
+ # Just link to the main docs page
127
+ COMMENT="📚 Documentation preview is available :
128
+ - [${BASE_PREVIEW_URL}](${BASE_PREVIEW_URL})"
129
+ fi
130
+
131
+ gh pr comment $PR_NUMBER --body "$COMMENT"
132
+ env :
133
+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
0 commit comments