Ejemplo n.º 1
0
        public VerifyPackageRequest(PackageMetadata packageMetadata, IEnumerable <User> possibleOwners)
        {
            Id                   = packageMetadata.Id;
            Version              = packageMetadata.Version.ToFullStringSafe();
            OriginalVersion      = packageMetadata.Version.OriginalVersion;
            HasSemVer2Version    = packageMetadata.Version.IsSemVer2;
            HasSemVer2Dependency = packageMetadata.GetDependencyGroups().Any(d => d.Packages.Any(
                                                                                 p => (p.VersionRange.HasUpperBound && p.VersionRange.MaxVersion.IsSemVer2) ||
                                                                                 (p.VersionRange.HasLowerBound && p.VersionRange.MinVersion.IsSemVer2)));

            // Verifiable fields
            Language = packageMetadata.Language;
            MinClientVersionDisplay  = packageMetadata.MinClientVersion.ToFullStringSafe();
            FrameworkReferenceGroups = packageMetadata.GetFrameworkReferenceGroups();
            Dependencies             = new DependencySetsViewModel(packageMetadata.GetDependencyGroups().AsPackageDependencyEnumerable());
            DevelopmentDependency    = packageMetadata.GetValueFromMetadata("developmentDependency");
            Authors      = packageMetadata.Authors.Flatten();
            Copyright    = packageMetadata.Copyright;
            Description  = packageMetadata.Description;
            IconUrl      = packageMetadata.IconUrl.ToEncodedUrlStringOrNull();
            LicenseUrl   = packageMetadata.LicenseUrl.ToEncodedUrlStringOrNull();
            ProjectUrl   = packageMetadata.ProjectUrl.ToEncodedUrlStringOrNull();
            ReleaseNotes = packageMetadata.ReleaseNotes;
            RequiresLicenseAcceptance = packageMetadata.RequireLicenseAcceptance;
            Summary = packageMetadata.Summary;
            Tags    = PackageHelper.ParseTags(packageMetadata.Tags);
            Title   = packageMetadata.Title;

            // Editable server-state
            Listed = true;
            Edit   = new EditPackageVersionReadMeRequest();

            PossibleOwners = possibleOwners.Select(u => u.Username).ToList();
        }
            public TheSaveReadMeMdIfChangedMethod()
            {
                _package = new Package
                {
                    HasReadMe = false,
                };
                _edit = new EditPackageVersionReadMeRequest
                {
                    ReadMe = new ReadMeRequest
                    {
                        SourceText = "# Title" + Environment.NewLine + "Some *groovy* content.",
                        SourceType = "written",
                    },
                    ReadMeState = PackageEditReadMeState.Changed,
                };
                _encoding = Encoding.UTF8;

                _packageFileService = new Mock <IPackageFileService>();
                _entitiesContext    = new Mock <IEntitiesContext>();
                _markdownService    = new Mock <IMarkdownService>();

                _target = new ReadMeService(
                    _packageFileService.Object,
                    _entitiesContext.Object,
                    _markdownService.Object);
            }
