Skip to content

Commit

Permalink
Add distinct diagnostic for contributors
Browse files Browse the repository at this point in the history
Also add argentine localization.
We now report org and team as hidden diagnostics, since we don't want to highlight particularly those two scenarios.
  • Loading branch information
kzu committed Aug 12, 2024
1 parent 3845393 commit ca82a9d
Show file tree
Hide file tree
Showing 12 changed files with 446 additions and 174 deletions.
100 changes: 64 additions & 36 deletions samples/dotnet/SponsorLink/DiagnosticsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using Humanizer;
using Humanizer.Localisation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static Devlooped.Sponsors.SponsorLink;
Expand All @@ -28,7 +29,12 @@ class DiagnosticsManager
// <Constant Include="Funding.Product" Value="[PRODUCT_NAME]" />
// <Constant Include="Funding.AnalyzerPrefix" Value="[PREFIX]" />
{ SponsorStatus.Unknown, CreateUnknown([.. Sponsorables.Keys], Funding.Product, Funding.Prefix) },
{ SponsorStatus.Sponsor, CreateSponsor([.. Sponsorables.Keys], Funding.Prefix) },
{ SponsorStatus.User, CreateSponsor([.. Sponsorables.Keys], Funding.Prefix) },
{ SponsorStatus.Contributor, CreateContributor([.. Sponsorables.Keys], Funding.Prefix) },
// NOTE: organization is a special case of sponsor, but we report it as hidden since the user isn't directly involved.
{ SponsorStatus.Organization, CreateSponsor([.. Sponsorables.Keys], Funding.Prefix, hidden: true) },
// NOTE: similar to organization, we don't show team membership in the IDE.
{ SponsorStatus.Team, CreateContributor([.. Sponsorables.Keys], Funding.Prefix, hidden: true) },
{ SponsorStatus.Expiring, CreateExpiring([.. Sponsorables.Keys], Funding.Prefix) },
{ SponsorStatus.Expired, CreateExpired([.. Sponsorables.Keys], Funding.Prefix) },
};
Expand Down Expand Up @@ -88,13 +94,12 @@ public SponsorStatus GetOrSetStatus(Func<AnalyzerOptions?> options)
/// <summary>
/// Pushes a diagnostic for the given product.
/// </summary>
/// <returns>The same diagnostic that was pushed, for chained invocations.</returns>
Diagnostic Push(Diagnostic diagnostic, string product = Funding.Product)
SponsorStatus Push(Diagnostic diagnostic, SponsorStatus status, string product = Funding.Product)
{
// We only expect to get one warning per sponsorable+product
// combination, and first one to set wins.
Diagnostics.TryAdd(product, diagnostic);
return diagnostic;
return status;
}

SponsorStatus GetOrSetStatus(Func<ImmutableArray<AdditionalText>> getAdditionalFiles, Func<AnalyzerConfigOptions?> getGlobalOptions)
Expand Down Expand Up @@ -122,44 +127,56 @@ SponsorStatus GetOrSetStatus(Func<ImmutableArray<AdditionalText>> getAdditionalF
if (installed != default && ((DateTime.Now - installed).TotalDays <= Funding.Grace))
{
// report unknown, either unparsed manifest or one with no expiration (which we never emit).
Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Unknown], null,
return Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Unknown], null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), nameof(SponsorStatus.Grace)),
Funding.Product, Sponsorables.Keys.Humanize(Resources.Or)));
return SponsorStatus.Grace;
Funding.Product, Sponsorables.Keys.Humanize(Resources.Or)),
SponsorStatus.Grace);
}
}

// report unknown, either unparsed manifest or one with no expiration (which we never emit).
Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Unknown], null,
return Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Unknown], null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), nameof(SponsorStatus.Unknown)),
Funding.Product, Sponsorables.Keys.Humanize(Resources.Or)));
return SponsorStatus.Unknown;
Funding.Product, Sponsorables.Keys.Humanize(Resources.Or)),
SponsorStatus.Unknown);
}
else if (exp < DateTime.Now)
{
// report expired or expiring soon if still within the configured days of grace period
if (exp.AddDays(Funding.Grace) < DateTime.Now)
{
// report expiring soon
Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Expiring], null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), nameof(SponsorStatus.Expiring))));
return SponsorStatus.Expiring;
return Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Expiring], null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), nameof(SponsorStatus.Expiring))),
SponsorStatus.Expiring);
}
else
{
// report expired
Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Expired], null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), nameof(SponsorStatus.Expired))));
return SponsorStatus.Expired;
return Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Expired], null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), nameof(SponsorStatus.Expired))),
SponsorStatus.Expired);
}
}
else
{
// report sponsor
Push(Diagnostic.Create(KnownDescriptors[SponsorStatus.Sponsor], null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), nameof(SponsorStatus.Sponsor)),
Funding.Product));
return SponsorStatus.Sponsor;
status =
claims.IsInRole("team") ?
SponsorStatus.Team :
claims.IsInRole("user") ?
SponsorStatus.User :
claims.IsInRole("contrib") ?
SponsorStatus.Contributor :
claims.IsInRole("org") ?
SponsorStatus.Organization :
SponsorStatus.Unknown;

