Esempio n. 1
0
        public async Task <string> CreateChannelAsync(string name, string classification, string barPassword)
        {
            _logger.LogInformation($"Creating new channel '{name}' with classification '{classification}'...");

            using (HttpClient client = CreateHttpClient(barPassword))
            {
                Channel channel = new Channel
                {
                    Classification = classification,
                    Name           = name
                };

                JsonSerializerSettings serializerSettings = new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                string body = JsonConvert.SerializeObject(channel, serializerSettings);

                HttpRequestManager requestManager = new HttpRequestManager(client, HttpMethod.Post, "/channels", _logger, body);

                HttpResponseMessage response = await requestManager.ExecuteAsync();

                dynamic responseContent = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync());

                _logger.LogInformation($"Creating new channel succeeded! Id is {responseContent.id}.");

                return(responseContent.id);
            }
        }
Esempio n. 2
0
 private async Task <HttpResponseMessage> QuerySubscriptionsAsync(string queryParameter, string barPassword)
 {
     using (HttpClient client = CreateHttpClient(barPassword))
     {
         HttpRequestManager requestManager = new HttpRequestManager(client, HttpMethod.Get, $"/subscriptions?{queryParameter}", _logger);
         return(await requestManager.ExecuteAsync());
     }
 }
Esempio n. 3
0
        public async Task <string> CreateSubscriptionAsync(string channelName, string sourceRepo, string targetRepo, string targetBranch, string updateFrequency, string mergePolicy, string barPassword)
        {
            _logger.LogInformation($"Creating new subscription with channel name '{channelName}', " +
                                   $"source repo '{sourceRepo}', " +
                                   $"target repo '{targetRepo}', " +
                                   $"target branch '{targetBranch}', " +
                                   $"update frequency '{updateFrequency}' " +
                                   $"and merge policy '{mergePolicy}'...");

            if (string.IsNullOrEmpty(channelName) || string.IsNullOrEmpty(sourceRepo) || string.IsNullOrEmpty(targetRepo) || string.IsNullOrEmpty(targetBranch) ||
                string.IsNullOrEmpty(updateFrequency) || string.IsNullOrEmpty(mergePolicy))
            {
                throw new ArgumentException("One of the following required fields is missing: channelName, sourceRepo, targetRepo, targetBranch, updateFrequency, mergePolity");
            }

            if (!Enum.TryParse(updateFrequency, out UpdateFrequency frequency))
            {
                throw new FormatException($"Failed to convert '{updateFrequency}' to UpdateFrequency.");
            }

            if (!Enum.TryParse(mergePolicy, out MergePolicy policy))
            {
                throw new FormatException($"Failed to convert '{mergePolicy}' to MergePolicy.");
            }

            using (HttpClient client = CreateHttpClient(barPassword))
            {
                SubscriptionData subscriptionData = new SubscriptionData
                {
                    ChannelName = channelName,
                    Policy      = new SubscriptionPolicy
                    {
                        MergePolicy     = policy,
                        UpdateFrequency = frequency
                    },
                    SourceRepository = sourceRepo,
                    TargetBranch     = targetBranch,
                    TargetRepository = targetRepo
                };

                JsonSerializerSettings serializerSettings = new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                };

                string body = JsonConvert.SerializeObject(subscriptionData, serializerSettings);

                HttpRequestManager requestManager = new HttpRequestManager(client, HttpMethod.Post, "/subscriptions", _logger, body);

                HttpResponseMessage response = await requestManager.ExecuteAsync();

                dynamic responseContent = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync());

                _logger.LogInformation($"Creating new subscription succeeded! Id is {responseContent.id}.");

                return(responseContent.id);
            }
        }
Esempio n. 4
0
        public static async Task <HttpResponseMessage> ExecuteGitCommand(this IGitRepo gitRepo, HttpMethod method, string requestUri, ILogger logger, string body = null, string versionOverride = null)
        {
            using (HttpClient client = gitRepo.CreateHttpClient(versionOverride))
            {
                HttpRequestManager requestManager = new HttpRequestManager(client, method, requestUri, logger, body, versionOverride);

                return(await requestManager.ExecuteAsync());
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Execute a remote github command using the REST APi
 /// </summary>
 /// <param name="method">Http method</param>
 /// <param name="requestUri">Request path</param>
 /// <param name="logger">Logger</param>
 /// <param name="body">Body if <paramref name="method"/> is POST or PATCH</param>
 /// <param name="versionOverride">Use alternate API version, if specified.</param>
 /// <returns></returns>
 private async Task <HttpResponseMessage> ExecuteRemoteGitCommandAsync(
     HttpMethod method,
     string requestUri,
     ILogger logger,
     string body            = null,
     string versionOverride = null,
     int retryCount         = 15)
 {
     using (HttpClient client = CreateHttpClient())
     {
         var requestManager = new HttpRequestManager(client, method, requestUri, logger, body, versionOverride);
         return(await requestManager.ExecuteAsync(retryCount));
     }
 }
Esempio n. 6
0
        public async Task <string> GetSubscriptionAsync(int subscriptionId, string barPassword)
        {
            using (HttpClient client = CreateHttpClient(barPassword))
            {
                _logger.LogInformation($"Querying for a subscription with id {subscriptionId}...");

                HttpRequestManager requestManager = new HttpRequestManager(client, HttpMethod.Get, $"/subscriptions/{subscriptionId}", _logger);

                HttpResponseMessage response = await requestManager.ExecuteAsync();

                _logger.LogInformation($"Querying for a subscription with id {subscriptionId} succeeded!");

                return(await response.Content.ReadAsStringAsync());
            }
        }