-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathScriptGenerationService.cs
More file actions
176 lines (154 loc) · 6.18 KB
/
ScriptGenerationService.cs
File metadata and controls
176 lines (154 loc) · 6.18 KB
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
// ---------------------------------------------------------------------------
// Copyright (c) Hassan Habib & Shri Humrudha Jagathisun All rights reserved.
// Licensed under the MIT License.
// See License.txt in the project root for license information.
// ---------------------------------------------------------------------------
using System.Collections.Generic;
using System.IO;
using ADotNet.Clients;
using ADotNet.Models.Pipelines.GithubPipelines.DotNets;
using ADotNet.Models.Pipelines.GithubPipelines.DotNets.Tasks;
using ADotNet.Models.Pipelines.GithubPipelines.DotNets.Tasks.SetupDotNetTaskV4s;
namespace ADotNet.Infrastructure.Build.Services
{
internal class ScriptGenerationService
{
private readonly ADotNetClient adotNetClient;
public ScriptGenerationService() =>
adotNetClient = new ADotNetClient();
public void GenerateBuildScript(string branchName, string projectName, string dotNetVersion)
{
var githubPipeline = new GithubPipeline
{
Name = "Build",
OnEvents = new Events
{
Push = new PushEvent { Branches = [branchName] },
PullRequest = new PullRequestEvent
{
Types = ["opened", "synchronize", "reopened", "closed"],
Branches = [branchName]
}
},
Jobs = new Dictionary<string, Job>
{
{
"build",
new Job
{
Name = "Build",
RunsOn = BuildMachines.UbuntuLatest,
Steps = new List<GithubTask>
{
new CheckoutTaskV3
{
Name = "Check out"
},
new SetupDotNetTaskV4
{
Name = "Setup .Net",
With = new TargetDotNetVersionV4
{
DotNetVersion = dotNetVersion
}
},
new RestoreTask
{
Name = "Restore"
},
new DotNetBuildTask
{
Name = "Build"
},
new TestTask
{
Name = "Test"
}
}
}
},
{
"add_tag",
new TagJobV1(
runsOn: BuildMachines.UbuntuLatest,
dependsOn: "build",
projectRelativePath: "ADotNet/ADotNet.csproj",
githubToken: "${{ secrets.PAT_FOR_TAGGING }}",
branchName: branchName)
{
Name = "Add Tag and Create Release"
}
},
{
"publish",
new PublishJobV3(
runsOn: BuildMachines.UbuntuLatest,
dependsOn: "add_tag",
dotNetVersion: dotNetVersion,
nugetApiKey: "${{ secrets.NUGET_ACCESS }}")
{
Name = "Publish to NuGet"
}
}
}
};
string buildScriptPath = "../../../../.github/workflows/build.yml";
string directoryPath = Path.GetDirectoryName(buildScriptPath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
adotNetClient.SerializeAndWriteToFile(
adoPipeline: githubPipeline,
path: buildScriptPath);
}
public void GeneratePrLintScript(string branchName)
{
var githubPipeline = new GithubPipeline
{
Name = "PR Linter",
OnEvents = new Events
{
PullRequest = new PullRequestEvent
{
Types = ["opened", "edited", "synchronize", "reopened", "closed"],
Branches = [branchName]
}
},
Jobs = new Dictionary<string, Job>
{
{
"label",
new LabelJobV2(runsOn: BuildMachines.UbuntuLatest)
{
Name = "Label",
}
},
{
"requireIssueOrTask",
new RequireIssueOrTaskJob()
{
Name = "Require Issue Or Task Association",
}
},
{
"setAuthorAsPrAssignee",
new SetAuthorAsPrAssigneeJob(runsOn: BuildMachines.UbuntuLatest)
{
Name = "Set Author As PR Assignee",
}
}
}
};
string buildScriptPath = "../../../../.github/workflows/prLinter.yml";
string directoryPath = Path.GetDirectoryName(buildScriptPath);
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
adotNetClient.SerializeAndWriteToFile(
adoPipeline: githubPipeline,
path: buildScriptPath);
}
}
}