-
Notifications
You must be signed in to change notification settings - Fork 2
199 lines (168 loc) · 6.29 KB
/
benchmark.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
197
198
199
name: benchmark
env:
CRANK_VERSION: '0.2.0-*'
DOTNET_CLI_TELEMETRY_OPTOUT: true
DOTNET_NOLOGO: true
DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION: 1
NUGET_XMLDOC_MODE: skip
TERM: xterm
on:
issue_comment:
types: [ created ]
permissions:
contents: read
issues: write
pull-requests: write
statuses: write
jobs:
benchmark:
name: benchmark
runs-on: ubuntu-latest
if: |
github.event.repository.fork == false &&
github.event.issue.pull_request != '' &&
contains(github.event.comment.body, '/benchmark')
steps:
- name: Parse comment
uses: actions/github-script@v7
id: parse-comment
with:
result-encoding: string
script: |
const availableBenchmarks = {
"guid": "Runs the GUID benchmark",
"openapi": "Runs the OpenAPI benchmark",
"root": "Runs the root benchmark",
"time": "Runs the time benchmark",
};
const owner = context.payload.repository.owner.login;
const repo = context.payload.repository.name;
const username = context.payload.comment.user.login;
const issue_number = context.issue.number;
try {
await github.rest.repos.checkCollaborator({
owner,
repo,
username,
});
} catch (error) {
const message = `@${username} You are not a repository collaborator; benchmarking is not allowed.`;
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: message,
});
throw new Error(message);
}
core.info(`Verified ${username} is a repo collaborator.`);
if (context.eventName !== "issue_comment") {
throw "Error: This action only works on issue_comment events.";
}
// Extract the benchmark arguments from the comment
const regex = /\/benchmark ([a-zA-Z\d\/\.\-\_]+)/;
const arguments = regex.exec(context.payload.comment.body);
// Generate help text with all available commands
if (arguments == null || arguments.length < 2 || !availableBenchmarks.hasOwnProperty(arguments[1])) {
let body = 'The `/benchmark` command accepts these values:\n';
for (const key in availableBenchmarks) {
body += `- \`/benchmark ${key}\`: ${availableBenchmarks[key]}\n`;
}
await github.rest.issues.createComment({
issue_number,
owner,
repo,
body,
});
throw new Error('Error: Invalid arguments, workflow stopped.');
}
const benchmark = arguments[1];
const workflowUrl = `${process.env.GITHUB_SERVER_URL}/${owner}/${repo}/actions/runs/${process.env.GITHUB_RUN_ID}`;
core.info(`Benchmark: ${benchmark}`);
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: `Started [${benchmark} benchmark](${workflowUrl}). :hourglass:`,
});
const { data: pull } = await github.rest.pulls.get({
owner,
repo,
pull_number: issue_number,
});
const sha = pull.head.sha;
await github.rest.repos.createCommitStatus({
owner,
repo,
sha,
state: 'pending',
target_url: workflowUrl,
description: `Benchmark ${benchmark} started...`,
context: `benchmarks / ${benchmark.toLowerCase()}`,
});
core.setOutput('benchmark', benchmark);
core.setOutput('commit-sha', sha);
- name: Checkout code
uses: actions/checkout@v4
- name: Setup .NET SDK for crank
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Setup .NET SDK for app
uses: actions/setup-dotnet@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install crank
shell: pwsh
run: |
dotnet tool install --global Microsoft.Crank.Agent --version "${env:CRANK_VERSION}"
dotnet tool install --global Microsoft.Crank.Controller --version "${env:CRANK_VERSION}"
dotnet tool install --global Microsoft.Crank.PullRequestBot --version "${env:CRANK_VERSION}"
- name: Run crank
shell: pwsh
env:
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BENCHMARK_NAME: ${{ steps.parse-comment.outputs.benchmark }}
PULL_REQUEST_ID: ${{ github.event.issue.number }}
run: |
./benchmark-crank.ps1 `
-Benchmark ${env:BENCHMARK_NAME} `
-Repository "${env:GITHUB_SERVER_URL}/${env:GITHUB_REPOSITORY}" `
-PullRequestId ${env:PULL_REQUEST_ID} `
-AccessToken ${env:ACCESS_TOKEN} `
-PublishResults
- name: Post result comment
uses: actions/github-script@v7
if: ${{ !cancelled() }}
env:
BENCHMARK_NAME: ${{ steps.parse-comment.outputs.benchmark }}
COMMIT_SHA: ${{ steps.parse-comment.outputs.commit-sha }}
OUTCOME: ${{ job.status }}
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = context.issue.number;
const benchmark = process.env.BENCHMARK_NAME;
const workflowUrl = `${process.env.GITHUB_SERVER_URL}/${owner}/${repo}/actions/runs/${process.env.GITHUB_RUN_ID}`;
const succeeded = process.env.OUTCOME === 'success';
const outcome = succeeded ? 'succeeded' : 'failed';
const emoji = succeeded ? ':white_check_mark:' : ':x:';
const state = succeeded ? 'success' : 'failure';
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: `Benchmark [${benchmark}](${workflowUrl}) ${outcome} ${emoji}`,
});
await github.rest.repos.createCommitStatus({
owner,
repo,
sha: process.env.COMMIT_SHA,
state,
target_url: workflowUrl,
description: `Benchmark ${benchmark} ${outcome}.`,
context: `benchmarks / ${benchmark.toLowerCase()}`
});