Exemplo n.º 1
0
        private void ExecuteImpl()
        {
            // skip SourceRoot that already has SourceLinkUrl set, or its SourceControl is not "git":
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata(Names.SourceRoot.SourceLinkUrl)) ||
                !string.Equals(SourceRoot.GetMetadata(Names.SourceRoot.SourceControl), SourceControlName, StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }

            var repoUrl = SourceRoot.GetMetadata(Names.SourceRoot.RepositoryUrl);

            if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri))
            {
                Log.LogError(Resources.ValueOfWithIdentityIsInvalid, Names.SourceRoot.RepositoryUrlFullName, SourceRoot.ItemSpec, repoUrl);
                return;
            }

            var mappings   = GetUrlMappings().ToArray();
            var contentUri = GetMatchingContentUri(mappings, repoUri.Host);

            if (contentUri == null)
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }

            bool IsHexDigit(char c)
            => c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';

            string revisionId = SourceRoot.GetMetadata(Names.SourceRoot.RevisionId);

            if (revisionId == null || revisionId.Length != 40 || !revisionId.All(IsHexDigit))
            {
                Log.LogError(Resources.ValueOfWithIdentityIsNotValidCommitHash, Names.SourceRoot.RevisionIdFullName, SourceRoot.ItemSpec, revisionId);
                return;
            }

            var relativeUrl = repoUri.LocalPath.TrimEnd('/');

            // The URL may or may not end with '.git', but raw.githubusercontent.com does not accept '.git' suffix:
            const string gitUrlSuffix = ".git";

            if (relativeUrl.EndsWith(gitUrlSuffix))
            {
                relativeUrl = relativeUrl.Substring(0, relativeUrl.Length - gitUrlSuffix.Length);
            }

            SourceLinkUrl = new Uri(contentUri, relativeUrl).ToString() + "/" + revisionId + "/*";
        }
        public override bool Execute()
        {
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata("SourceLinkUrl")) ||
                !string.Equals(SourceRoot.GetMetadata("SourceControl"), "tfvc", StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = "N/A";
                return(true);
            }

            var collectionUrl = SourceRoot.GetMetadata("CollectionUrl");

            if (!Uri.TryCreate(collectionUrl, UriKind.Absolute, out var collectionUri))
            {
                Log.LogError($"SourceRoot.CollectionUrl of '{SourceRoot.ItemSpec}' is invalid: '{collectionUrl}'");
                return(false);
            }

            // 'D' format: "effb7e66-f922-4dc9-a4dc-9bd5d3b01582"
            var projectIdStr = SourceRoot.GetMetadata("ProjectId");

            if (!Guid.TryParseExact(projectIdStr, "D", out var projectId))
            {
                Log.LogError($"SourceRoot.ProjectId of '{SourceRoot.ItemSpec}' is invalid: '{projectIdStr}'");
                return(false);
            }

            string revisionIdStr = SourceRoot.GetMetadata("RevisionId");

            if (revisionIdStr == null || !uint.TryParse(revisionIdStr, out var revisionId))
            {
                Log.LogError($"SourceRoot.RevisionId of '{SourceRoot.ItemSpec}' is not a valid changeset number: '{revisionIdStr}'");
                return(false);
            }

            string serverPath = SourceRoot.GetMetadata("ServerPath");

            if (serverPath == null || !serverPath.StartsWith("$", StringComparison.Ordinal))
            {
                Log.LogError($"SourceRoot.ServerPath of '{SourceRoot.ItemSpec}' is not a valid server path: '{revisionIdStr}'");
                return(false);
            }

            var escapedServerPath = string.Join("/", serverPath.Split('/').Select(Uri.EscapeDataString));

            SourceLinkUrl = new Uri(collectionUri, projectId.ToString("D")).ToString() +
                            "/_versionControl?version=" + revisionId + "&path=" + escapedServerPath + "/*";

            return(true);
        }
