Beispiel #1
0
        private async Task <GitHubClient> GetInstallationClientAsync(int installationId)
        {
            log.LogInformation($"Setting up client for installation ID: {installationId}");
            var jwt    = GetGitHubJWT(privateKeyXML, installationId);
            var header = new Octokit.ProductHeaderValue("PublishScheduler", "0.0.1");

            var appClient = new GitHubClient(header)
            {
                Credentials = new Credentials(jwt, AuthenticationType.Bearer)
            };

            log.LogInformation("Getting installation token.");

            // TODO: get installation ID from webhook payload

            // DEBUG: list all installation ids
            var installations = await appClient.GitHubApps.GetAllInstallationsForCurrent();

            foreach (var i in installations)
            {
                log.LogInformation($"installation: {i.Id} {i.HtmlUrl}");
            }

            var response = await appClient.GitHubApps.CreateInstallationToken(installationId);

            log.LogInformation($"Creating client for installation {installationId}");
            var client = new GitHubClient(header)
            {
                Credentials = new Credentials(response.Token)
            };

            return(client);
        }
        public IHttpActionResult ProcessHook(IssueCommentEvent commentEvent)
        {
            _baseUrl = commentEvent.Repository.Url.Scheme + "://" + commentEvent.Repository.Url.Host;
            _owner = commentEvent.Repository.Owner.Login;
            _repoName = commentEvent.Repository.Name;

            var creds = new InMemoryCredentialStore(new Credentials("derp", ApiKey));
            var headerVal = new ProductHeaderValue("GitHooks");
            _github = new GitHubClient(headerVal, creds, new Uri(_baseUrl));

            if (CheckComment(commentEvent.Comment.Body))
            {
                var branchName = GetBranchNameFromComment(commentEvent.Comment.Body);

                try
                {
                    var pullRequest = GetPullRequest(branchName, commentEvent.Issue.Number, commentEvent.Issue.Title);
                    var merge = MergePullRequest(pullRequest, commentEvent.Issue.Number, branchName, true);
                    DeleteBranch(merge, branchName, commentEvent.Issue.Number, pullRequest.Number);
                    LeaveSuccessfulMergeComment(commentEvent.Issue.Number, pullRequest.Number, branchName);
                }
                catch (RobotFallDownException e)
                {
                    return BadRequest();
                }
            }

            return Ok();
        }
        /// <summary>
        /// Creates a new EnterpriseProbe, used to check for the existence of GitHub Enterprise Instances
        /// </summary>
        /// <param name="productInformation">
        /// The name (and optionally version) of the product using this library. This is sent to the server as part of
        /// the user agent for analytics purposes.
        /// </param>
        /// <param name="httpClient">
        /// The client to use for executing requests
        /// </param>
        public EnterpriseProbe(ProductHeaderValue productInformation, IHttpClient httpClient)
        {
            Ensure.ArgumentNotNull(productInformation, "productHeader");
            Ensure.ArgumentNotNull(httpClient, "httpClient");

            this.productHeader = productInformation;
            this.httpClient = httpClient;
        }
Beispiel #4
0
 public static GitHubClient CreateClient(string token)
 {
     var headerValue = new ProductHeaderValue("Octopie");
     return new GitHubClient(headerValue)
     {
         Credentials = new Octokit.Credentials(token)
     };
 }
Beispiel #5
0
        private GitHubClient GetGitHubClientWithApiKey([NotNull] string apiKey)
        {
            var version            = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            var productInformation = new Octokit.ProductHeaderValue("DHGMS-Gitstronomy", version);
            var credentials        = new Credentials(apiKey);
            var credentialStore    = new InMemoryCredentialStore(credentials);

            return(new Octokit.GitHubClient(productInformation, credentialStore));
        }
