diff --git a/.github/workflows/update-main-version.yml b/.github/workflows/update-main-version.yml new file mode 100644 index 0000000..db3fd54 --- /dev/null +++ b/.github/workflows/update-main-version.yml @@ -0,0 +1,30 @@ +name: Update Main Version +run-name: Move ${{ github.event.inputs.major_version }} to ${{ github.event.inputs.target }} + +on: + workflow_dispatch: + inputs: + target: + description: The tag or reference to use + required: true + major_version: + type: choice + description: The major version to update + options: + - v1 + +jobs: + tag: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Git config + run: | + git config user.name github-actions + git config user.email github-actions@github.com + - name: Tag new target + run: git tag -f ${{ github.event.inputs.major_version }} ${{ github.event.inputs.target }} + - name: Push new tag + run: git push origin ${{ github.event.inputs.major_version }} --force \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..85baa39 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "structurizr" + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 6c0cc68..5d826e3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,46 @@ -# structurizr-pr-comment -This GitHub Action automatically makes a comment on each pull requests which updates Structurizr diagrams. It's designed to simplify the process of visualizing architecture diagrams changes in your codebase. +# Comment on PR with Structurizr Diagrams Action + +## Introduction + +This GitHub Action, designed to complement the [Generate Structurizr Diagrams Images from DSL](https://github.com/marketplace/actions/generate-structurizr-diagrams-images-from-dsl) action, automatically comments on pull requests with the previously generated Structurizr diagrams. It ensures that team members can easily review the latest architecture diagrams directly within the context of a PR. + +## Prerequisites + +To use this action effectively, you should have: + +- A GitHub repository. +- Completed the setup of the "Generate Structurizr Diagrams Images from DSL" action in your repository. + +## Usage + +This action is meant to be used in a separate workflow that triggers after the "Generate Structurizr Diagrams Images from DSL" workflow. To set it up: + +1. **Create a workflow file**: In your repository's `.github/workflows` directory, create a new file (e.g., `structurizr-diagrams-comment.yml`) with the following content: + +```yaml +name: Comment on PR with Structurizr Diagrams + +on: + workflow_run: + types: + - completed + workflows: + - "Update Structurizr Diagrams" + +jobs: + comment-on-pr: + if: github.event.workflow_run.conclusion == 'success' + runs-on: ubuntu-latest + steps: + - uses: sebastienfi/structurizr-pr-comment@v1 +``` + +## How It Works + +- The action triggers after the "Update Structurizr Diagrams" workflow completes. +- It retrieves information about the PR and lists the generated diagram files. +- If diagrams are updated, the action posts or updates a comment in the PR with the diagrams. + +## Contributing + +Contributions to improve this action are welcome. Ensure your contributions are well-documented and follow the coding standards of this project. diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..80315e8 --- /dev/null +++ b/action.yml @@ -0,0 +1,98 @@ +name: 'Comment on PR with Structurizr Diagrams' +description: 'Automatically comments on PRs with Structurizr diagrams.' +branding: + icon: 'activity' + color: 'white' +runs: + using: 'composite' + steps: + - name: Get PR Info + run: | + PREVIOUS_JOB_ID=$(jq -r '.id' <<< "$WORKFLOW_RUN_EVENT_OBJ") + echo "Previous Job ID: $PREVIOUS_JOB_ID" + echo "PREVIOUS_JOB_ID=$PREVIOUS_JOB_ID" >> "$GITHUB_ENV" + + PR_NUMBER=$(jq -r '.pull_requests[0].number' \ + <<< "$WORKFLOW_RUN_EVENT_OBJ") + + echo "PR Number: $PR_NUMBER" + echo "PR_NUMBER=$PR_NUMBER" >> "$GITHUB_ENV" + + HEAD_SHA=$(jq -r '.pull_requests[0].head.sha' \ + <<< "$WORKFLOW_RUN_EVENT_OBJ") + + echo "Head sha: $HEAD_SHA" + echo "HEAD_SHA=$HEAD_SHA" >> "$GITHUB_ENV" + shell: bash + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + WORKFLOW_RUN_EVENT_OBJ: ${{ toJSON(github.event.workflow_run) }} + + - name: Checkout Repository + uses: actions/checkout@v4 + with: + ref: ${{ github.event.workflow_run.head_branch }} + + - name: List Diagram Files + run: | + cd ${{ env.OUTPUT_DIR }} + echo 'PNG_FILES_MARKDOWN<> "$GITHUB_ENV" + for png_file in *.png; do + # Skip legend files in this loop + if [[ $png_file == *"-key.png" ]]; then + continue + fi + + base_name=$(basename $png_file .png) + legend_file="${base_name}-key.png" + + # URLs for diagram and its legend + png_raw_file_path="https://raw.githubusercontent.com/${{ github.repository }}/${{ env.HEAD_SHA }}/${{ env.OUTPUT_DIR }}/${png_file}" + legend_raw_file_path="https://raw.githubusercontent.com/${{ github.repository }}/${{ env.HEAD_SHA }}/${{ env.OUTPUT_DIR }}/${legend_file}" + + echo "
" >> "$GITHUB_ENV" + clean_base_name=${base_name#structurizr-} + echo "${clean_base_name}" >> "$GITHUB_ENV" + if [ -f "$legend_file" ]; then + echo "" >> "$GITHUB_ENV" + echo "" >> "$GITHUB_ENV" + fi + echo " " >> "$GITHUB_ENV" + echo "
" >> "$GITHUB_ENV" + done + echo EOF >> "$GITHUB_ENV" + shell: bash + env: + OUTPUT_DIR: 'docs/diagrams' + HEAD_SHA: ${{ env.HEAD_SHA }} + + - name: Find Comment + uses: peter-evans/find-comment@v2 + id: find-comment + env: + PR_NUMBER: ${{ env.PR_NUMBER }} + with: + issue-number: ${{ env.PR_NUMBER }} + comment-author: 'github-actions[bot]' + + - name: Comment on PR + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: ${{ env.PR_NUMBER }} + HEAD_SHA: ${{ env.HEAD_SHA }} + PREVIOUS_JOB_ID: ${{ env.PREVIOUS_JOB_ID }} + PNG_FILES_MARKDOWN: ${{ env.PNG_FILES_MARKDOWN }} + JOB_PATH: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ env.PREVIOUS_JOB_ID }}" + uses: peter-evans/create-or-update-comment@v3 + with: + issue-number: ${{ env.PR_NUMBER }} + comment-id: ${{ steps.find-comment.outputs.comment-id }} + edit-mode: replace + reactions: | + heart + hooray + body: | + ## C4 Diagrams + Commit: ${{ env.HEAD_SHA }} + Logs: ${{ env.JOB_PATH }} + ${{ env.PNG_FILES_MARKDOWN }} \ No newline at end of file