Пример #1
0
 public void InitializePublisher(int buildId, VssConnection connection)
 {
     ArgUtil.NotNull(connection, nameof(connection));
     _connection = connection;
     _buildId = buildId;
     _codeCoverageServer = HostContext.GetService<ICodeCoverageServer>();
 }
Пример #2
0
        public static VssConnection CreateConnection(Uri serverUri, VssCredentials credentials)
        {
            VssClientHttpRequestSettings settings = VssClientHttpRequestSettings.Default.Clone();
            settings.MaxRetryRequest = 5;

            // Remove Invariant from the list of accepted languages.
            //
            // The constructor of VssHttpRequestSettings (base class of VssClientHttpRequestSettings) adds the current
            // UI culture to the list of accepted languages. The UI culture will be Invariant on OSX/Linux when the
            // LANG environment variable is not set when the program starts. If Invariant is in the list of accepted
            // languages, then "System.ArgumentException: The value cannot be null or empty." will be thrown when the
            // settings are applied to an HttpRequestMessage.
            settings.AcceptLanguages.Remove(CultureInfo.InvariantCulture);

            var headerValues = new List<ProductInfoHeaderValue>();
            headerValues.Add(new ProductInfoHeaderValue($"VstsAgentCore-{BuildConstants.AgentPackage.PackageName}", Constants.Agent.Version));
            headerValues.Add(new ProductInfoHeaderValue($"({RuntimeInformation.OSDescription.Trim()})"));

            if (settings.UserAgent != null && settings.UserAgent.Count > 0)
            {
                headerValues.AddRange(settings.UserAgent);
            }

            settings.UserAgent = headerValues;

            VssConnection connection = new VssConnection(serverUri, credentials, settings);
            return connection;
        }
Пример #3
0
        public async Task DownloadAsync(IExecutionContext executionContext, ArtifactDefinition artifactDefinition, string localFolderPath)
        {
            ArgUtil.NotNull(artifactDefinition, nameof(artifactDefinition));
            ArgUtil.NotNull(executionContext, nameof(executionContext));
            ArgUtil.NotNullOrEmpty(localFolderPath, nameof(localFolderPath));

            int buildId = Convert.ToInt32(artifactDefinition.Version, CultureInfo.InvariantCulture);
            if (buildId <= 0)
            {
                throw new ArgumentException("artifactDefinition.Version");
            }

            var buildArtifactDetails = artifactDefinition.Details as BuildArtifactDetails;
            if (buildArtifactDetails == null)
            {
                throw new ArgumentException("artifactDefinition.Details");
            }

            // Get the list of available artifacts from build. 
            executionContext.Output(StringUtil.Loc("RMPreparingToGetBuildArtifactList"));

            var vssConnection = new VssConnection(buildArtifactDetails.TfsUrl, buildArtifactDetails.Credentials);
            var buildClient = vssConnection.GetClient<BuildHttpClient>();
            var xamlBuildClient = vssConnection.GetClient<XamlBuildHttpClient>();
            List<ServerBuildArtifact> buildArtifacts = null;
            DefinitionType buildDefinitionType = DefinitionType.Build;

            try
            {
                buildArtifacts = await buildClient.GetArtifactsAsync(buildArtifactDetails.Project, buildId);
            }
            catch (BuildNotFoundException)
            {
                buildArtifacts = await xamlBuildClient.GetArtifactsAsync(buildArtifactDetails.Project, buildId);
                buildDefinitionType = DefinitionType.Xaml;
            }

            // No artifacts found in the build => Fail it. 
            if (buildArtifacts == null || !buildArtifacts.Any())
            {
                throw new ArtifactDownloadException(StringUtil.Loc("RMNoBuildArtifactsFound", buildId));
            }

            // DownloadFromStream each of the artifact sequentially. 
            // TODO: Should we download them parallely?
            foreach (ServerBuildArtifact buildArtifact in buildArtifacts)
            {
                if (Match(buildArtifact, artifactDefinition))
                {
                    executionContext.Output(StringUtil.Loc("RMPreparingToDownload", buildArtifact.Name));
                    await this.DownloadArtifactAsync(executionContext, buildArtifact, artifactDefinition, localFolderPath, buildClient, xamlBuildClient, buildDefinitionType, buildId);
                }
                else
                {
                    executionContext.Warning(StringUtil.Loc("RMArtifactMatchNotFound", buildArtifact.Name));
                }
            }
        }
Пример #4
0
 public void InitializePublisher(IExecutionContext executionContext, VssConnection connection, string projectName, IResultReader resultReader)
 {
     Trace.Entering();
     _executionContext = executionContext;
     _projectName = projectName;
     _resultReader = resultReader;
     connection.InnerHandler.Settings.SendTimeout = TimeSpan.FromSeconds(PUBLISH_TIMEOUT);
     _testResultsServer = HostContext.GetService<ITestResultsServer>();
     _testResultsServer.InitializeServer(connection);
     Trace.Leaving();
 }
Пример #5
0
        public async Task ConnectAsync(VssConnection agentConnection)
        {
            _connection = agentConnection;
            if (!_connection.HasAuthenticated)
            {
                await _connection.ConnectAsync();
            }

            _taskAgentClient = _connection.GetClient<TaskAgentHttpClient>();
            _hasConnection = true;
        }
Пример #6
0
        public async Task PublishCoverageSummaryAsync(VssConnection connection, string project, int buildId, IEnumerable<CodeCoverageStatistics> coverageData, CancellationToken cancellationToken)
        {
            var testHttpClient = connection.GetClient<TestManagementHttpClient>();
            // <todo: Bug 402783> We are currently passing BuildFlavor and BuildPlatform = "" There value are required be passed to command
            CodeCoverageData data = new CodeCoverageData()
            {
                BuildFlavor = "",
                BuildPlatform = "",
                CoverageStats = coverageData.ToList()
            };

            await testHttpClient.UpdateCodeCoverageSummaryAsync(data, project, buildId, cancellationToken: cancellationToken);
        }
Пример #7
0
        /// <summary>
        /// Asynchronously initializes the sink.
        /// </summary>
        /// <param name="name">The configuration name of the sink.</param>
        /// <param name="cancellationToken">A token to monitor for cancellation requests. The default value is <see cref="System.Threading.CancellationToken.None" />.</param>
        /// <returns>
        /// A <see cref="Task" /> that represents the asynchronous initialize operation.
        /// </returns>
        public async Task InitializeAsync(string name, CancellationToken cancellationToken = default(CancellationToken))
        {
            await Task.Delay(0);

            var element = Configuration.TfsConfigurationSection.Current.Sinks[name];

            // todo: support other auth methods, see the auth samples in https://www.visualstudio.com/en-us/integrate/get-started/client-libraries/samples
            // * OAuth
            // * ADD
            //var vssCredentials = new VssCredentials(); // Active directory auth - NTLM against a Team Foundation Server
            //var vssCredentials = new VssClientCredentials(); // Visual Studio sign-in prompt. Would need work to make this not prompt at every startup
            var vssCredentials = new VssBasicCredential("", element.AccessToken);
            var connection = new VssConnection(new Uri(element.ProjectCollection), vssCredentials);
            _witClient = connection.GetClient<WorkItemTrackingHttpClient>();
        }
Пример #8
0
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                ShowUsage();
                return;
            }

            string accountUrl = args[0];
            var minFinishTime = DateTime.Parse(args[1]);
            var maxFinishTime = GetMaxFinishTime(DateTime.Parse(args[2]));

            Console.WriteLine("Getting projects in the account:");
            VssConnection connection = new VssConnection(new Uri(accountUrl), new VssAadCredential());

            var projectClient = connection.GetClient<ProjectHttpClient>();
            var projects = projectClient.GetProjects().Result;

            var buildClient = connection.GetClient<BuildHttpClient>();

            foreach (var project in projects)
            {
                var builds = buildClient.GetBuildsAsync(project.Id, minFinishTime: minFinishTime, maxFinishTime: maxFinishTime).Result;

                if (builds.Count > 0)
                {
                    Console.WriteLine($"{project.Name} project had {builds.Count} builds run between {minFinishTime} and {maxFinishTime}");
                    foreach (var build in builds)
                    {
                        ReportBuildInformation(build);
                    }
                }
                else
                {
                    Console.WriteLine($"Project {project.Name} did not having any builds during that time.");
                }
            }
            Console.WriteLine("Press a key.");
            Console.ReadKey();
        }
Пример #9
0
        public async Task <TaskResult> RunAsync(Pipelines.AgentJobRequestMessage message, CancellationToken jobRequestCancellationToken)
        {
            // Validate parameters.
            Trace.Entering();
            ArgUtil.NotNull(message, nameof(message));
            ArgUtil.NotNull(message.Resources, nameof(message.Resources));
            ArgUtil.NotNull(message.Variables, nameof(message.Variables));
            ArgUtil.NotNull(message.Steps, nameof(message.Steps));
            Trace.Info("Job ID {0}", message.JobId);

            DateTime jobStartTimeUtc = DateTime.UtcNow;

            // Agent.RunMode
            RunMode runMode;

            if (message.Variables.ContainsKey(Constants.Variables.Agent.RunMode) &&
                Enum.TryParse(message.Variables[Constants.Variables.Agent.RunMode].Value, ignoreCase: true, result: out runMode) &&
                runMode == RunMode.Local)
            {
                HostContext.RunMode = runMode;
            }

            ServiceEndpoint systemConnection = message.Resources.Endpoints.Single(x => string.Equals(x.Name, WellKnownServiceEndpointNames.SystemVssConnection, StringComparison.OrdinalIgnoreCase));

            // System.AccessToken
            if (message.Variables.ContainsKey(Constants.Variables.System.EnableAccessToken) &&
                StringUtil.ConvertToBoolean(message.Variables[Constants.Variables.System.EnableAccessToken].Value))
            {
                message.Variables[Constants.Variables.System.AccessToken] = new VariableValue(systemConnection.Authorization.Parameters["AccessToken"], false);
            }

            // back compat TfsServerUrl
            message.Variables[Constants.Variables.System.TFServerUrl] = systemConnection.Url.AbsoluteUri;

            // Make sure SystemConnection Url and Endpoint Url match Config Url base for OnPremises server
            // System.ServerType will always be there after M133
            if (!message.Variables.ContainsKey(Constants.Variables.System.ServerType) ||
                string.Equals(message.Variables[Constants.Variables.System.ServerType]?.Value, "OnPremises", StringComparison.OrdinalIgnoreCase))
            {
                ReplaceConfigUriBaseInJobRequestMessage(message);
            }

            // Setup the job server and job server queue.
            var            jobServer           = HostContext.GetService <IJobServer>();
            VssCredentials jobServerCredential = VssUtil.GetVssCredential(systemConnection);
            Uri            jobServerUrl        = systemConnection.Url;

            Trace.Info($"Creating job server with URL: {jobServerUrl}");
            // jobServerQueue is the throttling reporter.
            _jobServerQueue = HostContext.GetService <IJobServerQueue>();
            VssConnection jobConnection = VssUtil.CreateConnection(jobServerUrl, jobServerCredential, new DelegatingHandler[] { new ThrottlingReportHandler(_jobServerQueue) });
            await jobServer.ConnectAsync(jobConnection);

            _jobServerQueue.Start(message);
            HostContext.WritePerfCounter($"WorkerJobServerQueueStarted_{message.RequestId.ToString()}");

            IExecutionContext             jobContext = null;
            CancellationTokenRegistration?agentShutdownRegistration = null;

            try
            {
                // Create the job execution context.
                jobContext = HostContext.CreateService <IExecutionContext>();
                jobContext.InitializeJob(message, jobRequestCancellationToken);
                Trace.Info("Starting the job execution context.");
                jobContext.Start();
                jobContext.Section(StringUtil.Loc("StepStarting", message.JobDisplayName));

                agentShutdownRegistration = HostContext.AgentShutdownToken.Register(() =>
                {
                    // log an issue, then agent get shutdown by Ctrl-C or Ctrl-Break.
                    // the server will use Ctrl-Break to tells the agent that operating system is shutting down.
                    string errorMessage;
                    switch (HostContext.AgentShutdownReason)
                    {
                    case ShutdownReason.UserCancelled:
                        errorMessage = StringUtil.Loc("UserShutdownAgent");
                        break;

                    case ShutdownReason.OperatingSystemShutdown:
                        errorMessage = StringUtil.Loc("OperatingSystemShutdown", Environment.MachineName);
                        break;

                    default:
                        throw new ArgumentException(HostContext.AgentShutdownReason.ToString(), nameof(HostContext.AgentShutdownReason));
                    }
                    jobContext.AddIssue(new Issue()
                    {
                        Type = IssueType.Error, Message = errorMessage
                    });
                });

                // Validate directory permissions.
                string workDirectory = HostContext.GetDirectory(WellKnownDirectory.Work);
                Trace.Info($"Validating directory permissions for: '{workDirectory}'");
                try
                {
                    Directory.CreateDirectory(workDirectory);
                    IOUtil.ValidateExecutePermission(workDirectory);
                }
                catch (Exception ex)
                {
                    Trace.Error(ex);
                    jobContext.Error(ex);
                    return(await CompleteJobAsync(jobServer, jobContext, message, TaskResult.Failed));
                }

                // Set agent variables.
                AgentSettings settings = HostContext.GetService <IConfigurationStore>().GetSettings();
                jobContext.Variables.Set(Constants.Variables.Agent.Id, settings.AgentId.ToString(CultureInfo.InvariantCulture));
                jobContext.SetVariable(Constants.Variables.Agent.HomeDirectory, HostContext.GetDirectory(WellKnownDirectory.Root), isFilePath: true);
                jobContext.Variables.Set(Constants.Variables.Agent.JobName, message.JobDisplayName);
                jobContext.Variables.Set(Constants.Variables.Agent.MachineName, Environment.MachineName);
                jobContext.Variables.Set(Constants.Variables.Agent.Name, settings.AgentName);
                jobContext.Variables.Set(Constants.Variables.Agent.OS, VarUtil.OS);
                jobContext.SetVariable(Constants.Variables.Agent.RootDirectory, HostContext.GetDirectory(WellKnownDirectory.Work), isFilePath: true);
#if OS_WINDOWS
                jobContext.SetVariable(Constants.Variables.Agent.ServerOMDirectory, HostContext.GetDirectory(WellKnownDirectory.ServerOM), isFilePath: true);
#else
                jobContext.Variables.Set(Constants.Variables.Agent.AcceptTeeEula, settings.AcceptTeeEula.ToString());
#endif
                jobContext.SetVariable(Constants.Variables.Agent.WorkFolder, HostContext.GetDirectory(WellKnownDirectory.Work), isFilePath: true);
                jobContext.SetVariable(Constants.Variables.System.WorkFolder, HostContext.GetDirectory(WellKnownDirectory.Work), isFilePath: true);

                string toolsDirectory = HostContext.GetDirectory(WellKnownDirectory.Tools);
                Directory.CreateDirectory(toolsDirectory);
                jobContext.SetVariable(Constants.Variables.Agent.ToolsDirectory, toolsDirectory, isFilePath: true);

                // Setup TEMP directories
                _tempDirectoryManager = HostContext.GetService <ITempDirectoryManager>();
                _tempDirectoryManager.InitializeTempDirectory(jobContext);

                // todo: task server can throw. try/catch and fail job gracefully.
                // prefer task definitions url, then TFS collection url, then TFS account url
                var taskServer    = HostContext.GetService <ITaskServer>();
                Uri taskServerUri = null;
                if (!string.IsNullOrEmpty(jobContext.Variables.System_TaskDefinitionsUri))
                {
                    taskServerUri = new Uri(jobContext.Variables.System_TaskDefinitionsUri);
                }
                else if (!string.IsNullOrEmpty(jobContext.Variables.System_TFCollectionUrl))
                {
                    taskServerUri = new Uri(jobContext.Variables.System_TFCollectionUrl);
                }

                var taskServerCredential = VssUtil.GetVssCredential(systemConnection);
                if (taskServerUri != null)
                {
                    Trace.Info($"Creating task server with {taskServerUri}");
                    await taskServer.ConnectAsync(VssUtil.CreateConnection(taskServerUri, taskServerCredential));
                }

                // for back compat TFS 2015 RTM/QU1, we may need to switch the task server url to agent config url
                if (!string.Equals(message.Variables.GetValueOrDefault(Constants.Variables.System.ServerType)?.Value, "Hosted", StringComparison.OrdinalIgnoreCase))
                {
                    if (taskServerUri == null || !await taskServer.TaskDefinitionEndpointExist())
                    {
                        Trace.Info($"Can't determine task download url from JobMessage or the endpoint doesn't exist.");
                        var configStore = HostContext.GetService <IConfigurationStore>();
                        taskServerUri = new Uri(configStore.GetSettings().ServerUrl);
                        Trace.Info($"Recreate task server with configuration server url: {taskServerUri}");
                        await taskServer.ConnectAsync(VssUtil.CreateConnection(taskServerUri, taskServerCredential));
                    }
                }

                // Expand the endpoint data values.
                foreach (ServiceEndpoint endpoint in jobContext.Endpoints)
                {
                    jobContext.Variables.ExpandValues(target: endpoint.Data);
                    VarUtil.ExpandEnvironmentVariables(HostContext, target: endpoint.Data);
                }

                // Expand the repository property values.
                foreach (var repository in jobContext.Repositories)
                {
                    Dictionary <string, string> expandProperties = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                    foreach (var property in repository.Properties.GetItems())
                    {
                        expandProperties[property.Key] = JsonUtility.ToString(property.Value);
                    }

                    jobContext.Variables.ExpandValues(target: expandProperties);
                    VarUtil.ExpandEnvironmentVariables(HostContext, target: expandProperties);

                    foreach (var expandedProperty in expandProperties)
                    {
                        repository.Properties.Set <JToken>(expandedProperty.Key, JsonUtility.FromString <JToken>(expandedProperty.Value));
                    }
                }

                // Get the job extension.
                Trace.Info("Getting job extension.");
                var hostType         = jobContext.Variables.System_HostType;
                var extensionManager = HostContext.GetService <IExtensionManager>();
                // We should always have one job extension
                IJobExtension jobExtension =
                    (extensionManager.GetExtensions <IJobExtension>() ?? new List <IJobExtension>())
                    .Where(x => x.HostType.HasFlag(hostType))
                    .FirstOrDefault();
                ArgUtil.NotNull(jobExtension, nameof(jobExtension));

                List <IStep> jobSteps = new List <IStep>();
                try
                {
                    Trace.Info("Initialize job. Getting all job steps.");
                    var initializeResult = await jobExtension.InitializeJob(jobContext, message);

                    jobSteps.AddRange(initializeResult.PreJobSteps);
                    jobSteps.AddRange(initializeResult.JobSteps);
                    jobSteps.AddRange(initializeResult.PostJobStep);
                }
                catch (OperationCanceledException ex) when(jobContext.CancellationToken.IsCancellationRequested)
                {
                    // set the job to canceled
                    // don't log error issue to job ExecutionContext, since server owns the job level issue
                    Trace.Error($"Job is canceled during initialize.");
                    Trace.Error($"Caught exception: {ex}");
                    return(await CompleteJobAsync(jobServer, jobContext, message, TaskResult.Canceled));
                }
                catch (Exception ex)
                {
                    // set the job to failed.
                    // don't log error issue to job ExecutionContext, since server owns the job level issue
                    Trace.Error($"Job initialize failed.");
                    Trace.Error($"Caught exception from {nameof(jobExtension.InitializeJob)}: {ex}");
                    return(await CompleteJobAsync(jobServer, jobContext, message, TaskResult.Failed));
                }

                // trace out all steps
                Trace.Info($"Total job steps: {jobSteps.Count}.");
                Trace.Verbose($"Job steps: '{string.Join(", ", jobSteps.Select(x => x.DisplayName))}'");
                HostContext.WritePerfCounter($"WorkerJobInitialized_{message.RequestId.ToString()}");

                bool             processCleanup    = jobContext.Variables.GetBoolean("process.clean") ?? true;
                HashSet <string> existingProcesses = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                string           processLookupId   = null;
                if (processCleanup)
                {
                    processLookupId = $"vsts_{Guid.NewGuid()}";

                    // Set the VSTS_PROCESS_LOOKUP_ID env variable.
                    jobContext.SetVariable(Constants.ProcessLookupId, processLookupId, false, false);

                    // Take a snapshot of current running processes
                    Dictionary <int, Process> processes = SnapshotProcesses();
                    foreach (var proc in processes)
                    {
                        // Pid_ProcessName
                        existingProcesses.Add($"{proc.Key}_{proc.Value.ProcessName}");
                    }
                }

                // Run all job steps
                Trace.Info("Run all job steps.");
                var stepsRunner = HostContext.GetService <IStepsRunner>();
                try
                {
                    await stepsRunner.RunAsync(jobContext, jobSteps);
                }
                catch (Exception ex)
                {
                    // StepRunner should never throw exception out.
                    // End up here mean there is a bug in StepRunner
                    // Log the error and fail the job.
                    Trace.Error($"Caught exception from job steps {nameof(StepsRunner)}: {ex}");
                    jobContext.Error(ex);
                    return(await CompleteJobAsync(jobServer, jobContext, message, TaskResult.Failed));
                }
                finally
                {
                    if (processCleanup)
                    {
                        // Only check environment variable for any process that doesn't run before we invoke our process.
                        Dictionary <int, Process> currentProcesses = SnapshotProcesses();
                        foreach (var proc in currentProcesses)
                        {
                            if (existingProcesses.Contains($"{proc.Key}_{proc.Value.ProcessName}"))
                            {
                                Trace.Verbose($"Skip existing process. PID: {proc.Key} ({proc.Value.ProcessName})");
                            }
                            else
                            {
                                Trace.Info($"Inspecting process environment variables. PID: {proc.Key} ({proc.Value.ProcessName})");

                                string lookupId = null;
                                try
                                {
                                    lookupId = proc.Value.GetEnvironmentVariable(HostContext, Constants.ProcessLookupId);
                                }
                                catch (Exception ex)
                                {
                                    Trace.Verbose("Ignore any exception during read process environment variables.");
                                    Trace.Verbose(ex.ToString());
                                }

                                if (string.Equals(lookupId, processLookupId, StringComparison.OrdinalIgnoreCase))
                                {
                                    Trace.Info($"Terminate orphan process: pid ({proc.Key}) ({proc.Value.ProcessName})");
                                    try
                                    {
                                        proc.Value.Kill();
                                    }
                                    catch (Exception ex)
                                    {
                                        Trace.Error("Catch exception during orphan process cleanup.");
                                        Trace.Error(ex);
                                    }
                                }
                            }
                        }
                    }
                }

                Trace.Info($"Job result after all job steps finish: {jobContext.Result ?? TaskResult.Succeeded}");

                if (jobContext.Variables.GetBoolean(Constants.Variables.Agent.Diagnostic) ?? false)
                {
                    Trace.Info("Support log upload starting.");

                    IDiagnosticLogManager diagnosticLogManager = HostContext.GetService <IDiagnosticLogManager>();

                    try
                    {
                        await diagnosticLogManager.UploadDiagnosticLogsAsync(executionContext : jobContext, message : message, jobStartTimeUtc : jobStartTimeUtc);

                        Trace.Info("Support log upload complete.");
                    }
                    catch (Exception ex)
                    {
                        // Log the error but make sure we continue gracefully.
                        Trace.Info("Error uploading support logs.");
                        Trace.Error(ex);
                    }
                }

                Trace.Info("Completing the job execution context.");
                return(await CompleteJobAsync(jobServer, jobContext, message));
            }
            finally
            {
                if (agentShutdownRegistration != null)
                {
                    agentShutdownRegistration.Value.Dispose();
                    agentShutdownRegistration = null;
                }

                await ShutdownQueue(throwOnFailure : false);
            }
        }
Пример #10
0
        private BuildDropManager CreateBulidDropManager(AgentTaskPluginExecutionContext context, VssConnection connection)
        {
            var dedupStoreHttpClient = connection.GetClient <DedupStoreHttpClient>();
            var tracer = new CallbackAppTraceSource(str => context.Output(str), System.Diagnostics.SourceLevels.Information);

            dedupStoreHttpClient.SetTracer(tracer);
            var client           = new DedupStoreClientWithDataport(dedupStoreHttpClient, 16 * Environment.ProcessorCount);
            var buildDropManager = new BuildDropManager(client, tracer);

            return(buildDropManager);
        }
Пример #11
0
        public GitPullRequest CreatePullRequestInner(bool cleanUp)
        {
            VssConnection connection = this.Context.Connection;
            GitHttpClient gitClient  = connection.GetClient <GitHttpClient>();

            TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
            GitRepository        repo    = GitSampleHelpers.FindAnyRepository(this.Context, project.Id);

            // we need a new branch with changes in order to create a PR
            // first, find the default branch
            string defaultBranchName = GitSampleHelpers.WithoutRefsPrefix(repo.DefaultBranch);
            GitRef defaultBranch     = gitClient.GetRefsAsync(repo.Id, filter: defaultBranchName).Result.First();

            // next, craft the branch and commit that we'll push
            GitRefUpdate newBranch = new GitRefUpdate()
            {
                Name        = $"refs/heads/vsts-api-sample/{GitSampleHelpers.ChooseRefsafeName()}",
                OldObjectId = defaultBranch.ObjectId,
            };
            string       newFileName = $"{GitSampleHelpers.ChooseItemsafeName()}.md";
            GitCommitRef newCommit   = new GitCommitRef()
            {
                Comment = "Add a sample file",
                Changes = new GitChange[]
                {
                    new GitChange()
                    {
                        ChangeType = VersionControlChangeType.Add,
                        Item       = new GitItem()
                        {
                            Path = $"/vsts-api-sample/{newFileName}"
                        },
                        NewContent = new ItemContent()
                        {
                            Content     = "# Thank you for using VSTS!",
                            ContentType = ItemContentType.RawText,
                        },
                    }
                },
            };

            // create the push with the new branch and commit
            GitPush push = gitClient.CreatePushAsync(new GitPush()
            {
                RefUpdates = new GitRefUpdate[] { newBranch },
                Commits    = new GitCommitRef[] { newCommit },
            }, repo.Id).Result;

            // finally, create a PR
            var pr = gitClient.CreatePullRequestAsync(new GitPullRequest()
            {
                SourceRefName = newBranch.Name,
                TargetRefName = repo.DefaultBranch,
                Title         = $"Add {newFileName} (from VSTS REST samples)",
                Description   = "Adding this file from the pull request samples",
            },
                                                      repo.Id).Result;

            Console.WriteLine("project {0}, repo {1}", project.Name, repo.Name);
            Console.WriteLine("{0} (#{1}) {2} -> {3}",
                              pr.Title.Substring(0, Math.Min(40, pr.Title.Length)),
                              pr.PullRequestId,
                              pr.SourceRefName,
                              pr.TargetRefName);

            if (cleanUp)
            {
                // clean up after ourselves (and in case logging is on, don't log these calls)
                ClientSampleHttpLogger.SetSuppressOutput(this.Context, true);

                // abandon the PR
                GitPullRequest updatedPr = new GitPullRequest()
                {
                    Status = PullRequestStatus.Abandoned,
                };
                pr = gitClient.UpdatePullRequestAsync(updatedPr, repo.Id, pr.PullRequestId).Result;

                // delete the branch
                GitRefUpdateResult refDeleteResult = gitClient.UpdateRefsAsync(
                    new GitRefUpdate[]
                {
                    new GitRefUpdate()
                    {
                        OldObjectId = push.RefUpdates.First().NewObjectId,
                        NewObjectId = new string('0', 40),
                        Name        = push.RefUpdates.First().Name,
                    }
                },
                    repositoryId: repo.Id).Result.First();
            }

            return(pr);
        }
        public ReleaseClient(IOptions <VstsConfiguration> configuration)
        {
            _configuration = configuration;

            _vssConnection = SetConnection();
        }
        // Download for version 2. This decision was made because version 1 is sealed and we didn't want to break any existing customers.
        internal async Task DownloadAsyncV2(
            AgentTaskPluginExecutionContext context,
            PipelineArtifactDownloadParameters downloadParameters,
            DownloadOptions downloadOptions,
            CancellationToken cancellationToken)
        {
            VssConnection connection  = context.VssConnection;
            BuildServer   buildServer = new BuildServer(connection);

            // download all pipeline artifacts if artifact name is missing
            if (downloadOptions == DownloadOptions.MultiDownload)
            {
                List <BuildArtifact> artifacts;
                if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                {
                    artifacts = await buildServer.GetArtifactsAsync(downloadParameters.ProjectId, downloadParameters.PipelineId, cancellationToken);
                }
                else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                {
                    if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                    {
                        throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                    }
                    else
                    {
                        artifacts = await buildServer.GetArtifactsWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, cancellationToken);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Invalid {nameof(downloadParameters.ProjectRetrievalOptions)}!");
                }

                IEnumerable <BuildArtifact> buildArtifacts     = artifacts.Where(a => string.Equals(a.Resource.Type, PipelineArtifactConstants.Container, StringComparison.OrdinalIgnoreCase));
                IEnumerable <BuildArtifact> pipelineArtifacts  = artifacts.Where(a => string.Equals(a.Resource.Type, PipelineArtifactConstants.PipelineArtifact, StringComparison.OrdinalIgnoreCase));
                IEnumerable <BuildArtifact> fileShareArtifacts = artifacts.Where(a => string.Equals(a.Resource.Type, PipelineArtifactConstants.FileShareArtifact, StringComparison.OrdinalIgnoreCase));

                if (buildArtifacts.Any())
                {
                    FileContainerProvider provider = new FileContainerProvider(connection, this.tracer);
                    await provider.DownloadMultipleArtifactsAsync(downloadParameters, buildArtifacts, cancellationToken, context);
                }

                if (pipelineArtifacts.Any())
                {
                    PipelineArtifactProvider provider = new PipelineArtifactProvider(context, connection, this.tracer);
                    await provider.DownloadMultipleArtifactsAsync(downloadParameters, pipelineArtifacts, cancellationToken, context);
                }

                if (fileShareArtifacts.Any())
                {
                    FileShareProvider provider = new FileShareProvider(context, connection, this.tracer);
                    await provider.DownloadMultipleArtifactsAsync(downloadParameters, fileShareArtifacts, cancellationToken, context);
                }
            }
            else if (downloadOptions == DownloadOptions.SingleDownload)
            {
                // 1) get manifest id from artifact data
                BuildArtifact buildArtifact;
                if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                {
                    buildArtifact = await buildServer.GetArtifact(downloadParameters.ProjectId, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                }
                else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                {
                    if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                    {
                        throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                    }
                    else
                    {
                        buildArtifact = await buildServer.GetArtifactWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                    }
                }
                else
                {
                    throw new InvalidOperationException($"Invalid {nameof(downloadParameters.ProjectRetrievalOptions)}!");
                }

                ArtifactProviderFactory factory  = new ArtifactProviderFactory(context, connection, this.tracer);
                IArtifactProvider       provider = factory.GetProvider(buildArtifact);

                await provider.DownloadSingleArtifactAsync(downloadParameters, buildArtifact, cancellationToken, context);
            }
            else
            {
                throw new InvalidOperationException($"Invalid {nameof(downloadOptions)}!");
            }
        }
Пример #14
0
        public void GetTFSTickets()
        {
            Uri           orgUrl = new Uri("https://dev.azure.com/DCTTFS/");
            String        personalAccessToken = "ndzzurgegqnpmjwrqhwji2c5exr7wffx4bzjhuccdfprfsdjuydq";
            VssConnection connection          = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));

            WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>();

            Wiql wiql = new Wiql();

            wiql.Query = "SELECT [System.Id],[System.Title],[System.State],[Custom._Severity],[Custom._Module],[System.Description],[System.IterationPath],[Custom.Stream] FROM workitems where [System.State] = 'New'";

            WorkItemQueryResult tasks = witClient.QueryByWiqlAsync(wiql).Result;

            IEnumerable <WorkItemReference> tasksRefs;

            tasksRefs = tasks.WorkItems.OrderBy(x => x.Id);

            List <WorkItem> tasksList = witClient.GetWorkItemsAsync(tasksRefs.Select(wir => wir.Id)).Result;

            tickets = new List <Ticket>();


            foreach (var task in tasksList)
            {
                tickets.Add(new Ticket
                {
                    ID          = task.Id,
                    Title       = task.Fields["System.Title"].ToString(),
                    State       = task.Fields["System.State"].ToString(),
                    Description = "NA",
                    Severity    = task.Fields["Custom._Severity"].ToString(),
                    Module      = task.Fields["Custom._Module"].ToString(),
                    Sprint      = task.Fields["System.IterationPath"].ToString().Split('\\')[1],
                    Stream      = task.Fields["Custom.Stream"].ToString()
                });
            }


            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "server = tcp:hatch.database.windows.net,1433; initial catalog = acrf; persist security info = false; user id = dct; password = Duck@1234; multipleactiveresultsets = false; encrypt = true; trustservercertificate = false; connection timeout = 30;";
                // using the code here...


                conn.Open();
                //SqlCommand cmd = conn.CreateCommand();
                //SqlTransaction transaction;
                //transaction = conn.BeginTransaction();
                //cmd.Transaction = transaction;
                //cmd.Connection = conn;
                try
                {
                    foreach (var task in tasksList)
                    {
                        string sqlstr = "InsertTFSTickets";
                        //sqlstr = "insert into tbl_employee(empid,managerempid,profile,projectid,password,createdby,createdon) values (@empid,@createdby,@createdon)";
                        //sqlstr = "insert into TFSTicketsData(ID,Title,Description,State,Severity,Module) values (@ID,@Title,@Description,@State,@Severity,@Module)";
                        //sqlstr = "insert into tbl_employee(empid,managerempid,profile,projectid,password,status,experties,stream) values (@empid,@managerempid,@profile,@projectid,@password,@status,@experties,@stream)";


                        SqlCommand cmd = new SqlCommand(sqlstr, conn);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@ID", task.Id);
                        cmd.Parameters.AddWithValue("@Title", task.Fields["System.Title"].ToString());
                        cmd.Parameters.AddWithValue("@Description", "NA");
                        cmd.Parameters.AddWithValue("@State", task.Fields["System.State"].ToString());
                        cmd.Parameters.AddWithValue("@Severity", task.Fields["Custom._Severity"].ToString());
                        cmd.Parameters.AddWithValue("@Module", task.Fields["Custom._Module"].ToString());
                        cmd.Parameters.AddWithValue("@Sprint", task.Fields["System.IterationPath"].ToString().Split('\\')[1].Replace(" ", "").ToLower());
                        cmd.Parameters.AddWithValue("@Stream", task.Fields["Custom.Stream"].ToString());
                        cmd.ExecuteNonQuery();
                    }

                    //transaction.Commit();
                    conn.Close();
                    //result = "employee added successfully!";
                }
                catch (Exception ex)
                {
                    //transaction.Rollback();
                    conn.Close();
                    //global.errorhandlerclass.logerror(ex);
                    //result = ex.message;
                }
            }

            connection.Disconnect();
        }
Пример #15
0
        public T GetClient <T>(VssClientHttpRequestSettings settings) where T : VssHttpClientBase
        {
            var connection = new VssConnection(VssConnection.Uri, VssConnection.Credentials, settings);

            return(VssConnection.GetClient <T>());
        }
Пример #16
0
 public ClientFactory(VssConnection vssConnection)
 {
     VssConnection = vssConnection;
 }