Beispiel #6
0
        public Program()
        {
            applicationName = Info.ApplicationInfo.ApplicationName;
            applicationDescription = Info.ApplicationInfo.ApplicationDescription;

            var executingAssembly = typeof(Program).Assembly;
            AssemblyName = executingAssembly.GetName();
            ExecutingAssemblyDirectory = Path.GetDirectoryName(executingAssembly.Location);
            ProductHeader = new ProductHeaderValue(Info.ApplicationInfo.ApplicationSafeName, AssemblyName.Version.ToString());
        }
        public async Task ReturnsDefinitelyNotEnterpriseExistsForUnknownException()
        {
            var httpClient = Substitute.For<IHttpClient>();
            httpClient.Send(Args.Request, CancellationToken.None).ThrowsAsync(new InvalidOperationException());
            var productHeader = new ProductHeaderValue("GHfW", "99");
            var enterpriseProbe = new EnterpriseProbe(productHeader, httpClient);

            var result = await enterpriseProbe.Probe(new Uri("https://example.com/"));

            Assert.Equal(EnterpriseProbeResult.Failed, result);
        }
        public async Task ThrowsArgumentExceptionForGitHubDotComDomain()
        {
            var httpClient = Substitute.For<IHttpClient>();
            httpClient.Send(Args.Request, CancellationToken.None).ThrowsAsync(new InvalidOperationException());
            var productHeader = new ProductHeaderValue("GHfW", "99");
            var enterpriseProbe = new EnterpriseProbe(productHeader, httpClient);

            var result = await Assert.ThrowsAsync<ArgumentException>(async () => await enterpriseProbe.Probe(new Uri("https://github.com/")));

            Assert.Equal("enterpriseBaseUrl", result.ParamName);
        }
Beispiel #9
0
        public static GitHubClient CreateClient(string accessToken)
        {
            var productHeader = new Octokit.ProductHeaderValue("MyIssues", "0.1");
            var token         = new Credentials(accessToken);

            var _client = new Octokit.GitHubClient(productHeader);

            _client.Credentials = token;

            return(_client);
        }
        public async Task ReturnsExistsForApiExceptionWithCorrectHeaders()
        {
            var httpClient = Substitute.For<IHttpClient>();
            var response = Substitute.For<IResponse>();
            response.Headers["Server"].Returns("GitHub.com");
            response.Headers.ContainsKey("X-GitHub-Request-Id").Returns(true);
            var apiException = new ApiException(response);
            httpClient.Send(Args.Request, CancellationToken.None).ThrowsAsync(apiException);
            var productHeader = new ProductHeaderValue("GHfW", "99");
            var enterpriseProbe = new EnterpriseProbe(productHeader, httpClient);

            var result = await enterpriseProbe.Probe(new Uri("https://example.com/"));

            Assert.Equal(EnterpriseProbeResult.Ok, result);
        }
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            //Initialization of basic authentication for Accessing GitHub API
            Credentials      credentials     = new Credentials(ConfigurationManager.AppSettings["GitHubAPIToken"]);
            ICredentialStore credentialStore = new InMemoryCredentialStore(credentials);

            Octokit.ProductHeaderValue productHeader = new Octokit.ProductHeaderValue("VtexShoopingCart");

            //kernel.Bind<ICredentialStore>().ToMethod(context => new InMemoryCredentialStore(credentials));
            //kernel.Bind<IGitHubClient>().To<GitHubClient>().InRequestScope();

            kernel.Bind <IGitHubClient>().ToMethod(context => new GitHubClient(productHeader, credentialStore));
            kernel.Bind <IPriceRepository>().To <PriceRepository>().InRequestScope();
            kernel.Bind <IPriceService>().To <PriceService>().InRequestScope();
            kernel.Bind <IDeveloperService>().To <DeveloperService>().InRequestScope();
        }
        public async Task ReturnsExistsForResponseWithCorrectHeadersRegardlessOfResponse(HttpStatusCode httpStatusCode)
        {
            var response = Substitute.For<IResponse>();
            response.StatusCode.Returns(httpStatusCode);
            response.Headers["Server"].Returns("REVERSE-PROXY");
            response.Headers.ContainsKey("X-GitHub-Request-Id").Returns(true);
            var productHeader = new ProductHeaderValue("GHfW", "99");
            var httpClient = Substitute.For<IHttpClient>();
            httpClient.Send(Args.Request, CancellationToken.None).Returns(Task.FromResult(response));
            var enterpriseProbe = new EnterpriseProbe(productHeader, httpClient);

            var result = await enterpriseProbe.Probe(new Uri("https://example.com/"));

            Assert.Equal(EnterpriseProbeResult.Ok, result);
            httpClient.Received(1).Send(Arg.Is<Request>(r =>
                r.BaseAddress == new Uri("https://example.com") &&
                r.Endpoint == new Uri("/site/sha", UriKind.Relative) &&
                r.Method == HttpMethod.Get &&
                r.Timeout == TimeSpan.FromSeconds(3) &&
                r.Headers["User-Agent"] == "GHfW/99"
                ), CancellationToken.None);
        }
