private static async Task <Model.Repository> GetRepository(string installationid, string token, string repositoryid)
        {
            Model.Repository repository = null;
            string           next       = null;

            do
            {
                var url = $"https://api.github.com/user/installations/{installationid}/repositories";
                if (next != null)
                {
                    url += "?page=" + next;
                }

                var repositoriesRequest = new HttpRequestMessage(HttpMethod.Get, url);
                repositoriesRequest.Headers.Authorization = new AuthenticationHeaderValue("token", token);
                repositoriesRequest.AddGithubHeaders();
                var repositoriesResponse = await HttpClient.SendAsync(repositoriesRequest);

                next = ParseNextHeader(repositoriesResponse);
                var repositoriesJson = await repositoriesResponse.Content.ReadAsStringAsync();

                var repositoriesData = JsonConvert.DeserializeObject <Model.Repositories>(repositoriesJson);
                repository = repositoriesData.repositories.FirstOrDefault(x => x.id.ToString() == repositoryid);
            }while (repository == null && next != null);

            return(repository);
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        private static async Task <Model.Installations> GetInstallationsData(string token)
        {
            var installationsRequest = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/user/installations");

            installationsRequest.Headers.Authorization = new AuthenticationHeaderValue("token", token);
            installationsRequest.AddGithubHeaders();
            var installationsResponse = await HttpClient.SendAsync(installationsRequest);

            var installationsJson = await installationsResponse.Content.ReadAsStringAsync();

            return(JsonConvert.DeserializeObject <Model.Installations>(installationsJson));
        }
        public static async Task <HttpResponseMessage> ListAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "installations")] HttpRequestMessage req)
        {
            var token = req.ReadCookie("token");

            if (token == null)
            {
                throw new Exception("missing authentication");
            }

            var marketplaceTable     = GetTable("marketplace");
            var installationsRequest = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/user/installations");

            installationsRequest.Headers.Authorization = new AuthenticationHeaderValue("token", token);
            installationsRequest.AddGithubHeaders();
            var installationsResponse = await HttpClient.SendAsync(installationsRequest);

            var installationsJson = await installationsResponse.Content.ReadAsStringAsync();

            var installationsData = JsonConvert.DeserializeObject <Model.Installations>(installationsJson);

            var installations = await Task.WhenAll(installationsData.installations.Select(async x =>
            {
                var mktplc = await marketplaceTable.ExecuteAsync(
                    TableOperation.Retrieve <Common.TableModels.Marketplace>(x.account.id.ToString(), x.account.login));

                return(new
                {
                    x.id,
                    x.html_url,
                    x.account.login,
                    accountid = x.account.id,
                    x.account.avatar_url,
                    planId = (mktplc.Result as Common.TableModels.Marketplace)?.PlanId
                });
            }));

            var response = req.CreateResponse();

            response
            .SetJson(new { installations })
            .EnableCors();
            return(response);
        }
        public static async Task <HttpResponseMessage> ListRepositoriesAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "repositories/{installationid}/{page:int}")] HttpRequestMessage req,
            string installationid,
            int page)
        {
            var token = req.ReadCookie("token");

            if (token == null)
            {
                throw new Exception("missing authentication");
            }

            var installationTable = GetTable("installation");
            var url = $"https://api.github.com/user/installations/{installationid}/repositories";

            if (page > 1)
            {
                url += "?page=" + page;
            }

            var repositoriesRequest = new HttpRequestMessage(HttpMethod.Get, url);

            repositoriesRequest.Headers.Add("Authorization", "token " + token);
            repositoriesRequest.AddGithubHeaders();
            var repositoriesResponse = await HttpClient.SendAsync(repositoriesRequest);

            var repositoriesJson = await repositoriesResponse.Content.ReadAsStringAsync();

            var repositoriesData = JsonConvert.DeserializeObject <Model.Repositories>(repositoriesJson);

            var repositories = await Task.WhenAll(repositoriesData.repositories.Select(x =>
                                                                                       RepositoryResponse(x, installationTable, installationid)));

            var next     = ParseNextHeader(repositoriesResponse);
            var response = req.CreateResponse();

            response
            .SetJson(new { repositories, next })
            .EnableCors();
            return(response);
        }