Пример #17
0
        public static async Task LinkPrBuild(DiscordClient client, DiscordMessage message, int pr)
        {
            var prInfo = await githubClient.GetPrInfoAsync(pr, Config.Cts.Token).ConfigureAwait(false);

            if (prInfo.Number == 0)
            {
                await message.ReactWithAsync(Config.Reactions.Failure, prInfo.Message ?? "PR not found").ConfigureAwait(false);

                return;
            }

            var prState = prInfo.GetState();
            var embed   = prInfo.AsEmbed();

            if (prState.state == "Open" || prState.state == "Closed")
            {
                var    downloadHeader      = "Windows PR Build";
                var    downloadText        = "⏳ Pending...";
                var    linuxDownloadHeader = "Linux PR Build";
                string linuxDownloadText   = null;

                // windows build
                if (prInfo.StatusesUrl is string statusesUrl)
                {
                    if (await appveyorClient.GetPrDownloadAsync(prInfo.Number, prInfo.CreatedAt, Config.Cts.Token).ConfigureAwait(false) is ArtifactInfo artifactInfo)
                    {
                        if (artifactInfo.Artifact.Created is DateTime buildTime)
                        {
                            downloadHeader = $"{downloadHeader} ({(DateTime.UtcNow - buildTime.ToUniversalTime()).AsTimeDeltaDescription()} ago)";
                        }
                        var name = artifactInfo.Artifact.FileName;
                        name         = name.Replace("rpcs3-", "").Replace("_win64", "");
                        downloadText = $"[⏬ {name}]({artifactInfo.DownloadUrl})";
                    }
                    else
                    {
                        var statuses = await githubClient.GetStatusesAsync(statusesUrl, Config.Cts.Token).ConfigureAwait(false);

                        statuses     = statuses?.Where(s => s.Context == appveyorContext).ToList();
                        downloadText = statuses?.FirstOrDefault()?.Description ?? downloadText;
                    }
                }
                else if (await appveyorClient.GetPrDownloadAsync(prInfo.Number, prInfo.CreatedAt, Config.Cts.Token).ConfigureAwait(false) is ArtifactInfo artifactInfo)
                {
                    if (artifactInfo.Artifact.Created is DateTime buildTime)
                    {
                        downloadHeader = $"{downloadHeader} ({(DateTime.UtcNow - buildTime.ToUniversalTime()).AsTimeDeltaDescription()} ago)";
                    }
                    var name = artifactInfo.Artifact.FileName;
                    name         = name.Replace("rpcs3-", "").Replace("_win64", "");
                    downloadText = $"[⏬ {name}]({artifactInfo.DownloadUrl})";
                }

                // linux build
                var personalAccessToken = Config.AzureDevOpsToken;
                if (!string.IsNullOrEmpty(personalAccessToken))
                {
                    try
                    {
                        linuxDownloadText = "⏳ Pending...";
                        var azureCreds           = new VssBasicCredential("bot", personalAccessToken);
                        var azureConnection      = new VssConnection(new Uri("https://dev.azure.com/nekotekina"), azureCreds);
                        var azurePipelinesClient = azureConnection.GetClient <BuildHttpClient>();
                        var projectId            = new Guid("3598951b-4d39-4fad-ad3b-ff2386a649de");
                        var builds = await azurePipelinesClient.GetBuildsAsync(
                            projectId,
                            repositoryId : "RPCS3/rpcs3",
                            repositoryType : "GitHub",
                            reasonFilter : BuildReason.PullRequest,
                            cancellationToken : Config.Cts.Token
                            ).ConfigureAwait(false);

                        var filterBranch = $"refs/pull/{pr}/merge";
                        builds = builds
                                 .Where(b => b.SourceBranch == filterBranch && b.TriggerInfo.TryGetValue("pr.sourceSha", out var trc) && trc.Equals(prInfo.Head?.Sha, StringComparison.InvariantCultureIgnoreCase))
                                 .OrderByDescending(b => b.StartTime)
                                 .ToList();
                        var latestBuild = builds.FirstOrDefault();
                        if (latestBuild != null)
                        {
                            if (latestBuild.Status == BuildStatus.Completed && latestBuild.FinishTime.HasValue)
                            {
                                linuxDownloadHeader = $"{linuxDownloadHeader} ({(DateTime.UtcNow - latestBuild.FinishTime.Value.ToUniversalTime()).AsTimeDeltaDescription()} ago)";
                            }
                            var artifacts = await azurePipelinesClient.GetArtifactsAsync(projectId, latestBuild.Id, cancellationToken : Config.Cts.Token).ConfigureAwait(false);

                            var buildArtifact = artifacts.FirstOrDefault(a => a.Name.EndsWith(".GCC"));
                            var linuxBuild    = buildArtifact?.Resource;
                            if (linuxBuild?.DownloadUrl is string downloadUrl)
                            {
                                var name = buildArtifact.Name ?? $"Linux build {latestBuild.Id}.zip";
                                if (linuxBuild.DownloadUrl.Contains("format=zip", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    try
                                    {
                                        using var httpClient = HttpClientFactory.Create();
                                        using var stream     = await httpClient.GetStreamAsync(downloadUrl).ConfigureAwait(false);

                                        using var zipStream = ReaderFactory.Open(stream);
                                        while (zipStream.MoveToNextEntry())
                                        {
                                            if (zipStream.Entry.Key.EndsWith(".AppImage", StringComparison.InvariantCultureIgnoreCase))
                                            {
                                                name = Path.GetFileName(zipStream.Entry.Key);
                                                break;
                                            }
                                        }
                                    }
                                    catch (Exception e2)
                                    {
                                        Config.Log.Error(e2, "Failed to get linux build filename");
                                    }
                                }
                                name = name.Replace("rpcs3-", "").Replace("_linux64", "");
                                linuxDownloadText = $"[⏬ {name}]({downloadUrl})";
                            }
                            //var linuxBuildSize = linuxBuild?.Properties.TryGetValue("artifactsize", out var artifactSizeStr) && int.TryParse(artifactSizeStr, out var linuxBuildSize);
                        }
                    }
                    catch (Exception e)
                    {
                        Config.Log.Error(e, "Failed to get Azure DevOps build info");
                        linuxDownloadText = null; // probably due to expired access token
                    }
                }

                if (!string.IsNullOrEmpty(downloadText))
                {
                    embed.AddField(downloadHeader, downloadText, true);
                }
                if (!string.IsNullOrEmpty(linuxDownloadText))
                {
                    embed.AddField(linuxDownloadHeader, linuxDownloadText, true);
                }
            }
            else if (prState.state == "Merged")
            {
                var mergeTime  = prInfo.MergedAt.GetValueOrDefault();
                var now        = DateTime.UtcNow;
                var updateInfo = await compatApiClient.GetUpdateAsync(Config.Cts.Token).ConfigureAwait(false);

                if (updateInfo != null)
                {
                    if (DateTime.TryParse(updateInfo.LatestBuild?.Datetime, out var masterBuildTime) && masterBuildTime.Ticks >= mergeTime.Ticks)
                    {
                        embed = await updateInfo.AsEmbedAsync(client, false, embed).ConfigureAwait(false);
                    }
                    else
                    {
                        var waitTime = TimeSpan.FromMinutes(5);
                        if (now < (mergeTime + AvgBuildTime))
                        {
                            waitTime = mergeTime + AvgBuildTime - now;
                        }
                        embed.AddField("Latest master build", $"This pull request has been merged, and will be part of `master` very soon.\nPlease check again in {waitTime.AsTimeDeltaDescription()}.");
                    }
                }
            }
            await message.RespondAsync(embed : embed).ConfigureAwait(false);
        }
Пример #18
0
        public ReleaseDefinition CreateReleaseDefinition()
        {
            string projectName   = ClientSampleHelpers.FindAnyProject(this.Context).Name;
            string currentUserId = ClientSampleHelpers.GetCurrentUserId(this.Context).ToString();

            // If you want to override varaibles at release create time you should set 'AllowOverride'.
            ConfigurationVariableValue nonSceretVariable = new ConfigurationVariableValue();

            nonSceretVariable.Value         = "NonSecretValue";
            nonSceretVariable.IsSecret      = false;
            nonSceretVariable.AllowOverride = true;

            ConfigurationVariableValue sceretVariable = new ConfigurationVariableValue();

            sceretVariable.Value    = "SecretValue";
            sceretVariable.IsSecret = true;

            ConfigurationVariableValue nonOverrideVariable = new ConfigurationVariableValue();

            nonOverrideVariable.Value         = "NonOverridevariable";
            nonOverrideVariable.IsSecret      = false;
            nonOverrideVariable.AllowOverride = false;

            Dictionary <string, ConfigurationVariableValue> releaseLevelVariables = new Dictionary <string, ConfigurationVariableValue>();

            releaseLevelVariables.Add(NonSecretReleaseLevelVaraible, nonSceretVariable);
            releaseLevelVariables.Add("SecretReleaseLevelVaraible", sceretVariable);
            releaseLevelVariables.Add("NonOverridevariable", nonOverrideVariable);

            ReleaseDefinition definition = new ReleaseDefinition()
            {
                Name         = releaseDefinitionName,
                Revision     = 1,
                Variables    = releaseLevelVariables, // You can add varaibles at environment level also.
                Environments = new List <ReleaseDefinitionEnvironment>()
                {
                    new ReleaseDefinitionEnvironment()
                    {
                        Name         = "PROD",
                        DeployPhases = new List <DeployPhase>()
                        {
                            new AgentBasedDeployPhase()
                            {
                                Name            = "Run on agent",
                                Rank            = 1,
                                DeploymentInput = new AgentDeploymentInput()
                                {
                                    QueueId = 1
                                }
                            }
                        },
                        PreDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = false,
                                    Rank        = 1,
                                    Approver    = new IdentityRef()
                                    {
                                        Id = currentUserId
                                    }
                                },
                            }
                        },
                        PostDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = true,
                                    Rank        = 1
                                }
                            }
                        },
                        RetentionPolicy = new EnvironmentRetentionPolicy()
                        {
                            DaysToKeep     = 30,
                            ReleasesToKeep = 3,
                            RetainBuild    = true
                        }
                    }
                }
            };

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            // create a release definition
            ReleaseDefinition releaseDefinition = releaseClient.CreateReleaseDefinitionAsync(project: projectName, releaseDefinition: definition).Result;

            newlyCreatedReleaseDefinitionId = releaseDefinition.Id;

            Context.Log("{0} {1}", releaseDefinition.Id.ToString().PadLeft(6), releaseDefinition.Name);

            return(releaseDefinition);
        }
        /// <summary>
        /// Gets all team members for the given <paramref name="projectId" /> and <paramref name="teamId"/>.
        /// </summary>
        /// <param name="client">The <see cref="TeamHttpClient" /> to use.</param>
        /// <param name="connection">The connection for the <paramref name="client"/> that will be used to retrieve the identities for the team members.</param>
        /// <param name="projectId">The project identifier.</param>
        /// <param name="teamId">The team identifier whose members to retrieve.</param>
        /// <param name="pageSize">Page size to use while retrieving the projects.</param>
        /// <param name="userState">The user state object.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        /// <exception cref="System.ArgumentException">$The '{nameof(connection)}' parameter must be for the given '{nameof(client)}'</exception>
        public static Task<IReadOnlyCollection<Identity>> GetAllTeamMembers(this TeamHttpClient client, VssConnection connection, Guid projectId, Guid teamId, int pageSize = 10, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (client == null)
                throw new ArgumentNullException(nameof(client));
            if (connection == null)
                throw new ArgumentNullException(nameof(connection));
            if (Equals(Guid.Empty, projectId))
                throw new ArgumentException(nameof(projectId));
            if (Equals(Guid.Empty, teamId))
                throw new ArgumentException(nameof(teamId));

            return client.GetAllTeamMembers(connection, projectId.ToString(), teamId.ToString(), pageSize, userState, cancellationToken);
        }
        public void AddRemoveAADGroupMembership()
        {
            // Get the client
            VssConnection   connection  = Context.Connection;
            GraphHttpClient graphClient = connection.GetClient <GraphHttpClient>();

            //
            // Part 1: create a group at the account level
            //

            GraphGroupCreationContext createGroupContext = new GraphGroupVstsCreationContext
            {
                DisplayName = "Developers-" + Guid.NewGuid(),
                Description = "Group created via client library"
            };
            GraphGroup parentGroup           = graphClient.CreateGroupAsync(createGroupContext).Result;
            string     parentGroupDescriptor = parentGroup.Descriptor;

            Context.Log("New group created! ID: {0}", parentGroupDescriptor);

            //
            // Part 2: add the AAD group
            //

            GraphGroupCreationContext addAADGroupContext = new GraphGroupOriginIdCreationContext
            {
                OriginId = "a42aad15-d654-4b16-9309-9ee34d5aacfb"
            };
            GraphGroup aadGroup           = graphClient.CreateGroupAsync(addAADGroupContext).Result;
            string     aadGroupDescriptor = aadGroup.Descriptor;

            Context.Log("AAD group added! ID: {0}", aadGroupDescriptor);

            //
            // Part 3: Make the AAD group a member of the VSTS 'Developers' group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "CreateMembershipAADGroup");
            GraphMembership graphMembership = graphClient.AddMembershipAsync(aadGroupDescriptor, parentGroupDescriptor).Result;

            //
            // Part 4: get the membership
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipAADGroup");
            graphMembership = graphClient.GetMembershipAsync(aadGroupDescriptor, parentGroupDescriptor).Result;

            //
            // Part 5: Check to see if the AAD group is a member of the VSTS 'Developers' group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "CheckMembershipAADGroup");
            graphClient.CheckMembershipExistenceAsync(aadGroupDescriptor, parentGroupDescriptor).SyncResult();

            //
            // Part 6: Get every group the subject(AAD group) is a member of
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipsAADGroupDown");
            List <GraphMembership> membershipsForUser = graphClient.GetMembershipsAsync(aadGroupDescriptor).Result;

            //
            // Part 7: Get every member of the VSTS 'Developers' group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipsAADGroupUp");
            List <GraphMembership> membershipsOfGroup = graphClient.GetMembershipsAsync(parentGroupDescriptor, Microsoft.VisualStudio.Services.Graph.GraphTraversalDirection.Down.ToString()).Result; //Bug 967647: REST: GetMembershipsAsync shouldn't take direction as string, it should be the GraphTraversalDirection enum

            //
            // Part 8: Remove member from the group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "DeleteMembershipAADGroup");
            graphClient.RemoveMembershipAsync(aadGroupDescriptor, parentGroupDescriptor).SyncResult();
            try
            {
                graphClient.CheckMembershipExistenceAsync(aadGroupDescriptor, parentGroupDescriptor).SyncResult();
            }
            catch (Exception e)
            {
                Context.Log("AAD Group is no longer a member of the group:" + e.Message);
            }

            //
            // Part 9: delete the groups
            //
            graphClient.DeleteGroupAsync(aadGroupDescriptor).SyncResult();
            graphClient.DeleteGroupAsync(parentGroupDescriptor).SyncResult();
        }
        static void Main(string[] args)
        {
            string collectionUri;

            //set to Uri of the TFS collection
            //if this code is running in Build/Release workflow, we will fetch collection Uri from environment variable
            //See https://www.visualstudio.com/en-us/docs/build/define/variables for full list of agent environment variables
            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                collectionUri = Environment.GetEnvironmentVariable("SYSTEM_TEAMFOUNDATIONCOLLECTIONURI");
                Console.WriteLine("Fetched Collection (or VSTS account) from environment variable SYSTEM_TEAMFOUNDATIONCOLLECTIONURI: {0}", collectionUri);
            }
            else // set it to TFS instance of your choice
            {
                collectionUri = "http://buildmachine1:8080/tfs/DefaultCollection";
                Console.WriteLine("Using Collection (or VSTS account): {0}", collectionUri);
            }

            //authentication..
            VssConnection connection = new VssConnection(new Uri(collectionUri), new VssCredentials());

            //set the team project name in which the test results must be published...
            // get team project name from the agent environment variables if the script is running in Build workflow..
            string teamProject;

            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                teamProject = Environment.GetEnvironmentVariable("SYSTEM_TEAMPROJECT");
                Console.WriteLine("Fetched team project from environment variable SYSTEM_TEAMPROJECT: {0}", teamProject);
            }
            else //else set it the team project of your choice...
            {
                teamProject = "FabrikamFiber";
                Console.WriteLine("Using team project: {0}", teamProject);
            }

            // get the build number to publis results against...

            string buildNumber = null, buildUri = null, releaseUri = null, releaseEnvironmentUri = null; int buildId;

            // if this code is running in build/release workflow, we will use agent environment variables for fetch build/release Uris to associate information
            if (Environment.GetEnvironmentVariable("TF_BUILD") == "True")
            {
                //If RELEASE_RELEASEURI variable is set, then this code is running in the Release workflow, so we fetch release details
                if (Environment.GetEnvironmentVariable("RELEASE_RELEASEURI") != "")
                {
                    releaseUri = Environment.GetEnvironmentVariable("RELEASE_RELEASEURI");
                    Console.WriteLine("Fetched release uri from environment variable RELEASE_RELEASEURI: {0}", releaseUri);

                    releaseEnvironmentUri = Environment.GetEnvironmentVariable("RELEASE_ENVIRONMENTURI");
                    Console.WriteLine("Fetched release environemnt uri from environment variable RELEASE_ENVIRONMENTURI: {0}", releaseEnvironmentUri);
                }
                //note that the build used to deploy and test a Release is an Aritfact.
                //If you have multiple builds or are using external artifacts like Jenkins, make sure you use Aritfact variables to find the build information
                //See https://www.visualstudio.com/en-us/docs/release/author-release-definition/understanding-tasks#predefvariables for pre-defined variables available in release
                //See https://www.visualstudio.com/en-us/docs/release/author-release-definition/understanding-artifacts#variables for artifact variables documentation
                //For example, you'll have to use RELEASE_ARTIFACTS_<aftifactname>_BUILDID to find the build number.
                //Here we are assuming a simple setup, where we are working with Team Build and using Build variables instead...

                //build number is human readable format of the build name, you can confiure it's format in build options..
                buildNumber = Environment.GetEnvironmentVariable("BUILD_BUILDNUMBER");
                Console.WriteLine("Fetched build number from environment variable BUILD_BUILDNUMBER: {0}", buildNumber);

                //build id is the id associated with the build number, which we will use to associate the test run with...
                buildId = Convert.ToInt32(Environment.GetEnvironmentVariable("BUILD_BUILDID"));
                Console.WriteLine("Fetched build id from environment variable BUILD_BUILDID: {0}", buildId);

                //build uri is a more elaborate form of build id, in the vstfs:///Build/Build/<id> format...
                //We will use this for querying test runs against this build...
                buildUri = Environment.GetEnvironmentVariable("BUILD_BUILDURI");
                Console.WriteLine("Fetched build uri from environment variable BUILD_BUILDURI: {0}", buildUri);
            }
            else //if the code is running in standalone mode, you'll have to use Build and Release APIs to fetch the build and release information...
            //see https://www.visualstudio.com/en-us/docs/integrate/api/build/overview for build APIs...
            //and https://www.visualstudio.com/en-us/docs/release/overview for release APIs...
            {
                buildNumber           = "20161124.2";
                buildId               = 3;
                buildUri              = "vstfs:///Build/Build/40";
                releaseUri            = "vstfs:///ReleaseManagement/Release/2";
                releaseEnvironmentUri = "vstfs:///ReleaseManagement/Environment/2";
                Console.WriteLine("Using build number: {0}; build id: {1}; build uri: {2}; release uri: {3}; release environment uri: {4}", buildNumber, buildId, buildUri, releaseUri, releaseEnvironmentUri);
            }


            //Client to use test run and test result APIs...

            TestManagementHttpClient client        = connection.GetClient <TestManagementHttpClient>();

            //Query all test runs publishing against a release environmment...
            //Ideally, we'd use the GetTestRunsAsync with release uri and release environment uri filters here,
            //but GetTestRunsAsync does not support those filters yet...
            //Hence we will use GetTestRunsByQueryAsync...

            QueryModel      runQuery               = new QueryModel("Select * from TestRun where releaseUri='" + releaseUri + "' and releaseEnvironmentUri='" + releaseEnvironmentUri + "'");
            IList <TestRun> TestRunsAgainstRelease = client.GetTestRunsByQueryAsync(runQuery, teamProject).Result;

            // if any of the test runs has tests that have not passed, then flag failure...

            bool notAllTestsPassed = false;

            foreach (TestRun t in TestRunsAgainstRelease)
            {
                Console.WriteLine("Test run: {0}; Total tests: {1}; Passed tests: {2} ", t.Name, t.TotalTests, t.PassedTests);
                if (t.TotalTests != t.PassedTests)
                {
                    notAllTestsPassed = true;
                }
                //though we don't need to get test results,
                //we are getting test results and printing tests that failed just to demo how to query results in a test run
                IList <TestCaseResult> TestResutsInRun = client.GetTestResultsAsync(project: teamProject, runId: t.Id).Result;
                foreach (TestCaseResult r in TestResutsInRun)
                {
                    Console.WriteLine("Test: {0}; Outcome {1}", r.TestCaseTitle, r.Outcome);
                }
            }

            if (notAllTestsPassed)
            {
                Console.WriteLine("Not all tests passed.. Returning failure... ");
                Environment.Exit(1);
            }
            else
            {
                Console.WriteLine("All tests passed.. Returning success... ");
                Environment.Exit(0);
            }
        }
        public WorkItem CreateWorkItem(string title, string type, string project, WorkItem parent = null, string wikiPage = null, string storyPoint = null)
        {
            // Construct the object containing field values required for the new work item
            JsonPatchDocument patchDocument = new JsonPatchDocument();

            if (parent != null)
            {
                patchDocument.Add(
                    new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/relations/-",
                    Value     = new
                    {
                        rel        = "System.LinkTypes.Hierarchy-Reverse",
                        url        = parent.Url,
                        attributes = new
                        {
                            comment = "decomposition of work"
                        }
                    }
                }
                    );
            }

            if (storyPoint != null)
            {
                patchDocument.Add(
                    new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/Microsoft.VSTS.Scheduling.StoryPoints",
                    Value     = storyPoint
                });
            }

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.Title",
                Value     = title
            });


            if (wikiPage != null)
            {
                patchDocument.Add(
                    new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/Microsoft.VSTS.Common.AcceptanceCriteria",
                    Value     = "<div>See wiki, behaviour specs</div>"
                });
                patchDocument.Add(
                    new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/relations/-",
                    Value     = new
                    {
                        rel        = "ArtifactLink",
                        url        = "vstfs:///Wiki/WikiPage/fa742a1f-cba5-4e3e-a211-56f965e0126f%2F190850b1-176a-4e4d-b754-a7ae918899af%2FCASA%2FESB%20(iPaas)%2FESB%20Phase%202%20(RPA%20(Drone)%20Registration)%2FIntegration%20Point%20Register%2FIP-23%20-%20Get%20RPA%20Reference%20Data",
                        attributes = new
                        {
                            name = "Wiki Page"
                        }
                    }
                });
            }
            //patchDocument.Add(
            //    new JsonPatchOperation()
            //    {
            //        Operation = Operation.Add,
            //        Path = "/fields/System.AreaPath",
            //        Value = targetAreaPath
            //    }            );

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.IterationPath",
                Value     = @"Enterprise Service Bus\Sprint 11"
            });

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.Tags",
                Value     = "EAP API"
            }
                );

            // Get a client
            VssConnection connection             = this.Context.Connection;
            var           workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>();

            // Create the new work item
            WorkItem newWorkItem = workItemTrackingClient.CreateWorkItemAsync(patchDocument, project, type).Result;

            Console.WriteLine("Created work item ID {0} {1}", newWorkItem.Id, newWorkItem.Fields["System.Title"]);

            return(newWorkItem);
        }
