Skip to content

Commit b07c089

Browse files
Allow creating releases and uploading commits from MSBuild (#3462)
1 parent 5152a3c commit b07c089

File tree

2 files changed

+110
-1
lines changed

2 files changed

+110
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Users can now automatically create releases and associated commits via sentry-cli and MSBuild properties ([#3462](https://github.com/getsentry/sentry-dotnet/pull/3462))
8+
59
### Fixes
10+
611
- Race condition in `SentryMessageHandler` ([#3477](https://github.com/getsentry/sentry-dotnet/pull/3477))
712

813
## 4.9.0

src/Sentry/buildTransitive/Sentry.targets

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@
3737
<!-- Set defaults for the Sentry properties. -->
3838
<SentryUploadSymbols Condition="'$(SentryUploadSymbols)' == ''">false</SentryUploadSymbols>
3939
<SentryUploadSources Condition="'$(SentryUploadSources)' == ''">false</SentryUploadSources>
40+
<SentrySetCommits Condition="'$(SentrySetCommits)' == ''">false</SentrySetCommits>
41+
<!-- SentryCreateRelease is implied if SentrySetCommits==true -->
42+
<SentryCreateRelease Condition="'$(SentrySetCommits)' == 'true'">true</SentryCreateRelease>
43+
<SentryCreateRelease Condition="'$(SentryCreateRelease)' == ''">false</SentryCreateRelease>
4044

4145
<!-- This property controls if the Sentry CLI is to be used at all. Setting false will disable all Sentry CLI usage.
4246
We're explicitly skipping uploads for Sentry projects because they interfere with CLI integration test asserts. -->
4347
<UseSentryCLI Condition="
4448
'$(UseSentryCLI)' == ''
45-
and ('$(SentryUploadSymbols)' == 'true' or '$(SentryUploadSources)' == 'true' or $(SentryUploadAndroidProguardMapping) == 'true')
49+
and ('$(SentryUploadSymbols)' == 'true' or '$(SentryUploadSources)' == 'true' or $(SentryUploadAndroidProguardMapping) == 'true' or $(SentryCreateRelease) == 'true' or $(SentrySetCommits) == 'true')
4650
and '$(MSBuildProjectName)' != 'Sentry'
4751
and !$(MSBuildProjectName.StartsWith('Sentry.'))">true</UseSentryCLI>
4852
</PropertyGroup>
@@ -92,6 +96,9 @@
9296
<SentryCLIBaseCommand>&quot;$(SentryCLI)&quot;</SentryCLIBaseCommand>
9397
<SentryCLIBaseCommand Condition="'$(SentryCLIBaseOptions.Trim())' != ''">$(SentryCLIBaseCommand) $(SentryCLIBaseOptions.Trim())</SentryCLIBaseCommand>
9498

99+
<SentryReleaseOptions Condition="'$(SentryProject)' != ''">$(SentryReleaseOptions) --project $(SentryProject)</SentryReleaseOptions>
100+
<SentrySetCommitOptions Condition="'$(SentrySetCommitOptions)' == ''">--auto</SentrySetCommitOptions>
101+
95102
<SentryCLIUploadOptions Condition="'$(SentryOrg)' != ''">$(SentryCLIUploadOptions) --org $(SentryOrg)</SentryCLIUploadOptions>
96103
<SentryCLIUploadOptions Condition="'$(SentryProject)' != ''">$(SentryCLIUploadOptions) --project $(SentryProject)</SentryCLIUploadOptions>
97104
<SentryCLIDebugFilesUploadCommand>$(SentryCLIBaseCommand) debug-files upload</SentryCLIDebugFilesUploadCommand>
@@ -242,5 +249,102 @@
242249
<Warning Condition="'$(_SentryCLIExitCode)' != '0'" Text="Sentry CLI could not upload proguard mapping." />
243250
</Target>
244251

252+
<UsingTask TaskName="SentryGetApplicationVersion" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
253+
<ParameterGroup>
254+
<AssemblyPath Required="true" />
255+
<Release Output="true" />
256+
</ParameterGroup>
257+
<Task>
258+
<Using Namespace="System"/>
259+
<Using Namespace="System.Reflection"/>
260+
<Code Type="Fragment" Language="cs">
261+
<![CDATA[
262+
try
263+
{
264+
#nullable enable
265+
Log.LogMessage($"Loading assembly: {AssemblyPath}");
266+
var path = Path.GetFullPath(AssemblyPath);
267+
var assembly = Assembly.LoadFile(path);
268+
269+
Log.LogMessage($"Getting assembly name...");
270+
var assemblyName = assembly.GetName();
271+
var name = assemblyName.Name;
272+
Log.LogMessage($"Assembly name: {name}");
273+
274+
Log.LogMessage($"Reading AssemblyInformationalVersionAttribute...");
275+
string? version = null;
276+
try
277+
{
278+
var infoVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
279+
version = infoVersionAttribute?.InformationalVersion;
280+
}
281+
catch
282+
{
283+
Log.LogMessage($"Failed to read informational version attribute");
284+
}
285+
version ??= assemblyName.Version?.ToString();
286+
Log.LogMessage($"Version: {version}");
287+
288+
Log.LogMessage($"Compiling release information...");
289+
if (!string.IsNullOrWhiteSpace(name) && !string.IsNullOrWhiteSpace(version))
290+
{
291+
Release = version.Contains('@')
292+
? version // Don't add name prefix if it's already set by the user
293+
: $"{name}@{version}";
294+
}
295+
#nullable disable
296+
}
297+
catch
298+
{
299+
Log.LogWarning($"Failed to get version from {AssemblyPath}");
300+
}
301+
]]>
302+
</Code>
303+
</Task>
304+
</UsingTask>
305+
306+
<PropertyGroup>
307+
<!-- Check if the release has been provided as an environment variable -->
308+
<_SentryRelease Condition="'$(_SentryRelease)' == '' And '$(SENTRY_RELEASE)' != ''">$(SENTRY_RELEASE)</_SentryRelease>
309+
</PropertyGroup>
310+
311+
<Target Name="_GetSentryRelease" AfterTargets="DispatchToInnerBuilds;AfterBuild" Condition="'$(SentryCreateRelease)' == 'true' And '$(UseSentryCLI)' == 'true'">
312+
313+
<Message Importance="High" Text="Getting Sentry Release..." />
314+
315+
<SentryGetApplicationVersion Condition="'$(_SentryRelease)' == ''" AssemblyPath="$(IntermediateOutputPath)$(TargetName)$(TargetExt)">
316+
<Output TaskParameter="Release" PropertyName="_SentryRelease" />
317+
</SentryGetApplicationVersion>
318+
319+
<Exec ConsoleToMSBuild="true" Condition="'$(_SentryRelease)' == ''"
320+
Command="sentry-cli releases propose-version">
321+
<Output TaskParameter="ConsoleOutput" PropertyName="_SentryRelease"/>
322+
</Exec>
323+
324+
<Message Importance="High" Text="Sentry Release: $(_SentryRelease)" />
325+
</Target>
326+
327+
<!-- Set release information after the build -->
328+
<Target Name="_CreateSentryRelease" AfterTargets="Build" DependsOnTargets="_GetSentryRelease"
329+
Condition="'$(SentryCLI)' != '' and '$(SentryCreateRelease)' == 'true'">
330+
<Message Importance="High" Text="Creating Sentry Release: $(_SentryRelease)" />
331+
<Exec
332+
Command="$(SentryCLIBaseCommand) releases new '$(_SentryRelease)' $(SentryReleaseOptions)"
333+
IgnoreExitCode="true" ContinueOnError="WarnAndContinue">
334+
<Output TaskParameter="ExitCode" PropertyName="_SentryCLIExitCode" />
335+
</Exec>
336+
</Target>
337+
338+
<!-- Send commit details to Sentry -->
339+
<Target Name="_SentrySetCommits" AfterTargets="Build" DependsOnTargets="_CreateSentryRelease"
340+
Condition="'$(SentryCLI)' != '' and '$(SentrySetCommits)' == 'true'">
341+
<Message Importance="High" Text="Setting Sentry commits" />
342+
<Exec
343+
Command="$(SentryCLIBaseCommand) releases set-commits $(SentrySetCommitOptions) '$(_SentryRelease)' $(SentryReleaseOptions)"
344+
IgnoreExitCode="true" ContinueOnError="WarnAndContinue">
345+
<Output TaskParameter="ExitCode" PropertyName="_SentryCLIExitCode" />
346+
</Exec>
347+
</Target>
348+
245349
<Import Condition="'$(MSBuildProjectName)' != 'Sentry' and !$(MSBuildProjectName.StartsWith('Sentry.')) and '$(IsSentryTestProject)' == ''" Project="$(MSBuildThisFileDirectory)Sentry.Native.targets"/>
246350
</Project>

0 commit comments

Comments
 (0)