Skip to content

Commit

Permalink
check if imgbot branch exists when manually triggering
Browse files Browse the repository at this point in the history
  • Loading branch information
dabutvin committed Apr 12, 2020
1 parent 11a1a3f commit c1a81fa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 24 deletions.
41 changes: 37 additions & 4 deletions Auth/InstallationFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Auth.Extensions;
using Common;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.WindowsAzure.Storage;
Expand Down Expand Up @@ -185,6 +186,17 @@ public static async Task<HttpResponseMessage> RequestRepositoryCheckAsync(
throw new Exception("repository request mismatch");
}

var response = req.CreateResponse();
response.EnableCors();

var imgbotBranch = await GetImgbotBranch(repository, token);
if (imgbotBranch != null)
{
// branch already exists
response.SetJson(new { status = "branchexists" });
return response;
}

await routerQueue.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(new Common.Messages.RouterMessage
{
CloneUrl = repository.html_url,
Expand All @@ -193,10 +205,7 @@ await routerQueue.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObj
RepoName = repository.name,
})));

var response = req.CreateResponse();
response
.SetJson(new { status = "OK" })
.EnableCors();
response.SetJson(new { status = "OK" });
return response;
}

Expand Down Expand Up @@ -357,5 +366,29 @@ private static string ParseNextHeader(HttpResponseMessage response)

return repository;
}

private static async Task<Model.Branch> GetImgbotBranch(Model.Repository repository, string token)
{
Model.Branch imgbotBranch = null;

try
{
var imgbotBranchRequest = new HttpRequestMessage(HttpMethod.Get, repository.branches_url.Replace("{/branch}", $"/{KnownGitHubs.BranchName}"));
imgbotBranchRequest.Headers.Authorization = new AuthenticationHeaderValue("token", token);
imgbotBranchRequest.AddGithubHeaders();
var imgbotBranchResponse = await HttpClient.SendAsync(imgbotBranchRequest);
var imbotBranchJson = await imgbotBranchResponse.Content.ReadAsStringAsync();
imgbotBranch = JsonConvert.DeserializeObject<Model.Branch>(imbotBranchJson);
if (imgbotBranch.name != KnownGitHubs.BranchName)
{
return null;
}
}
catch
{
}

return imgbotBranch;
}
}
}
11 changes: 11 additions & 0 deletions Auth/Model/Branch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// <auto-generated/>

namespace Auth.Model
{
public class Branch
{
public string name { get; set; }
public bool @protected { get; set; }
public string protection_url { get; set; }
}
}
46 changes: 26 additions & 20 deletions Web/src/app/vue/components/Repository.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,33 +108,39 @@ export default {
var vm = this;
vm.checking = true;
axios
.get(
`${settings.authhost}/api/repositories/check/${vm.installationid}/${vm.current.id}`,
{
withCredentials: true
}
)
.then(() => {
var interval = setInterval(() => {
axios
.get(
`${settings.authhost}/api/repositories/${vm.installationid}/repository/${vm.current.id}`,
{
withCredentials: true
}
)
.then(response => {
if (
vm.current.lastchecked !==
response.data.repository.lastchecked
) {
clearInterval(interval);
vm.current = response.data.repository;
vm.checking = false;
}
});
}, 30000);
.then((checkResponse) => {
if (checkResponse.data.status === "branchexists") {
alert("The Imgbot branch already exists in this repo. If you want a new round of optimization please delete this branch and try again.");
vm.checking = false;
} else {
var interval = setInterval(() => {
axios
.get(
`${settings.authhost}/api/repositories/${vm.installationid}/repository/${vm.current.id}`,
{
withCredentials: true
}
)
.then(response => {
if (
vm.current.lastchecked !==
response.data.repository.lastchecked
) {
clearInterval(interval);
vm.current = response.data.repository;
vm.checking = false;
}
});
}, 30000);
}
});
},
loadSettings: function() {
Expand Down

0 comments on commit c1a81fa

Please sign in to comment.