if (KnownDescriptors.TryGetValue(status, out var descriptor))
return Push(Diagnostic.Create(descriptor, null,
properties: ImmutableDictionary.Create<string, string?>().Add(nameof(SponsorStatus), status.ToString()),
Funding.Product), status);

return status;
}
}

Expand All @@ -168,26 +185,15 @@ SponsorStatus GetOrSetStatus(Func<ImmutableArray<AdditionalText>> getAdditionalF
{
nameof(SponsorStatus.Grace) => SponsorStatus.Grace,
nameof(SponsorStatus.Unknown) => SponsorStatus.Unknown,
nameof(SponsorStatus.Sponsor) => SponsorStatus.Sponsor,
nameof(SponsorStatus.User) => SponsorStatus.User,
nameof(SponsorStatus.Expiring) => SponsorStatus.Expiring,
nameof(SponsorStatus.Expired) => SponsorStatus.Expired,
_ => null,
}
: null;

internal static DiagnosticDescriptor CreateSponsor(string[] sponsorable, string prefix) => new(
$"{prefix}100",
Resources.Sponsor_Title,
Resources.Sponsor_Message,
"SponsorLink",
DiagnosticSeverity.Info,
isEnabledByDefault: true,
description: Resources.Sponsor_Description,
helpLinkUri: "https://github.com/devlooped#sponsorlink",
"DoesNotSupportF1Help");

internal static DiagnosticDescriptor CreateUnknown(string[] sponsorable, string product, string prefix) => new(
$"{prefix}101",
$"{prefix}100",
Resources.Unknown_Title,
Resources.Unknown_Message,
"SponsorLink",
Expand All @@ -200,7 +206,7 @@ SponsorStatus GetOrSetStatus(Func<ImmutableArray<AdditionalText>> getAdditionalF
WellKnownDiagnosticTags.NotConfigurable);

internal static DiagnosticDescriptor CreateExpiring(string[] sponsorable, string prefix) => new(
$"{prefix}103",
$"{prefix}101",
Resources.Expiring_Title,
Resources.Expiring_Message,
"SponsorLink",
Expand All @@ -211,7 +217,7 @@ SponsorStatus GetOrSetStatus(Func<ImmutableArray<AdditionalText>> getAdditionalF
"DoesNotSupportF1Help", WellKnownDiagnosticTags.NotConfigurable);

internal static DiagnosticDescriptor CreateExpired(string[] sponsorable, string prefix) => new(
$"{prefix}104",
$"{prefix}102",
Resources.Expired_Title,
Resources.Expired_Message,
"SponsorLink",
Expand All @@ -220,4 +226,26 @@ SponsorStatus GetOrSetStatus(Func<ImmutableArray<AdditionalText>> getAdditionalF
description: string.Format(CultureInfo.CurrentCulture, Resources.Expired_Description, string.Join(" ", sponsorable)),
helpLinkUri: "https://github.com/devlooped#autosync",
"DoesNotSupportF1Help", WellKnownDiagnosticTags.NotConfigurable);

internal static DiagnosticDescriptor CreateSponsor(string[] sponsorable, string prefix, bool hidden = false) => new(
$"{prefix}105",
Resources.Sponsor_Title,
Resources.Sponsor_Message,
"SponsorLink",
hidden ? DiagnosticSeverity.Hidden : DiagnosticSeverity.Info,
isEnabledByDefault: true,
description: Resources.Sponsor_Description,
helpLinkUri: "https://github.com/devlooped#sponsorlink",
"DoesNotSupportF1Help");

internal static DiagnosticDescriptor CreateContributor(string[] sponsorable, string prefix, bool hidden = false) => new(
$"{prefix}106",
Resources.Contributor_Title,
Resources.Contributor_Message,
"SponsorLink",
hidden ? DiagnosticSeverity.Hidden : DiagnosticSeverity.Info,
isEnabledByDefault: true,
description: Resources.Contributor_Description,
helpLinkUri: "https://github.com/devlooped#sponsorlink",
"DoesNotSupportF1Help");
}
172 changes: 172 additions & 0 deletions samples/dotnet/SponsorLink/Resources.es-AR.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Unknown_Description" xml:space="preserve">
<value>Patrocinar los proyectos en que dependés asegura que se mantengan activos, y que recibas el apoyo que necesitás. También es muy económico y está disponible en todo el mundo!
Por favor considerá apoyar el proyecto patrocinando en {0} y ejecutando posteriormente 'sponsor sync {1}'.</value>
</data>
<data name="Unknown_Message" xml:space="preserve">
<value>Por favor considerá apoyar {0} patrocinando @{1} 🙏</value>
</data>
<data name="Unknown_Title" xml:space="preserve">
<value>Estado de patrocinio desconocido</value>
</data>
<data name="Expired_Description" xml:space="preserve">
<value>Funcionalidades exclusivas para patrocinadores pueden no estar disponibles. Ejecutá 'sponsor sync {0}' y, opcionalmente, habilita la sincronización automática.</value>
</data>
<data name="Expired_Message" xml:space="preserve">
<value>El estado de patrocino ha expirado y la sincronización automática no está habilitada.</value>
</data>
<data name="Expired_Title" xml:space="preserve">
<value>El estado de patrocino ha expirado</value>
</data>
<data name="Sponsor_Description" xml:space="preserve">
<value>Sos un verdadero héroe. Tu patrocinio ayuda a mantener el proyecto vivo y próspero 🙏.</value>
</data>
<data name="Sponsor_Message" xml:space="preserve">
<value>Gracias por apoyar a {0} con tu patrocinio 💟!</value>
</data>
<data name="Sponsor_Title" xml:space="preserve">
<value>Sos un patrocinador del proyecto, sos lo máximo 💟!</value>
</data>
<data name="Expiring_Description" xml:space="preserve">
<value>El estado de patrocino ha expirado y estás en un período de gracia. Ejecutá 'sponsor sync {0}' y, opcionalmente, habilitá la sincronización automática.</value>
</data>
<data name="Expiring_Message" xml:space="preserve">
<value>El estado de patrocino necesita actualización periódica y la sincronización automática no está habilitada.</value>
</data>
<data name="Expiring_Title" xml:space="preserve">
<value>El estado de patrocino ha expirado y el período de gracia terminará pronto</value>
</data>
<data name="And" xml:space="preserve">
<value>y</value>
</data>
<data name="Or" xml:space="preserve">
<value>o</value>
</data>
<data name="Contributor_Description" xml:space="preserve">
<value>Gracias por ser parte del equipo por tu contribución 🙏.</value>
</data>
<data name="Contributor_Message" xml:space="preserve">
<value>Gracias por ser parte del equipo {0} con tu contribución 💟!</value>
</data>
<data name="Contributor_Title" xml:space="preserve">
<value>Sos un contribuidor al proyecto, sos groso 💟!</value>
</data>
</root>
9 changes: 9 additions & 0 deletions samples/dotnet/SponsorLink/Resources.es.resx
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,13 @@ Por favor considera apoyar el proyecto patrocinando en {0} y ejecutando posterio
<data name="Or" xml:space="preserve">
<value>o</value>
</data>
<data name="Contributor_Description" xml:space="preserve">
<value>Gracias por ser parte del equipo por tu contribución 🙏.</value>
</data>
<data name="Contributor_Message" xml:space="preserve">
<value>Gracias por ser parte del equipo {0} con tu contribución 💟!</value>
</data>
<data name="Contributor_Title" xml:space="preserve">
<value>Eres un contribuidor al proyecto, eres lo máximo 💟!</value>
</data>
</root>
9 changes: 9 additions & 0 deletions samples/dotnet/SponsorLink/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,13 @@ Please consider supporting the project by sponsoring at {0} and running 'sponsor
<data name="Or" xml:space="preserve">
<value>or</value>
</data>
<data name="Contributor_Description" xml:space="preserve">
<value>Thanks for being part of the team with your contributions 🙏.</value>
</data>
<data name="Contributor_Message" xml:space="preserve">
<value>Thank you for being part of team {0} with your contributions 💟!</value>
</data>
<data name="Contributor_Title" xml:space="preserve">
<value>You are a contributor to the project, you rock 💟!</value>
</data>
</root>
Loading

0 comments on commit ca82a9d

Please sign in to comment.