public static string ComposeLocalizationRepoName(string repoName, string locale, Dictionary <string, LocRepoFromAmbientConfiguration> format) { LocRepoFromAmbientConfiguration value = format[repoName]; if (value.targetRepo == null) { return(null); } LocRepo targetRepo = value.targetRepo; if (string.IsNullOrEmpty(targetRepo.Name)) { return(repoName + "." + locale); } else { locale = locale.Replace("-", ""); return(targetRepo.Name.Replace("<target_locale_without_dash>", locale)); } }
protected override void Load(object obj) { if (obj == null) { return; } List <GitHubRepository> repos = obj as List <GitHubRepository>; Dictionary <string, LocRepoFromAmbientConfiguration> repoInfo = Util.GetRepoInfoFromAmbientConfiguration(); using (DataTable dt = new DataTable(), dt1 = new DataTable()) { dt.Columns.Add("RepositoryId"); dt.Columns.Add("GitRepositoryAccount"); dt.Columns.Add("GitRepositoryName"); dt.Columns.Add("GitRepositoryUrl"); dt.Columns.Add("CreatedAt"); dt.Columns.Add("Repository"); dt.Columns.Add("IsLocalization"); dt.Columns.Add("Locale"); foreach (GitHubRepository repo in repos) { int isLocalization = 1; string repository = "", locale = ""; string[] localeInfo = Util.GetLocalizationInfo(repo.RepositoryName, repoInfo); if (localeInfo != null && localeInfo.Length == 2 && !string.IsNullOrEmpty(localeInfo[0]) && !string.IsNullOrEmpty(localeInfo[1])) { repository = localeInfo[0]; locale = localeInfo[1]; } if (locale.Equals("en-us")) { isLocalization = 0; repo.IsLocalization = false; } if (locale.Length == 4) { locale = locale.Insert(2, "-"); } dt.Rows.Add(repo.PartitionKey, repo.Owner, repo.RepositoryName, repo.RepositoryUrl, repo.Timestamp, repository, isLocalization, locale); } //Add localization repositories that don't have en-us repository. dt1.Columns.Add("RepositoryId"); dt1.Columns.Add("GitRepositoryAccount"); dt1.Columns.Add("GitRepositoryName"); dt1.Columns.Add("GitRepositoryUrl"); dt1.Columns.Add("CreatedAt"); dt1.Columns.Add("Repository"); dt1.Columns.Add("IsLocalization"); dt1.Columns.Add("Locale"); string token = Util.GetGithubToken("MIMDocs-pr.cs-cz"); string[] repositoryException = configManager.GetConfig("BackendJobs", "RepositoryException").Split(';'); InsightDBHelper.InsightDBHelper.ConnectDBWithConnectString(OPSDataSyncConnStr); DataRow[] dataRow = InsightDBHelper.InsightDBHelper.ExecuteQuery( string.Format("SELECT DISTINCT GitRepositoryName FROM OPS_Repositories_LocOnly WITH(NOLOCK)"), true); HashSet <string> locRepoSet = new HashSet <string>(); if (dataRow != null && dataRow.Length > 0) { foreach (DataRow row in dataRow) { locRepoSet.Add(row.ItemArray[0].ToString()); } } foreach (string repo in repositoryException) { LocRepoFromAmbientConfiguration value = repoInfo[repo]; if (value.sourceRepo == null || value.targetRepo == null) { continue; } LocRepo sourceRepo = value.sourceRepo; LocRepo targetRepo = value.targetRepo; string[] items = Util.GetLocalizationList(repo, "master", token, repoInfo); if (items == null || items.Length == 0) { continue; } foreach (string locale in items) { string localizatonRepoName = Util.ComposeLocalizationRepoName(repo, locale, repoInfo); if (locRepoSet.Count == 0 || !locRepoSet.Contains(localizatonRepoName)) { string repoId = System.Guid.NewGuid().ToString("D"); string remoteUrl = sourceRepo.RemoteUrl; string repoUrl = remoteUrl.Substring(0, remoteUrl.LastIndexOf("/")) + "/" + localizatonRepoName; dt1.Rows.Add(repoId, targetRepo.Owner, localizatonRepoName, repoUrl, "", repo, 1, locale); } } } InsightDBHelper.InsightDBHelper.ConnectDBWithConnectString(OPSDataSyncConnStr); Dictionary <string, DataTable> paramDic1 = new Dictionary <string, DataTable>(); paramDic1.Add("GitRepoTableParam?GitRepoType", dt1); InsightDBHelper.InsightDBHelper.ExecuteSP("SP_OPSStoreGitRepoLocOnly", paramDic1); Dictionary <string, DataTable> paramDic = new Dictionary <string, DataTable>(); paramDic.Add("GitRepoTableParam?GitRepoType", dt); InsightDBHelper.InsightDBHelper.ExecuteSP("SP_OPSStoreGitRepo", paramDic, true, 600); } // Get prod repo InsightDBHelper.InsightDBHelper.ConnectDBWithConnectString(OPSDataSyncConnStr); DataRow[] prodRepoRow = InsightDBHelper.InsightDBHelper.ExecuteQuery("SELECT DISTINCT RepositoryId FROM OPS_Repositories_live WITH (NOLOCK)", true); if (prodRepoRow == null || prodRepoRow.Length == 0) { return; } List <DataRow> prodRepoList = prodRepoRow.ToList(); List <GitHubRepository> prod_all_tmp = repos.Where(v => prodRepoList.Exists(t => t.ItemArray[0].ToString().Equals(v.PartitionKey, StringComparison.OrdinalIgnoreCase))).ToList(); string testRepositoryIdString = configManager.GetConfig("BackendJobs", "TestRepositoryId"); string[] testIdList = testRepositoryIdString.Split(';'); //Add two test repositoryIds prod_all_tmp.AddRange(repos.Where(v => Array.Exists(testIdList, t => t.Equals(v.PartitionKey, StringComparison.OrdinalIgnoreCase))).ToList()); SharedObject_Prod_All = prod_all_tmp; SharedObject_Prod_GitHub = prod_all_tmp.Where(v => v.GitRepositoryType.Equals("GitHub", StringComparison.OrdinalIgnoreCase)).ToList(); SharedObject_Prod_VSO = prod_all_tmp.Where(v => v.GitRepositoryType.Equals("Vso", StringComparison.OrdinalIgnoreCase)).ToList(); return; }
public static string[] GetLocalizationList(string repoName, string branch, string token, Dictionary <string, LocRepoFromAmbientConfiguration> format) { LocRepoFromAmbientConfiguration value = format[repoName]; if (value.sourceRepo == null || value.handbackRepo == null) { return(null); } LocRepo sourceRepo = value.sourceRepo; LocRepo handbackRepo = value.handbackRepo; //root folder of source repo string localConfigUrl = "https://raw.githubusercontent.com/" + sourceRepo.Owner + "/" + repoName + "/" + branch + "/.localization-config"; JavaScriptSerializer j = new JavaScriptSerializer(); Regex reg = new Regex("\"[a-z]{2}-[a-z]{2}\""); if (repoName.Equals("smbmadeira-content-pr", StringComparison.OrdinalIgnoreCase)) { reg = new Regex("[a-z]{2}-[A-Z]{2}"); } try { string jsonContent = Util.GetJsonContent(localConfigUrl, token); //Many errors in json files of different repos, here we directly parser the string. MatchCollection mc = reg.Matches(jsonContent); string[] ret = new string[mc.Count]; for (int i = 0; i < ret.Length; i++) { ret[i] = mc[i].Value.Substring(1, 5); } return(ret.Distinct().ToArray()); } catch (Exception ex) { if (ex.Message.Contains("404")) { try { //ol-config/<source-repo-owner>/<source-repo-name>/<source-repo-branch>/.localization-config localConfigUrl = "https://raw.githubusercontent.com/" + handbackRepo.Owner + "/" + handbackRepo.Name + "/master/ol-config/" + sourceRepo.Owner + "/" + repoName + "/" + branch + "/.localization-config"; string jsonContent = Util.GetJsonContent(localConfigUrl, token); //Many errors in json files of different repos, here we directly parser the string. MatchCollection mc = reg.Matches(jsonContent); string[] ret = new string[mc.Count]; for (int i = 0; i < ret.Length; i++) { if (repoName.Equals("smbmadeira-content-pr", StringComparison.OrdinalIgnoreCase)) { ret[i] = mc[i].Value.ToLower(); } else { ret[i] = mc[i].Value.Substring(1, 5); } } return(ret.Distinct().ToArray()); } catch (Exception) { return(null); } } throw ex; } }
private List <GitRepoTopicLocalizationStatus> ParseOLData(string repoId, string repoName, string branch, string locale, string token, LocRepoFromAmbientConfiguration value) { List <GitRepoTopicLocalizationStatus> ret = new List <GitRepoTopicLocalizationStatus>(); LocRepo targetRepo = value.targetRepo; LocRepo handbackRepo = value.handbackRepo; JavaScriptSerializer j = new JavaScriptSerializer(); j.MaxJsonLength = int.MaxValue; string url = "https://raw.githubusercontent.com/" + targetRepo.Owner + "/" + repoName + "/" + branch + "/.translation-state"; try { string jsonContent = Util.GetJsonContent(url, token); var pair = j.DeserializeObject(jsonContent) as IDictionary <string, object>; ParseJsonContent(ret, pair, repoId, locale, branch); return(ret); //#region TO DO: Change to use get data API //// 1. Get all commits for repositoty //var modelSHAs = new List<string>(); //var commits = await github.Repository.Commits.GetAll(repository.Owner.Login, repository.Name); //foreach (var commit in commits) //{ // modelSHAs.Add(commit.Sha); //} //// 2. Get all references //var refs = github.GitDatabase.Reference.GetAll(repository.Owner.Login, repository.Name).Result; //if (null == refs || 0 == refs.Count) // return ret; //var currentRefSet = refs.Where(x => x.Ref.EndsWith(branch, StringComparison.OrdinalIgnoreCase)); //if (null == currentRefSet || 0 == currentRefSet.Count()) // return ret; //var currentRef = currentRefSet.ElementAt(0).Object.Sha; //// 3. Get content using get data API //// currentRef is not correct in this scenario. We must find the SHA of the blob. Actually, the SHA will be returned in API github.Repository.Content.GetAllContents(string, string, string, string). //var blobContent = github.GitDatabase.Blob.Get(repository.Owner.Login, repository.Name, currentRef).Result; //var convertedContents = System.Text.Encoding.Default.GetString(Convert.FromBase64String(blobContent.Content)); //#endregion } catch (Exception ex) { if (ex.Message.Contains("404")) { try { //ol-handback/<taget-repo-owner>/<target-repo-name>/<target-repo-branch>/olsysfiles/.translation-state url = "https://raw.githubusercontent.com/" + handbackRepo.Owner + "/" + handbackRepo.Name + "/" + branch + "/ol-handback/" + targetRepo.Owner + "/" + repoName + "/" + targetRepo.WorkingBranch + "/olsysfiles/.translation-state"; string jsonContent = Util.GetJsonContent(url, token); var pair = j.DeserializeObject(jsonContent) as IDictionary <string, object>; ParseJsonContent(ret, pair, repoId, locale, branch); return(ret); } catch (Exception innerEx) { if (innerEx.Message.Contains("404")) { return(ret); } throw innerEx; } } throw ex; } }