Skip to content

Create PR channel #4152

Create PR channel

Create PR channel #4152

name: Create PR channel
on:
workflow_run:
workflows: [Upload driver packages]
types:
- completed
jobs:
download-artifacts:
runs-on: ubuntu-latest
outputs:
drivers: ${{ steps.set-output.outputs.drivers }}
pr_number: ${{ steps.pr_number.outputs.pr_number }}
head_ref: ${{ steps.head_ref.outputs.head_ref }}
steps:
- name: Download artifacts
uses: dawidd6/action-download-artifact@v6
with:
workflow: upload-driver-packages.yml
run_id: ${{ github.event.workflow_run.id }}
- run: echo "pr_number=$(cat pr_number/pr_number.txt)" >> $GITHUB_ENV
- run: echo "head_ref=$(cat head_ref/head_ref.txt)" >> $GITHUB_ENV
- run: rm -rf pr_number
- run: rm -rf head_ref
- run: |
echo "drivers=$(ls | jq -Rrsc 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
id: set-output
- id: pr_number
uses: actions/github-script@v5
with:
script: core.setOutput('pr_number', ${{ env.pr_number }})
- id: head_ref
uses: actions/github-script@v5
with:
script: core.setOutput('head_ref', '${{ env.head_ref }}')
get-channel:
runs-on: ubuntu-latest
needs:
- download-artifacts
outputs:
channel-json: ${{ steps.set-outputs.outputs.channel-json }}
steps:
- name: List channels
id: channels
uses: fjogeleit/http-request-action@master
with:
url: 'https://api.smartthings.com/distchannels/'
method: GET
bearerToken: ${{ secrets.TOKEN }}
timeout: 10000
- name: Store channel name
uses: actions/github-script@v5
with:
script: core.exportVariable('channel_name', '${{ format('{0}{1}', needs.download-artifacts.outputs.head_ref, needs.download-artifacts.outputs.pr_number) }}')
- name: Create the channel
if: ${{ !contains(steps.channels.outputs.response, env.channel_name) }}
id: channel
uses: fjogeleit/http-request-action@master
with:
url: 'https://api.smartthings.com/distchannels/'
bearerToken: ${{ secrets.TOKEN }}
data: '{
"name": "${{ env.channel_name }}",
"description": "Automatically generated from PR #${{ needs.download-artifacts.outputs.pr_number }}",
"type": "DRIVER",
"termsOfServiceUrl": "${{ github.server_url }}/${{ github.repository }}/pull/${{ needs.download-artifacts.outputs.pr_number }}"
}'
- name: Set the channel environment variable
if: ${{ !contains(steps.channels.outputs.response, env.channel_name) }}
run: echo 'CHANNEL_JSON=${{ steps.channel.outputs.response }}' >> $GITHUB_ENV
- name: Get existing channel JSON
if: ${{ contains(steps.channels.outputs.response, env.channel_name) }}
run: |
jq -r ' .items[] | select(.name=="${{ env.channel_name }}") | @json ' <<< '${{ steps.channels.outputs.response}}' > channel.json
echo "CHANNEL_JSON=$(cat channel.json)" >> $GITHUB_ENV
- name: set outputs
id: set-outputs
uses: actions/github-script@v5
with:
script: core.setOutput('channel-json', ${{ env.CHANNEL_JSON }})
assign-artifact:
runs-on: ubuntu-latest
needs:
- download-artifacts
- get-channel
strategy:
matrix:
driver: ${{ fromJSON(needs.download-artifacts.outputs.drivers) }}
steps:
- name: Download artifacts
uses: dawidd6/action-download-artifact@v6
with:
workflow: upload-driver-packages.yml
name: ${{ matrix.driver }}
run_id: ${{ github.event.workflow_run.id }}
- name: Upload the driver
id: driver
run: |
echo "response=$(curl --location --request POST 'https://api.smartthings.com/drivers/package/' --header 'Authorization: Bearer ${{ secrets.TOKEN }}' --header 'Content-Type: application/zip' --data-binary @${{ matrix.driver }}.zip)" >> $GITHUB_OUTPUT
- name: Driver upload failed
if: ${{ !contains(steps.driver.outputs.response, 'driverId') }}
uses: actions/github-script@v3
with:
script: |
core.setFailed('The driver failed to upload: ${{ steps.driver.outputs.response }}')
- name: Assign the driver
uses: fjogeleit/http-request-action@master
with:
url: 'https://api.smartthings.com/distchannels/${{ fromJSON(needs.get-channel.outputs.channel-json).channelId }}/drivers/'
bearerToken: ${{ secrets.TOKEN }}
data: '{
"driverId": "${{ fromJSON(steps.driver.outputs.response).driverId }}",
"version": "${{ fromJSON(steps.driver.outputs.response).version }}"
}'
get-invite:
runs-on: ubuntu-latest
needs: get-channel
outputs:
invite-url: ${{ steps.set-output.outputs.invite-url }}
steps:
- name: Get current channel invites
id: current
uses: fjogeleit/http-request-action@master
with:
url: 'https://api.smartthings.com/invites?resource=st1:developer:${{secrets.UUID}}:channel/${{ fromJSON(needs.get-channel.outputs.channel-json).channelId }}'
bearerToken: ${{ secrets.TOKEN }}
method: GET
- name: Count current channel invites
id: count
run: echo "count=$(echo '${{ steps.current.outputs.response }}' | jq '.items | length')" >> $GITHUB_OUTPUT
- name: Create invite
if: ${{ steps.count.outputs.count == 0 }}
id: invite
uses: fjogeleit/http-request-action@master
with:
url: 'https://api.smartthings.com/invites'
bearerToken: ${{ secrets.TOKEN }}
data: '{
"resource": {
"root": {
"service": "developer",
"id": "${{ secrets.UUID }}"
},
"components" : [
{
"kind": "channel",
"id": "${{ fromJSON(needs.get-channel.outputs.channel-json).channelId }}"
}
]
},
"profileId": "${{ secrets.PROFILE_ID }}",
"metadata": {
"name": "${{ fromJSON(needs.get-channel.outputs.channel-json).name }}",
"description": "${{ fromJSON(needs.get-channel.outputs.channel-json).description }}",
"owner": "https://github.com/${{ github.actor }}",
"termsUrl": "${{ fromJSON(needs.get-channel.outputs.channel-json).termsOfServiceUrl }}"
}
}'
- run: echo "INVITE_URL=${{ fromJSON(steps.invite.outputs.response).acceptURL }}" >> $GITHUB_ENV
if: ${{ steps.count.outputs.count == 0 }}
- name: Get existing invite
if: ${{ steps.count.outputs.count > 0 }}
run: |
echo '${{ steps.current.outputs.response }}' | jq -r ' .items[0].acceptUrl ' | xargs -I % echo 'INVITE_URL=%' >> $GITHUB_ENV
- name: Set ouput
id: set-output
uses: actions/github-script@v5
with:
script: core.setOutput('invite-url', "${{ env.INVITE_URL }}" )
comment-on-pr:
runs-on: ubuntu-latest
needs:
- get-invite
- download-artifacts
- assign-artifact
steps:
- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
body-includes: Invitation URL
comment-author: 'github-actions[bot]'
issue-number: ${{ needs.download-artifacts.outputs.pr_number }}
- name: post comment
if: ${{ steps.fc.outputs.comment-id == 0 }}
uses: peter-evans/create-or-update-comment@v2
with:
body: |
Invitation URL:
${{ needs.get-invite.outputs.invite-url }}
edit-mode: replace
issue-number: ${{ needs.download-artifacts.outputs.pr_number }}