Esempio n. 1
0
        public async Task <TfsWebHookInfo[]> ListWebHooks(string accessToken, TfsRepositoryInfo repository, string accountEndpoint)
        {
            CommonUtils.ValidateNullArgument("accessToken", accessToken);
            CommonUtils.ValidateNullArgument("repository", repository);
            CommonUtils.ValidateNullArgument("accountEndpoint", accountEndpoint);

            var requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/_apis/hooks/subscriptions?api-version={1}&publisherId=tfs", accountEndpoint.TrimEnd('/'), VsoApiVersion);

            using (var client = CreateTfsClient(accessToken))
            {
                using (var response = await client.GetAsync(requestUri))
                {
                    var hooks = await ProcessResponse <TfsResult <TfsWebHookInfo> >("GetWebHookInfo", response);

                    return(hooks.value.Where(hook =>
                    {
                        if (repository.project != null && hook.publisherInputs != null && hook.consumerInputs != null && hook.consumerInputs.url != null)
                        {
                            return String.Equals(repository.id, hook.publisherInputs.repository, StringComparison.OrdinalIgnoreCase) &&
                            String.Equals(repository.project.id, hook.publisherInputs.projectId, StringComparison.OrdinalIgnoreCase);
                        }

                        return false;
                    }).ToArray());
                }
            }
        }
Esempio n. 2
0
        private async Task <TfsWebHookInfo> GetWebHookInfo(string accessToken, TfsRepositoryInfo repository, string accountEndpoint, string hookUrl)
        {
            var hooks = await QueryWebHooks(accessToken, repository, accountEndpoint);

            var hookUri = new Uri(hookUrl);

            return(hooks.FirstOrDefault(hook => String.Equals(new Uri(hook.consumerInputs.url).Host, hookUri.Host, StringComparison.OrdinalIgnoreCase)));
        }
Esempio n. 3
0
        public async Task <TfsWebHookInfo[]> QueryWebHooks(string accessToken, TfsRepositoryInfo repository, string accountEndpoint)
        {
            CommonUtils.ValidateNullArgument("accessToken", accessToken);
            CommonUtils.ValidateNullArgument("repository", repository);
            CommonUtils.ValidateNullArgument("accountEndpoint", accountEndpoint);

            var requestUri = string.Format(CultureInfo.InvariantCulture, "{0}/_apis/hooks/subscriptionsQuery?api-version=3.1", accountEndpoint.TrimEnd('/'));

            using (var client = CreateTfsClient(accessToken))
            {
                var query = new TfsWebHookQuery
                {
                    publisherId           = "tfs",
                    publisherInputFilters = new[]
                    {
                        new TfsWebHookFilter
                        {
                            conditions = new[]
                            {
                                new TfsQueryCondition
                                {
                                    inputId    = "repository",
                                    @operator  = "equals",
                                    inputValue = repository.id
                                }
                            }
                        }
                    }
                };

                using (var response = await client.PostAsJsonAsync(requestUri, query))
                {
                    var hooks = await ProcessResponse <TfsWebHookResult>("QueryWebHooks", response);

                    return(hooks.results.Where(hook =>
                    {
                        if (repository.project != null && hook.publisherInputs != null && hook.consumerInputs != null && hook.consumerInputs.url != null)
                        {
                            return String.Equals(repository.id, hook.publisherInputs.repository, StringComparison.OrdinalIgnoreCase) &&
                            String.Equals(repository.project.id, hook.publisherInputs.projectId, StringComparison.OrdinalIgnoreCase);
                        }

                        return false;
                    }).ToArray());
                }
            }
        }
