|
| 1 | +name: Add a Comment to Asana Task |
| 2 | +description: Adds a comment to the Asana task. |
| 3 | +inputs: |
| 4 | + access-token: |
| 5 | + description: "Asana access token" |
| 6 | + required: true |
| 7 | + type: string |
| 8 | + task-url: |
| 9 | + description: "Task URL" |
| 10 | + required: false |
| 11 | + type: string |
| 12 | + task-id: |
| 13 | + description: "Task ID" |
| 14 | + required: false |
| 15 | + type: string |
| 16 | + comment: |
| 17 | + description: "Comment to add to the Asana task" |
| 18 | + required: false |
| 19 | + type: string |
| 20 | + template-name: |
| 21 | + description: | |
| 22 | + Name of a template file (without extension) for the comment, relative to 'templates' subdirectory of the action. |
| 23 | + The file is processed by envsubst before being sent to Asana. |
| 24 | + required: false |
| 25 | + type: string |
| 26 | +runs: |
| 27 | + using: "composite" |
| 28 | + steps: |
| 29 | + - id: extract-task-id |
| 30 | + if: ${{ inputs.task-url }} |
| 31 | + uses: ./.github/actions/asana-extract-task-id |
| 32 | + with: |
| 33 | + task-url: ${{ inputs.task-url }} |
| 34 | + access-token: ${{ inputs.access-token }} |
| 35 | + |
| 36 | + - id: process-template-payload |
| 37 | + if: ${{ inputs.template-name }} |
| 38 | + shell: bash |
| 39 | + env: |
| 40 | + TEMPLATE_PATH: ${{ github.action_path }}/templates/${{ inputs.template-name }}.yml |
| 41 | + run: | |
| 42 | + if [ ! -f $TEMPLATE_PATH ]; then |
| 43 | + echo "::error::Template file not found at $TEMPLATE_PATH" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | +
|
| 47 | + # Process the template file with envsubst, turn into JSON, remove leading spaces and newlines on non-empty lines, and compact the JSON |
| 48 | + payload="$(envsubst < $TEMPLATE_PATH | yq -o=j | sed -E 's/\\n( *)([^\\n])/\2/g' | jq -c)" |
| 49 | + echo "payload-base64=$(base64 <<< $payload)" >> $GITHUB_OUTPUT |
| 50 | +
|
| 51 | + - id: process-comment-payload |
| 52 | + if: ${{ inputs.comment }} |
| 53 | + shell: bash |
| 54 | + env: |
| 55 | + COMMENT: ${{ inputs.comment }} |
| 56 | + WORKFLOW_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 57 | + run: | |
| 58 | + payload="{ \"data\": { \"text\": \"${COMMENT}\n\nWorkflow URL: ${WORKFLOW_URL}\" } }" |
| 59 | + echo "payload-base64=$(base64 <<< $payload)" >> $GITHUB_OUTPUT |
| 60 | +
|
| 61 | + - id: add-comment |
| 62 | + shell: bash |
| 63 | + env: |
| 64 | + ASANA_ACCESS_TOKEN: ${{ inputs.access-token }} |
| 65 | + TASK_ID: ${{ inputs.task-id || steps.extract-task-id.outputs.task-id }} |
| 66 | + PAYLOAD_BASE64: ${{ steps.process-template-payload.outputs.payload-base64 || steps.process-comment-payload.outputs.payload-base64 }} |
| 67 | + run: | |
| 68 | + return_code=$(curl -fLSs "https://app.asana.com/api/1.0/tasks/${TASK_ID}/stories" \ |
| 69 | + -H "Authorization: Bearer ${ASANA_ACCESS_TOKEN}" \ |
| 70 | + -H 'content-type: application/json' \ |
| 71 | + --write-out '%{http_code}' \ |
| 72 | + --output /dev/null \ |
| 73 | + -d "$(base64 -d <<< $PAYLOAD_BASE64)") |
| 74 | +
|
| 75 | + if [ $return_code -ne 201 ]; then |
| 76 | + echo "::error::Failed to add a comment to the Asana task" |
| 77 | + exit 1 |
| 78 | + fi |
0 commit comments