Skip to content

Instantly share code, notes, and snippets.

@tyconsulting
Last active April 17, 2022 18:47
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyconsulting/f8de503de3df164a6163a3299656d516 to your computer and use it in GitHub Desktop.
Save tyconsulting/f8de503de3df164a6163a3299656d516 to your computer and use it in GitHub Desktop.
Azure Function that fetch file content from private GitHub repository - http://blog.tyang.org/2017/05/19/deploying-arm-templates-with-artifacts-located-in-a-private-github-repository/
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.IO;
using System.Text;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger proxy function processed a request. RequestUri={req.RequestUri}");
// parse query parameter
string GitHubUri = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "githuburi", true) == 0)
.Value;
string GitHubAccessToken = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "githubaccesstoken", true) == 0)
.Value;
log.Info($"GitHubPrivateRepoFileFecher function is trying to get file content from {GitHubUri}");
Encoding outputencoding = Encoding.GetEncoding("ASCII");
var ProxyResponse = new HttpResponseMessage();
HttpStatusCode statuscode = new HttpStatusCode();
if (GitHubUri == null)
{
statuscode = HttpStatusCode.BadRequest;
StringContent errorcontent =new StringContent("Please pass the GitHub raw file content URI (raw.githubusercontent.com) in the request URI string", outputencoding);
ProxyResponse = req.CreateResponse(statuscode, errorcontent);
ProxyResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
ProxyResponse.Content = errorcontent;
} else if (GitHubAccessToken == null) {
statuscode = HttpStatusCode.BadRequest;
StringContent errorcontent =new StringContent("Please pass the GitHub personal access token in the request URI string", outputencoding);
ProxyResponse = req.CreateResponse(statuscode, errorcontent);
ProxyResponse.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
ProxyResponse.Content = errorcontent;
} else {
string strAuthHeader = "token " + GitHubAccessToken;
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3.raw");
client.DefaultRequestHeaders.Add("Authorization", strAuthHeader);
HttpResponseMessage response = await client.GetAsync(GitHubUri);
ProxyResponse = response;
}
return ProxyResponse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment