/// <summary> /// If repo is public, will try to download via web link instead of calling API /// </summary> private async Task GetFileReference(string path, Func <HttpResponseMessage, Task> actionWhenSuccess) { string branch = await this.GetBranch(); // use web link, assume it is public by default to avoid calling API too often (would get block) string fileUrl = string.Format( CultureInfo.InvariantCulture, Constants.Repository.BitbucketRawFileWebFormat, UserName, RepositoryName, branch, path.Trim(Constants.Path.SlashChars)); // trim slash to remove slash at the begining string token = null; // if it is public, try to use repo access token thru API if (await this.IsPrivate()) { SourceControlInfo scmInfo = await this.GetScmInfo(); token = scmInfo.token; fileUrl = string.Format(CultureInfo.InvariantCulture, Constants.Repository.BitbucketApiRawFile, UserName, RepositoryName, branch, path); } using (HttpClient client = CreateHttpClient(accessToken: token)) using (HttpResponseMessage response = await client.GetAsync(fileUrl)) { await actionWhenSuccess(response); } }
private async Task <HttpClient> CreateHttpClient() { SourceControlInfo scmInfo = await this.GetScmInfo(); // if user doesn`t have a token, "scmInfo.token" will be null. In this case HttpClient will be created without token return(CreateHttpClient(accessToken: scmInfo.token)); }
/// <summary> /// LoadDetailsFromConfigFile: /// Loads details from the config file. It populates the global variable cfProducts with /// product names. If the productName parameter is not empty and contains a valid product /// name, then the productDetails parameter is populated with the build information for /// the specified product. /// </summary> /// <param name="configFileName">[required, in] The path and filename of the config file</param> /// <param name="productName">[optional, in] The name of the product to be loaded</param> /// <param name="productDetails">[optional, out] Required only if productName is specified</param> /// <returns>A boolean value indicating if product names were successfully added to the /// cfProducts variable</returns> private bool LoadDetailsFromConfigFile(string configFileName, string productName, out ProductDetails productDetails) { productDetails = null; //Local variables bool hasMoreProducts = true; //Erase any previously loaded information cfProducts = null; XmlTextReader xmlReader = null; XmlValidatingReader validator = null; System.Xml.XPath.XPathDocument doc; System.Xml.XPath.XPathNavigator nav; try { xmlReader = new XmlTextReader(configFileName); validator = new XmlValidatingReader(xmlReader); validator.ValidationType = ValidationType.DTD; //Any errors will cause an XmlException to be thrown while (validator.Read()) { } doc = new System.Xml.XPath.XPathDocument(configFileName); nav = doc.CreateNavigator(); } catch (Exception) { if (xmlReader != null) xmlReader.Close(); if (validator != null) validator.Close(); return false; } #region Load PRODUCT names (and product details if specified) from the XML configuration file //Move to the SIBuilder Tag nav.MoveToFirstChild(); if (!nav.HasChildren) { //MessageBox.Show ("The configuration file does not contain any products that can be built", errorDialogCaption); xmlReader.Close(); validator.Close(); return false; } this.cfProducts = new ArrayList(); //Move to the first PRODUCT in the configuration file nav.MoveToFirstChild (); hasMoreProducts = true; while (hasMoreProducts) { this.cfProducts.Add (nav.GetAttribute ("Name", "")); //Check if the Name attribute was returned; if not declare config file corrupt if (this.cfProducts[this.cfProducts.Count - 1].ToString() == String.Empty) { this.cfProducts = null; xmlReader.Close(); validator.Close(); return false; } #region Load details of this product if requested by the user //Check if the user wants details about the current PRODUCT if ((productName != "") && (cfProducts[cfProducts.Count -1].ToString() == productName)) { productDetails = new ProductDetails(); //Add the product name productDetails.Name = productName; //Get the Versioning information productDetails.MajorVersion = nav.GetAttribute ("MajorVersion", ""); productDetails.MinorVersion = nav.GetAttribute ("MinorVersion", ""); productDetails.BuildNumber = nav.GetAttribute ("BuildNumber", ""); //Get the other product details if (nav.HasChildren) { nav.MoveToFirstChild(); bool moreProductDetails = true; bool moreChildren = true; while (moreProductDetails) { switch (nav.Name.ToUpper()) { case "SOURCECONTROL": //Move into the children of the SOURCECONTROL node moreChildren= nav.MoveToFirstChild(); if (!moreChildren) break; while (moreChildren) { if (productDetails.SourceControlInfo== null) productDetails.SourceControlInfo = new ArrayList(); SourceControlInfo scInfo = new SourceControlInfo(); scInfo.Executable = nav.Value; scInfo.Arguments = nav.GetAttribute ("Args", ""); productDetails.SourceControlInfo.Add (scInfo); moreChildren = nav.MoveToNext(); } //Move back to the SOURCECONTROL node nav.MoveToParent(); break; case "PATHS": //Move into the children of the PATHS node moreChildren = nav.MoveToFirstChild(); if (!moreChildren) break; while (moreChildren) { switch (nav.Name.ToUpper()) { case "BUILDS_LOCATION": productDetails.BuildsLocation = nav.Value; break; case "LOGS_LOCATION": productDetails.LogsLocation = nav.Value; break; case "DEVENV_FILE": productDetails.DevenvFile = nav.Value; break; case "INSTALLSHIELD_SCRIPT": productDetails.InstallShieldScript = nav.Value; break; case "INSTALLSHIELD_OUTPUT": productDetails.InstallShieldOutput = nav.Value; break; default: break; } moreChildren = nav.MoveToNext(); } //Move back to the PATHS node nav.MoveToParent(); break; case "BUILDMAIL": //Move to the children of the BUILDMAIL node moreChildren = nav.MoveToFirstChild(); if (!moreChildren) break; while (moreChildren) { switch (nav.Name.ToUpper()) { case "RECIPIENTS": productDetails.MailRecipients = nav.Value; break; case "MAILSENDER": productDetails.MailSenderAddress = nav.GetAttribute ("Address", ""); productDetails.MailServerAddress = nav.GetAttribute ("SmtpServer", ""); break; default: break; } moreChildren = nav.MoveToNext(); } //Move back to the BUILDMAIL node nav.MoveToParent(); break; case "BUILD_SOLUTIONS": //Move to the children of the BUILD_SOLUTIONS node moreChildren = nav.MoveToFirstChild(); if (!moreChildren) break; while (moreChildren) { switch (nav.Name.ToUpper()) { case "SOLUTION": if (productDetails.SolutionInfo == null) productDetails.SolutionInfo = new ArrayList(); SolutionInfo solInfo = new SolutionInfo(); solInfo.SolutionPath = nav.Value; solInfo.ProjectName = nav.GetAttribute ("Project", ""); solInfo.BuildConfig = nav.GetAttribute ("BuildConfig", ""); productDetails.SolutionInfo.Add (solInfo); break; default: break; } moreChildren = nav.MoveToNext(); } //Move back to the BUILD_SOLUTIONS node nav.MoveToParent(); break; default: break; } moreProductDetails = nav.MoveToNext(); } } } #endregion hasMoreProducts = nav.MoveToNext(); } #endregion xmlReader.Close(); validator.Close(); return true; }
public override async Task <bool> HasScmInfo() { SourceControlInfo info = await this.GetScmInfo(); return(!string.IsNullOrWhiteSpace(info.token) && !string.IsNullOrWhiteSpace(info.refreshToken)); }