Skip to content

Commit 4059489

Browse files
[Backport 7.12] Introduce automated scheduled codegeneration (#5448)
Co-authored-by: Martijn Laarman <Mpdreamz@gmail.com>
1 parent 1def02c commit 4059489

File tree

8 files changed

+80
-12
lines changed

8 files changed

+80
-12
lines changed

.ci/make.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ case $CMD in
4242
TASK=release
4343
TASK_ARGS=("$VERSION" "$output_folder" "skiptests")
4444
;;
45+
codegen)
46+
TASK=codegen
47+
# VERSION is BRANCH here for now
48+
TASK_ARGS=("$VERSION")
49+
;;
4550
*)
4651
echo -e "\nUsage:\n\t $CMD is not supported right now\n"
4752
exit 1

.github/workflows/make-codegen.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Code Generation
2+
on:
3+
schedule:
4+
- cron: "0 0/2 * * *"
5+
6+
7+
jobs:
8+
active-branches:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- id: dump-branches
13+
run: |
14+
curl -s "https://artifacts-api.elastic.co/v1/branches" | jq "[ .branches[] | select(. | startswith(\"6\") | not) ]" --compact-output
15+
curl -s "https://artifacts-api.elastic.co/v1/branches" | jq "[ .branches[] | select(. | startswith(\"6\") | not) ]" --compact-output > branches.json
16+
- id: set-matrix
17+
run: echo "::set-output name=matrix::$(cat branches.json)"
18+
outputs:
19+
matrix: ${{ steps.set-matrix.outputs.matrix }}
20+
21+
codegen:
22+
name: Sync
23+
needs: active-branches
24+
runs-on: ubuntu-latest
25+
strategy:
26+
fail-fast: false
27+
matrix:
28+
branch: ${{ fromJson(needs.active-branches.outputs.matrix) }}
29+
steps:
30+
- uses: actions/checkout@v2
31+
with:
32+
ref: "${{ matrix.branch }}"
33+
- run: "./.ci/make.sh codegen ${{ matrix.branch }}"
34+
name: "code-gen ${{ matrix.branch }}"
35+
- name: "PR ${{ matrix.branch }}"
36+
# fixate to known release.
37+
uses: peter-evans/create-pull-request@5ea31358e901bfaf28ca19849903d802fd0a891b
38+
with:
39+
token: ${{ secrets.GITHUB_TOKEN }}
40+
branch: "fix/${{ matrix.branch }}/code-gen"
41+
delete-branch: true
42+
base: "${{ matrix.branch }}"
43+
commit-message: "[codegen] ${{ matrix.branch }} synchronization"
44+
title: '[codegen] ${{ matrix.branch }} synchronization'
45+
body: |
46+
This synchronizes the client's ${{ matrix.branch }} branch with the server.
47+
labels: "infra,code-gen"

build/scripts/Commandline.fs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ Targets:
3939
* benchmark [non-interactive] [url] [username] [password]
4040
- Runs a benchmark from Tests.Benchmarking and indexes the results to [url] when provided.
4141
If non-interactive runs all benchmarks without prompting
42-
* codegen
43-
- runs the code generator interactively
42+
* codegen <branch> [arguments]
43+
- runs the code generator, the first argument is required and dictates the branch to sync from.
44+
- Any other arguments are passed down to the tool directly
4445
* documentation
4546
- runs the doc generation, without running any tests
4647
@@ -88,13 +89,15 @@ Execution hints can be provided anywhere on the command line
8889

8990
type BenchmarkArguments = { Endpoint: string; Username: string option; Password: string option; }
9091
type ClusterArguments = { Name: string; Version: string option; }
92+
type CodeGenArguments = { Branch: string; }
9193
type CommandArguments =
9294
| Unknown
9395
| SetVersion of VersionArguments
9496
| Test of TestArguments
9597
| Integration of IntegrationArguments
9698
| Benchmark of BenchmarkArguments
9799
| Cluster of ClusterArguments
100+
| CodeGen of CodeGenArguments
98101

