public JobGetConfigCommand(IJenkinsContext context, string jobName) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (string.IsNullOrEmpty(jobName)) { throw new ArgumentException("Value cannot be empty!", nameof(jobName)); } Url = NetPath.Combine(context.BaseUrl, "job", jobName, "config.xml"); UserName = context.UserName; Password = context.Password; Crumb = context.Crumb; OnWrite = request => { request.Method = "GET"; }; OnReadAsync = async response => { var document = await ReadXmlAsync(response); Result = new JenkinsProject(document.Root); }; }
public JobCreateCommand(IJenkinsContext context, string jobName, JenkinsProject job) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (string.IsNullOrEmpty(jobName)) { throw new ArgumentException("Value cannot be empty!", nameof(jobName)); } if (job == null) { throw new ArgumentNullException(nameof(job)); } Url = NetPath.Combine(context.BaseUrl, "createItem") + NetPath.Query(new { name = jobName }); UserName = context.UserName; Password = context.Password; Crumb = context.Crumb; OnWriteAsync = async request => { request.Method = "POST"; request.ContentType = "application/xml"; await WriteXmlAsync(request, job.Node); }; }
/// <summary> /// Updates the configuration on an existing Job in Jenkins asynchronously. /// </summary> /// <param name="jobName">The name of the Job.</param> /// <param name="job">The updated description of the Job.</param> /// <param name="token">An optional token for aborting the request.</param> /// <exception cref="JenkinsNetException"></exception> public async Task UpdateConfigurationAsync(string jobName, JenkinsProject job, CancellationToken token = default(CancellationToken)) { try { await new JobUpdateConfigurationCommand(context, jobName, job).RunAsync(token); } catch (Exception error) { throw new JenkinsNetException($"Failed to update Jenkins Job configuration '{jobName}'!", error); } }
/// <summary> /// Updates the configuration on an existing Job in Jenkins. /// </summary> /// <param name="jobName">The name of the Job.</param> /// <param name="job">The updated description of the Job.</param> /// <exception cref="JenkinsNetException"></exception> public void UpdateConfiguration(string jobName, JenkinsProject job) { try { new JobUpdateConfigurationCommand(context, jobName, job).Run(); } catch (Exception error) { throw new JenkinsNetException($"Failed to update Jenkins Job configuration '{jobName}'!", error); } }
/// <summary> /// Creates a new Job in Jenkins. /// </summary> /// <param name="jobName">The name of the Job to create.</param> /// <param name="job">The description of the Job to create.</param> /// <exception cref="JenkinsNetException"></exception> public void Create(string jobName, JenkinsProject job) { try { new JobCreateCommand(context, jobName, job).Run(); } catch (Exception error) { throw new JenkinsNetException($"Failed to create Jenkins Job '{jobName}'!", error); } }
/// <summary> /// Creates a new Job in Jenkins asynchronously. /// </summary> /// <param name="jobName">The name of the Job to create.</param> /// <param name="job">The description of the Job to create.</param> /// <param name="token">An optional token for aborting the request.</param> /// <exception cref="JenkinsNetException"></exception> public async Task CreateAsync(string jobName, JenkinsProject job, CancellationToken token = default) { try { await new JobCreateCommand(context, jobName, job).RunAsync(token); } catch (Exception error) { throw new JenkinsNetException($"Failed to create Jenkins Job '{jobName}'!", error); } }
public async Task JobCreate(string jobName, JenkinsProject job, string root = null) { if (string.IsNullOrEmpty(jobName)) { throw new ArgumentException("Value cannot be empty!", nameof(jobName)); } if (job == null) { throw new ArgumentNullException(nameof(job)); } string url = null; if (string.IsNullOrWhiteSpace(root)) { url = NetPath.Combine("createItem") + NetPath.Query(new { name = jobName }); } else { if (root.StartsWith("job/") || root.StartsWith("/job/")) { url = NetPath.Combine(root, "createItem") + NetPath.Query(new { name = jobName }); } else { url = NetPath.Combine("job", root, "createItem") + NetPath.Query(new { name = jobName }); } } var xmlSettings = new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Fragment, Indent = false, }; var contentData = ""; using (var sw = new StringWriter()) { using (var writer = XmlWriter.Create(sw, xmlSettings)) { job.Node.WriteTo(writer); } contentData = sw.ToString(); } using (var response = await _httpClient.PostAsync(url, new StringContent(contentData, Encoding.UTF8, "application/xml"))) { if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { await JobUpdate(jobName, job, root); } } }
/// <summary> /// Creates a new Job in Jenkins. /// </summary> /// <param name="jobName">The name of the Job to create.</param> /// <param name="job">The description of the Job to create.</param> /// <exception cref="JenkinsNetException"></exception> public void Create(string jobName, JenkinsProject job, string root = null) { try { httpClient.JobCreate(jobName, job, root).Wait(); } catch (Exception error) { throw new JenkinsNetException($"Failed to create Jenkins Job '{jobName}'!", error); } }
static void CreateJob() { var jobClient = serviceProvider.GetService <JenkinsClientJobs>(); var str = File.ReadAllText($"{AppContext.BaseDirectory}\\jekins-job-scm-git-template.xml"); XDocument xmlDoc = XDocument.Parse(str); XNode folderConfig = xmlDoc.Root; var project = new JenkinsProject(folderConfig); //jobClient.Create("/job/kf-wuh06-assign-project-1", "wuh06-test", project); jobClient.Create("wuh06-test", project, "/job/kf-wuh06-assign-project-1"); }
private static async Task SetupDeleteJobAsync(JenkinsClient client) { var jenkins = await client.GetAsync(); if (jenkins.Jobs.Any(x => string.Equals(x.Name, "Delete Job", System.StringComparison.OrdinalIgnoreCase))) { return; } var createJob = await client.Jobs.GetConfigurationAsync("Test Job"); var deleteJob = new JenkinsProject(createJob.Node); await client.Jobs.CreateAsync("Delete Job", deleteJob); }
public async Task <JenkinsBuild> GetJenkinsBuild(JenkinsProject connector, int buildNumber, CancellationToken token) { if (this.BuildRequest == null) { throw new InvalidOperationException($"{nameof(this.JobRequest)} must be handled for {nameof(FakeJenkinsApi)} to work."); } var eventArgs = new BuildEventArgs(connector, buildNumber); this.BuildRequest(this, eventArgs); if (eventArgs.ResponseDelay > 0) { await Task.Delay(eventArgs.ResponseDelay, token); } return(eventArgs.Result); }
public BuildEventArgs(JenkinsProject connector, int buildNumber) : base(connector) { this.BuildNumber = buildNumber; }
public JobEventArgs(JenkinsProject connector) : base(connector) { }
public FakeApiEventArgs(JenkinsProject connector) { this.Connector = connector; }
public void BuildsShouldNotBeMissedWhenBuildsAreRunningOneAfterAnother() { var scheduler = new ObservationScheduler(); var api = new FakeJenkinsApi(); int state = 0; api.JobRequest += (s, e) => { if (state == 0) { e.Result = ApiHelper.GetProject(ObservationState.Running, 10, 7); state++; } else if (state == 1) { e.Result = ApiHelper.GetProject(ObservationState.Running, 11, 7); state++; } else { e.Result = ApiHelper.GetProject(ObservationState.Running, 12, 7); scheduler.Stop(false); } }; api.BuildRequest += (s, e) => { if (e.BuildNumber < 10) { e.Result = ApiHelper.GetBuild(ObservationState.Unstable, e.BuildNumber); } else if (state == 0) { e.Result = ApiHelper.GetBuild(ObservationState.Running, e.BuildNumber); } else if (state == 1) { var buildSt = e.BuildNumber == 10 ? ObservationState.Success : ObservationState.Running; e.Result = ApiHelper.GetBuild(buildSt, e.BuildNumber); } else { ObservationState buildSt; if (e.BuildNumber == 10) { buildSt = ObservationState.Success; } else if (e.BuildNumber == 11) { buildSt = ObservationState.Failure; } else { buildSt = ObservationState.Running; } e.Result = ApiHelper.GetBuild(buildSt, e.BuildNumber); } }; var subj = new JenkinsProject(new ConnectorConfiguration(nameof(JenkinsProject)), api); scheduler.Observe(subj, 0); scheduler.Start(); while (scheduler.Running) { } var jenkinsStatuses = subj.Snapshots.Select(snap => snap.Status).OfType <JenkinsStatus>().ToList(); Assert.That(jenkinsStatuses.Count, Is.EqualTo(5)); var expectedBuilds = new[] { new Tuple <int, ObservationState>(11, ObservationState.Failure), new Tuple <int, ObservationState>(10, ObservationState.Success), new Tuple <int, ObservationState>(9, ObservationState.Unstable), new Tuple <int, ObservationState>(8, ObservationState.Unstable), new Tuple <int, ObservationState>(7, ObservationState.Unstable), }; foreach (var expectedBuild in expectedBuilds) { Assert.That(jenkinsStatuses.Any(stat => stat.BuildNumber == expectedBuild.Item1 && stat.State == expectedBuild.Item2), Is.True, $"Build No. {expectedBuild} was not present in snapshots."); } }
public JobUpdateConfigurationCommand(IJenkinsContext context, string jobName, JenkinsProject job) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (string.IsNullOrEmpty(jobName)) { throw new ArgumentException("Value cannot be empty!", nameof(jobName)); } if (job == null) { throw new ArgumentNullException(nameof(job)); } Url = NetPath.Combine(context.BaseUrl, "job", jobName, "config.xml"); UserName = context.UserName; Password = context.Password; Crumb = context.Crumb; OnWrite = request => { request.Method = "POST"; request.ContentType = "application/xml"; WriteXml(request, job.Node); }; #if NET_ASYNC OnWriteAsync = async(request, token) => { request.Method = "POST"; request.ContentType = "application/xml"; await WriteXmlAsync(request, job.Node, token); }; #endif }