Beispiel #13
0
        public async Task<IGitHubClient> GetClientAsync(CancellationToken cancellationToken)
        {
            if (_gitHubClient != null)
            {
                return _gitHubClient;
            }

            var token = await _configApi.GetAsync(ConfigKey.With("github-token"), cancellationToken).ConfigureAwait(false);
            if (string.IsNullOrEmpty(token))
            {
                token = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
            }

            var productHeaderValue = new ProductHeaderValue(
                "TheBorg.Plugins.GitHub",
                GetType().Assembly.GetName().Version.ToString());

            _gitHubClient = new GitHubClient(productHeaderValue)
                {
                    Credentials = new Credentials(token)
                };
            return _gitHubClient;
        }
Beispiel #14
0
 /// <summary>
 /// Creates a new connection instance used to make requests of the GitHub API.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 public Connection(ProductHeaderValue productInformation)
     : this(productInformation, _defaultGitHubApiUrl, _anonymousCredentials)
 {
 }
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to the specified baseAddress.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="credentialStore">Provides credentials to the client when making requests</param>
 /// <param name="baseAddress">
 /// The address to point this client to. Typically used for GitHub Enterprise 
 /// instances</param>
 public GitHubClient(ProductHeaderValue productInformation, ICredentialStore credentialStore, Uri baseAddress)
     : this(new Connection(productInformation, FixUpBaseUri(baseAddress), credentialStore))
 {
 }
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to 
 /// https://api.github.com/
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="credentialStore">Provides credentials to the client when making requests</param>
 public GitHubClient(ProductHeaderValue productInformation, ICredentialStore credentialStore)
     : this(new Connection(productInformation, credentialStore))
 {
 }
