Skip to content

Commit

Permalink
chore(workflow): add update action
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschuerch committed May 2, 2024
1 parent 2dc5d07 commit 64eccb7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 69 deletions.
64 changes: 8 additions & 56 deletions .github/actions/deploy-to-netlify/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,18 @@ inputs:
package_name:
description: The package that will be deployed
required: true
access-token:
description: The access token to use for commenting.
required: true

outputs:
preview-url:
description: The deployed preview url.
value: https://${{ steps.netlify_deploy.outputs.url_alias }}--${{ inputs.netlify_site_url }}

runs:
using: composite
steps:
- name: Find Preview Comment
uses: peter-evans/find-comment@v3
id: preview_comment
with:
token: ${{ inputs.comment_token }}
issue-number: ${{ inputs.id }}
comment-author: ${{ inputs.comment_author }}
body-includes: Preview environment ready

- name: Create Initial Preview Comment
uses: peter-evans/create-or-update-comment@v4
if: ${{ steps.preview_comment.outputs.comment-id == 0 }}
with:
token: ${{ inputs.comment_token }}
issue-number: ${{ inputs.id }}
edit-mode: replace
body: Preview environments are getting posted here, as soon as they are ready!
reactions: eyes

- name: Find Initial Preview Comment
uses: peter-evans/find-comment@v3
id: initial_preview_comment
with:
token: ${{ inputs.comment_token }}
issue-number: ${{ inputs.id }}
comment-author: ${{ inputs.comment_author }}
body-includes: Preview environments are getting posted here, as soon as they are ready!

- name: Install netlify-cli
shell: bash
run: pnpm i -g netlify-cli@16
Expand All @@ -89,31 +69,3 @@ runs:
url_alias=`echo "preview-${{ inputs.id }}" | iconv -t ascii//TRANSLIT | sed -E 's/[~\^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+\|-+$//g' | sed -E 's/^-+//g' | sed -E 's/-+$//g' | tr A-Z a-z`
netlify deploy --alias $url_alias --build false --dir ${{ inputs.folder }} --site ${{ inputs.netlify_site_id }} --filter ${{inputs.package_name}}
echo "url_alias=$url_alias" >> $GITHUB_OUTPUT
- name: Prepare Comment Message
id: comment
shell: bash
run: |
echo "message=Preview environment ready: https://${{ steps.netlify_deploy.outputs.url_alias }}--${{ inputs.netlify_site_url }}" >> $GITHUB_OUTPUT
- name: Replace Preview Comment
uses: peter-evans/create-or-update-comment@v4
if: ${{ steps.initial_preview_comment.outputs.comment-id != 0 }}
with:
token: ${{ inputs.comment_token }}
comment-id: ${{ steps.initial_preview_comment.outputs.comment-id }}
issue-number: ${{ inputs.id }}
edit-mode: replace
body: ${{ steps.comment.outputs.message }}
reactions: rocket

- name: Append Preview Comment
uses: peter-evans/create-or-update-comment@v4
if: ${{ steps.initial_preview_comment.outputs.comment-id == 0 && (steps.preview_comment.outputs.comment-id == 0 || !contains(steps.preview_comment.outputs.comment-body, steps.comment.outputs.message)) }}
with:
token: ${{ inputs.comment_token }}
comment-id: ${{ steps.preview_comment.outputs.comment-id }}
issue-number: ${{ inputs.id }}
edit-mode: append
body: ${{ steps.comment.outputs.message }}
reactions: rocket
17 changes: 4 additions & 13 deletions .github/actions/preview/message/create/action.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Preview message creation
description: Append a preview url message in the pr body, if not already present
name: Create preview message
description: Add a preview comment to the pr, if not already present

inputs:
access-token:
Expand All @@ -14,6 +14,7 @@ runs:
github-token: ${{ inputs.access-token }}
script: |
const commentTitle = '**Related Previews**'
const commentInitialBody = 'Preview URLs will be added here, once they are ready... ![loader](https://github.com/swisspost/design-system/assets/9716662/49a75898-7093-4ffb-9460-071ff194459d)'
const credentials = {
repo: context.repo.repo,
Expand All @@ -27,16 +28,6 @@ runs:
if (!hasPreviewComment) {
github.rest.issues.createComment({
...credentials,
body: `${commentTitle}\nPreview URLs will be added here, once they are ready... ![loader](https://github.com/swisspost/design-system/assets/9716662/49a75898-7093-4ffb-9460-071ff194459d)`
body: `${commentTitle}\n${commentInitialBody}`
})
} else {
const previewComment = comments.find(c => c.user.login === 'swisspost-bot' && c.body.includes(commentTitle))
console.log(previewComment.body);
console.log('-------------');
const body = previewComment.body
.replace('Preview URLs will be added here, once they are ready... ![loader](https://github.com/swisspost/design-system/assets/9716662/49a75898-7093-4ffb-9460-071ff194459d)', '')
.concat('- previewurl\n')
console.log('-------------');
console.log(body);
}
46 changes: 46 additions & 0 deletions .github/actions/preview/message/update/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Update preview message
description: Add a preview url to the existing preview comment, if not already present

inputs:
access-token:
description: The access token to use for commenting.
required: true
preview-url:
description: The preview url to add in the comment.
required: true

runs:
using: composite
steps:
- uses: actions/github-script@v7
env:
PREVIEW_URL: ${{ inputs.preview-url }}
with:
github-token: ${{ inputs.access-token }}
script: |
const commentTitle = '**Related Previews**'
const commentInitialBody = 'Preview URLs will be added here, once they are ready... ![loader](https://github.com/swisspost/design-system/assets/9716662/49a75898-7093-4ffb-9460-071ff194459d)'
const credentials = {
repo: context.repo.repo,
owner: context.repo.owner,
issue_number: context.issue.number
}
const comments = (await github.rest.issues.listComments({ ...credentials })).data || []
const previewComment = comments.find(c => c.user.login === 'swisspost-bot' && c.body.includes(commentTitle))
if (previewComment) {
const hasPreviewUrl = body.includes(PREVIEW_URL)
if (!hasPreviewUrl) {
const body = previewComment.body
.replace(commentInitialBody, '')
.concat(`- ${PREVIEW_URL}\n`)
github.rest.issues.updateComment({
...credentials,
body: body
})
}
}
6 changes: 6 additions & 0 deletions .github/workflows/deploy-documentation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ jobs:
comment_token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
comment_author: swisspost-bot
package_name: '@swisspost/design-system-documentation'

- name: Update preview message
uses: ./.github/actions/preview/message/update
with:
access-token: ${{ secrets.SWISSPOSTDEVS_ACCESS_TOKEN }}
preview-url: https://${{ steps.deploy.outputs.preview-url }}

0 comments on commit 64eccb7

Please sign in to comment.