Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of <see cref="SrcResource"/>.
        /// </summary>
        /// <param name="repositoriesEndPoint">The base end point extended by this resource.</param>
        /// <param name="accountName">The name of the account or team in which is located the repository we want to browse.</param>
        /// <param name="repoSlugOrName">The slug or name of the repository we want to browse. (this may also be the repository UUID).</param>
        /// <param name="revision">The name of the revision to browse. This may be a commit hash, a branch name, a tag name, or null to target the last commit of the main branch.</param>
        /// <param name="path">An optional path to a sub directory if you want to start to browse somewhere else that at the root path.</param>
        public SrcResource(RepositoriesEndPoint repositoriesEndPoint, string accountName, string repoSlugOrName, string revision = null, string path = null)
        {
            RepositoriesEndPoint = repositoriesEndPoint ?? throw new ArgumentNullException(nameof(repositoriesEndPoint));

            var repoPath = $"{accountName.GuidOrValue()}/{repoSlugOrName.ToSlug()}";

            // full build of the SrcPath value is delayed so that when revision is null errors are send
            // only when caller really try to do a request and not when building the resource object
            string BuildSrcPath()
            {
                var rootSrcPath = $"{repoPath}/src/";

                if (string.IsNullOrEmpty(revision))
                {
                    try
                    {
                        // calling the src endpoint redirect to the hash of the last commit of the main branch
                        // https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/src#get
                        var redirectLocation = repositoriesEndPoint.GetRedirectLocation(rootSrcPath);
                        revision = redirectLocation.Segments[redirectLocation.Segments.Length - 1];
                    }
                    catch (BitbucketV2Exception e) when(e.HttpStatusCode == HttpStatusCode.NotFound)
                    {
                        // mimic the error that bitbucket send when we perform calls on src endpoint with a revision name
                        var errorResponse = new ErrorResponse {
                            type = "Error", error = new Error {
                                message = $"Repository {repoPath} not found"
                            }
                        };

                        throw new BitbucketV2Exception(HttpStatusCode.NotFound, errorResponse);
                    }
                }

                return(UrlHelper.ConcatPathSegments(rootSrcPath, revision, path).EnsureEndsWith('/'));
            }

            SrcPath = new Lazy <string>(BuildSrcPath);
        }