Beispiel #17
0
        async Task<int> main(string[] args)
        {
            using (var logger = new SetupLogLogger(false) { Level = Splat.LogLevel.Info }) {
                Splat.Locator.CurrentMutable.Register(() => logger, typeof(Splat.ILogger));

                var releaseDir = default(string);
                var repoUrl = default(string);
                var token = default(string);

                opts = new OptionSet() {
                    "Usage: SyncGitHubReleases.exe command [OPTS]",
                    "Builds a Releases directory from releases on GitHub",
                    "",
                    "Options:",
                    { "h|?|help", "Display Help and exit", _ => {} },
                    { "r=|releaseDir=", "Path to a release directory to download to", v => releaseDir = v},
                    { "u=|repoUrl=", "The URL to the repository root page", v => repoUrl = v},
                    { "t=|token=", "The OAuth token to use as login credentials", v => token = v},
                };

                opts.Parse(args);

                if (token == null || repoUrl == null || repoUrl.StartsWith("http", true, CultureInfo.InvariantCulture) == false) {
                    ShowHelp();
                    return -1;
                }

                var releaseDirectoryInfo = new DirectoryInfo(releaseDir ?? Path.Combine(".", "Releases"));
                if (!releaseDirectoryInfo.Exists) releaseDirectoryInfo.Create();

                var repoUri = new Uri(repoUrl);
                var userAgent = new ProductHeaderValue("SyncGitHubReleases", Assembly.GetExecutingAssembly().GetName().Version.ToString());
                var client = new GitHubClient(userAgent, repoUri) {
                    Credentials = new Credentials(token)
                };

                var nwo = nwoFromRepoUrl(repoUrl);
                var releases = await client.Release.GetAll(nwo.Item1, nwo.Item2);

                await releases.ForEachAsync(async release => {
                    // NB: Why do I have to double-fetch the release assets? It's already in GetAll
                    var assets = await client.Release.GetAssets(nwo.Item1, nwo.Item2, release.Id);

                    await assets
                        .Where(x => x.Name.EndsWith(".nupkg", StringComparison.OrdinalIgnoreCase))
                        .Where(x => {
                            var fi = new FileInfo(Path.Combine(releaseDirectoryInfo.FullName, x.Name));
                            return !(fi.Exists && fi.Length == x.Size);
                        })
                        .ForEachAsync(async x => {
                            var target = new FileInfo(Path.Combine(releaseDirectoryInfo.FullName, x.Name));
                            if (target.Exists) target.Delete();

                            var hc = new HttpClient();
                            var rq = new HttpRequestMessage(HttpMethod.Get, x.Url);
                            rq.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/octet-stream"));
                            rq.Headers.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue(userAgent.Name, userAgent.Version));
                            rq.Headers.Add("Authorization", "Bearer " + token);

                            var resp = await hc.SendAsync(rq);
                            resp.EnsureSuccessStatusCode();

                            using (var from = await resp.Content.ReadAsStreamAsync())
                            using (var to = File.OpenWrite(target.FullName)) {
                                await from.CopyToAsync(to);
                            }
                        });
                });

                var entries = releaseDirectoryInfo.GetFiles("*.nupkg")
                    .AsParallel()
                    .Select(x => ReleaseEntry.GenerateFromFile(x.FullName));

                ReleaseEntry.WriteReleaseFile(entries, Path.Combine(releaseDirectoryInfo.FullName, "RELEASES"));
            }

            return 0;
        }
Beispiel #18
0
        /// <summary>
        /// Pulls down commit data from GitHub and creates events for each commit, which are then streamed to Splunk.
        /// </summary>
        /// <remarks>
        /// This function will be invoked once for each instance of the modular input, though that invocation
        /// may or may not be in separate processes, depending on how the modular input is configured. It should
        /// extract the arguments it needs from <tt>inputDefinition</tt>, then write events to <tt>eventWriter</tt>
        /// (which is thread safe).
        /// </remarks>
        /// <param name="inputDefinition">The definition for this instance of the GitHub input, representing a GitHub repository.</param>
        /// <param name="eventWriter">An object that handles writing events to Splunk.</param>
        public override async Task StreamEventsAsync(InputDefinition inputDefinition, EventWriter eventWriter)
        {
            var owner = ((SingleValueParameter)inputDefinition.Parameters["Owner"]).ToString();
            var repository = ((SingleValueParameter)inputDefinition.Parameters["Repository"]).ToString();
            var checkpointFilePath = Path.Combine(inputDefinition.CheckpointDirectory, owner + " " + repository + ".txt");

            var productHeader = new ProductHeaderValue("splunk-sdk-csharp-github-commits");
            ObservableGitHubClient client;

            if (!inputDefinition.Parameters.ContainsKey("Token") || String.IsNullOrWhiteSpace(((SingleValueParameter)inputDefinition.Parameters["Token"]).ToString()))
            {
                client = new ObservableGitHubClient(productHeader);
            }
            else
            {
                client = new ObservableGitHubClient(productHeader, new InMemoryCredentialStore(new Credentials(((SingleValueParameter)inputDefinition.Parameters["Token"]).ToString())));
            }

            var shaKeys = new HashSet<string>();

            var fileReader = new StreamReader(File.Open(checkpointFilePath, System.IO.FileMode.OpenOrCreate));
            string line;
            while (!String.IsNullOrWhiteSpace(line = await fileReader.ReadLineAsync()))
            {
                shaKeys.Add(line);
            }
            fileReader.Close(); 

            bool done = false;
            var fileWriter = new StreamWriter(checkpointFilePath);
            // Use Rx to stream an event for each commit as they come in
            client.Repository.Commits.GetAll(owner, repository).Subscribe(
                async githubCommit =>
                {
                    if (!shaKeys.Contains(githubCommit.Sha))
                    {
                        await StreamCommit(githubCommit, eventWriter, owner, repository);
                        await fileWriter.WriteLineAsync(githubCommit.Sha); // Write to the checkpoint file
                        shaKeys.Add(githubCommit.Sha);
                        await eventWriter.LogAsync(Severity.Info, repository + " indexed a Github commit with sha: " + githubCommit.Sha);
                    }
                },
                async e =>
                {
                    //error handing goes here
                    await eventWriter.LogAsync(Severity.Error, e.GetType() + " - " + e.StackTrace);
                },
                () =>
                {
                    //completion handling goes here
                    fileWriter.Close();
                    done = true;
                }
            );

            // Wait for Rx subscribe to finish above
            while (!done)
            {
                await Task.Delay(100);
            }

        }