99102
type PassedArguments = {
100103
NonInteractive: bool;
@@ -193,11 +196,13 @@ Execution hints can be provided anywhere on the command line
193196
| ["build"]
194197
| ["clean"]
195198
| ["benchmark"]
196-
| ["codegen"; ]
197199
| ["documentation"; ]
198200
| ["profile"] -> parsed
199201
| "rest-spec-tests" :: tail -> { parsed with RemainingArguments = tail }
200202

203+
| "codegen" :: branch :: tail ->
204+
{ parsed with CommandArguments = CodeGen { Branch = branch }; RemainingArguments = tail }
205+
201206
| ["release"; version] -> { parsed with CommandArguments = SetVersion { Version = version; OutputLocation = None }; }
202207
| ["release"; version; path] ->
203208
if (not <| System.IO.Directory.Exists path) then failwithf "'%s' is not an existing directory" (Path.getFullName path)

build/scripts/ReposTooling.fs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ module ReposTooling =
2929

3030
Shell.deleteDir tempDir
3131

32-
let GenerateApi () =
33-
//TODO allow branch name to be passed for CI
32+
let GenerateApi args =
33+
let branch = match args.CommandArguments with | CodeGen a -> a.Branch | _ -> raise <| Exception("Branch is required")
3434
let folder = Path.getDirectory (Paths.ProjFile "ApiGenerator")
3535
let timeout = TimeSpan.FromMinutes(120.)
36-
// Building to make sure XML docs files are there, faster then relying on the ApiGenerator to emit these
37-
// from a compilation unit
38-
Tooling.DotNet.ExecInWithTimeout folder ["run"; "-c"; " Release"; ] timeout |> ignore
36+
Tooling.DotNet.ExecInWithTimeout folder
37+
(["run"; "-c"; " Release"; "--"; "--branch"; branch; "--download"] @ args.RemainingArguments) timeout
38+
|> ignore
3939

4040
let RestSpecTests args =
4141
let folder = Path.getDirectory (Paths.TestProjFile "Tests.YamlRunner")

build/scripts/Targets.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module Main =
109109
command "cluster" [ "restore"; "full-build" ] <| fun _ ->
110110
ReposTooling.LaunchCluster parsed
111111

112-
command "codegen" [ ] ReposTooling.GenerateApi
112+
command "codegen" [ ] <| fun _ -> ReposTooling.GenerateApi parsed
113113

114114
command "rest-spec-tests" [ ] <| fun _ ->
115115
ReposTooling.RestSpecTests parsed.RemainingArguments

build/scripts/scripts.fsproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<Content Include="..\..\.github\workflows\unified-release.yml">
3232
<Link>unified-release.yml</Link>
3333
</Content>
34+
<Content Include="..\..\.github\workflows\make-codegen.yml">
35+
<Link>make-codegen.yml</Link>
36+
</Content>
3437
</ItemGroup>
3538
<ItemGroup>
3639
<PackageReference Include="FSharp.Core" Version="5.0.0" />

src/ApiGenerator/Configuration/GeneratorLocations.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public static string Root
3131

3232
var directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
3333

34+
//If we run this tool using dotnet run --project src/ApiGenerator
35+
var pathFromSolutionRoot = Path.Combine(directoryInfo.FullName, "src", "ApiGenerator");
36+
if (Directory.Exists(pathFromSolutionRoot))
37+
{
38+
_root = pathFromSolutionRoot + "/";
39+
return _root;
40+
}
41+
3442
var dotnetRun =
3543
directoryInfo.Name == "ApiGenerator" &&
3644
directoryInfo.Parent != null &&

src/ApiGenerator/Configuration/ViewLocations.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
namespace ApiGenerator.Configuration
5+
namespace ApiGenerator.Configuration
66
{
77
public static class ViewLocations
88
{
99
public static string Root { get; } = $@"{GeneratorLocations.Root}Views/";
1010
private static string HighLevelRoot { get; } = $@"{Root}/HighLevel/";
1111
public static string HighLevel(params string[] paths) => HighLevelRoot + string.Join("/", paths);
12-
12+
1313
private static string LowLevelRoot { get; } = $@"{Root}/LowLevel/";
1414
public static string LowLevel(params string[] paths) => LowLevelRoot + string.Join("/", paths);
1515
}
16-
}
16+
}

0 commit comments

Comments
 (0)