public static async Task <string> updateStack([ActivityTrigger] UpdateBaseImageRequest request, ILogger log) { SecretsUtils _secretsUtils = new SecretsUtils(); await _secretsUtils.GetSecrets(); GitHubUtils _githubUtils = new GitHubUtils(_secretsUtils._gitToken); String timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss"); String random = new Random().Next(0, 9999).ToString(); String prname = String.Format("blimp{0}{1}", timeStamp, random); String parent = String.Format("D:\\local\\Temp\\blimp{0}{1}", timeStamp, random); String repoUrl = String.Format("https://github.com/Azure-App-Service/{0}-template.git", request.stack); String repoName = String.Format("{0}-template", request.stack); _githubUtils.CreateDir(parent); String localTemplateRepoPath = String.Format("{0}\\{1}", parent, repoName); _githubUtils.Clone(repoUrl, localTemplateRepoPath, "dev"); _githubUtils.Checkout(localTemplateRepoPath, prname); // edit the configs await updateConfig(String.Format("{0}\\{1}", localTemplateRepoPath, "blessedImageConfig-dev.json"), request.NewBaseImage); await updateConfig(String.Format("{0}\\{1}", localTemplateRepoPath, "blessedImageConfig-master.json"), request.NewBaseImage); await updateConfig(String.Format("{0}\\{1}", localTemplateRepoPath, "blessedImageConfig-temp.json"), request.NewBaseImage); await updateConfig(String.Format("{0}\\{1}", localTemplateRepoPath, "blessedImageConfig-save.json"), request.NewBaseImage); _githubUtils.Stage(localTemplateRepoPath, "*"); _githubUtils.CommitAndPush(localTemplateRepoPath, prname, String.Format("[blimp] new base image {0}", request.NewBaseImage)); String pullRequestURL = String.Format("https://api.github.com/repos/{0}/{1}-template/pulls?access_token={2}", "azure-app-service", request.stack, _secretsUtils._gitToken); String body = "{ " + "\"title\": " + JsonConvert.SerializeObject("[blimp] Update Base Image") + ", " + "\"body\": " + JsonConvert.SerializeObject("[blimp] auto generated Update Base Image") + ", " + "\"head\": " + JsonConvert.SerializeObject("azure-app-service:" + prname) + ", " + "\"base\": " + JsonConvert.SerializeObject("dev") + "}"; HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("patricklee2"); HttpResponseMessage response = null; response = await httpClient.PostAsync(pullRequestURL, new StringContent(body)); // fails on empty commits String result = await response.Content.ReadAsStringAsync(); System.Console.WriteLine(response.ToString()); System.Console.WriteLine(result); //if (response.StatusCode == HttpStatusCode.UnprocessableEntity) //{ // System.Console.WriteLine("Unable to make PR due to no differnce"); //} _githubUtils.gitDispose(localTemplateRepoPath); _githubUtils.Delete(parent); return(""); }
public static void rubyBase(List <Repo> repos, String root, String upstream) { Repository repo = new Repository(upstream); foreach (Repo r in repos) { // pull temps String dest = root + "\\" + r.name; Repository.Clone(r.clone_url, dest, new CloneOptions { BranchName = "master" }); // move String version = r.name.ToLower().Replace("rubybase-", ""); GitHubUtils githubUtils = new GitHubUtils("fake"); githubUtils.Delete(upstream + "\\base_images\\" + version, skipGit: true); githubUtils.DeepCopy(dest, upstream + "\\base_images\\" + version); // stage Commands.Stage(repo, upstream + "\\base_images\\" + version); } }
public static async void createPR(String stack) { String _gitToken = File.ReadAllText("../../../gitToken.txt"); // clone master String timeStamp = DateTime.Now.ToString("yyyyMMddHHmmss"); String root = String.Format("D:\\local\\temp\\blimpPR{0}", timeStamp); String upstream = root + "\\" + stack; String upstreamURL = String.Format("https://github.com/Azure-App-Service/{0}.git", stack); String branch = String.Format("blimp{0}", timeStamp); Repository.Clone(upstreamURL, upstream, new CloneOptions { BranchName = "dev" }); // branch Repository repo = new Repository(upstream); repo.CreateBranch(branch); Commands.Checkout(repo, branch); // list temp repos HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("patricklee2"); HttpResponseMessage response = null; List <Repo> resultList = new List <Repo>(); List <Repo> stackRepos = null; int run = 0; while (true) { String generatedReposURL = String.Format("https://api.github.com/orgs/{0}/repos?page={1}&per_page=30&sort=full_name&direction=asc", "blessedimagepipeline", run); response = await httpClient.GetAsync(generatedReposURL); response.EnsureSuccessStatusCode(); string contentString = await response.Content.ReadAsStringAsync(); List <Repo> l = JsonConvert.DeserializeObject <List <Repo> >(contentString); resultList.AddRange(l); run++; if (l.Count < 30) { break; } } switch (stack) { case "dotnetcore": stackRepos = resultList.FindAll(isDotnetcoreRepo); break; case "node": stackRepos = resultList.FindAll(isNodeRepo); break; case "php": stackRepos = resultList.FindAll(isPhpRepo); break; case "python": stackRepos = resultList.FindAll(isPythonRepo); break; case "ruby": stackRepos = resultList.FindAll(isRubyRepo); rubyBase(resultList.FindAll(isRubyBaseRepo), root, upstream); break; } // List<Repo> stackRepos = resultList.FindAll(isStackRepo(stack)); foreach (Repo r in stackRepos) { try { Console.WriteLine("copying " + r.full_name); // pull temps String dest = root + "\\" + r.name; Repository.Clone(r.clone_url, dest, new CloneOptions { BranchName = "dev" }); // move String version = r.name.ToLower().Replace(stack + "-", ""); String suffix = ""; if (stack.Equals("php")) { suffix = "-apache"; } GitHubUtils githubUtils = new GitHubUtils("fake"); githubUtils.Delete(upstream + "\\" + version + suffix, skipGit: true); githubUtils.DeepCopy(dest, upstream + "\\" + version + suffix); // stage Commands.Stage(repo, upstream + "\\" + version + suffix); } catch (LibGit2Sharp.NameConflictException e) { } catch (Exception e) { Console.WriteLine(e.ToString()); } } // git commit // Create the committer's signature and commit //_log.Info("git commit"); Signature author = new Signature("blimp", "*****@*****.**", DateTime.Now); Signature committer = author; // Commit to the repository try { Commit commit = repo.Commit("blimp", author, committer); } catch (Exception e) { //_log.info("Empty commit"); } Remote remote = repo.Network.Remotes.Add("upstream", upstreamURL); repo.Branches.Update(repo.Head, b => b.Remote = remote.Name, b => b.UpstreamBranch = repo.Head.CanonicalName); // git push //_log.Info("git push"); LibGit2Sharp.PushOptions options = new LibGit2Sharp.PushOptions(); options.CredentialsProvider = new CredentialsHandler( (url, usernameFromUrl, types) => new UsernamePasswordCredentials() { Username = _gitToken.Trim(), Password = String.Empty }); repo.Network.Push(repo.Branches[branch], options); // fails if branch already exists //create PR String pullRequestURL = String.Format("https://api.github.com/repos/{0}/{1}/pulls?access_token={2}", "azure-app-service", stack, _gitToken); String body = "{ " + "\"title\": " + JsonConvert.SerializeObject("sync from templates") + ", " + "\"body\": " + JsonConvert.SerializeObject("sync from templates") + ", " + "\"head\": " + JsonConvert.SerializeObject("azure-app-service:" + branch) + ", " + "\"base\": " + JsonConvert.SerializeObject("dev") + "}"; response = await httpClient.PostAsync(pullRequestURL, new StringContent(body)); // fails on empty commits String result = await response.Content.ReadAsStringAsync(); System.Console.WriteLine(response.ToString()); System.Console.WriteLine(result); if (response.StatusCode == HttpStatusCode.UnprocessableEntity) { System.Console.WriteLine("Unable to make PR due to no differnce"); } //cleanup //new DirectoryInfo(root).Delete(true); }