-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
60 lines (51 loc) · 1.99 KB
/
action.yml
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
name: 'Snippet Permalink Updater'
description: 'Updates a Markdown file with a permalink to a specific code snippet'
author: Tibor Schmidt <[email protected]>
inputs:
snippet_file:
description: 'Path to the file containing the snippet'
required: true
start_marker:
description: 'Start marker for the snippet'
required: true
end_marker:
description: 'End marker for the snippet'
required: true
markdown_file:
description: 'Path to the Markdown file to update'
default: 'README.md'
replace_marker:
description: 'Marker in Markdown file to identify where to replace the permalink'
required: true
outputs:
permalink:
description: 'The generated permalink'
runs:
using: 'composite'
steps:
- name: Get latest commit ID for snippet file
id: get-commit
shell: bash
run: |
COMMIT_ID=$(git log -n 1 --pretty=format:%H -- ${{ inputs.snippet_file }})
echo "commit_id=$COMMIT_ID" >> $GITHUB_OUTPUT
- name: Find line numbers and update Markdown file
shell: bash
run: |
START_LINE=$(grep -n -- "${{ inputs.start_marker }}" "${{ inputs.snippet_file }}" | cut -d: -f1)
END_LINE=$(grep -n -- "${{ inputs.end_marker }}" "${{ inputs.snippet_file }}" | cut -d: -f1)
if [ -z "$START_LINE" ] || [ -z "$END_LINE" ]; then
echo "Markers not found in snippet file"
exit 1
fi
START_LINE=$((START_LINE + 1))
END_LINE=$((END_LINE - 1))
PERMALINK="https://github.com/${GITHUB_REPOSITORY}/blob/${{ steps.get-commit.outputs.commit_id }}/${{ inputs.snippet_file }}#L${START_LINE}-L${END_LINE}"
echo "permalink=$PERMALINK" >> $GITHUB_OUTPUT
sed "/${{ inputs.replace_marker }}/!b;n;c$PERMALINK" \
"${{ inputs.markdown_file }}" > temp_file && \
mv temp_file "${{ inputs.markdown_file }}"
- name: Output diff for the changed file
shell: bash
run: |
git diff "${{ inputs.markdown_file }}"