Exemplo n.º 3
0
        private string GetSourceLinkQuery()
        {
            bool IsHexDigit(char c)
            => c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';

            string revisionId = SourceRoot.GetMetadata(Names.SourceRoot.RevisionId);

            if (revisionId == null || revisionId.Length != 40 || !revisionId.All(IsHexDigit))
            {
                Log.LogError(Resources.ValueOfWithIdentityIsNotValidCommitHash, Names.SourceRoot.RevisionIdFullName, SourceRoot.ItemSpec, revisionId);
                return(null);
            }

            return($"api-version=1.0&versionType=commit&version={revisionId}&path=/*");
        }
        public override bool Execute()
        {
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata("SourceLinkUrl")) ||
                !string.Equals(SourceRoot.GetMetadata("SourceControl"), "git", StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = "N/A";
                return(true);
            }

            var repoUrl = SourceRoot.GetMetadata("RepositoryUrl");

            if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri))
            {
                Log.LogError($"SourceRoot.RepositoryUrl of '{SourceRoot.ItemSpec}' is invalid: '{repoUrl}'");
                return(false);
            }

            if (!repoUri.Host.Equals("github.com", StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = "N/A";
                return(true);
            }

            bool IsHexDigit(char c)
            => c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';

            string revisionId = SourceRoot.GetMetadata("RevisionId");

            if (revisionId == null || revisionId.Length != 40 || !revisionId.All(IsHexDigit))
            {
                Log.LogError($"SourceRoot.RevisionId of '{SourceRoot.ItemSpec}' is not a valid commit hash: '{revisionId}'");
                return(false);
            }

            var relativeUrl = repoUri.LocalPath.TrimEnd('/');

            // The URL may or may not end with '.git', but raw.githubusercontent.com does not accept '.git' suffix:
            const string gitUrlSuffix = ".git";

            if (relativeUrl.EndsWith(gitUrlSuffix))
            {
                relativeUrl = relativeUrl.Substring(0, relativeUrl.Length - gitUrlSuffix.Length);
            }

            SourceLinkUrl = new Uri(s_rawGitHub, relativeUrl).ToString() + "/" + revisionId + "/*";
            return(true);
        }
Exemplo n.º 5
0
        private void ExecuteImpl()
        {
            // skip SourceRoot that already has SourceLinkUrl set, or its SourceControl is not "git":
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata(Names.SourceRoot.SourceLinkUrl)) ||
                !string.Equals(SourceRoot.GetMetadata(Names.SourceRoot.SourceControl), SourceControlName, StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }

            var gitUrl = SourceRoot.GetMetadata(Names.SourceRoot.RepositoryUrl);

            if (string.IsNullOrEmpty(gitUrl))
            {
                SourceLinkUrl = NotApplicableValue;
                Log.LogWarning(CommonResources.UnableToDetermineRepositoryUrl);
                return;
            }

            if (!Uri.TryCreate(gitUrl, UriKind.Absolute, out var gitUri))
            {
                Log.LogError(CommonResources.ValueOfWithIdentityIsInvalid, Names.SourceRoot.RepositoryUrlFullName, SourceRoot.ItemSpec, gitUrl);
                return;
            }

            var mappings = GetUrlMappings(gitUri).ToArray();

            if (Log.HasLoggedErrors)
            {
                return;
            }

            if (mappings.Length == 0)
            {
                Log.LogError(CommonResources.AtLeastOneRepositoryHostIsRequired, HostsItemGroupName, ProviderDisplayName);
                return;
            }

            var contentUri = GetMatchingContentUri(mappings, gitUri, out var hostItem);

            if (contentUri == null)
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }
Exemplo n.º 6
0
        public override bool Execute()
        {
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata(Names.SourceRoot.SourceLinkUrl)) ||
                !string.Equals(SourceRoot.GetMetadata(Names.SourceRoot.SourceControl), SourceControlName, StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = NotApplicableValue;
                return(true);
            }

            var repoUrl = SourceRoot.GetMetadata(Names.SourceRoot.RepositoryUrl);

            if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri))
            {
                Log.LogError(Resources.ValueOfOWithIdentityIsInvalid, Names.SourceRoot.RepositoryUrlFullName, SourceRoot.ItemSpec, repoUrl);
                return(false);
            }

            var map = TryGetStandardUriMap();

            if (map != null && map.TryGetValue(repoUri, out var mappedUri))
            {
                repoUri = mappedUri;
            }

            string domain = string.IsNullOrEmpty(Domain) ? DefaultDomain : Domain;

            if (!TryParseRepositoryUrl(repoUri, domain, out var projectName, out var repositoryName))
            {
                SourceLinkUrl = NotApplicableValue;
                return(true);
            }

            var query = GetSourceLinkQuery();

            if (query == null)
            {
                return(false);
            }

            SourceLinkUrl = $"{repoUri.Scheme}://{repoUri.Authority}/{projectName}/_apis/git/repositories/{repositoryName}/items?" + query;
            return(true);
        }
        public override bool Execute()
        {
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata("SourceLinkUrl")) ||
                !string.Equals(SourceRoot.GetMetadata("SourceControl"), "git", StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = "N/A";
                return(true);
            }

            var repoUrl = SourceRoot.GetMetadata("RepositoryUrl");

            if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri))
            {
                Log.LogError($"SourceRoot.RepositoryUrl of '{SourceRoot.ItemSpec}' is invalid: '{repoUrl}'");
                return(false);
            }

            if (!TryParseRepositoryUrl(repoUri, out var projectName, out var repositoryName))
            {
                SourceLinkUrl = "N/A";
                return(true);
            }

            bool IsHexDigit(char c)
            => c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';

            string revisionId = SourceRoot.GetMetadata("RevisionId");

            if (revisionId == null || revisionId.Length != 40 || !revisionId.All(IsHexDigit))
            {
                Log.LogError($"SourceRoot.RevisionId of '{SourceRoot.ItemSpec}' is not a valid commit hash: '{revisionId}'");
                return(false);
            }

            SourceLinkUrl = $"{repoUri.Scheme}://{repoUri.Host}/{projectName}/_apis/git/repositories/{repositoryName}/items?api-version=1.0&versionType=commit&version={revisionId}&path=/*";
            return(true);
        }
