-
Notifications
You must be signed in to change notification settings - Fork 464
196 lines (191 loc) · 7.76 KB
/
create-channel.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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 }}