Пример #23
0
        public async Task <Boolean> CreateSessionAsync(CancellationToken token)
        {
            Trace.Entering();

            // Settings
            var configManager = HostContext.GetService <IConfigurationManager>();

            _settings = configManager.LoadSettings();
            var serverUrl = _settings.ServerUrl;

            Trace.Info(_settings);

            // Capabilities.
            _term.WriteLine(StringUtil.Loc("ScanToolCapabilities"));
            Dictionary <string, string> systemCapabilities = await HostContext.GetService <ICapabilitiesManager>().GetCapabilitiesAsync(_settings, token);

            // Create connection.
            Trace.Verbose("Loading Credentials");
            var            credMgr = HostContext.GetService <ICredentialManager>();
            VssCredentials creds   = credMgr.LoadCredentials();
            Uri            uri     = new Uri(serverUrl);
            VssConnection  conn    = ApiUtil.CreateConnection(uri, creds);

            var agent = new TaskAgentReference
            {
                Id      = _settings.AgentId,
                Name    = _settings.AgentName,
                Version = Constants.Agent.Version,
            };
            string sessionName      = $"{Environment.MachineName ?? "AGENT"}";
            var    taskAgentSession = new TaskAgentSession(sessionName, agent, systemCapabilities);

            string errorMessage      = string.Empty;
            bool   encounteringError = false;

            _term.WriteLine(StringUtil.Loc("ConnectToServer"));
            while (true)
            {
                token.ThrowIfCancellationRequested();
                Trace.Info($"Attempt to create session.");
                try
                {
                    Trace.Info("Connecting to the Agent Server...");
                    await _agentServer.ConnectAsync(conn);

                    _session = await _agentServer.CreateAgentSessionAsync(
                        _settings.PoolId,
                        taskAgentSession,
                        token);

                    Trace.Info($"Session created.");
                    if (encounteringError)
                    {
                        _term.WriteLine(StringUtil.Loc("QueueConnected", DateTime.UtcNow));
                        _sessionCreationExceptionTracker.Clear();
                        encounteringError = false;
                    }

                    return(true);
                }
                catch (OperationCanceledException) when(token.IsCancellationRequested)
                {
                    Trace.Info("Session creation has been cancelled.");
                    throw;
                }
                catch (Exception ex)
                {
                    Trace.Error("Catch exception during create session.");
                    Trace.Error(ex);

                    if (!IsSessionCreationExceptionRetriable(ex))
                    {
                        _term.WriteError(StringUtil.Loc("SessionCreateFailed", ex.Message));
                        return(false);
                    }

                    if (!encounteringError) //print the message only on the first error
                    {
                        _term.WriteError(StringUtil.Loc("QueueConError", DateTime.UtcNow, ex.Message, _sessionCreationRetryInterval.TotalSeconds));
                        encounteringError = true;
                    }

                    Trace.Info("Sleeping for {0} seconds before retrying.", _sessionCreationRetryInterval.TotalSeconds);
                    await HostContext.Delay(_sessionCreationRetryInterval, token);
                }
            }
        }
Пример #24
0
        public void UpdateTickets()
        {
            Uri               orgUrl = new Uri("https://dev.azure.com/DCTTFS/");
            String            personalAccessToken = "ndzzurgegqnpmjwrqhwji2c5exr7wffx4bzjhuccdfprfsdjuydq";
            JsonPatchDocument patchDocument       = new JsonPatchDocument();

            VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));

            WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>();

            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "server = tcp:hatch.database.windows.net,1433; initial catalog = acrf; persist security info = false; user id = dct; password = Duck@1234; multipleactiveresultsets = false; encrypt = true; trustservercertificate = false; connection timeout = 30;";
                // using the code here...
                conn.Close();
                //conn.Open();
                //SqlCommand cmd = conn.CreateCommand();
                //SqlTransaction transaction;
                //transaction = conn.BeginTransaction();
                //cmd.Transaction = transaction;
                //cmd.Connection = conn;
                try
                {
                    string     sqlstr = "GetRecommendations";
                    SqlCommand cmd    = new SqlCommand(sqlstr, conn);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();

                    List <Recommendation> TFSList = new List <Recommendation>();
                    Recommendation        rec     = null;

                    while (reader.Read())
                    {
                        rec           = new Recommendation();
                        rec.ID        = Convert.ToInt32(reader["ID"]);
                        rec.Developer = reader["Developer"].ToString();
                        TFSList.Add(rec);
                    }
                    //transaction.Commit();
                    conn.Close();
                    //result = "employee added successfully!";

                    foreach (var task in TFSList)
                    {
                        patchDocument.Clear();

                        patchDocument.Add(
                            new JsonPatchOperation()
                        {
                            Operation = Operation.Add,
                            Path      = "/fields/Custom.Developer",
                            Value     = task.Developer
                        }
                            );

                        WorkItem result = witClient.UpdateWorkItemAsync(patchDocument, task.ID).Result;
                    }
                }
                catch (Exception ex)
                {
                    //transaction.Rollback();
                    conn.Close();
                    //global.errorhandlerclass.logerror(ex);
                    //result = ex.message;
                }
                conn.Close();
            }
        }
 public Task <(DedupStoreClient client, BlobStoreClientTelemetry telemetry)> CreateDedupClientAsync(bool verbose, Action <string> traceOutput, VssConnection connection, CancellationToken cancellationToken)
 {
     telemetrySender = new TestTelemetrySender();
     return(Task.FromResult((client: (DedupStoreClient)null, telemetry: new BlobStoreClientTelemetry(
                                 NoopAppTraceSource.Instance,
                                 baseAddress,
                                 telemetrySender))));
 }
        // Download with minimatch patterns, V1.
        internal async Task DownloadAsync(
            AgentTaskPluginExecutionContext context,
            PipelineArtifactDownloadParameters downloadParameters,
            DownloadOptions downloadOptions,
            CancellationToken cancellationToken)
        {
            VssConnection               connection = context.VssConnection;
            BlobStoreClientTelemetry    clientTelemetry;
            DedupManifestArtifactClient dedupManifestClient = DedupManifestArtifactClientFactory.Instance.CreateDedupManifestClient(context, connection, cancellationToken, out clientTelemetry);
            BuildServer buildServer = new BuildServer(connection);

            using (clientTelemetry)
            {
                // download all pipeline artifacts if artifact name is missing
                if (downloadOptions == DownloadOptions.MultiDownload)
                {
                    List <BuildArtifact> artifacts;
                    if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                    {
                        artifacts = await buildServer.GetArtifactsAsync(downloadParameters.ProjectId, downloadParameters.PipelineId, cancellationToken);
                    }
                    else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                    {
                        if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                        {
                            throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                        }
                        else
                        {
                            artifacts = await buildServer.GetArtifactsWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, cancellationToken);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException($"Invalid {nameof(downloadParameters.ProjectRetrievalOptions)}!");
                    }

                    IEnumerable <BuildArtifact> pipelineArtifacts = artifacts.Where(a => string.Equals(a.Resource.Type, PipelineArtifactConstants.PipelineArtifact, StringComparison.OrdinalIgnoreCase));
                    if (pipelineArtifacts.Count() == 0)
                    {
                        throw new ArgumentException("Could not find any pipeline artifacts in the build.");
                    }
                    else
                    {
                        context.Output(StringUtil.Loc("DownloadingMultiplePipelineArtifacts", pipelineArtifacts.Count()));

                        var artifactNameAndManifestIds = pipelineArtifacts.ToDictionary(
                            keySelector: (a) => a.Name, // keys should be unique, if not something is really wrong
                            elementSelector: (a) => DedupIdentifier.Create(a.Resource.Data));
                        // 2) download to the target path
                        var options = DownloadDedupManifestArtifactOptions.CreateWithMultiManifestIds(
                            artifactNameAndManifestIds,
                            downloadParameters.TargetDirectory,
                            proxyUri: null,
                            minimatchPatterns: downloadParameters.MinimatchFilters,
                            minimatchFilterWithArtifactName: downloadParameters.MinimatchFilterWithArtifactName);

                        PipelineArtifactActionRecord downloadRecord = clientTelemetry.CreateRecord <PipelineArtifactActionRecord>((level, uri, type) =>
                                                                                                                                  new PipelineArtifactActionRecord(level, uri, type, nameof(DownloadAsync), context));
                        await clientTelemetry.MeasureActionAsync(
                            record : downloadRecord,
                            actionAsync : async() =>
                        {
                            await AsyncHttpRetryHelper.InvokeVoidAsync(
                                async() =>
                            {
                                await dedupManifestClient.DownloadAsync(options, cancellationToken);
                            },
                                maxRetries: 3,
                                tracer: tracer,
                                canRetryDelegate: e => true,
                                context: nameof(DownloadAsync),
                                cancellationToken: cancellationToken,
                                continueOnCapturedContext: false);
                        });

                        // Send results to CustomerIntelligence
                        context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineArtifact, record: downloadRecord);
                    }
                }
                else if (downloadOptions == DownloadOptions.SingleDownload)
                {
                    // 1) get manifest id from artifact data
                    BuildArtifact buildArtifact;
                    if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                    {
                        buildArtifact = await buildServer.GetArtifact(downloadParameters.ProjectId, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                    }
                    else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                    {
                        if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                        {
                            throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                        }
                        else
                        {
                            buildArtifact = await buildServer.GetArtifactWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException($"Invalid {nameof(downloadParameters.ProjectRetrievalOptions)}!");
                    }

                    var manifestId = DedupIdentifier.Create(buildArtifact.Resource.Data);
                    var options    = DownloadDedupManifestArtifactOptions.CreateWithManifestId(
                        manifestId,
                        downloadParameters.TargetDirectory,
                        proxyUri: null,
                        minimatchPatterns: downloadParameters.MinimatchFilters);

                    PipelineArtifactActionRecord downloadRecord = clientTelemetry.CreateRecord <PipelineArtifactActionRecord>((level, uri, type) =>
                                                                                                                              new PipelineArtifactActionRecord(level, uri, type, nameof(DownloadAsync), context));
                    await clientTelemetry.MeasureActionAsync(
                        record : downloadRecord,
                        actionAsync : async() =>
                    {
                        await AsyncHttpRetryHelper.InvokeVoidAsync(
                            async() =>
                        {
                            await dedupManifestClient.DownloadAsync(options, cancellationToken);
                        },
                            maxRetries: 3,
                            tracer: tracer,
                            canRetryDelegate: e => true,
                            context: nameof(DownloadAsync),
                            cancellationToken: cancellationToken,
                            continueOnCapturedContext: false);
                    });

                    // Send results to CustomerIntelligence
                    context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineArtifact, record: downloadRecord);
                }
                else
                {
                    throw new InvalidOperationException($"Invalid {nameof(downloadOptions)}!");
                }
            }
        }