Exemplo n.º 8
0
        private void ExecuteImpl()
        {
            // skip SourceRoot that already has SourceLinkUrl set, or its SourceControl is not "git":
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata(Names.SourceRoot.SourceLinkUrl)) ||
                !string.Equals(SourceRoot.GetMetadata(Names.SourceRoot.SourceControl), SourceControlName, StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }

            var repoUrl = SourceRoot.GetMetadata(Names.SourceRoot.RepositoryUrl);

            if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri))
            {
                Log.LogError(Resources.ValueOfWithIdentityIsInvalid, Names.SourceRoot.RepositoryUrlFullName, SourceRoot.ItemSpec, repoUrl);
                return;
            }

            var map = TryGetStandardUriMap();

            if (map != null && map.TryGetValue(repoUri, out var mappedUri))
            {
                repoUri = mappedUri;
            }

            string domain;

            if (string.IsNullOrEmpty(Domain))
            {
                domain = DefaultDomain;
            }
            else
            {
                bool isHostUri(Uri uri) => uri.PathAndQuery == "/" && uri.UserInfo == "";

                domain = Domain;
                if (!Uri.TryCreate("http://" + domain, UriKind.Absolute, out var domainUri) || !isHostUri(domainUri))
                {
                    Log.LogError(Resources.ValuePassedToTaskParameterNotValidDomainName, nameof(Domain), domain);
                    return;
                }
            }

            if (!TryParseRepositoryUrl(repoUri, domain, out var projectName, out var repositoryName, out var collectionName))
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }

            var query = GetSourceLinkQuery();

            if (query == null)
            {
                return;
            }

            // Although VSTS does not have non-default collections, TFS does.
            // This package can be used for both VSTS and TFS.
            string collectionPath = (collectionName == null || StringComparer.OrdinalIgnoreCase.Equals(collectionName, "DefaultCollection")) ? "" : "/" + collectionName;

            SourceLinkUrl = $"{repoUri.Scheme}://{repoUri.Authority}{collectionPath}/{projectName}/_apis/git/repositories/{repositoryName}/items?" + query;
        }
Exemplo n.º 9
0
        private void ExecuteImpl()
        {
            // skip SourceRoot that already has SourceLinkUrl set, or its SourceControl is not "git":
            if (!string.IsNullOrEmpty(SourceRoot.GetMetadata(Names.SourceRoot.SourceLinkUrl)) ||
                !string.Equals(SourceRoot.GetMetadata(Names.SourceRoot.SourceControl), SourceControlName, StringComparison.OrdinalIgnoreCase))
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }

            var repoUrl = SourceRoot.GetMetadata(Names.SourceRoot.RepositoryUrl);

            if (!Uri.TryCreate(repoUrl, UriKind.Absolute, out var repoUri))
            {
                Log.LogError(CommonResources.ValueOfWithIdentityIsInvalid, Names.SourceRoot.RepositoryUrlFullName, SourceRoot.ItemSpec, repoUrl);
                return;
            }

            var mappings = GetUrlMappings().ToArray();

            if (Log.HasLoggedErrors)
            {
                return;
            }

            if (mappings.Length == 0)
            {
                Log.LogError(CommonResources.AtLeastOneRepositoryHostIsRequired, HostsItemGroupName, ProviderDisplayName);
                return;
            }

            var contentUri = GetMatchingContentUri(mappings, repoUri);

            if (contentUri == null)
            {
                SourceLinkUrl = NotApplicableValue;
                return;
            }

            bool IsHexDigit(char c)
            => c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F';

            string revisionId = SourceRoot.GetMetadata(Names.SourceRoot.RevisionId);

            if (revisionId == null || revisionId.Length != 40 || !revisionId.All(IsHexDigit))
            {
                Log.LogError(CommonResources.ValueOfWithIdentityIsNotValidCommitHash, Names.SourceRoot.RevisionIdFullName, SourceRoot.ItemSpec, revisionId);
                return;
            }

            var relativeUrl = repoUri.LocalPath.TrimEnd('/');

            // The URL may or may not end with '.git' (case-sensitive), but content URLs do not include '.git' suffix:
            const string gitUrlSuffix = ".git";

            if (relativeUrl.EndsWith(gitUrlSuffix, StringComparison.Ordinal))
            {
                relativeUrl = relativeUrl.Substring(0, relativeUrl.Length - gitUrlSuffix.Length);
            }

            SourceLinkUrl = BuildSourceLinkUrl(contentUri.ToString(), relativeUrl, revisionId);
        }