Beispiel #19
0
 public static GitHubClient GetClient()
 {
     ProductHeaderValue header = new ProductHeaderValue("GitProfileShop");
     GitHubClient client = new GitHubClient(header);
     return client;
 }
Beispiel #20
0
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to
 /// https://api.github.com/
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 public GitHubClient(ProductHeaderValue productInformation)
     : this(new Connection(productInformation))
 {
 }
Beispiel #21
0
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to the specified baseAddress.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="credentialStore">Provides credentials to the client when making requests</param>
 /// <param name="baseAddress">
 /// The address to point this client to. Typically used for GitHub Enterprise
 /// instances</param>
 public GitHubClient(ProductHeaderValue productInformation, ICredentialStore credentialStore, Uri baseAddress)
     : this(new Connection(productInformation, FixUpBaseUri(baseAddress), credentialStore))
 {
 }
Beispiel #22
0
 /// <summary>
 /// Creates a new connection instance used to make requests of the GitHub API.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="baseAddress">
 /// The address to point this client to such as https://api.github.com or the URL to a GitHub Enterprise
 /// instance</param>
 /// <param name="credentialStore">Provides credentials to the client when making requests</param>
 public Connection(ProductHeaderValue productInformation, Uri baseAddress, ICredentialStore credentialStore)
     : this(productInformation, baseAddress, credentialStore, new HttpClientAdapter(), new SimpleJsonSerializer())
 {
 }
Beispiel #23
0
 /// <summary>
 /// Creates a new connection instance used to make requests of the GitHub API.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="credentialStore">Provides credentials to the client when making requests</param>
 public Connection(ProductHeaderValue productInformation, ICredentialStore credentialStore)
     : this(productInformation, _defaultGitHubApiUrl, credentialStore)
 {
 }
Beispiel #24
0
 /// <summary>
 /// Creates a new connection instance used to make requests of the GitHub API.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="baseAddress">
 /// The address to point this client to such as https://api.github.com or the URL to a GitHub Enterprise
 /// instance</param>
 public Connection(ProductHeaderValue productInformation, Uri baseAddress)
     : this(productInformation, baseAddress, _anonymousCredentials)
 {
 }