Пример #27
0
        public static List <WorkItem> getBugs(VssConnection connection)
        {
            // Create instance of WorkItemTrackingHttpClient using VssConnection
            WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>();

            List <WorkItem> bugList = new List <WorkItem>();
            // Get 2 levels of query hierarchy items
            List <QueryHierarchyItem> queryHierarchyItems = witClient.GetQueriesAsync(teamProjectName, depth: 2).Result;

            // Search for 'My Queries' folder
            QueryHierarchyItem myQueriesFolder = queryHierarchyItems.FirstOrDefault(qhi => qhi.Name.Equals("My Queries"));

            if (myQueriesFolder != null)
            {
                string queryName = "REST Sample";

                // See if our 'REST Sample' query already exists under 'My Queries' folder.
                QueryHierarchyItem newBugsQuery = null;
                if (myQueriesFolder.Children != null)
                {
                    newBugsQuery = myQueriesFolder.Children.FirstOrDefault(qhi => qhi.Name.Equals(queryName));
                }
                if (newBugsQuery == null)
                {
                    // if the 'REST Sample' query does not exist, create it.
                    newBugsQuery = new QueryHierarchyItem()
                    {
                        Name     = queryName,
                        Wiql     = "SELECT [System.Id],[System.WorkItemType],[System.Title],[System.AssignedTo],[System.State],[System.Tags] FROM WorkItems WHERE [System.TeamProject] = @project AND [System.WorkItemType] = 'Bug' AND [System.State] = 'New'",
                        IsFolder = false
                    };
                    newBugsQuery = witClient.CreateQueryAsync(newBugsQuery, teamProjectName, myQueriesFolder.Name).Result;
                }

                // run the 'REST Sample' query
                WorkItemQueryResult result = witClient.QueryByIdAsync(newBugsQuery.Id).Result;

                if (result.WorkItems.Any())
                {
                    int       skip      = 0;
                    const int batchSize = 100;
                    IEnumerable <WorkItemReference> workItemRefs;
                    do
                    {
                        workItemRefs = result.WorkItems.Skip(skip).Take(batchSize);
                        if (workItemRefs.Any())
                        {
                            // get details for each work item in the batch
                            List <WorkItem> workItems = witClient.GetWorkItemsAsync(workItemRefs.Select(wir => wir.Id)).Result;
                            foreach (WorkItem workItem in workItems)
                            {
                                bugList.Add(workItem);
                                // write work item to console
                                Console.WriteLine("{0} {1}", workItem.Id, workItem.Fields["System.Title"]);
                            }
                        }
                        skip += batchSize;
                    }while (workItemRefs.Count() == batchSize);
                }
                else
                {
                    Console.WriteLine("No bugs were returned from query.");
                }
            }

            return(bugList);
        }
        // Upload from target path to Azure DevOps BlobStore service through DedupManifestArtifactClient, then associate it with the build
        internal async Task UploadAsync(
            AgentTaskPluginExecutionContext context,
            Guid projectId,
            int pipelineId,
            string name,
            string source,
            CancellationToken cancellationToken)
        {
            VssConnection connection = context.VssConnection;

            BlobStoreClientTelemetry    clientTelemetry;
            DedupManifestArtifactClient dedupManifestClient = DedupManifestArtifactClientFactory.Instance.CreateDedupManifestClient(context, connection, cancellationToken, out clientTelemetry);

            using (clientTelemetry)
            {
                //Upload the pipeline artifact.
                PipelineArtifactActionRecord uploadRecord = clientTelemetry.CreateRecord <PipelineArtifactActionRecord>((level, uri, type) =>
                                                                                                                        new PipelineArtifactActionRecord(level, uri, type, nameof(UploadAsync), context));

                PublishResult result = await clientTelemetry.MeasureActionAsync(
                    record : uploadRecord,
                    actionAsync : async() => await AsyncHttpRetryHelper.InvokeAsync(
                        async() =>
                {
                    return(await dedupManifestClient.PublishAsync(source, cancellationToken));
                },
                        maxRetries: 3,
                        tracer: tracer,
                        canRetryDelegate: e => true,     // this isn't great, but failing on upload stinks, so just try a couple of times
                        cancellationToken: cancellationToken,
                        continueOnCapturedContext: false)

                    );

                // Send results to CustomerIntelligence
                context.PublishTelemetry(area: PipelineArtifactConstants.AzurePipelinesAgent, feature: PipelineArtifactConstants.PipelineArtifact, record: uploadRecord);

                // 2) associate the pipeline artifact with an build artifact
                BuildServer buildServer = new BuildServer(connection);
                Dictionary <string, string> propertiesDictionary = new Dictionary <string, string>();
                propertiesDictionary.Add(PipelineArtifactConstants.RootId, result.RootId.ValueString);
                propertiesDictionary.Add(PipelineArtifactConstants.ProofNodes, StringUtil.ConvertToJson(result.ProofNodes.ToArray()));
                propertiesDictionary.Add(PipelineArtifactConstants.ArtifactSize, result.ContentSize.ToString());

                BuildArtifact buildArtifact = await AsyncHttpRetryHelper.InvokeAsync(
                    async() =>
                {
                    return(await buildServer.AssociateArtifactAsync(projectId,
                                                                    pipelineId,
                                                                    name,
                                                                    context.Variables.GetValueOrDefault(WellKnownDistributedTaskVariables.JobId)?.Value ?? string.Empty,
                                                                    ArtifactResourceTypes.PipelineArtifact,
                                                                    result.ManifestId.ValueString,
                                                                    propertiesDictionary,
                                                                    cancellationToken));
                },
                    maxRetries : 3,
                    tracer : tracer,
                    canRetryDelegate : e => e is TimeoutException || e.InnerException is TimeoutException,
                    cancellationToken : cancellationToken,
                    continueOnCapturedContext : false);

                context.Output(StringUtil.Loc("AssociateArtifactWithBuild", buildArtifact.Id, pipelineId));
            }
        }
Пример #29
0
        public static List <WorkItem> getMyCodeReviews(VssConnection connection)
        {
            WorkItemTrackingHttpClient witClient     = connection.GetClient <WorkItemTrackingHttpClient>();
            List <WorkItem>            myCodeReviews = new List <WorkItem>();

            Wiql codeReviewsQuery = new Wiql()
            {
                Query = @"SELECT
                            [System.Id],
                            [System.Links.LinkType],
                            [System.Title],
                            [System.State],
                            [System.Reason],
                            [System.AssignedTo],
                            [Microsoft.VSTS.Common.StateCode],
                            [Microsoft.VSTS.Common.ClosedBy],
                            [Microsoft.VSTS.CodeReview.ClosedStatus],
                            [Microsoft.VSTS.CodeReview.ClosedStatusCode],
                            [Microsoft.VSTS.CodeReview.ClosingComment],
                            [System.CommentCount]
                        FROM workitemLinks
                        WHERE
                            (
                                [Source].[System.TeamProject] = @project
                                AND [Source].[System.WorkItemType] IN GROUP 'Microsoft.CodeReviewRequestCategory'
                            )
                            AND (
                                [System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
                            )
                            AND (
                                [Target].[System.WorkItemType] IN GROUP 'Microsoft.CodeReviewResponseCategory'
                            )
                        ORDER BY [System.CreatedDate] DESC,
                            [System.Id]
                        MODE (Recursive, ReturnMatchingChildren)"
                        //MODE (MustContain)"
            };
            WorkItemQueryResult result = witClient.QueryByWiqlAsync(codeReviewsQuery, teamProjectName).Result;


            if (result.WorkItemRelations.Any())
            {
                int       skip      = 0;
                const int batchSize = 100;
                IEnumerable <WorkItemLink> workItemLinks;

                do
                {
                    result.WorkItemRelations.Skip(skip).Take(batchSize);
                    workItemLinks = result.WorkItemRelations.Skip(skip).Take(batchSize);
                    if (workItemLinks.Any())
                    {
                        // get details for each work item in the batch
                        //List<WorkItem> workItems = witClient.GetWorkItemsAsync(workItemLinks.Select(wir => wir.Target.Id), null, null, WorkItemExpand.All).Result;
                        List <string> fields = new List <String>()
                        {
                            "System.Title", "System.WorkItemType",
                            "System.State", "Microsoft.VSTS.Common.StateCode",
                            "Microsoft.VSTS.CodeReview.ClosedStatus",
                            "Microsoft.VSTS.CodeReview.ClosedStatusCode",
                            "Microsoft.VSTS.CodeReview.ClosingComment"
                        };
                        List <WorkItem> workItems = witClient.GetWorkItemsAsync(workItemLinks.Select(wir => wir.Target.Id), fields, null, null).Result;

                        foreach (WorkItem workItem in workItems)
                        {
                            myCodeReviews.Add(workItem);
                            string closedStatus     = workItem.Fields.ContainsKey("Microsoft.VSTS.CodeReview.ClosedStatus") ? workItem.Fields["Microsoft.VSTS.CodeReview.ClosedStatus"].ToString() : "*";
                            string closedStatusCode = workItem.Fields.ContainsKey("Microsoft.VSTS.CodeReview.ClosedStatusCode") ? workItem.Fields["Microsoft.VSTS.CodeReview.ClosedStatusCode"].ToString() : "*";
                            // write work item to console
                            string sItem = $"WorkItemId : {workItem.Id} - " +
                                           $"Title : {workItem.Fields["System.Title"]} - " +
                                           $"Type : {workItem.Fields["System.WorkItemType"]} - " +
                                           $"State : {workItem.Fields["System.State"]} -" +
                                           $"StateCode : { workItem.Fields["Microsoft.VSTS.Common.StateCode"]} - " +
                                           $"ClosedStatus : {closedStatus} - " +
                                           $"ClosedStatusCode : {closedStatusCode}";

                            Console.WriteLine(sItem);
                        }
                    }
                    skip += batchSize;
                }while (workItemLinks.Count() == batchSize);
            }
            else
            {
                Console.WriteLine("No CodeReviews returned from query.");
            }
            return(myCodeReviews);
        }
        public static DedupManifestArtifactClient CreateDedupManifestClient(AgentTaskPluginExecutionContext context, VssConnection connection, out BlobStoreClientTelemetry telemetry)
        {
            var dedupStoreHttpClient = connection.GetClient <DedupStoreHttpClient>();
            var tracer = new CallbackAppTraceSource(str => context.Output(str), SourceLevels.Information);

            dedupStoreHttpClient.SetTracer(tracer);
            var client = new DedupStoreClientWithDataport(dedupStoreHttpClient, PipelineArtifactProvider.GetDedupStoreClientMaxParallelism(context));

            return(new DedupManifestArtifactClient(telemetry = new BlobStoreClientTelemetry(tracer, dedupStoreHttpClient.BaseAddress), client, tracer));
        }
Пример #31
0
        public void AddRemoveAADUserByUPNToGroup()
        {
            // Get the client
            VssConnection   connection  = Context.Connection;
            GraphHttpClient graphClient = connection.GetClient <GraphHttpClient>();

            //
            // Part 1: create a group at the account level
            //
            GraphGroupCreationContext createGroupContext = new GraphGroupVstsCreationContext
            {
                DisplayName = "Developers-" + Guid.NewGuid(),
                Description = "Group created via client library"
            };

            GraphGroup newVSTSGroup = graphClient.CreateGroupAsync(createGroupContext).Result;
            IEnumerable <VisualStudio.Services.Common.SubjectDescriptor> parentGroup = new List <VisualStudio.Services.Common.SubjectDescriptor>()
            {
                newVSTSGroup.Descriptor
            };
            string groupDescriptor = newVSTSGroup.Descriptor;

            Context.Log("New group created! ID: {0}", groupDescriptor);

            //
            // Part 2: add the AAD user
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "MaterializeAADUserByOIDAsMember");
            GraphUserCreationContext addAADUserContext = new GraphUserPrincipalNameCreationContext
            {
                PrincipalName = "*****@*****.**"
            };

            GraphUser newUser        = graphClient.CreateUserAsync(addAADUserContext, parentGroup).Result;
            string    userDescriptor = newUser.Descriptor;

            Context.Log("New user added! ID: {0}", userDescriptor);

            //
            // Part 3: get the user
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetUser-AddRemoveAADUserByUPNToGroup");
            newUser = graphClient.GetUserAsync(userDescriptor).Result;

            //
            // Part 4: remove the user
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "DeleteUser-AddRemoveAADUserByUPNToGroup");
            graphClient.DeleteUserAsync(userDescriptor).SyncResult();

            // Try to get the deleted user
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipState-AddRemoveAADUserByUPNToGroup");
            GraphMembershipState membershipState = graphClient.GetMembershipStateAsync(userDescriptor).Result;

            try
            {
                if (membershipState.Active)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Context.Log("The deleted user is not disabled!");
            }

            // Part 5: remove the group
            graphClient.DeleteGroupAsync(groupDescriptor).SyncResult();

            // Try to get the deleted group
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipStateAADGroup");
            membershipState = graphClient.GetMembershipStateAsync(groupDescriptor).Result;
            try
            {
                if (membershipState.Active)
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Context.Log("The deleted group is not disabled!");
            }
        }
