-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
142 lines (121 loc) · 5.28 KB
/
action.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
name: 'Update scrum.org classes'
description: 'Update scrum.org classes'
inputs:
trainer_id:
description: 'Id of the trainer'
required: true
ghost_token:
description: 'Ghost API token'
required: true
runs:
using: "composite"
steps:
- run: |
$trainerId = $env:trainerId
$token = $env:token
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Module -Name JWT -force
function get-markdown {
param(
[string]$trainerId,
[string[]]$courses
)
if ($courses)
{
$body = @{
trainerId = "$trainerId"
courseTypes = $courses
}
} else {
$body = @{ trainerId = "$trainerId" }
}
$classes = Invoke-RestMethod -Uri "https://www.scrum.org/api/class-search" -Method POST -Body ($body | ConvertTo-Json)
if ($classes.items.Length -gt 0) {
$markdown = "`n| Course | Dates | Language | Location |`n|--------|-------|----------|----------|`n"
foreach ($course in $classes.items) {
$startDate = get-date -date $course.StartDate
$endDate = get-date -date $course.EndDate
$dateString = ""
if ($startDate -eq $endDate) {
$dateString = "$($startDate.Day) $($startDate.ToString("MMM yyyy"))"
}
elseif ($startDate.Month -eq $endDate.Month) {
$dateString = "$($startDate.Day)-$($endDate.Day) $($startDate.ToString("MMM yyyy"))"
}
elseif ($startDate.Month -ne $endDate.Month) {
$dateString = "$($startDate.Day) $($startDate.ToString("MMM"))-$($endDate.Day) $($endDate.ToString("MMM yyyy"))"
}
$markdown += "| [$($course.Title)](https://scrum.org$($course.url)) | $dateString | $($course.languages[0]) | $($course.location) | "
$markdown += "`n"
}
return $markdown
}
}
$parts = $token -split ":"
$id = $parts[0]
Write-Output "::add-mask::$id"
$secret = $parts[1]
Write-Output "::add-mask::$secret"
$key = [Convert]::FromHexString($secret)
Write-Output "::add-mask::$key"
$jwttoken = New-Jwt -Header (@{
"alg" = "HS256"
"kid" = $id
"typ" = "JWT"
} | ConvertTo-Json) -PayloadJson (@{
"exp" = ([DateTimeOffset](Get-Date).AddMinutes(2)).ToUnixTimeSeconds()
"iat" = ([DateTimeOffset](Get-Date)).ToUnixTimeSeconds()
"aud" = "/admin/"
} | ConvertTo-Json) -Secret $key
Write-Output "::add-mask::$jwttoken"
$headers = @{
Authorization = "Ghost $jwttoken"
"Content-Type" = "application/json"
}
$alltrainingpages = Invoke-RestMethod -Uri "https://scrumbug.ghost.io/ghost/api/admin/pages/" -Method GET -Headers $headers
foreach ($page in $alltrainingpages.pages) {
$trainingpage = $page
$uri = "https://scrumbug.ghost.io/ghost/api/admin/pages/$($trainingpage.id)/"
$trainingPages = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
$mobiledoc = $trainingPages.pages[0].mobiledoc | ConvertFrom-Json -AsHashtable
foreach ($card in $mobiledoc.cards) {
if ($card[0] -eq "markdown") {
$markdown = $card[1].markdown
if ($markdown -match "<!--SCRUM TRAINING(:(?<course>\d+))?-->")
{
if ($Matches.course)
{
$course = $Matches.course
$trainingMarkdown = get-markdown -trainerId $trainerId -courses @($course)
}
else {
$trainingMarkdown = get-markdown -trainerId $trainerId
}
}
$card[1].markdown = $markdown -replace "(?s)(?<=<!--SCRUM TRAINING(:\d+)?-->).*(?=<!--/SCRUM TRAINING-->)", $trainingMarkdown
}
}
$mobiledoc = $mobiledoc | ConvertTo-Json -Depth 10
$original = ($trainingPages.pages[0].mobiledoc | ConvertFrom-Json -AsHashtable) | ConvertTo-Json -Depth 10
if ($original -ne $mobiledoc)
{
$body = @{
"pages" = @(
@{
"updated_at" = $trainingPages.pages[0].updated_at
"mobiledoc" = $mobiledoc
}
)
}
Write-Host "Updating: $($trainingpage.slug)"
$result = Invoke-RestMethod -Uri $uri -Method PUT -Body ($body | ConvertTo-Json) -Headers $headers
}
else
{
Write-Host "Skipping: $($trainingpage.slug)"
}
}
shell: pwsh
env:
trainerId: ${{ inputs.trainer_id }}
token: ${{ inputs.ghost_token }}