/// <summary> /// Get the items in the repository at the given path. /// </summary> /// <param name="repositoryId">The ID of the repository to query</param> /// <param name="paths">Paths (and filters) of the items to query</param> /// <param name="includeMetadata">True to include item metadata</param> /// <returns>A list of commits in the Git repository</returns> public async Task<IEnumerable<IEnumerable<Item>>> GetItems(Guid repositoryId, IEnumerable<Tuple<string, ItemFilters>> paths, bool includeMetadata = false) { Assert.NotNull(repositoryId, "repositoryId"); Assert.NotNull(paths, "paths"); var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/itemsBatch", HttpMethod.Post); request.AddUrlSegment("RepositoryId", repositoryId.ToString()); // This header instructs VSO to return metadata rather than the item contents request.AddHeader("Accept", "application/json"); request.AddBody(new { itemDescriptors = paths.Select(x => new { path = x.Item1, version = x.Item2.Revision != null ? x.Item2.Revision.Version : "", versionType = x.Item2.Revision != null ? x.Item2.Revision.Type.ToString().ToLowerInvariant() : "", versionOptions = x.Item2.RevisionOptions.ToString().ToLowerInvariant(), recursionLevel = x.Item2.RecursionLevel.ToString().ToLowerInvariant(), }), includeContentMetadata = includeMetadata ? "true" : "false", }); Sequence<List<Item>> list = await Executor.Execute<Sequence<List<Item>>>(request); return list.Value; }
/// <summary> /// Get the item in the repository at the given path. /// </summary> /// <param name="repositoryId">The ID of the repository to query</param> /// <param name="path">Path of the item to query</param> /// <param name="filters">Filters to provide additional query information</param> /// <param name="includeMetadata">To include item metadata or not</param> /// <returns>A list of commits in the Git repository</returns> public async Task<IEnumerable<Item>> GetItem(Guid repositoryId, string path, ItemFilters filters = null, bool includeMetadata = false) { Assert.NotNull(repositoryId, "repositoryId"); Assert.NotNull(path, "path"); var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/items"); request.AddUrlSegment("RepositoryId", repositoryId.ToString()); // This header instructs VSO to return metadata rather than the item contents request.AddHeader("Accept", "application/json"); filters = filters ?? new ItemFilters(); request.AddParameter("scopePath", path); request.AddOptionalParameter("recursionLevel", filters.RecursionLevel, () => { return filters.RecursionLevel != RecursionLevel.None; }); if (includeMetadata) { request.AddParameter("includeContentMetadata", "true"); } Sequence<Item> list = await Executor.Execute<Sequence<Item>>(request); return list.Value; }
/// <summary> /// Download the contents of a tree as a zip file. /// </summary> /// <param name="repositoryId">The ID of the repository</param> /// <param name="treeId">The object ID of the tree</param> /// <param name="outputStream">The stream to write to asynchronously</param> public async Task DownloadTree(Guid repositoryId, ObjectId treeId, Stream outputStream) { Assert.NotNull(repositoryId, "repositoryId"); Assert.NotNull(treeId, "treeId"); Assert.NotNull(outputStream, "outputStream"); var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/trees/{TreeId}"); request.AddUrlSegment("RepositoryId", repositoryId.ToString()); request.AddUrlSegment("TreeId", treeId.ToString()); request.AddUrlSegment("$format", "zip"); request.AddHeader("Accept", "application/zip"); await Executor.Execute(request, outputStream); }
/// <summary> /// Download a blob's contents. /// </summary> /// <param name="repositoryId">The ID of the repository</param> /// <param name="blobId">The object ID of the blob</param> /// <param name="format">The format to download as</param> /// <param name="outputStream">The stream to write the response content to asynchronously</param> public async Task DownloadBlob(Guid repositoryId, ObjectId blobId, BlobFormat format, Stream outputStream) { Assert.NotNull(repositoryId, "repositoryId"); Assert.NotNull(blobId, "blobId"); Assert.NotNull(outputStream, "outputStream"); var request = new TfsRestRequest("/_apis/git/repositories/{RepositoryId}/blobs/{BlobId}"); request.AddUrlSegment("RepositoryId", repositoryId.ToString()); request.AddUrlSegment("BlobId", blobId.ToString()); if (format == BlobFormat.Zip) { request.AddUrlSegment("$format", "zip"); request.AddHeader("Accept", "application/zip"); } else { request.AddUrlSegment("$format", "octetstream"); request.AddHeader("Accept", "application/octet-stream"); } await Executor.Execute(request, outputStream); }