Skip to content

Commit a80a1e7

Browse files
committed
add DeleteBranchFunction (fixes #82)
1 parent 82ac9a6 commit a80a1e7

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed

.vscode/tasks.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,20 @@
121121
"reveal": "always"
122122
},
123123
"problemMatcher": "$func-watch"
124+
},
125+
{
126+
"label": "Run DeleteBranchFunction",
127+
"type": "shell",
128+
"dependsOn": "build",
129+
"options": {
130+
"cwd": "${workspaceFolder}/DeleteBranchFunction/bin/Debug/netstandard2.0"
131+
},
132+
"command": "func host start",
133+
"isBackground": true,
134+
"presentation": {
135+
"reveal": "always"
136+
},
137+
"problemMatcher": "$func-watch"
124138
}
125139
]
126140
}
127-
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Common.Messages
2+
{
3+
public class DeleteBranchMessage
4+
{
5+
public int InstallationId { get; set; }
6+
7+
public string RepoName { get; set; }
8+
9+
public string Owner { get; set; }
10+
11+
public string CloneUrl { get; set; }
12+
}
13+
}

DeleteBranchFunction/DeleteBranch.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading.Tasks;
4+
using Common;
5+
using Common.Messages;
6+
using Common.TableModels;
7+
using Install;
8+
using Microsoft.Azure.WebJobs;
9+
using Microsoft.Extensions.Logging;
10+
using Octokit.Internal;
11+
12+
namespace DeleteBranchFunction
13+
{
14+
public static class DeleteBranch
15+
{
16+
[Singleton("{InstallationId}")] // https://github.com/Azure/azure-webjobs-sdk/wiki/Singleton#scenarios
17+
[FunctionName("DeleteBranch")]
18+
public static async Task Trigger(
19+
[QueueTrigger("deletebranchmessage")]DeleteBranchMessage deleteBranchMessage,
20+
[Table("installation", "{InstallationId}", "{RepoName}")] Installation installation,
21+
ILogger logger,
22+
ExecutionContext context)
23+
{
24+
var installationTokenProvider = new InstallationTokenProvider();
25+
await RunAsync(deleteBranchMessage, installation, installationTokenProvider, logger, context).ConfigureAwait(false);
26+
}
27+
28+
public static async Task RunAsync(
29+
DeleteBranchMessage deleteBranchMessage,
30+
Installation installation,
31+
IInstallationTokenProvider installationTokenProvider,
32+
ILogger logger,
33+
ExecutionContext context)
34+
{
35+
if (installation == null)
36+
{
37+
logger.LogError("No installation found for {InstallationId}", deleteBranchMessage.InstallationId);
38+
throw new Exception($"No installation found for InstallationId: {deleteBranchMessage.InstallationId}");
39+
}
40+
41+
var installationToken = await installationTokenProvider.GenerateAsync(
42+
new InstallationTokenParameters
43+
{
44+
AccessTokensUrl = string.Format(KnownGitHubs.AccessTokensUrlFormat, installation.InstallationId),
45+
AppId = KnownGitHubs.AppId,
46+
},
47+
File.OpenText(Path.Combine(context.FunctionDirectory, $"../{KnownGitHubs.AppPrivateKey}")));
48+
49+
logger.LogInformation("DeleteBranchFunction: Deleting imgbot branch for {Owner}/{RepoName}", installation.Owner, installation.RepoName);
50+
51+
var inMemoryCredentialStore = new InMemoryCredentialStore(new Octokit.Credentials(KnownGitHubs.Username, installationToken.Token));
52+
var githubClient = new Octokit.GitHubClient(new Octokit.ProductHeaderValue("ImgBot"), inMemoryCredentialStore);
53+
54+
var imgbotRefName = $"heads/{KnownGitHubs.BranchName}";
55+
Octokit.Reference imgbotBranchRef = null;
56+
try
57+
{
58+
imgbotBranchRef = await githubClient.Git.Reference.Get(installation.Owner, installation.RepoName, imgbotRefName);
59+
}
60+
catch
61+
{
62+
}
63+
64+
if (imgbotBranchRef == null)
65+
{
66+
logger.LogInformation("DeleteBranchFunction: No imgbot branch found for {Owner}/{RepoName}", installation.Owner, installation.RepoName);
67+
return;
68+
}
69+
70+
await githubClient.Git.Reference.Delete(installation.Owner, installation.RepoName, imgbotRefName);
71+
logger.LogInformation("DeleteBranchFunction: Successfully deleted imgbot branch for {Owner}/{RepoName}", installation.Owner, installation.RepoName);
72+
}
73+
}
74+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>netstandard2.0</TargetFramework>
4+
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Octokit" Version="0.32.0" />
9+
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
10+
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
11+
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="host.json">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="local.settings.json">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="imgbot.2017-08-23.private-key.pem">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\Install\Install.csproj" />
28+
<ProjectReference Include="..\Common\Common.csproj" />
29+
</ItemGroup>
30+
31+
<Import Project="../ImgBot.targets" />
32+
33+
</Project>

DeleteBranchFunction/host.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "2.0"
3+
}

ImgBot.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 2012
44
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenPrFunction", "OpenPrFunction\OpenPrFunction.csproj", "{5A6476B2-18FC-4A93-8DA9-E4A667662EC5}"
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Auth", "Auth\Auth.csproj",
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketplaceSyncFunction", "MarketplaceSyncFunction\MarketplaceSyncFunction.csproj", "{31FB826F-41D7-4D06-B0DB-99F5DF3519DF}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeleteBranchFunction", "DeleteBranchFunction\DeleteBranchFunction.csproj", "{B2E354C4-067D-40E9-A957-92FFFEB2E856}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -67,5 +69,9 @@ Global
6769
{31FB826F-41D7-4D06-B0DB-99F5DF3519DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
6870
{31FB826F-41D7-4D06-B0DB-99F5DF3519DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
6971
{31FB826F-41D7-4D06-B0DB-99F5DF3519DF}.Release|Any CPU.Build.0 = Release|Any CPU
72+
{B2E354C4-067D-40E9-A957-92FFFEB2E856}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
73+
{B2E354C4-067D-40E9-A957-92FFFEB2E856}.Debug|Any CPU.Build.0 = Debug|Any CPU
74+
{B2E354C4-067D-40E9-A957-92FFFEB2E856}.Release|Any CPU.ActiveCfg = Release|Any CPU
75+
{B2E354C4-067D-40E9-A957-92FFFEB2E856}.Release|Any CPU.Build.0 = Release|Any CPU
7076
EndGlobalSection
7177
EndGlobal

0 commit comments

Comments
 (0)