/// <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 rootSrcPath = $"{accountName.GuidOrValue()}/{repoSlugOrName.ToSlug()}/src/"; if (string.IsNullOrEmpty(revision)) { // This may potentially be optimized since this call will first hit a redirect toward an url that contains the revision // but the actual architecture of the code doesn't allow us to fetch just the redirect location of a GET request. // so we found back the data we need in the response of the call to the url where we are redirected. var rootEntry = repositoriesEndPoint.GetTreeEntry(rootSrcPath); revision = rootEntry.commit.hash; } SrcPath = UrlHelper.ConcatPathSegments(rootSrcPath, revision, path).EnsureEndsWith('/'); }
/// <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 { // This may potentially be optimized since this call will first hit a redirect toward an url that contains the revision // but the actual architecture of the code doesn't allow us to fetch just the redirect location of a GET request. // so we found back the data we need in the response of the call to the url where we are redirected. var rootEntry = repositoriesEndPoint.GetTreeEntry(rootSrcPath); revision = rootEntry.commit.hash; } 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); }
/// <summary> /// Gets the metadata of a specified sub path in this resource. /// <remarks> /// Since it can be difficult to guess which field is filled or not in a <see cref="TreeEntry"/>, /// we suggest you to use <see cref="GetSrcEntry"/> method instead of that one, /// except if you really want to retrieve the raw model as returned by BitBucket. /// </remarks> /// </summary> /// <param name="subPath">The path to the file or directory, or null to retrieve the metadata of the root of this resource.</param> public TreeEntry GetTreeEntry(string subPath = null) { return(RepositoriesEndPoint.GetTreeEntry(SrcPath.Value, subPath)); }