Ejemplo n.º 3
0
        public virtual void StartEditPackageRequest(Package package, EditPackageVersionReadMeRequest request, User editingUser)
        {
            var edit = new PackageEdit
            {
                // No longer change these fields as they are no longer editable.
                // To be removed in a next wave of package immutability implementation.
                // (when no pending edits left to be processed in the db)
                Authors      = package.FlattenedAuthors,
                Copyright    = package.Copyright,
                Description  = package.Description,
                IconUrl      = package.IconUrl,
                LicenseUrl   = package.LicenseUrl,
                ProjectUrl   = package.ProjectUrl,
                ReleaseNotes = package.ReleaseNotes,
                RequiresLicenseAcceptance = package.RequiresLicenseAcceptance,
                Summary = package.Summary,
                Tags    = package.Tags,
                Title   = package.Title,

                // Readme
                ReadMeState = request.ReadMeState,

                // Other
                User       = editingUser,
                Package    = package,
                Timestamp  = DateTime.UtcNow,
                TriedCount = 0,
            };

            EntitiesContext.Set <PackageEdit>().Add(edit);
            // Note: EditPackageRequests are completed asynchronously by the worker role.
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Save a pending ReadMe if changes are detected.
        /// </summary>
        /// <param name="package">Package entity associated with the ReadMe.</param>
        /// <param name="edit">Package edit entity.</param>
        /// <returns>True if a ReadMe is pending, false otherwise.</returns>
        public async Task <bool> SavePendingReadMeMdIfChanged(Package package, EditPackageVersionReadMeRequest edit, Encoding encoding)
        {
            var activeReadMe = package.HasReadMe ?
                               NormalizeNewLines(await GetReadMeMdAsync(package)) :
                               null;

            var newReadMe = HasReadMeSource(edit?.ReadMe) ?
                            NormalizeNewLines(await GetReadMeMdAsync(edit.ReadMe, encoding)) :
                            null;

            var hasReadMe = !string.IsNullOrWhiteSpace(newReadMe);

            if (hasReadMe && !newReadMe.Equals(activeReadMe))
            {
                await _packageFileService.SavePendingReadMeMdFileAsync(package, newReadMe);

                edit.ReadMeState = PackageEditReadMeState.Changed;
            }
            else if (!hasReadMe && !string.IsNullOrEmpty(activeReadMe))
            {
                await _packageFileService.DeleteReadMeMdFileAsync(package, isPending : true);

                edit.ReadMeState = PackageEditReadMeState.Deleted;
            }
            else
            {
                edit.ReadMeState = PackageEditReadMeState.Unchanged;
            }

            return(hasReadMe);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Save a pending ReadMe if changes are detected.
        /// </summary>
        /// <param name="package">Package entity associated with the ReadMe.</param>
        /// <param name="edit">Package version edit readme request.</param>
        /// <param name="encoding">The encoding used when reading the existing readme.</param>
        /// <param name="commitChanges">Whether or not to commit the pending changes to the database.</param>
        /// <returns>True if the package readme changed, otherwise false.</returns>
        public async Task <bool> SaveReadMeMdIfChanged(
            Package package,
            EditPackageVersionReadMeRequest edit,
            Encoding encoding,
            bool commitChanges)
        {
            var activeReadMe = package.HasReadMe ?
                               NormalizeNewLines(await GetReadMeMdAsync(package)) :
                               null;

            var newReadMe = HasReadMeSource(edit?.ReadMe) ?
                            NormalizeNewLines(await GetReadMeMdAsync(edit.ReadMe, encoding)) :
                            null;

            var hasReadMe = !string.IsNullOrWhiteSpace(newReadMe);

            if (hasReadMe && !newReadMe.Equals(activeReadMe))
            {
                await _packageFileService.SaveReadMeMdFileAsync(package, newReadMe);

                edit.ReadMeState = PackageEditReadMeState.Changed;

                // Save entity to db.
                package.HasReadMe = true;

                if (commitChanges)
                {
                    await _entitiesContext.SaveChangesAsync();
                }
            }
            else if (!hasReadMe && !string.IsNullOrEmpty(activeReadMe))
            {
                await _packageFileService.DeleteReadMeMdFileAsync(package);

                edit.ReadMeState = PackageEditReadMeState.Deleted;

                // Save entity to db.
                package.HasReadMe = false;

                if (commitChanges)
                {
                    await _entitiesContext.SaveChangesAsync();
                }
            }
            else
            {
                edit.ReadMeState = PackageEditReadMeState.Unchanged;
            }

            return(edit.ReadMeState != PackageEditReadMeState.Unchanged);
        }
Ejemplo n.º 6
0
        public VerifyPackageRequest(PackageMetadata packageMetadata, IEnumerable <User> possibleOwners, PackageRegistration existingPackageRegistration)
        {
            Id                   = packageMetadata.Id;
            Version              = packageMetadata.Version.ToFullStringSafe();
            OriginalVersion      = packageMetadata.Version.OriginalVersion;
            HasSemVer2Version    = packageMetadata.Version.IsSemVer2;
            HasSemVer2Dependency = packageMetadata.GetDependencyGroups().Any(d => d.Packages.Any(
                                                                                 p => (p.VersionRange.HasUpperBound && p.VersionRange.MaxVersion.IsSemVer2) ||
                                                                                 (p.VersionRange.HasLowerBound && p.VersionRange.MinVersion.IsSemVer2)));

            // Verifiable fields
            Language = packageMetadata.Language;
            MinClientVersionDisplay  = packageMetadata.MinClientVersion.ToFullStringSafe();
            FrameworkReferenceGroups = packageMetadata.GetFrameworkReferenceGroups();
            Dependencies             = new DependencySetsViewModel(packageMetadata.GetDependencyGroups().AsPackageDependencyEnumerable());
            DevelopmentDependency    = packageMetadata.DevelopmentDependency;
            Authors                   = packageMetadata.Authors.Flatten();
            Copyright                 = packageMetadata.Copyright;
            Description               = packageMetadata.Description;
            IconUrl                   = packageMetadata.IconUrl.ToEncodedUrlStringOrNull();
            LicenseUrl                = packageMetadata.LicenseUrl.ToEncodedUrlStringOrNull();
            LicenseExpression         = packageMetadata.LicenseMetadata?.Type == LicenseType.Expression ? packageMetadata.LicenseMetadata?.License : null;
            ProjectUrl                = packageMetadata.ProjectUrl.ToEncodedUrlStringOrNull();
            RepositoryUrl             = packageMetadata.RepositoryUrl.ToEncodedUrlStringOrNull();
            RepositoryType            = packageMetadata.RepositoryType;
            ReleaseNotes              = packageMetadata.ReleaseNotes;
            RequiresLicenseAcceptance = packageMetadata.RequireLicenseAcceptance;
            Summary                   = packageMetadata.Summary;
            Tags    = PackageHelper.ParseTags(packageMetadata.Tags);
            Title   = packageMetadata.Title;
            IsNewId = existingPackageRegistration == null;
            if (!IsNewId)
            {
                ExistingOwners = string.Join(", ", ParseUserList(existingPackageRegistration.Owners));
            }

            // Editable server-state
            Listed         = true;
            Edit           = new EditPackageVersionReadMeRequest();
            PossibleOwners = ParseUserList(possibleOwners);
        }
Ejemplo n.º 7
0
        public ManagePackageViewModel(Package package, User currentUser, IReadOnlyList <ReportPackageReason> reasons, UrlHelper url, string readMe, bool isManageDeprecationEnabled)
            : base(package, currentUser)
        {
            IsCurrentUserAnAdmin = currentUser != null && currentUser.IsAdministrator;

            DeletePackagesRequest = new DeletePackagesRequest
            {
                Packages = new List <string>
                {
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "{0}|{1}",
                        package.PackageRegistration.Id,
                        package.Version)
                },
                ReasonChoices = reasons
            };

            IsLocked = package.PackageRegistration.IsLocked;

            IsManageDeprecationEnabled = isManageDeprecationEnabled;

            var versionSelectPackages = package.PackageRegistration.Packages
                                        .Where(p => p.PackageStatusKey == PackageStatus.Available || p.PackageStatusKey == PackageStatus.Validating)
                                        .OrderByDescending(p => new NuGetVersion(p.Version))
                                        .ToList();

            VersionSelectList                 = new List <SelectListItem>();
            VersionListedStateDictionary      = new Dictionary <string, VersionListedState>();
            VersionReadMeStateDictionary      = new Dictionary <string, VersionReadMeState>();
            VersionDeprecationStateDictionary = new Dictionary <string, VersionDeprecationState>();

            var submitUrlTemplate    = url.PackageVersionActionTemplate("Edit");
            var getReadMeUrlTemplate = url.PackageVersionActionTemplate("GetReadMeMd");

            foreach (var versionSelectPackage in versionSelectPackages)
            {
                var text  = PackageHelper.GetSelectListText(versionSelectPackage);
                var value = NuGetVersionFormatter.Normalize(versionSelectPackage.Version);
                VersionSelectList.Add(new SelectListItem
                {
                    Text     = text,
                    Value    = value,
                    Selected = package == versionSelectPackage
                });

                VersionListedStateDictionary.Add(
                    value,
                    new VersionListedState(versionSelectPackage));

                var model = new TrivialPackageVersionModel(versionSelectPackage);
                VersionReadMeStateDictionary.Add(
                    value,
                    new VersionReadMeState(
                        submitUrlTemplate.Resolve(model),
                        getReadMeUrlTemplate.Resolve(model),
                        null));

                VersionDeprecationStateDictionary.Add(
                    value,
                    new VersionDeprecationState(versionSelectPackage, text));
            }

            // Update edit model with the readme.md data.
            ReadMe = new EditPackageVersionReadMeRequest();
            if (package.HasReadMe)
            {
                ReadMe.ReadMe.SourceType = ReadMeService.TypeWritten;
                ReadMe.ReadMe.SourceText = readMe;
            }
        }