Beispiel #25
0
 /// <summary>
 /// Creates a new connection instance used to make requests of the GitHub API.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="httpClient">
 /// The client to use for executing requests
 /// </param>
 public Connection(ProductHeaderValue productInformation, IHttpClient httpClient)
     : this(productInformation, _defaultGitHubApiUrl, _anonymousCredentials, httpClient, new SimpleJsonSerializer())
 {
 }
        public async Task <IEnumerable <SearchResults> > SerachInRepository(SearchRequest searchRequest)
        {
            string errorMessage = string.Empty;
            List <SearchResults> searchResults = new List <SearchResults>();
            var productInformation             = new ProductHeaderValue("GithubProxy");
            var credentials = new Credentials(Configuration["TokkenGitHub"]);
            var client      = new GitHubClient(productInformation)
            {
                Credentials = credentials
            };

            try
            {
                Language         language = (Language)searchRequest.Language;
                SearchCodeResult result   = await client.Search.SearchCode(
                    new SearchCodeRequest()
                {
                    In       = new CodeInQualifier[] { CodeInQualifier.Path },
                    Language = language,
                    Repos    = new RepositoryCollection {
                        searchRequest.RepositoryName
                    }
                });

                if (result != null && result.Items.Count > 0)
                {
                    foreach (var item in result.Items)
                    {
                        searchResults.Add(new SearchResults()
                        {
                            FileName = item.Name, Path = item.Path
                        });
                    }
                }
            }

            catch (Octokit.ApiValidationException ex)
            {
                if (ex.ApiError != null && ex.ApiError.Errors != null)
                {
                    foreach (var item in ex.ApiError.Errors)
                    {
                        errorMessage += item.Message.ToString();
                    }
                }
                logger?.LogError(ex.ToString() + errorMessage);  //Example of  Error handling
            }
            catch (Octokit.RepositoryFormatException ex)
            {
                logger?.LogError(ex.ToString());
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                logger?.LogError(ex.ToString());  //Example of  Error handling
            }
            finally
            {
            }
            return(searchResults);
        }
 public EnterpriseProbeTask(IProgram program, IHttpClient httpClient)
 {
     productHeader = program.ProductHeader;
     this.httpClient = httpClient;
 }
Beispiel #28
0
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to
 /// https://api.github.com/
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="credentialStore">Provides credentials to the client when making requests</param>
 public GitHubClient(ProductHeaderValue productInformation, ICredentialStore credentialStore)
     : this(new Connection(productInformation, credentialStore))
 {
 }
Beispiel #29
0
 public Connection(ProductHeaderValue productInformation, Uri baseAddress, ICredentialStore credentialStore)
     : this(productInformation, baseAddress, credentialStore, new HttpClientAdapter(HttpMessageHandlerFactory.CreateDefault), new SimpleJsonSerializer())
 {
 }
        // Use the Octokit client library to render the markdown into HTML
        private async Task PreviewMarkDownAsync()
        {
            SetStatus("validating ...");
            try
            {
                if (CheckCredentials())
                {
                    string html = "";

                    // Lazy-load the client. I could refactor this to a lazy-loaded property
                    if (_gitHubClient == null)
                    {
                        var productHeaderValue = new ProductHeaderValue(_vM.GitHubUsername, _vM.GitHubPassword);
                        _gitHubClient = new GitHubClient(productHeaderValue);
                    }

                    html = await _gitHubClient.Miscellaneous.RenderRawMarkdown(MarkDownText.Text);

                    // Could trigger a validation of the text right now!
                    // MessageBox.Show(html);
                    ContentPreview.NavigateToString(string.Format(_previewTemplate, html));
                }
            }
            catch(ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
            }

            SetStatus("validation and rendering complete");
        }
Beispiel #31
0
 public WikiProbe(IProgram program, IHttpClient httpClient)
 {
     this.productHeader = program.ProductHeader;
     this.httpClient = httpClient;
 }
 public EnterpriseProbe(ProductHeaderValue productInformation)
     : this(productInformation, new HttpClientAdapter(HttpMessageHandlerFactory.CreateDefault))
 {
 }
 public SimpleApiClientFactory(IProgram program, Lazy<IEnterpriseProbeTask> enterpriseProbe, Lazy<IWikiProbe> wikiProbe)
 {
     productHeader = program.ProductHeader;
     lazyEnterpriseProbe = enterpriseProbe;
     lazyWikiProbe = wikiProbe;
 }
