Exemplo n.º 1
0
        public void Initialize(IBuildServerWatcher buildServerWatcher, ISettingsSource config, Func <ObjectId, bool> isCommitInRevisionGrid = null)
        {
            if (_buildServerWatcher != null)
            {
                throw new InvalidOperationException("Already initialized");
            }

            _buildServerWatcher = buildServerWatcher;
            _settings           = IntegrationSettings.ReadFrom(config);

            if (!_settings.IsValid())
            {
                return;
            }

            _projectUrl = _buildServerWatcher.ReplaceVariables(_settings.ProjectUrl);

            if (!Uri.IsWellFormedUriString(_projectUrl, UriKind.Absolute) || string.IsNullOrWhiteSpace(_settings.ApiToken))
            {
                return;
            }

            _apiClient = new ApiClient(_projectUrl, _settings.ApiToken);
            if (CacheAzureDevOps == null || CacheAzureDevOps.Id != CacheKey)
            {
                CacheAzureDevOps      = null;
                _buildDefinitionsTask = _apiClient.GetBuildDefinitionsAsync(_settings.BuildDefinitionFilter);
            }
            else
            {
                _buildDefinitions = CacheAzureDevOps.BuildDefinitions;
            }
        }
Exemplo n.º 2
0
        private async Task ObserveBuildsAsync(DateTime?sinceDate, bool running, IObserver <BuildInfo> observer, CancellationToken cancellationToken)
        {
            if (_apiClient == null)
            {
                observer.OnCompleted();
                return;
            }

            try
            {
                if (_buildDefinitions == null)
                {
                    _buildDefinitions = await _buildDefinitionsTask.JoinAsync();

                    if (_buildDefinitions == null)
                    {
                        observer.OnCompleted();
                        return;
                    }

                    CacheAzureDevOps = new CacheAzureDevOps {
                        Id = CacheKey, BuildDefinitions = _buildDefinitions
                    };
                }

                if (_buildDefinitions == null)
                {
                    observer.OnCompleted();
                    return;
                }

                var builds = running ?
                             FilterRunningBuilds(await _apiClient.QueryRunningBuildsAsync(_buildDefinitions)) :
                             await _apiClient.QueryFinishedBuildsAsync(_buildDefinitions, sinceDate);

                foreach (var build in builds)
                {
                    observer.OnNext(CreateBuildInfo(build));
                }
            }
            catch (OperationCanceledException)
            {
                // Do nothing, the observer is already stopped
            }
            catch (Exception ex)
            {
                observer.OnError(ex);
            }
        }
        private async Task ObserveBuildsAsync(DateTime?sinceDate, bool running, IObserver <BuildInfo> observer, CancellationToken cancellationToken)
        {
            if (_apiClient == null)
            {
                observer.OnCompleted();
                return;
            }

            try
            {
                if (_buildDefinitions == null)
                {
                    try
                    {
                        _buildDefinitions = await _buildDefinitionsTask.JoinAsync();

                        if (_buildDefinitions == null)
                        {
                            observer.OnCompleted();
                            return;
                        }

                        CacheAzureDevOps = new CacheAzureDevOps {
                            Id = CacheKey, BuildDefinitions = _buildDefinitions
                        };
                    }
                    catch (UnauthorizedAccessException)
                    {
                        string errorMessage = _badTokenErrorMessage.Text;
                        ManageError(errorMessage);

                        if (ProjectOnErrorKey == null || ProjectOnErrorKey != CacheKey)
                        {
                            ProjectOnErrorKey = CacheKey;

                            var btnOpenSettings = new TaskDialogButton("btnOpenSettings", "Open settings");
                            var btnIgnore       = new TaskDialogButton("btnIgnoreError", "Ignore");
                            using var errorDialog = new TaskDialog
                                  {
                                      InstructionText = errorMessage,
                                      Icon            = TaskDialogStandardIcon.Error,
                                      Cancelable      = true,
                                      Caption         = _buildIntegrationErrorCaption.Text,
                                      Controls        = { btnOpenSettings, btnIgnore }
                                  };

                            btnOpenSettings.Click += (sender, e) => errorDialog.Close(TaskDialogResult.Yes);
                            btnIgnore.Click       += (sender, e) => errorDialog.Close(TaskDialogResult.No);

                            var result = errorDialog.Show();
                            if (result == TaskDialogResult.Yes)
                            {
                                ProjectOnErrorKey = null;
                                _openSettings.Invoke();
                            }
                        }

                        return;
                    }
                    catch (Exception ex)
                    {
                        string errorMessage = _genericErrorMessage.Text + ex.Message;
                        ManageError(errorMessage);

                        if (ProjectOnErrorKey == null || ProjectOnErrorKey != CacheKey)
                        {
                            ProjectOnErrorKey = CacheKey;
                            MessageBox.Show(errorMessage, _buildIntegrationErrorCaption.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        return;
                    }
                }

                void ManageError(string errorMessage)
                {
                    _apiClient = null;
                    observer.OnNext(new BuildInfo {
                        CommitHashList = new[] { ObjectId.WorkTreeId }, Description = errorMessage, Status = BuildInfo.BuildStatus.Failure
                    });
                    observer.OnCompleted();
                }

                if (_buildDefinitions == null)
                {
                    observer.OnCompleted();
                    return;
                }

                var builds = running ?
                             FilterRunningBuilds(await _apiClient.QueryRunningBuildsAsync(_buildDefinitions)) :
                             await _apiClient.QueryFinishedBuildsAsync(_buildDefinitions, sinceDate);

                foreach (var build in builds)
                {
                    observer.OnNext(CreateBuildInfo(build));
                }
            }
            catch (OperationCanceledException)
            {
                // Do nothing, the observer is already stopped
            }
            catch (Exception ex)
            {
                observer.OnError(ex);
            }
        }