Beispiel #1
0
        public async Task <IActionResult> PostWebook([FromHeader(Name = "X-GitHub-Event")] string @event
                                                     , [FromHeader(Name = "X-GitHub-Delivery")] string delivery
                                                     , [FromHeader(Name = "X-Hub-Signature")] string signature
                                                     , [FromServices] IAppGithubExporterCli cli, [FromServices] ILoggingService remoteLogger)
        {
            byte[] body;
            string json;

            await using (var ms = new MemoryStream())
            {
                await Request.Body.CopyToAsync(ms);

                body = ms.ToArray();
                json = Encoding.UTF8.GetString(body);
            }
            var secret = _configuration.GetValue <string>("GithubApp:WebhookEventSecret");

            if (null != secret)
            {
                HMACSHA1 hmac = new HMACSHA1(Encoding.ASCII.GetBytes(secret));
                string   calculatedSignature =
                    $"sha1={string.Concat(hmac.ComputeHash(body).Select(x => x.ToString("x2")))}".ToLower();
                if (signature?.ToLower() != calculatedSignature)
                {
                    return(new BadRequestObjectResult("Signature mismatch"));
                }
            }

            var serializer = new Octokit.Internal.SimpleJsonSerializer();

            _logger.LogInformation("{event} content: {json}", @event.ToLower(), json);
            switch (@event?.ToLower())
            {
            case "ping":
                var ping = serializer.Deserialize <PingEvent>(json);
                _logger.LogInformation($"PINGed by {ping?.Sender?.Login}");
                break;

            case "marketplace_purchase":
                var purchase = serializer.Deserialize <MarketplacePurchaseEvent>(json);
                var document = purchase.ToSetupLogDocument(@event?.ToLower(), cli.RequestId,
                                                           cli.AppName, delivery?.ToLower());
                await remoteLogger.ReportSetupLog(document);

                break;

            default:
                _logger.LogError("Unknown webhook event: `{event}`", @event);
                break;
            }
            return(new OkResult());
        }
        public async System.Threading.Tasks.Task <IActionResult> HandlerForPushAsync(string?id, JsonElement data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Octokit.Internal.IJsonSerializer _jsonSerializer = new Octokit.Internal.SimpleJsonSerializer();
            var issueEvent = _jsonSerializer.Deserialize <IssueEventPayload>(data.ToString());

            // get the values from the payload data
            var issueNumber    = issueEvent.Issue.Number;
            var installationId = (int)issueEvent.Installation.Id;
            var owner          = issueEvent.Repository.Owner.Login;
            var repo           = issueEvent.Repository;

            if (installationId != _options.Value.InstallationId)
            {
                return(BadRequest("Invalid github webhook installation id"));
            }

            IGitHubClient installationClient = await _gitHubClientFactory.GetGitHubInstallationClient();

            if (issueEvent.Action != "labeled")
            {
                return(Ok());
            }

            var label = data.GetProperty("label").GetString("name");

            await _issueSubscription.TryAddLabel(new IssueReference(repo.Owner.Login, repo.Name, issueNumber, issueEvent.Issue.Id, issueEvent.Issue.NodeId), label, DateTime.UtcNow);

            return(Ok());
        }
Beispiel #3
0
 public async Task<List<Language>> GetLanguages()
 {
     var data = await BlobCache.LocalMachine.DownloadUrl(LanguagesUrl, absoluteExpiration: DateTimeOffset.Now.AddDays(1));
     var serializer = new Octokit.Internal.SimpleJsonSerializer();
     var decodedData = Encoding.UTF8.GetString(data, 0, data.Length);
     return serializer.Deserialize<List<Language>>(decodedData);
 }
Beispiel #4
0
 public async Task<ShowcaseRepositories> GetShowcaseRepositories(string showcaseSlug)
 {
     var url = string.Format(ShowcaseUrl, showcaseSlug);
     var data = await BlobCache.LocalMachine.DownloadUrl(url, absoluteExpiration: DateTimeOffset.Now.AddDays(1));
     var serializer = new Octokit.Internal.SimpleJsonSerializer();
     var decodedData = Encoding.UTF8.GetString(data, 0, data.Length);
     return serializer.Deserialize<ShowcaseRepositories>(decodedData);
 }
Beispiel #5
0
        public async Task <List <Language> > GetLanguages()
        {
            var data = await BlobCache.LocalMachine.DownloadUrl(LanguagesUrl, absoluteExpiration : DateTimeOffset.Now.AddDays(1));

            var serializer  = new Octokit.Internal.SimpleJsonSerializer();
            var decodedData = Encoding.UTF8.GetString(data, 0, data.Length);

            return(serializer.Deserialize <List <Language> >(decodedData));
        }
Beispiel #6
0
        public async Task <ShowcaseRepositories> GetShowcaseRepositories(string showcaseSlug)
        {
            var url  = string.Format(ShowcaseUrl, showcaseSlug);
            var data = await BlobCache.LocalMachine.DownloadUrl(url, absoluteExpiration : DateTimeOffset.Now.AddDays(1));

            var serializer  = new Octokit.Internal.SimpleJsonSerializer();
            var decodedData = Encoding.UTF8.GetString(data, 0, data.Length);

            return(serializer.Deserialize <ShowcaseRepositories>(decodedData));
        }
Beispiel #7
0
 public async Task<List<Octokit.Repository>> GetTrendingRepositories(string since, string language = null)
 {
     var query = "?since=" + since;
     if (!string.IsNullOrEmpty(language))
         query += string.Format("&language={0}", language);
     var data = await BlobCache.LocalMachine.DownloadUrl(TrendingUrl + query, absoluteExpiration: DateTimeOffset.Now.AddHours(1));
     var serializer = new Octokit.Internal.SimpleJsonSerializer();
     var decodedData = Encoding.UTF8.GetString(data, 0, data.Length);
     return serializer.Deserialize<List<Octokit.Repository>>(decodedData);
 }
Beispiel #8
0
        public async Task <List <Octokit.Repository> > GetTrendingRepositories(string since, string language = null)
        {
            var query = "?since=" + since;

            if (!string.IsNullOrEmpty(language))
            {
                query += string.Format("&language={0}", language);
            }
            var data = await BlobCache.LocalMachine.DownloadUrl(TrendingUrl + query, absoluteExpiration : DateTimeOffset.Now.AddHours(1));

            var serializer  = new Octokit.Internal.SimpleJsonSerializer();
            var decodedData = Encoding.UTF8.GetString(data, 0, data.Length);

            return(serializer.Deserialize <List <Octokit.Repository> >(decodedData));
        }
        public async Task <IList <Octokit.Repository> > GetTrendingRepositories(string since, string language = null)
        {
            var query = "?since=" + Uri.EscapeDataString(since);

            if (!string.IsNullOrEmpty(language))
            {
                query += string.Format("&language={0}", Uri.EscapeDataString(language));
            }

            var client     = new HttpClient();
            var serializer = new Octokit.Internal.SimpleJsonSerializer();
            var msg        = await client.GetAsync(TrendingUrl + query).ConfigureAwait(false);

            var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(serializer.Deserialize <List <Octokit.Repository> >(content));
        }