Пример #32
0
        public ReleaseDefinition CreateReleaseDefinition()
        {
            string projectName = ClientSampleHelpers.FindAnyProject(this.Context).Name;

            ReleaseDefinition definition = new ReleaseDefinition()
            {
                Name         = releaseDefinitionName,
                Revision     = 1,
                Environments = new List <ReleaseDefinitionEnvironment>()
                {
                    new ReleaseDefinitionEnvironment()
                    {
                        Name         = "PROD",
                        DeployPhases = new List <DeployPhase>()
                        {
                            new AgentBasedDeployPhase()
                            {
                                Name = "Run on agent",
                                Rank = 1
                            }
                        },
                        PreDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = true,
                                    Rank        = 1
                                }
                            }
                        },
                        PostDeployApprovals = new ReleaseDefinitionApprovals()
                        {
                            Approvals = new List <ReleaseDefinitionApprovalStep>()
                            {
                                new ReleaseDefinitionApprovalStep()
                                {
                                    IsAutomated = true,
                                    Rank        = 1
                                }
                            }
                        },
                        RetentionPolicy = new EnvironmentRetentionPolicy()
                        {
                            DaysToKeep     = 30,
                            ReleasesToKeep = 3,
                            RetainBuild    = true
                        }
                    }
                }
            };

            // Get a release client instance
            VssConnection     connection    = Context.Connection;
            ReleaseHttpClient releaseClient = connection.GetClient <ReleaseHttpClient>();

            // create a release definition
            ReleaseDefinition releaseDefinition = releaseClient.CreateReleaseDefinitionAsync(project: projectName, releaseDefinition: definition).Result;

            newlyCreatedReleaseDefinitionId = releaseDefinition.Id;

            Console.WriteLine("{0} {1}", releaseDefinition.Id.ToString().PadLeft(6), releaseDefinition.Name);

            return(releaseDefinition);
        }
        /// <summary>
        /// Gets all team members for the given <paramref name="project" /> and <paramref name="team" />.
        /// </summary>
        /// <param name="client">The <see cref="TeamHttpClient" /> to use.</param>
        /// <param name="connection">The connection for the <paramref name="client" /> that will be used to retrieve the identities for the team members.</param>
        /// <param name="project">The project.</param>
        /// <param name="team">The team.</param>
        /// <param name="pageSize">Page size to use while retrieving the projects.</param>
        /// <param name="userState">The user state object.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        public static Task<IReadOnlyCollection<Identity>> GetAllTeamMembers(this TeamHttpClient client, VssConnection connection, TeamProject project, WebApiTeam team, int pageSize = 10, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (client == null)
                throw new ArgumentNullException(nameof(client));
            if (connection == null)
                throw new ArgumentNullException(nameof(connection));
            if (project == null)
                throw new ArgumentNullException(nameof(project));
            if (team == null)
                throw new ArgumentNullException(nameof(team));

            return client.GetAllTeamMembers(connection, project.Id, team.Id, pageSize, userState, cancellationToken);
        }
Пример #34
0
        // Download with minimatch patterns.
        internal async Task DownloadAsync(
            AgentTaskPluginExecutionContext context,
            PipelineArtifactDownloadParameters downloadParameters,
            DownloadOptions downloadOptions,
            CancellationToken cancellationToken)
        {
            VssConnection connection       = context.VssConnection;
            var           buildDropManager = this.CreateBulidDropManager(context, connection);
            BuildServer   buildHelper      = new BuildServer(connection);

            // download all pipeline artifacts if artifact name is missing
            if (downloadOptions == DownloadOptions.MultiDownload)
            {
                List <BuildArtifact> artifacts;
                if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                {
                    artifacts = await buildHelper.GetArtifactsAsync(downloadParameters.ProjectId, downloadParameters.PipelineId, cancellationToken);
                }
                else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                {
                    if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                    {
                        throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                    }
                    else
                    {
                        artifacts = await buildHelper.GetArtifactsWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, cancellationToken);
                    }
                }
                else
                {
                    throw new InvalidOperationException("Unreachable code!");
                }

                IEnumerable <BuildArtifact> pipelineArtifacts = artifacts.Where(a => a.Resource.Type == PipelineArtifactTypeName);
                if (pipelineArtifacts.Count() == 0)
                {
                    throw new ArgumentException("Could not find any pipeline artifacts in the build.");
                }
                else
                {
                    context.Output(StringUtil.Loc("DownloadingMultiplePipelineArtifacts", pipelineArtifacts.Count()));

                    var artifactNameAndManifestIds = pipelineArtifacts.ToDictionary(
                        keySelector: (a) => a.Name, // keys should be unique, if not something is really wrong
                        elementSelector: (a) => DedupIdentifier.Create(a.Resource.Data));
                    // 2) download to the target path
                    var options = DownloadPipelineArtifactOptions.CreateWithMultiManifestIds(
                        artifactNameAndManifestIds,
                        downloadParameters.TargetDirectory,
                        proxyUri: null,
                        minimatchPatterns: downloadParameters.MinimatchFilters);
                    await buildDropManager.DownloadAsync(options, cancellationToken);
                }
            }
            else if (downloadOptions == DownloadOptions.SingleDownload)
            {
                // 1) get manifest id from artifact data
                BuildArtifact buildArtifact;
                if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectId)
                {
                    buildArtifact = await buildHelper.GetArtifact(downloadParameters.ProjectId, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                }
                else if (downloadParameters.ProjectRetrievalOptions == BuildArtifactRetrievalOptions.RetrieveByProjectName)
                {
                    if (string.IsNullOrEmpty(downloadParameters.ProjectName))
                    {
                        throw new InvalidOperationException("Project name can't be empty when trying to fetch build artifacts!");
                    }
                    else
                    {
                        buildArtifact = await buildHelper.GetArtifactWithProjectNameAsync(downloadParameters.ProjectName, downloadParameters.PipelineId, downloadParameters.ArtifactName, cancellationToken);
                    }
                }
                else
                {
                    throw new InvalidOperationException("Unreachable code!");
                }

                var manifestId = DedupIdentifier.Create(buildArtifact.Resource.Data);
                var options    = DownloadPipelineArtifactOptions.CreateWithManifestId(
                    manifestId,
                    downloadParameters.TargetDirectory,
                    proxyUri: null,
                    minimatchPatterns: downloadParameters.MinimatchFilters);

                await buildDropManager.DownloadAsync(options, cancellationToken);
            }
            else
            {
                throw new InvalidOperationException("Unreachable code!");
            }
        }
Пример #35
0
        public async Task DownloadAsync(IExecutionContext executionContext, ArtifactDefinition artifactDefinition, string localFolderPath)
        {
            ArgUtil.NotNull(artifactDefinition, nameof(artifactDefinition));
            ArgUtil.NotNull(executionContext, nameof(executionContext));
            ArgUtil.NotNullOrEmpty(localFolderPath, nameof(localFolderPath));

            int buildId = Convert.ToInt32(artifactDefinition.Version, CultureInfo.InvariantCulture);

            if (buildId <= 0)
            {
                throw new ArgumentException("artifactDefinition.Version");
            }

            var buildArtifactDetails = artifactDefinition.Details as BuildArtifactDetails;

            if (buildArtifactDetails == null)
            {
                throw new ArgumentException("artifactDefinition.Details");
            }

            // Get the list of available artifacts from build.
            executionContext.Output(StringUtil.Loc("RMPreparingToGetBuildArtifactList"));

            var vssConnection   = new VssConnection(buildArtifactDetails.TfsUrl, buildArtifactDetails.Credentials);
            var buildClient     = vssConnection.GetClient <BuildHttpClient>();
            var xamlBuildClient = vssConnection.GetClient <XamlBuildHttpClient>();
            List <ServerBuildArtifact> buildArtifacts      = null;
            DefinitionType             buildDefinitionType = DefinitionType.Build;

            try
            {
                buildArtifacts = await buildClient.GetArtifactsAsync(buildArtifactDetails.Project, buildId);
            }
            catch (BuildNotFoundException)
            {
                buildArtifacts = await xamlBuildClient.GetArtifactsAsync(buildArtifactDetails.Project, buildId);

                buildDefinitionType = DefinitionType.Xaml;
            }

            // No artifacts found in the build => Fail it.
            if (buildArtifacts == null || !buildArtifacts.Any())
            {
                throw new ArtifactDownloadException(StringUtil.Loc("RMNoBuildArtifactsFound", buildId));
            }

            // DownloadFromStream each of the artifact sequentially.
            // TODO: Should we download them parallely?
            foreach (ServerBuildArtifact buildArtifact in buildArtifacts)
            {
                if (Match(buildArtifact, artifactDefinition))
                {
                    executionContext.Output(StringUtil.Loc("RMPreparingToDownload", buildArtifact.Name));
                    await this.DownloadArtifactAsync(executionContext, buildArtifact, artifactDefinition, localFolderPath, buildClient, xamlBuildClient, buildDefinitionType, buildId);
                }
                else
                {
                    executionContext.Warning(StringUtil.Loc("RMArtifactMatchNotFound", buildArtifact.Name));
                }
            }
        }
        public void AddRemoveUserMembership()
        {
            // Get the client
            VssConnection   connection  = Context.Connection;
            GraphHttpClient graphClient = connection.GetClient <GraphHttpClient>();

            //
            // Part 1: create a group at the account level
            //

            GraphGroupCreationContext createGroupContext = new GraphGroupVstsCreationContext
            {
                DisplayName = "Developers-" + Guid.NewGuid(),
                Description = "Group created via client library"
            };

            GraphGroup newGroup        = graphClient.CreateGroupAsync(createGroupContext).Result;
            string     groupDescriptor = newGroup.Descriptor;

            Context.Log("New group created! ID: {0}", groupDescriptor);

            //
            // Part 2: add the user
            //

            GraphUserCreationContext addUserContext = new GraphUserPrincipalNameCreationContext
            {
                PrincipalName = "*****@*****.**"
            };

            GraphUser newUser        = graphClient.CreateUserAsync(addUserContext).Result;
            string    userDescriptor = newUser.Descriptor;

            Context.Log("New user added! ID: {0}", userDescriptor);

            //
            // Part 3: Make the user a member of the group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "CreateMembershipUser");
            GraphMembership graphMembership = graphClient.AddMembershipAsync(userDescriptor, groupDescriptor).Result;

            //
            // Part 4: get the membership
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipUser");
            graphMembership = graphClient.GetMembershipAsync(userDescriptor, groupDescriptor).Result;

            //
            // Part 5: Check to see if the user is a member of the group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "CheckMembershipUser");
            graphClient.CheckMembershipExistenceAsync(userDescriptor, groupDescriptor).SyncResult();

            //
            // Part 6: Get every group the subject(user) is a member of
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipsUserUp");
            List <GraphMembership> membershipsForUser = graphClient.GetMembershipsAsync(userDescriptor).Result;

            //
            // Part 7: Get every member of the group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "GetMembershipsGroupDown");
            List <GraphMembership> membershipsOfGroup = graphClient.GetMembershipsAsync(groupDescriptor, Microsoft.VisualStudio.Services.Graph.GraphTraversalDirection.Down.ToString()).Result; //Bug 967647: REST: GetMembershipsAsync shouldn't take direction as string, it should be the GraphTraversalDirection enum

            //
            // Part 8: Remove member from the group
            //
            ClientSampleHttpLogger.SetOperationName(this.Context, "DeleteMembershipUser");
            graphClient.RemoveMembershipAsync(userDescriptor, groupDescriptor).SyncResult();
            try {
                graphClient.CheckMembershipExistenceAsync(userDescriptor, groupDescriptor).SyncResult();
            }
            catch (Exception e) {
                Context.Log("User is no longer a member of the group:" + e.Message);
            }

            //
            // Part 9: delete the group
            //

            graphClient.DeleteGroupAsync(groupDescriptor).SyncResult();

            //
            // Part 10: remove the user

            graphClient.DeleteUserAsync(userDescriptor).SyncResult();

            //
            // Try to get the deleted user
            try
            {
                newUser = graphClient.GetUserAsync(userDescriptor).Result;

                // TODO: Disable no longer a field of GraphUser if (!newUser.Disabled) throw new Exception();
            }
            catch (Exception)
            {
                Context.Log("The deleted user is not disabled!");
            }
        }
