Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating releases and uploading commits from MSBuild #3462

Merged
merged 11 commits into from
Jul 12, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Added build properties to automatically create releases and associated commits ([#3462](https://github.com/getsentry/sentry-dotnet/pull/3462))

### Fixes

- The SDK no longer fails to create a trace root ([#3453](https://github.com/getsentry/sentry-dotnet/pull/3453))
Expand Down
124 changes: 123 additions & 1 deletion src/Sentry/buildTransitive/Sentry.targets
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@
<!-- Set defaults for the Sentry properties. -->
<SentryUploadSymbols Condition="'$(SentryUploadSymbols)' == ''">false</SentryUploadSymbols>
<SentryUploadSources Condition="'$(SentryUploadSources)' == ''">false</SentryUploadSources>
<SentrySetCommits Condition="'$(SentrySetCommits)' == ''">false</SentrySetCommits>
<!-- SentryCreateRelease is implied if SentrySetCommits==true -->
<SentryCreateRelease Condition="'$(SentrySetCommits)' == 'true'">true</SentryCreateRelease>
<SentryCreateRelease Condition="'$(SentryCreateRelease)' == ''">false</SentryCreateRelease>

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

<SentryReleaseOptions Condition="'$(SentryProject)' != ''">$(SentryReleaseOptions) --project $(SentryProject)</SentryReleaseOptions>

<SentrySetCommitOptions Condition="'$(SentrySetCommitOptions)' == ''">--auto</SentrySetCommitOptions>

<SentryCLIUploadOptions Condition="'$(SentryOrg)' != ''">$(SentryCLIUploadOptions) --org $(SentryOrg)</SentryCLIUploadOptions>
<SentryCLIUploadOptions Condition="'$(SentryProject)' != ''">$(SentryCLIUploadOptions) --project $(SentryProject)</SentryCLIUploadOptions>
<SentryCLIDebugFilesUploadCommand>$(SentryCLIBaseCommand) debug-files upload</SentryCLIDebugFilesUploadCommand>
Expand Down Expand Up @@ -242,5 +250,119 @@
<Warning Condition="'$(_SentryCLIExitCode)' != '0'" Text="Sentry CLI could not upload proguard mapping." />
</Target>

<UsingTask TaskName="SentryGetReleaseFromInformationalVersion" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
<ParameterGroup>
<AssemblyPath Required="true" />
<Release Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.Reflection"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
try
{
var path = Path.GetFullPath(AssemblyPath);
var assembly = Assembly.LoadFile(path);

var assemblyName = assembly.GetName();
var name = assemblyName.Name;
var version = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(version))
{
Release = $"{name}@{version}";
}
}
catch
{
}
]]>
</Code>
</Task>
</UsingTask>

<UsingTask TaskName="SentryGetReleaseFromAppManifest" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"
Condition="$(TargetFramework.Contains('ios')) Or $(TargetFramework.Contains('maccatalyst'))">
<ParameterGroup>
<AppManifestPath Required="true" />
<Release Output="true" />
</ParameterGroup>
<Task>
<Reference Include="$(_XamarinTaskAssembly)" />
<Using Namespace="Xamarin.MacDev"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
try
{
var plist = PDictionary.FromFile(AppManifestPath);

var packageName = plist.GetCFBundleIdentifier();
var packageVersion = plist.GetCFBundleShortVersionString();
var buildVersion = plist.GetCFBundleVersion();

Release = $"{packageName}@{packageVersion}+{buildVersion}";
}
catch
{
}
]]>
</Code>
</Task>
</UsingTask>

<Target Name="_GetSentryRelease" AfterTargets="DispatchToInnerBuilds;AfterBuild" Condition="'$(SentryCreateRelease)' == 'true' And '$(UseSentryCLI)' == 'true'">

<Message Importance="High" Text="Getting Sentry Release..." />

<PropertyGroup Condition="'$(SentryRelease)' == '' And $(TargetFramework.Contains('android'))">
<SentryRelease>$(ApplicationId)@$(ApplicationDisplayVersion)+$(ApplicationVersion)</SentryRelease>
</PropertyGroup>

<SentryGetReleaseFromAppManifest AppManifestPath="$(_TemporaryAppManifest)"
Condition="'$(SentryRelease)' == '' And ($(TargetFramework.Contains('ios')) Or $(TargetFramework.Contains('maccatalyst')))">
<Output TaskParameter="Release" PropertyName="SentryRelease" />
</SentryGetReleaseFromAppManifest>

<SentryGetReleaseFromInformationalVersion Condition="'$(SentryRelease)' == ''" AssemblyPath="$(IntermediateOutputPath)$(TargetName)$(TargetExt)">
<Output TaskParameter="Release" PropertyName="SentryRelease" />
</SentryGetReleaseFromInformationalVersion>

<GetAssemblyIdentity Condition="'$(SentryRelease)' == '' And '$(TargetName)' != ''" AssemblyFiles="$(IntermediateOutputPath)$(TargetName)$(TargetExt)">
<Output TaskParameter="Assemblies" ItemName="MainAssemblyIdentity" />
</GetAssemblyIdentity>
<PropertyGroup Condition="'$(SentryRelease)' == '' And '@(MainAssemblyIdentity)' != ''">
<SentryRelease>%(MainAssemblyIdentity.Name)@%(Version)</SentryRelease>
</PropertyGroup>

<Exec ConsoleToMSBuild="true" Condition="'$(SentryRelease)' == ''"
Command="sentry-cli releases propose-version">
<Output TaskParameter="ConsoleOutput" PropertyName="SentryRelease"/>
</Exec>

<Message Importance="High" Text="Sentry Release: $(SentryRelease)" />
</Target>

<!-- Set release information after the build -->
<Target Name="_CreateSentryRelease" AfterTargets="Build" DependsOnTargets="_GetSentryRelease"
Condition="'$(SentryCLI)' != '' and '$(SentryCreateRelease)' == 'true'">
<Message Importance="High" Text="Creating Sentry Release: $(SentryRelease)" />
<Exec
Command="$(SentryCLIBaseCommand) releases new '$(SentryRelease)' $(SentryReleaseOptions)"
IgnoreExitCode="true" ContinueOnError="WarnAndContinue">
<Output TaskParameter="ExitCode" PropertyName="_SentryCLIExitCode" />
</Exec>
</Target>

<!-- Send commit details to Sentry -->
<Target Name="_SentrySetCommits" AfterTargets="Build" DependsOnTargets="_CreateSentryRelease"
Condition="'$(SentryCLI)' != '' and '$(SentrySetCommits)' == 'true'">
<Message Importance="High" Text="Setting Sentry commits" />
<Exec
Command="$(SentryCLIBaseCommand) releases set-commits $(SentrySetCommitOptions) '$(SentryRelease)' $(SentryReleaseOptions)"
IgnoreExitCode="true" ContinueOnError="WarnAndContinue">
<Output TaskParameter="ExitCode" PropertyName="_SentryCLIExitCode" />
</Exec>
</Target>

<Import Condition="'$(MSBuildProjectName)' != 'Sentry' and !$(MSBuildProjectName.StartsWith('Sentry.')) and '$(IsSentryTestProject)' == ''" Project="$(MSBuildThisFileDirectory)Sentry.Native.targets"/>
</Project>
Loading