Beispiel #34
0
 public EnterpriseProbe(ProductHeaderValue productInformation)
     : this(productInformation, new HttpClientAdapter(HttpMessageHandlerFactory.CreateDefault))
 {
 }
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to 
 /// https://api.github.com/
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 public GitHubClient(ProductHeaderValue productInformation)
     : this(new Connection(productInformation))
 {
 }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "POST", Route = null)]
            HttpRequestMessage req, ILogger log)
        {
            var payloadJson = await req.Content.ReadAsStringAsync();

            SimpleJsonSerializer serializer   = new SimpleJsonSerializer();
            IssueEventPayload    issuePayload = serializer.Deserialize <IssueEventPayload>(payloadJson);

            if (issuePayload.Issue.User.Type.HasValue &&
                issuePayload.Issue.User.Type.Value == AccountType.Bot)
            {
                log.LogInformation("Comment is from DarcBot, ignoring.");
                return(new OkObjectResult($"Ignoring DarcBot comment"));
            }

            if (issuePayload.Action != "opened" &&
                issuePayload.Action != "reopened" &&
                issuePayload.Action != "closed")
            {
                log.LogInformation($"Received github action '{issuePayload.Action}', nothing to do");
                return(new OkObjectResult($"DarcBot has nothing to do with github issue action '{issuePayload.Action}'"));
            }

            // Determine identifiable information for triage item
            TriageItem triageItem = GetTriageItemProperties(issuePayload.Issue.Body);

            triageItem.Url = issuePayload.Issue.HtmlUrl;

            if (triageItem == null)
            {
                /* Item is not a triage item (does not contain identifiable information), do nothing */
                log.LogInformation($"{issuePayload.Issue.Url} is not a triage type issue.");
                return(new OkObjectResult("No identifiable information detected"));
            }

            int.TryParse(System.Environment.GetEnvironmentVariable("AppId"), out int appId);

            // Create jwt token
            // Private key is stored in Azure Key vault by downloading the private key (pem file) from GitHub, then
            // using the Azure CLI to store the value in key vault.
            // ie: az keyvault secret set --vault-name [vault name] --name GitHubApp-DarcBot-PrivateKey --encoding base64 --file [pem key file path]
            GitHubAppTokenProvider gitHubTokenProvider = new GitHubAppTokenProvider();
            var installationToken = gitHubTokenProvider.GetAppTokenFromEnvironmentVariableBase64(appId, "PrivateKey");

            // create client using jwt as a bearer token
            var          userAgent = new Octokit.ProductHeaderValue("DarcBot");
            GitHubClient appClient = new GitHubClient(userAgent)
            {
                Credentials = new Credentials(installationToken, AuthenticationType.Bearer),
            };

            // using the client, create an installation token
            AccessToken token = await appClient.GitHubApps.CreateInstallationToken(issuePayload.Installation.Id);

            // with the installation token, create a new GitHubClient that has the apps permissions
            var gitHubClient = new GitHubClient(new ProductHeaderValue("DarcBot-Installation"))
            {
                Credentials = new Credentials(token.Token)
            };

            if (issuePayload.Action == "created" ||
                issuePayload.Action == "opened" ||
                issuePayload.Action == "reopened")
            {
                // First, look for duplicate issues that are open
                var openIssues = new RepositoryIssueRequest
                {
                    Filter        = IssueFilter.All,
                    State         = ItemStateFilter.Open,
                    SortProperty  = IssueSort.Created,
                    SortDirection = SortDirection.Ascending,
                };
                openIssues.Labels.Add(_darcBotLabelName);

                log.LogInformation("Getting open issues");
                var issues = await gitHubClient.Issue.GetAllForRepository(issuePayload.Repository.Id, openIssues);

                log.LogInformation($"There are {issues.Count} open issues with the '{_darcBotLabelName}' label");
                foreach (var checkissue in issues)
                {
                    if (checkissue.Number != issuePayload.Issue.Number)
                    {
                        TriageItem issueItem = GetTriageItemProperties(checkissue.Body);
                        if (triageItem.Equals(issueItem))
                        {
                            await gitHubClient.Issue.Comment.Create(issuePayload.Repository.Id, issuePayload.Issue.Number, $"DarcBot has detected a duplicate issue.\n\nClosing as duplicate of {checkissue.HtmlUrl}\n\nFor more information see {_docLink}");

                            var issueUpdate = new IssueUpdate
                            {
                                State = ItemState.Closed,
                            };
                            await gitHubClient.Issue.Update(issuePayload.Repository.Id, issuePayload.Issue.Number, issueUpdate);

                            return(new OkObjectResult($"Resolved as duplicate of {checkissue.Number}"));
                        }
                    }
                }

                // No duplicates, add label and move issue to triage
                var issue = await gitHubClient.Issue.Get(issuePayload.Repository.Id, issuePayload.Issue.Number);

                var update = issue.ToUpdate();
                update.AddLabel(_darcBotLabelName);
                await gitHubClient.Issue.Update(issuePayload.Repository.Id, issuePayload.Issue.Number, update);

                triageItem.UpdatedCategory = "InTriage";
            }

            if (issuePayload.Action == "closed")
            {
                IReadOnlyList <IssueComment> comments = gitHubClient.Issue.Comment.GetAllForIssue(issuePayload.Repository.Id, issuePayload.Issue.Number).Result;

                foreach (var comment in comments)
                {
                    // Look for category information in comment
                    string category = GetDarcBotProperty("category", comment.Body);
                    if (!string.IsNullOrEmpty(category))
                    {
                        triageItem.UpdatedCategory = category;
                    }
                }
            }

            log.LogInformation($"buildId: {triageItem.BuildId}, recordId: {triageItem.RecordId}, index: {triageItem.Index}, category: {triageItem.UpdatedCategory}, url: {triageItem.Url}");

            await IngestTriageItemsIntoKusto(new[] { triageItem }, log);

            await gitHubClient.Issue.Comment.Create(issuePayload.Repository.Id, issuePayload.Issue.Number, $"DarcBot has updated the 'TimelineIssuesTriage' database.\n**PowerBI reports may take up to 24 hours to refresh**\n\nSee {_docLink} for more information and 'darcbot' usage.");

            return(new OkObjectResult("Success"));
        }
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to the specified baseAddress.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="baseAddress">
 /// The address to point this client to. Typically used for GitHub Enterprise 
 /// instances</param>
 public GitHubClient(ProductHeaderValue productInformation, Uri baseAddress)
     : this(new Connection(productInformation, FixUpBaseUri(baseAddress)))
 {
 }
 public ApiClientFactory(ILoginCache loginCache, IProgram program, ILoggingConfiguration config)
 {
     LoginCache = loginCache;
     productHeader = program.ProductHeader;
     config.Configure();
 }
Beispiel #39
0
 /// <summary>
 /// Create a new instance of the GitHub API v3 client pointing to the specified baseAddress.
 /// </summary>
 /// <param name="productInformation">
 /// The name (and optionally version) of the product using this library. This is sent to the server as part of
 /// the user agent for analytics purposes.
 /// </param>
 /// <param name="baseAddress">
 /// The address to point this client to. Typically used for GitHub Enterprise
 /// instances</param>
 public GitHubClient(ProductHeaderValue productInformation, Uri baseAddress)
     : this(new Connection(productInformation, FixUpBaseUri(baseAddress)))
 {
 }
Beispiel #40
0
 private GitHubClient CreateGitHubClient()
 {
     var configuration = (IConfiguration)HttpContext.RequestServices.GetService(typeof(IConfiguration));
     var productIdentifier = configuration["github.productIdentifier"];
     var header = new ProductHeaderValue(productIdentifier);
     return new GitHubClient(header);
 }