Пример #37
0
        static void Main(string[] args)
        {
            IServiceProvider   services = ServiceProviderBuilder.GetServiceProvider(args);
            IOptions <APIKeys> options  = services.GetRequiredService <IOptions <APIKeys> >();
            asciiArtClass      asciiArt = new asciiArtClass();

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("\n\n");
            foreach (string line in asciiArt.azureArtArr)
            {
                Console.WriteLine(line);
            }

            //use the httpclient
            VssCredentials creds = new VssBasicCredential(string.Empty, options.Value.PAT);

            // Connect to Azure DevOps Services
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("\nConnecting...");
            VssConnection connection = new VssConnection(new Uri(c_collectionUri + options.Value.OrgName), creds);

            ProjectHttpClient projClient = connection.GetClientAsync <ProjectHttpClient>().Result;
            IPagedList <TeamProjectReference> projects = projClient.GetProjects().Result;

            // Get a GitHttpClient to talk to the Git endpoints
            using (GitHttpClient gitClient = connection.GetClient <GitHttpClient>())
            {
                foreach (TeamProjectReference p in projects)
                {
                    Console.WriteLine("\n\nProject --- " + p.Name);
                    List <GitRepository> repoList = gitClient.GetRepositoriesAsync(p.Name).Result;

                    GitRepository masterRepo = repoList.Where(x => x.Name == "master" || x.Name == p.Name).FirstOrDefault();
                    Console.WriteLine("Repo Name --- " + masterRepo.Name);

                    //set a filter to only return commits within the last 7 days
                    GitQueryCommitsCriteria searches = new GitQueryCommitsCriteria();
                    searches.FromDate = DateTime.Now.AddDays(-7).ToShortDateString();

                    List <GitCommitRef> commits = gitClient.GetCommitsBatchAsync(searches, masterRepo.Id).Result;

                    foreach (GitCommitRef cmt in commits)
                    {
                        Console.WriteLine("\n\nProject --- " + p.Name);
                        Console.WriteLine("Repo Name --- " + masterRepo.Name);
                        Console.WriteLine("\nCommit ID - #{0}\nBy - {1}\nEmail - {2}\nOn - {3}", cmt.CommitId, cmt.Author.Name, cmt.Author.Email, cmt.Author.Date.ToLongDateString(), cmt.Comment);
                        GitCommitChanges changes = gitClient.GetChangesAsync(p.Name, cmt.CommitId, p.Name).Result;

                        Console.WriteLine("Files:");
                        foreach (GitChange change in changes.Changes)
                        {
                            Console.WriteLine("{0}: {1}", change.ChangeType, change.Item.Path);
                        }
                    }
                }
            }

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("\n\n");
            foreach (string line in asciiArt.tieFArtArr)
            {
                Console.WriteLine(line);
            }
        }
        public WorkItem CreateAndLinkToWorkItem()
        {
            string title       = "My new work item with links";
            string description = "This is a new work item that has a link also created on it.";
            string linkUrl     = Context.GetValue <WorkItem>("$newWorkItem1").Url; //get the url of a previously added work item

            JsonPatchDocument patchDocument = new JsonPatchDocument();

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.Title",
                Value     = title
            }
                );

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.Description",
                Value     = description
            }
                );

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.History",
                Value     = "Jim has the most context around this."
            }
                );

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/relations/-",
                Value     = new
                {
                    rel        = "System.LinkTypes.Hierarchy-Reverse",
                    url        = linkUrl,
                    attributes = new
                    {
                        comment = "decomposition of work"
                    }
                }
            }
                );

            VssConnection connection = Context.Connection;
            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>();

            // Get the project to create the sample work item in
            TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);

            WorkItem result = workItemTrackingClient.CreateWorkItemAsync(patchDocument, project.Name, "Task").Result;

            return(result);
        }
Пример #39
0
        public void GetLatestDeveloper()
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "server = tcp:hatch.database.windows.net,1433; initial catalog = acrf; persist security info = false; user id = dct; password = Duck@1234; multipleactiveresultsets = false; encrypt = true; trustservercertificate = false; connection timeout = 30;";
                // using the code here...
                conn.Close();
                //conn.Open();
                //SqlCommand cmd = conn.CreateCommand();
                //SqlTransaction transaction;
                //transaction = conn.BeginTransaction();
                //cmd.Transaction = transaction;
                //cmd.Connection = conn;
                try
                {
                    string     sqlstr = "GetTFSTickets";
                    SqlCommand cmd    = new SqlCommand(sqlstr, conn);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();

                    List <LatestDeveloper> TFSList = new List <LatestDeveloper>();
                    LatestDeveloper        rec     = null;

                    while (reader.Read())
                    {
                        rec           = new LatestDeveloper();
                        rec.ID        = Convert.ToInt32(reader["ID"]);
                        rec.Developer = null;
                        rec.Status    = null;
                        TFSList.Add(rec);
                    }
                    //transaction.Commit();
                    conn.Close();


                    //TFSList.Add(new LatestDeveloper
                    //{
                    //    ID = 7,
                    //    Developer = ""
                    //}); ;

                    //TFSList.Add(new LatestDeveloper
                    //{
                    //    ID =8,
                    //    Developer = ""
                    //}); ;
                    //TFSList.Add(new LatestDeveloper
                    //{
                    //    ID = 9,
                    //    Developer = ""
                    //}); ;
                    //TFSList.Add(new LatestDeveloper
                    //{
                    //    ID = 10,
                    //    Developer = ""
                    //}); ;


                    string INParam = string.Join(",", TFSList.Select(x => x.ID));


                    Uri           orgUrl = new Uri("https://dev.azure.com/DCTTFS/");
                    String        personalAccessToken = "ndzzurgegqnpmjwrqhwji2c5exr7wffx4bzjhuccdfprfsdjuydq";
                    VssConnection connection          = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));

                    WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>();

                    Wiql wiql = new Wiql();

                    wiql.Query = "SELECT [System.Id],[Custom.Developer],[System.State] FROM workitems where [System.Id] IN (" + INParam + ") ";

                    WorkItemQueryResult tasks = witClient.QueryByWiqlAsync(wiql).Result;

                    IEnumerable <WorkItemReference> tasksRefs;
                    tasksRefs = tasks.WorkItems.OrderBy(x => x.Id);

                    List <WorkItem> tasksList = witClient.GetWorkItemsAsync(tasksRefs.Select(wir => wir.Id)).Result;

                    string sqlstrmulti = "";

                    foreach (var task in tasksList)
                    {
                        sqlstrmulti = sqlstrmulti += "Update TFSTicketsData set Developer='" + task.Fields["Custom.Developer"].ToString() + "' ,State='" + task.Fields["System.State"].ToString() + "'  where ID=" + task.Id + " ;";
                    }

                    conn.Open();
                    SqlCommand cmd1 = new SqlCommand(sqlstrmulti, conn);
                    cmd1.CommandType = System.Data.CommandType.Text;
                    SqlDataReader sdr = cmd1.ExecuteReader();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    //transaction.Rollback();
                    conn.Close();
                    //global.errorhandlerclass.logerror(ex);
                    //result = ex.message;
                }
                conn.Close();
            }
        }
        public NotificationSubscription CreateUpdateDeleteSubscription()
        {
            // Get the client
            VssConnection          connection         = Context.Connection;
            NotificationHttpClient notificationClient = connection.GetClient <NotificationHttpClient>();

            //
            // Part 1: create a subscription to get notified about certain pull request events
            //

            // Create parameters for the new subscription
            NotificationSubscriptionCreateParameters createParams = new NotificationSubscriptionCreateParameters()
            {
                Description = "Someone is waiting on one of my pull requests",
                Filter      = new ExpressionFilter("ms.vss-code.git-pullrequest-event")
                {
                    FilterModel = new ExpressionFilterModel()
                    {
                        Clauses = new ExpressionFilterClause[]
                        {
                            new ExpressionFilterClause()
                            {
                                FieldName       = "Vote",
                                Operator        = "Changes to",
                                Value           = "Waiting for author",
                                LogicalOperator = "And"
                            },
                            new ExpressionFilterClause()
                            {
                                FieldName       = "Created by",
                                Operator        = "=",
                                Value           = "[Me]",
                                LogicalOperator = "And"
                            }
                        }
                    }
                },
                Channel = new UserSubscriptionChannel()
            };

            // Scope to only events from one project
            ProjectHttpClient projectClient = this.Context.Connection.GetClient <ProjectHttpClient>();

            Guid   projectId;
            String projectName;

            if (!this.Context.TryGetValue <String>("projectName", out projectName))
            {
                // Get the ID of the first project
                projectId = projectClient.GetProjects().Result.First().Id;
            }
            else
            {
                // Get the ID of the specified project
                projectId = projectClient.GetProject(projectName).Result.Id;
            }

            createParams.Scope = new SubscriptionScope()
            {
                Id = projectId
            };

            NotificationSubscription newSubscription = notificationClient.CreateSubscriptionAsync(createParams).Result;
            String subscriptionId = newSubscription.Id;

            Context.Log("New subscription created! ID: {0}", subscriptionId);

            //
            // Part 2: disable and delete the subscription
            //

            // Disable the new subscription
            NotificationSubscriptionUpdateParameters updateParams = new NotificationSubscriptionUpdateParameters()
            {
                Status = SubscriptionStatus.Disabled
            };

            newSubscription = notificationClient.UpdateSubscriptionAsync(updateParams, subscriptionId).Result;

            Context.Log("Is subscription disabled? {0}", newSubscription.Status < 0);

            // Delete the subscription
            notificationClient.DeleteSubscriptionAsync(subscriptionId).SyncResult();

            // Try to get the subscription (should result in an exception)
            try
            {
                newSubscription = notificationClient.GetSubscriptionAsync(subscriptionId, SubscriptionQueryFlags.IncludeFilterDetails).Result;
            } catch (Exception e)
            {
                Context.Log("Unable to get the deleted subscription:" + e.Message);
            }

            // Try again (the default query flags says to return deleted subscriptions so this should work)
            newSubscription = notificationClient.GetSubscriptionAsync(subscriptionId).Result;

            return(newSubscription);
        }
        /// <summary>
        /// Gets all team members for the given <paramref name="projectId" /> and <paramref name="teamId"/>.
        /// </summary>
        /// <param name="client">The <see cref="TeamHttpClient" /> to use.</param>
        /// <param name="connection">The connection for the <paramref name="client"/> that will be used to retrieve the identities for the team members.</param>
        /// <param name="projectId">The project identifier.</param>
        /// <param name="teamId">The team identifier whose members to retrieve.</param>
        /// <param name="pageSize">Page size to use while retrieving the projects.</param>
        /// <param name="userState">The user state object.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        /// <exception cref="System.ArgumentException">$The '{nameof(connection)}' parameter must be for the given '{nameof(client)}'</exception>
        public static async Task<IReadOnlyCollection<Identity>> GetAllTeamMembers(this TeamHttpClient client, VssConnection connection, string projectId, string teamId, int pageSize = 10, object userState = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (client == null)
                throw new ArgumentNullException(nameof(client));
            if (connection == null)
                throw new ArgumentNullException(nameof(connection));
            if (projectId == null)
                throw new ArgumentNullException(nameof(projectId));
            if (teamId == null)
                throw new ArgumentNullException(nameof(teamId));
            
            if(Equals(client.BaseAddress, connection.Uri))
                throw new ArgumentException($"The '{nameof(connection)}' parameter must be for the base uri of the VSTS / TFS system", nameof(connection));

            var result = new List<Identity>();
            var identityReferences = new List<IdentityRef>();

            int currentPage = 0;
            var teamMembersForCurrentPage = (await client.GetTeamMembersAsync(projectId, teamId, pageSize, currentPage, userState, cancellationToken).ConfigureAwait(false)).ToList();
            while (teamMembersForCurrentPage.Count > 0)
            {
                cancellationToken.ThrowIfCancellationRequested();
                identityReferences.AddRange(teamMembersForCurrentPage);

                // check whether the recently returned item(s) were less than the max page size
                if (teamMembersForCurrentPage.Count < pageSize)
                    break; // if so, break the loop as we've read all instances

                // otherwise continue
                cancellationToken.ThrowIfCancellationRequested();
                teamMembersForCurrentPage = (await client.GetTeamMembersAsync(projectId, teamId, pageSize, currentPage, userState, cancellationToken).ConfigureAwait(false)).ToList();
            }

            cancellationToken.ThrowIfCancellationRequested();
            if (identityReferences.Count > 0)
            {
                using (var identityHttpClient = await connection.GetClientAsync<IdentityHttpClient>(cancellationToken).ConfigureAwait(false))
                {
                    result.AddRange(await identityHttpClient.ReadIdentitiesAsync(identityReferences.Select(identityReference => new Guid(identityReference.Id)).ToList(), cancellationToken: cancellationToken).ConfigureAwait(false));
                }
            }

            cancellationToken.ThrowIfCancellationRequested();
            return result;
        }