|
| 1 | +name: 'Snippet Permalink Updater' |
| 2 | +description: 'Updates a Markdown file with a permalink to a specific code snippet' |
| 3 | + |
| 4 | +author: Tibor Schmidt <robitx@gmail.com> |
| 5 | + |
| 6 | +inputs: |
| 7 | + snippet_file: |
| 8 | + description: 'Path to the file containing the snippet' |
| 9 | + required: true |
| 10 | + start_marker: |
| 11 | + description: 'Start marker for the snippet' |
| 12 | + required: true |
| 13 | + end_marker: |
| 14 | + description: 'End marker for the snippet' |
| 15 | + required: true |
| 16 | + markdown_file: |
| 17 | + description: 'Path to the Markdown file to update' |
| 18 | + default: 'README.md' |
| 19 | + replace_marker: |
| 20 | + description: 'Marker in Markdown file to identify where to replace the permalink' |
| 21 | + required: true |
| 22 | + permalink_template: |
| 23 | + description: 'Template for the permalink' |
| 24 | + required: true |
| 25 | +outputs: |
| 26 | + permalink: |
| 27 | + description: 'The generated permalink' |
| 28 | +runs: |
| 29 | + using: 'composite' |
| 30 | + steps: |
| 31 | + - name: Get latest commit ID for snippet file |
| 32 | + id: get-commit |
| 33 | + shell: bash |
| 34 | + run: | |
| 35 | + COMMIT_ID=$(git log -n 1 --pretty=format:%H -- ${{ inputs.snippet_file }}) |
| 36 | + echo "commit_id=$COMMIT_ID" >> $GITHUB_OUTPUT |
| 37 | +
|
| 38 | + - name: Find line numbers and update Markdown file |
| 39 | + shell: bash |
| 40 | + run: | |
| 41 | + START_LINE=$(grep -n "${{ inputs.start_marker }}" "${{ inputs.snippet_file }}" | cut -d: -f1) |
| 42 | + END_LINE=$(grep -n "${{ inputs.end_marker }}" "${{ inputs.snippet_file }}" | cut -d: -f1) |
| 43 | + |
| 44 | + if [ -z "$START_LINE" ] || [ -z "$END_LINE" ]; then |
| 45 | + echo "Markers not found in snippet file" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + |
| 49 | + REPO=$(echo $GITHUB_REPOSITORY) |
| 50 | + PERMALINK=$(echo "${{ inputs.permalink_template }}" | |
| 51 | + sed "s|{{REPO}}|$REPO|g" | |
| 52 | + sed "s|{{COMMIT_ID}}|${{ steps.get-commit.outputs.commit_id }}|g" | |
| 53 | + sed "s|{{FILE_PATH}}|${{ inputs.snippet_file }}|g" | |
| 54 | + sed "s|{{START_LINE}}|$START_LINE|g" | |
| 55 | + sed "s|{{END_LINE}}|$END_LINE|g") |
| 56 | + |
| 57 | + echo "permalink=$PERMALINK" >> $GITHUB_OUTPUT |
| 58 | + |
| 59 | + # Use sed with a temp file to replace the line after the marker |
| 60 | + sed "/${{ inputs.replace_marker }}/!b;n;c$PERMALINK" \ |
| 61 | + "${{ inputs.markdown_file }}" > temp_file && \ |
| 62 | + mv temp_file "${{ inputs.markdown_file }}" |
0 commit comments