-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate-github-actions.mjs
146 lines (130 loc) · 4.84 KB
/
generate-github-actions.mjs
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
import fs from "fs";
import cubes from "./data.json" assert { type: "json" };
const envs = ["test", "int", "prod"];
const queries = [
"DataCubeComponents",
"DataCubeMetadata",
"DataCubeObservations",
"DataCubePreview",
"PossibleFilters",
];
const generateAutoTests = () => {
const commands = envs.flatMap((env) =>
queries.flatMap((query) =>
cubes.map(
(cube) =>
"K6_PROMETHEUS_RW_USERNAME=${{ secrets.K6_PROMETHEUS_RW_USERNAME }} K6_PROMETHEUS_RW_PASSWORD=${{ secrets.K6_PROMETHEUS_RW_PASSWORD }} K6_PROMETHEUS_RW_SERVER_URL=${{ secrets.K6_PROMETHEUS_RW_SERVER_URL }} K6_PROMETHEUS_RW_TREND_STATS=avg " +
getRunCommand(
env,
query,
cube,
`https://${
env === "prod" ? "" : `${env}.`
}visualize.admin.ch/api/graphql`,
true,
false
)
)
)
);
const file = `# GENERATED FILE, DO NOT EDIT MANUALLY - use yarn run github:codegen instead
name: GraphQL performance tests (auto)
on:
workflow_dispatch:
schedule:
- cron: "37 * * * *"
jobs:
run_tests:
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v4
- name: Download, unzip and install k6 binary
run: |
wget https://github.com/grafana/k6/releases/download/v0.49.0/k6-v0.49.0-linux-amd64.tar.gz
tar -xzf k6-v0.49.0-linux-amd64.tar.gz
sudo cp k6-v0.49.0-linux-amd64/k6 /usr/local/bin/k6
export PATH=$PATH:/usr/local/bin
${commands
.map(
(command, i) => ` - name: Run k6 test (iteration ${i + 1})
run: ${command}`
)
.join("\n")}`;
fs.writeFileSync("./.github/workflows/performance-tests.yml", file);
};
generateAutoTests();
const generatePRTests = () => {
const commands = queries.flatMap((query) =>
cubes.map((cube) =>
getRunCommand(
"PR",
query,
cube,
"${{ github.event.deployment_status.target_url }}/api/graphql",
false,
true
)
)
);
const file = `# GENERATED FILE, DO NOT EDIT MANUALLY - use yarn run github:codegen instead
name: GraphQL performance tests (PR)
on: [deployment_status]
env:
SUMMARY: ''
jobs:
run_tests:
if: github.event.deployment_status.state == 'success'
name: Run tests
runs-on: ubuntu-latest
steps:
- name: Check out
uses: actions/checkout@v4
- name: Send an HTTP request to start up the server
run: |
curl -s '\${{ github.event.deployment_status.target_url }}/api/graphql' -X 'POST' -H 'Content-Type: application/json' -d '{"operationName":"DataCubeObservations","variables":{"locale":"en","sourceType":"sparql","sourceUrl":"https://lindas.admin.ch/query","cubeFilter":{"iri":"https://energy.ld.admin.ch/sfoe/bfe_ogd84_einmalverguetung_fuer_photovoltaikanlagen/9","filters":{"https://energy.ld.admin.ch/sfoe/bfe_ogd84_einmalverguetung_fuer_photovoltaikanlagen/Kanton":{"type":"single","value":"https://ld.admin.ch/canton/1"}}}},"query":"query DataCubeObservations($sourceType: String!, $sourceUrl: String!, $locale: String!, $cubeFilter: DataCubeObservationFilter!) { dataCubeObservations(sourceType: $sourceType, sourceUrl: $sourceUrl, locale: $locale, cubeFilter: $cubeFilter) }"}' > /dev/null
- name: Download, unzip and install k6 binary
run: |
wget https://github.com/grafana/k6/releases/download/v0.49.0/k6-v0.49.0-linux-amd64.tar.gz
tar -xzf k6-v0.49.0-linux-amd64.tar.gz
sudo cp k6-v0.49.0-linux-amd64/k6 /usr/local/bin/k6
export PATH=$PATH:/usr/local/bin
${commands
.map(
(command, i) => ` - name: Run k6 test (iteration ${i + 1})
run: echo "SUMMARY=\${{ env.SUMMARY }}$(${command})" >> $GITHUB_ENV`
)
.join("\n")}
- name: GQL performance tests ❌
if: \${{ env.SUMMARY != '' }}
run: |
curl --request POST \
--url https://api.github.com/repos/\${{ github.repository }}/statuses/\${{ github.sha }} \
--header 'authorization: Bearer \${{ secrets.GITHUB_TOKEN }}' \
--header 'content-type: application/json' \
--data '{
"context": "GQL performance tests",
"state": "failure",
"description": "\${{ env.SUMMARY }}"
}'
`;
fs.writeFileSync("./.github/workflows/performance-tests-pr.yml", file);
};
generatePRTests();
function getRunCommand(
env,
query,
cube,
endpoint,
sendToPrometheus = true,
checkTiming = true
) {
return `k6 run${
sendToPrometheus ? " -o experimental-prometheus-rw" : ""
} --tag testid=${query} --env ENV=${env} --env ENDPOINT=${endpoint} --env CUBE_IRI=${
cube.iri
} --env CUBE_LABEL=${cube.label} --env CHECK_TIMING=${
checkTiming ? "true" : "false"
} --env WORKSPACE=\${{ github.workspace }} --quiet - <k6/performance-tests/graphql/${query}.js`;
}