/// <summary> /// Create a new branch in a repository /// </summary> /// <param name="repoUri">Repo to create a branch in</param> /// <param name="newBranch">New branch name</param> /// <param name="baseBranch">Base of new branch</param> /// <returns></returns> public async Task CreateBranchAsync(string repoUri, string newBranch, string baseBranch) { _logger.LogInformation( $"Verifying if '{newBranch}' branch exist in repo '{repoUri}'. If not, we'll create it..."); (string owner, string repo) = ParseRepoUri(repoUri); string latestSha = await GetLastCommitShaAsync(owner, repo, baseBranch); string body; string gitRef = $"refs/heads/{newBranch}"; var githubRef = new GitHubRef(gitRef, latestSha); try { // If this succeeds, then the branch exists and we should // update the branch to latest. using (await this.ExecuteRemoteGitCommandAsync( HttpMethod.Get, $"repos/{owner}/{repo}/branches/{newBranch}", _logger, retryCount: 0)) { } githubRef.Force = true; body = JsonConvert.SerializeObject(githubRef, _serializerSettings); using (await this.ExecuteRemoteGitCommandAsync( new HttpMethod("PATCH"), $"repos/{owner}/{repo}/git/{gitRef}", _logger, body)) { } _logger.LogInformation($"Branch '{newBranch}' exists, updated"); } catch (HttpRequestException exc) when(exc.Message.Contains(((int)HttpStatusCode.NotFound).ToString())) { _logger.LogInformation($"'{newBranch}' branch doesn't exist. Creating it..."); body = JsonConvert.SerializeObject(githubRef, _serializerSettings); using (await this.ExecuteRemoteGitCommandAsync( HttpMethod.Post, $"repos/{owner}/{repo}/git/refs", _logger, body)) { } _logger.LogInformation($"Branch '{newBranch}' created in repo '{repoUri}'!"); return; } catch (HttpRequestException exc) { _logger.LogError( $"Checking if '{newBranch}' branch existed in repo '{repoUri}' failed with '{exc.Message}'"); throw; } }
public async Task CreateBranchAsync(string repoUri, string newBranch, string baseBranch) { _logger.LogInformation( $"Verifying if '{newBranch}' branch exist in repo '{repoUri}'. If not, we'll create it..."); string ownerAndRepo = GetOwnerAndRepoFromRepoUri(repoUri); string latestSha = await GetLastCommitShaAsync(ownerAndRepo, baseBranch); string body; string gitRef = $"refs/heads/{newBranch}"; var githubRef = new GitHubRef(gitRef, latestSha); HttpResponseMessage response = null; try { response = await this.ExecuteGitCommand( HttpMethod.Get, $"repos/{ownerAndRepo}/branches/{newBranch}", _logger); githubRef.Force = true; body = JsonConvert.SerializeObject(githubRef, _serializerSettings); await this.ExecuteGitCommand( new HttpMethod("PATCH"), $"repos/{ownerAndRepo}/git/{gitRef}", _logger, body); } catch (HttpRequestException exc) when(exc.Message.Contains(((int)HttpStatusCode.NotFound).ToString())) { _logger.LogInformation($"'{newBranch}' branch doesn't exist. Creating it..."); body = JsonConvert.SerializeObject(githubRef, _serializerSettings); response = await this.ExecuteGitCommand( HttpMethod.Post, $"repos/{ownerAndRepo}/git/refs", _logger, body); _logger.LogInformation($"Branch '{newBranch}' created in repo '{repoUri}'!"); return; } catch (HttpRequestException exc) { _logger.LogError( $"Checking if '{newBranch}' branch existed in repo '{repoUri}' failed with '{exc.Message}'"); throw; } _logger.LogInformation($"Branch '{newBranch}' exists."); }