Create a gist now

Instantly share code, notes, and snippets.

@edsnider /Function.cs Secret
Last active Oct 26, 2017

What would you like to do?
Mobile Center webhook triggered Azure Function
using System.Net;
using System.Text.RegularExpressions;
static readonly HttpClient _httpClient = new HttpClient();
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
var githubApiToken = "";
var githubApiUserAgent = "";
var trelloApiToken = "";
var trelloApiKey = "";
var content = await req.Content.ReadAsStringAsync();
log.Info($"Content: {content}");
// Read app build data coming from Mobile Center
dynamic data = await req.Content.ReadAsAsync<object>();
string releaseNotes = data?.release_notes;
string platform = data?.platform;
string version = data?.short_version;
string build = data?.version;
if (string.IsNullOrWhiteSpace(releaseNotes))
{
return req.CreateResponse(HttpStatusCode.BadRequest, "No release notes specified.");
}
// Find the PR # from the release notes
var prNumber = Regex.Match(releaseNotes, "(Merge pull request #)([0-9]+)").Groups[2];
if (!prNumber.Success)
{
return req.CreateResponse(HttpStatusCode.BadRequest, "No PR number found in release notes.");
}
log.Info($"Pull request: {prNumber}");
// Get PR details from GitHub
var prUrl = $"https://api.github.com/repos/edsnider/sampleapp/pulls/{prNumber}";
var prRequest = new HttpRequestMessage(HttpMethod.Get, prUrl);
prRequest.Headers.Add("Authorization", $"token {githubApiToken}");
prRequest.Headers.Add("User-Agent", $"{githubApiUserAgent}");
var prResponse = await _httpClient.SendAsync(prRequest);
if (!prResponse.IsSuccessStatusCode)
{
return req.CreateResponse(HttpStatusCode.BadRequest, "Failed to get PR from GitHub.");
}
// Read PR data coming back from GitHub
dynamic pr = await prResponse.Content.ReadAsAsync<object>();
string prBody = pr?.body;
if (string.IsNullOrWhiteSpace(prBody))
{
return req.CreateResponse(HttpStatusCode.BadRequest, "No PR body.");
}
log.Info($"PR Body: {prBody}");
// Find the Trello card from the PR body
var cardId = Regex.Match(prBody, "trello\\.com\\/c\\/(.{8})").Groups[1];
if (!cardId.Success)
{
return req.CreateResponse(HttpStatusCode.BadRequest, "No Trello card Id found in PR.");
}
log.Info($"card id: {cardId}");
// Post app build details to Trello card
var cardComments = $"{platform} app build {version} ({build})";
var cardCommentsUrl = $"https://api.trello.com/1/cards/{cardId}/actions/comments?text={cardComments}&key={trelloApiKey}&token={trelloApiToken}";
var cardCommentsRequest = new HttpRequestMessage(HttpMethod.Post, cardCommentsUrl);
var cardCommentsResponse = await _httpClient.SendAsync(cardCommentsRequest);
if (!cardCommentsResponse.IsSuccessStatusCode)
{
return req.CreateResponse(HttpStatusCode.BadRequest, "Failed to post comments to Trello card.");
}
return req.CreateResponse(HttpStatusCode.OK, "Trello card updated with app build info.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment