Created
June 14, 2020 14:18
-
-
Save abhishekgoenka/9759256f995e7f7c9cbcb7872c8591c0 to your computer and use it in GitHub Desktop.
Same code to reproduce error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NLog; | |
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.IO.Compression; | |
using Microsoft.VisualStudio.Services.Common; | |
using Microsoft.VisualStudio.Services.WebApi; | |
using Microsoft.TeamFoundation.SourceControl.WebApi; | |
using Microsoft.TeamFoundation.Build.WebApi; | |
using Microsoft.TeamFoundation.Core.WebApi; | |
using System.IO; | |
using Newtonsoft.Json.Linq; | |
using System.Drawing; | |
namespace Utils.Dashboard | |
{ | |
public partial class FrmAzureDashboard : Form | |
{ | |
private static readonly Logger _logger = LogManager.GetCurrentClassLogger(); | |
List<Models.Build> builds; | |
static readonly string TFUrl = "https://dev.azure.com/MyOrg-corp"; | |
static readonly string UserPAT = "Some PAT Token"; // PAT token | |
BuildHttpClient BuildClient; | |
ProjectHttpClient ProjectClient; | |
GitHttpClient GitClient; | |
TfvcHttpClient TfvsClient; | |
public FrmAzureDashboard() | |
{ | |
InitializeComponent(); | |
} | |
private async void FrmAzureDashboard_Load(object sender, EventArgs e) | |
{ | |
int buildDefinationID = 792; | |
// connect | |
ConnectWithPAT(TFUrl, UserPAT); | |
builds = await GetBuildListAsync(buildDefinationID); | |
CboBuilds.DataSource = builds; | |
CboBuilds.DisplayMember = "buildNumber"; | |
CboBuilds.ValueMember = "buildNumber"; | |
} | |
private async Task<List<Models.Build>> GetBuildListAsync(int buildDefinationID) | |
{ | |
List<Models.Build> buildResult = new List<Models.Build>(); | |
try | |
{ | |
// Get data about a specific repository | |
var project = await GetProjectAsync(); | |
var allBuilds = await BuildClient.GetBuildsAsync2(project.Id, definitions: new List<int> { buildDefinationID }); | |
foreach (var item in allBuilds) | |
{ | |
buildResult.Add(new Models.Build { id = item.Id, buildNumber = item.BuildNumber, result = item.Result }); | |
} | |
return buildResult; | |
} | |
catch (Exception ex) | |
{ | |
_logger.Info(ex.ToString()); | |
return buildResult; | |
} | |
} | |
private void CboBuilds_SelectedIndexChanged(object sender, EventArgs e) | |
{ | |
if (CboBuilds.SelectedItem != null) | |
{ | |
var selected = CboBuilds.SelectedItem as Models.Build; | |
LblBuildResult.Text = selected.result.ToString(); | |
BtnDownloadKHP.Enabled = selected.result == BuildResult.Succeeded; | |
} | |
} | |
private async void BtnDownloadKHP_Click(object sender, EventArgs e) | |
{ | |
ClearFolder(); | |
const string DOWNLOAD_FILE_PATH = "C:\\temp\\ArtifactDownload\\ABC.ZIP"; | |
const string ARTIFACT_NAME = "homepage"; | |
// get selected build | |
var selectedBuild = CboBuilds.SelectedItem as Models.Build; | |
// get current project | |
var project = await GetProjectAsync(); | |
// get artifact | |
var buildArtifact = await BuildClient.GetArtifactAsync(project.Id, selectedBuild.id, ARTIFACT_NAME); | |
// download in default browser | |
// I am able to browse the DownloadUrl | |
System.Diagnostics.Process.Start(buildArtifact.Resource.DownloadUrl); | |
// todo: following line throws an error | |
Stream zipStream = await BuildClient.GetArtifactContentZipAsync(project.Id, selectedBuild.id, ARTIFACT_NAME); | |
using (FileStream zipFile = new FileStream(DOWNLOAD_FILE_PATH, FileMode.Create)) | |
{ | |
zipStream.CopyTo(zipFile); | |
} | |
} | |
void ConnectWithPAT(string ServiceURL, string PAT) | |
{ | |
VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT)); | |
InitClients(connection); | |
} | |
void InitClients(VssConnection Connection) | |
{ | |
BuildClient = Connection.GetClient<BuildHttpClient>(); | |
ProjectClient = Connection.GetClient<ProjectHttpClient>(); | |
GitClient = Connection.GetClient<GitHttpClient>(); | |
TfvsClient = Connection.GetClient<TfvcHttpClient>(); | |
} | |
async Task<TeamProject> GetProjectAsync() | |
{ | |
string TeamProjectName = "MyOrg-PD"; | |
return await ProjectClient.GetProject(TeamProjectName); | |
} | |
private void ClearFolder() | |
{ | |
Helper.DeleteFolder("C:\\temp\\ArtifactDownload"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment