From 2c0d639e9b2eb34f7efcf88474b864987f5b2bd3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vladim=C3=ADr=20Sl=C3=A1vik?= <vslavik@redhat.com>
Date: Thu, 24 Aug 2023 17:37:23 +0200
Subject: [PATCH] infra: Comment about images built when all is done

---
 .github/workflows/build-iso.yml    | 41 ++++++++++++++++++++----------
 .github/workflows/build-iso.yml.j2 | 41 ++++++++++++++++++++----------
 2 files changed, 56 insertions(+), 26 deletions(-)

diff --git a/.github/workflows/build-iso.yml b/.github/workflows/build-iso.yml
index 0a00431c152..b7c4e5ade21 100644
--- a/.github/workflows/build-iso.yml
+++ b/.github/workflows/build-iso.yml
@@ -201,19 +201,6 @@ jobs:
           path: |
             images/*.iso
 
-      - name: Add comment with link to PR
-        if: github.event_name != 'workflow_dispatch'
-        env:
-          GH_TOKEN: ${{ github.token }}
-        run: |
-          sha="${{ needs.pr-info.outputs.sha }}"
-          url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-          echo -n "\`boot.iso\` built successfully based on commit $sha. " >> comment.txt
-          echo -e "Download it from the bottom of the [job status page]($url).\n" >> comment.txt
-          echo "Comment to be posted:"
-          cat comment.txt
-          gh pr comment ${{ github.event.issue.number }} -F comment.txt
-
       - name: Set result status
         if: always()
         uses: octokit/request-action@v2.x
@@ -304,3 +291,31 @@ jobs:
           target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+
+  finalize:
+    needs: [pr-info, boot-iso, live-cd]
+    if: ${{ always() && !cancelled() && github.event_name != 'workflow_dispatch' && needs.pr-info.outputs.allowed_user == 'true' }}
+    steps:
+      - name: Add comment with link to PR
+        env:
+          GH_TOKEN: ${{ github.token }}
+        run: |
+          sha="${{ needs.pr-info.outputs.sha }}"
+          url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}#artifacts"
+
+          echo -n "Images built based on commit $sha:\n" >> comment.txt
+          if [[ "${{ needs.pr-info.outputs.args }}" == *"--boot.iso"* ]] ; then
+            # <job>.result can be success, failure, cancelled, or skipped
+            # https://docs.github.com/en/actions/learn-github-actions/contexts#jobs-context
+            echo "- `boot.iso`: ${{ needs.boot-iso.result }}" >> comment.txt
+          fi
+          if [[ "${{ needs.pr-info.outputs.args }}" == *"--live"* ]] ; then
+            echo "- Live: ${{ needs.live-cd.result }}" >> comment.txt
+          fi
+          echo -e "\n\nDownload the results from the bottom of the [job status page]($url).\n" >> comment.txt
+
+          echo "Comment to be posted:"
+          cat comment.txt
+
+          gh pr comment ${{ github.event.issue.number }} -F comment.txt
diff --git a/.github/workflows/build-iso.yml.j2 b/.github/workflows/build-iso.yml.j2
index f60fea69141..a4a774aa528 100644
--- a/.github/workflows/build-iso.yml.j2
+++ b/.github/workflows/build-iso.yml.j2
@@ -195,19 +195,6 @@ jobs:
           path: |
             images/*.iso
 
-      - name: Add comment with link to PR
-        if: github.event_name != 'workflow_dispatch'
-        env:
-          GH_TOKEN: ${{ github.token }}
-        run: |
-          sha="${{ needs.pr-info.outputs.sha }}"
-          url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
-          echo -n "\`boot.iso\` built successfully based on commit $sha. " >> comment.txt
-          echo -e "Download it from the bottom of the [job status page]($url).\n" >> comment.txt
-          echo "Comment to be posted:"
-          cat comment.txt
-          gh pr comment ${{ github.event.issue.number }} -F comment.txt
-
       - name: Set result status
         if: always()
         uses: octokit/request-action@v2.x
@@ -298,4 +285,32 @@ jobs:
           target_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+
+
+  finalize:
+    needs: [pr-info, boot-iso, live-cd]
+    if: ${{ always() && !cancelled() && github.event_name != 'workflow_dispatch' && needs.pr-info.outputs.allowed_user == 'true' }}
+    steps:
+      - name: Add comment with link to PR
+        env:
+          GH_TOKEN: ${{ github.token }}
+        run: |
+          sha="${{ needs.pr-info.outputs.sha }}"
+          url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}#artifacts"
+
+          echo -n "Images built based on commit $sha:\n" >> comment.txt
+          if [[ "${{ needs.pr-info.outputs.args }}" == *"--boot.iso"* ]] ; then
+            # <job>.result can be success, failure, cancelled, or skipped
+            # https://docs.github.com/en/actions/learn-github-actions/contexts#jobs-context
+            echo "- `boot.iso`: ${{ needs.boot-iso.result }}" >> comment.txt
+          fi
+          if [[ "${{ needs.pr-info.outputs.args }}" == *"--live"* ]] ; then
+            echo "- Live: ${{ needs.live-cd.result }}" >> comment.txt
+          fi
+          echo -e "\n\nDownload the results from the bottom of the [job status page]($url).\n" >> comment.txt
+
+          echo "Comment to be posted:"
+          cat comment.txt
+
+          gh pr comment ${{ github.event.issue.number }} -F comment.txt
 {% endif %}