-
Notifications
You must be signed in to change notification settings - Fork 5
134 lines (111 loc) · 4.86 KB
/
create-summary-quarter.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
name: Create Summary (Quarter)
on:
schedule:
- cron: '0 8 1 * *'
workflow_dispatch:
inputs:
year:
description: 'Year to create the summary of'
required: true
quarter:
description: 'Quarter to create the summary of'
required: true
jobs:
publish:
runs-on: ubuntu-latest
name: Create Summary
steps:
- name: Generate access token
id: generate_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.SURVEY_SUMMARY_APP_ID }}
private_key: ${{ secrets.SURVEY_SUMMARY_APP_PRIVATE_KEY }}
installation_retrieval_mode: "repository"
installation_retrieval_payload: "OpenTTD/survey-web"
- name: Checkout
uses: actions/checkout@v4
with:
token: ${{ steps.generate_token.outputs.token }}
- name: Install rclone
shell: bash
run: |
curl -sL https://rclone.org/install.sh | sudo bash
rclone config create --no-obscure openttd s3 \
provider Cloudflare \
access_key_id ${{ secrets.R2_SURVEY_ACCESS_KEY_ID }} \
secret_access_key ${{ secrets.R2_SURVEY_SECRET_ACCESS_KEY }} \
endpoint ${{ secrets.R2_SURVEY_ENDPOINT }} \
acl private \
no_check_bucket true
- name: Calculate dates
shell: bash
id: dates
run: |
year="${{ inputs.year }}"
quarter="${{ inputs.quarter }}"
# If no year/quarter is given, detect the quarter before today.
# This assumes this script is started on the first day of the next quarter.
if [ -z "${year}" ] || [ -z "${quarter}" ]; then
year=$(date -d "yesterday" +%Y)
quarter=$(date -d "yesterday" +%q)
fi
# Calculate the start and end of the quarter.
start_month=$(( (${quarter} - 1) * 3 + 1 ))
end_month=$(( ${quarter} * 3 ))
start_date=$(date -d "${year}-${start_month}-01" +%Y-%m-%d)
end_date=$(date -d "${year}-${end_month}-01 +1 month -1 day" +%Y-%m-%d)
# Ensure the start date is actually in the quarter we want.
start_check=$(date -d "${start_date}" +%Y-%q)
if [ "${start_check}" != "${year}-${quarter}" ]; then
echo "Start date ${start_date} is not in ${year}-${quarter}, but in ${start_check}"
exit 1
fi
# Ensure the end date is actually in the quarter we want.
end_check=$(date -d "${end_date}" +%Y-%q)
if [ "${end_check}" != "${year}-${quarter}" ]; then
echo "End date ${end_date} is not in ${year}-${quarter}, but in ${end_check}"
exit 1
fi
echo "Quarter: ${quarter}"
echo "Year: ${year}"
echo "Start date: ${start_date}"
echo "End date: ${end_date}"
echo "quarter=${quarter}" >> "$GITHUB_OUTPUT"
echo "year=${year}" >> "$GITHUB_OUTPUT"
echo "start_date=${start_date}" >> "$GITHUB_OUTPUT"
echo "end_date=${end_date}" >> "$GITHUB_OUTPUT"
- name: Download packed results
shell: bash
run: |
echo "Downloading packs for quarter ${{ steps.dates.outputs.quarter }} in ${{ steps.dates.outputs.year }}: [${{ steps.dates.outputs.start_date }} .. ${{ steps.dates.outputs.end_date }}]"
mkdir -p packed
for i in $(seq 0 93); do
date=$(date -d "${{ steps.dates.outputs.start_date }} +${i} days" +%Y-%m-%d)
date_year=$(date -d "${{ steps.dates.outputs.start_date }} +${i} days" +%Y)
date_month=$(date -d "${{ steps.dates.outputs.start_date }} +${i} days" +%m)
echo "Downloading ${date}"
rclone copy -v openttd:survey-packed-prod/${date_year}/${date_month}/openttd-survey-pack.${date}.tar.xz packed
if [ "${date}" = "${{ steps.dates.outputs.end_date }}" ]; then
break
fi
done
- name: Run analysis
shell: bash
run: |
mkdir -p _data/summaries/${{ steps.dates.outputs.year }}
python -m analysis q packed/* > _data/summaries/${{ steps.dates.outputs.year }}/q${{ steps.dates.outputs.quarter }}.json
- name: Create summary entry
shell: bash
run: |
python -m create_markdown q ${{ steps.dates.outputs.year }} ${{ steps.dates.outputs.quarter }} ${{ steps.dates.outputs.start_date }} ${{ steps.dates.outputs.end_date }}
- name: Commit and push
shell: bash
run: |
git config --global user.name "OpenTTD Survey"
git config --global user.email "[email protected]"
git add _data/summaries/${{ steps.dates.outputs.year }}/q${{ steps.dates.outputs.quarter }}.json
git add _summaries/${{ steps.dates.outputs.year }}/q${{ steps.dates.outputs.quarter }}.md
git add _summaries/${{ steps.dates.outputs.year }}/q${{ steps.dates.outputs.quarter }}
git commit -m "Add: summary for Q${{ steps.dates.outputs.quarter }} of ${{ steps.dates.outputs.year }}"
git push