Esempio n. 4
0
        public async Task <bool> TestWebHook(string accessToken, TfsRepositoryInfo repository, string accountEndpoint, string hookUrl)
        {
            CommonUtils.ValidateNullArgument("accessToken", accessToken);
            CommonUtils.ValidateNullArgument("repository", repository);
            CommonUtils.ValidateNullArgument("hookUrl", hookUrl);
            CommonUtils.ValidateNullArgument("accountEndpoint", accountEndpoint);

            var hook = await GetWebHookInfo(accessToken, repository, accountEndpoint, hookUrl);

            if (hook == null)
            {
                throw new InvalidOperationException("Vso TestWebHook: no hook found for " + repository.RepoUrl);
            }

            var hookUri = new Uri(hookUrl);
            var test    = new TfsTestHookInfo();

            test.subscriptionId           = hook.id;
            test.details                  = new TfsTestHookDetails();
            test.details.eventType        = "git.push";
            test.details.consumerActionId = "deployWebApp";
            test.details.consumerId       = "azureAppService";
            test.details.publisherId      = "tfs";

            var creds = String.IsNullOrEmpty(hookUri.UserInfo) ? new string[0] : hookUri.UserInfo.Split(':');

            test.details.consumerInputs     = new TfsConsumerInputs();
            test.details.consumerInputs.url = String.Format("{0}://{1}{2}", hookUri.Scheme, hookUri.Authority, hookUri.PathAndQuery);
            test.details.consumerInputs.basicAuthUsername = creds.Length > 0 ? creds[0] : null;
            test.details.consumerInputs.basicAuthPassword = creds.Length > 1 ? creds[1] : null;

            var repoUri    = new Uri(repository.url);
            var requestUri = String.Format("{0}/_apis/hooks/testNotifications?api-version={1}", accountEndpoint.TrimEnd('/'), VsoApiVersion);

            using (var client = CreateTfsClient(accessToken))
            {
                using (var response = await client.PostAsJsonAsync(requestUri, test))
                {
                    return(await ProcessEmptyResponse("TestWebHook", response));
                }
            }
        }
Esempio n. 5
0
        public async Task <TfsWebHookInfo> AddWebHook(string accessToken, TfsRepositoryInfo repository, string accountEndpoint, string hookUrl, string branch = null)
        {
            CommonUtils.ValidateNullArgument("accessToken", accessToken);
            CommonUtils.ValidateNullArgument("repository", repository);
            CommonUtils.ValidateNullArgument("hookUrl", hookUrl);
            CommonUtils.ValidateNullArgument("accountEndpoint", accountEndpoint);

            await RemoveWebHook(accessToken, repository, accountEndpoint, hookUrl);

            var hookUri = new Uri(hookUrl);
            var hook    = new TfsWebHookInfo();

            hook.eventType          = "git.push";
            hook.consumerActionId   = "deployWebApp";
            hook.consumerId         = "azureAppService";
            hook.consumerInputs     = new TfsConsumerInputs();
            hook.consumerInputs.url = String.Format("{0}://{1}{2}", hookUri.Scheme, hookUri.Authority, hookUri.PathAndQuery);

            var creds = String.IsNullOrEmpty(hookUri.UserInfo) ? new string[0] : hookUri.UserInfo.Split(':');

            hook.consumerInputs.basicAuthUsername = creds.Length > 0 ? creds[0] : null;
            hook.consumerInputs.basicAuthPassword = creds.Length > 1 ? creds[1] : null;
            hook.publisherId                = "tfs";
            hook.publisherInputs            = new TfsPublisherInputs();
            hook.publisherInputs.branch     = branch ?? "master";
            hook.publisherInputs.projectId  = repository.project.id;
            hook.publisherInputs.repository = repository.id;

            var repoUri    = new Uri(repository.url);
            var requestUri = String.Format("{0}/_apis/hooks/subscriptions?api-version={1}", accountEndpoint.TrimEnd('/'), VsoApiVersion);

            using (var client = CreateTfsClient(accessToken))
            {
                using (var response = await client.PostAsJsonAsync(requestUri, hook))
                {
                    return(await ProcessResponse <TfsWebHookInfo>("AddWebHook", response));
                }
            }
        }
Esempio n. 6
0
        public async Task <bool> RemoveWebHook(string accessToken, TfsRepositoryInfo repository, string accountEndpoint, string hookUrl)
        {
            CommonUtils.ValidateNullArgument("accessToken", accessToken);
            CommonUtils.ValidateNullArgument("repository", repository);
            CommonUtils.ValidateNullArgument("hookUrl", hookUrl);
            CommonUtils.ValidateNullArgument("accountEndpoint", accountEndpoint);

            var hook = await GetWebHookInfo(accessToken, repository, accountEndpoint, hookUrl);

            if (hook == null)
            {
                return(false);
            }

            var requestUri = String.Format("{0}?api-version={1}", hook.url, VsoApiVersion);

            using (var client = CreateTfsClient(accessToken))
            {
                using (var response = await client.DeleteAsync(requestUri))
                {
                    return(await ProcessEmptyResponse("RemoveWebHook", response));
                }
            }
        }