Exemple #1
0
        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);
            };
        }
        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);
                }
            }
        }