示例#1
0
        public async Task <CiCdDto> CreateAsync(CiCdDto dto)
        {
            encryptionService.Encrypt(dto);

            CiCd cicd = new CiCd
            {
                Id             = Guid.NewGuid(),
                Endpoint       = dto.Endpoint,
                Kind           = dto.Kind,
                IsEnabled      = dto.IsEnabled,
                IsGuestEnabled = dto.IsGuestEnabled,
                ApiKey         = dto.ApiKey,
                Password       = dto.Password,
                Username       = dto.Username
            };

            context.CicdSources.Add(cicd);
            await context.SaveChangesAsync();

            return(new CiCdDto
            {
                ApiKey = cicd.ApiKey,
                CiCdId = cicd.Id,
                Endpoint = cicd.Endpoint,
                Kind = cicd.Kind,
                Username = cicd.Username,
                Password = cicd.Password
            });
        }
示例#2
0
 protected override void AddAuthentication(HttpClient client, CiCdDto cicd)
 {
     if (!string.IsNullOrWhiteSpace(cicd.Username) && !string.IsNullOrWhiteSpace(cicd.Password))
     {
         client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Base64Encode($"{cicd.Username}:{cicd.Password}"));
     }
 }
示例#3
0
        public async Task <CiCdDto> UpdateAsync(CiCdDto dto)
        {
            encryptionService.Encrypt(dto);

            var cicd = context.CicdSources.Find(dto.CiCdId);

            cicd.Endpoint       = dto.Endpoint;
            cicd.Kind           = dto.Kind;
            cicd.IsEnabled      = dto.IsEnabled;
            cicd.IsGuestEnabled = dto.IsGuestEnabled;
            cicd.Password       = dto.Password;
            cicd.Username       = dto.Username;
            cicd.ApiKey         = dto.ApiKey;

            context.CicdSources.Update(cicd);
            await context.SaveChangesAsync();

            return(new CiCdDto
            {
                ApiKey = cicd.ApiKey,
                CiCdId = cicd.Id,
                Endpoint = cicd.Endpoint,
                Kind = cicd.Kind,
                Username = cicd.Username,
                Password = cicd.Password
            });
        }
示例#4
0
        public Task <List <CiCdBuildDto> > GetBuildsAsync(RepositoryDto repository, CiCdDto cicd)
        {
            var picker = new UniqueRandomPicker <string>(With.Between(1).And(FakeBuilds.Length).Elements, new UniqueRandomGenerator());
            var items  = picker.From(FakeBuilds);

            return(Task.FromResult(items.Select(x => new CiCdBuildDto {
                Name = x, WebUrl = $"http://ci.rbxd.ds:8090/path/to/build/{x}"
            }).ToList()));
        }
示例#5
0
        protected HttpClient GetHttpClient(CiCdDto cicd)
        {
            if (!cache.TryGetValue(cicd.Endpoint, out HttpClient client))
            {
                client = new HttpClient(new HttpClientHandler {
                    ServerCertificateCustomValidationCallback = (req, cert, chain, policy) => true
                });
                AddAuthentication(client, cicd);
                cache.Set(cicd.Endpoint, client);
            }

            return(client);
        }
示例#6
0
        protected override void AddAuthentication(HttpClient client, CiCdDto cicd)
        {
            bool UserAndApiKey = !string.IsNullOrWhiteSpace(cicd.Username) && !string.IsNullOrWhiteSpace(cicd.ApiKey);

            if (UserAndApiKey)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", Base64Encode($"{cicd.Username}:{cicd.ApiKey}"));
            }
            else
            {
                bool UserAndPassword = !string.IsNullOrWhiteSpace(cicd.Username) && !string.IsNullOrWhiteSpace(cicd.Password);

                if (UserAndPassword)
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", Base64Encode($"{cicd.Username}:{cicd.Password}"));
                }
            }
        }
示例#7
0
        protected override async Task <List <CiCdBuildDto> > TryGetBuildsAsync(RepositoryDto repository, CiCdDto cicd)
        {
            var builds = new List <CiCdBuildDto>();

            try
            {
                var client   = GetHttpClient(cicd);
                var authMode = cicd.IsGuestEnabled ? "guestAuth" : "httpAuth";
                var query    = cicd.Endpoint.Trim('/') + $"/{authMode}/app/rest/vcs-roots";

                logger.LogTrace($"Checking {query} for builds related to {repository.WebUrl}");

                var vcsRootsResponse = await client.GetAsync(query, HttpCompletionOption.ResponseContentRead);

                var vcsRootsDocument = XDocument.Parse(await vcsRootsResponse.Content.ReadAsStringAsync());

                foreach (var vcsRootElement in vcsRootsDocument.Root.Elements("vcs-root"))
                {
                    var uri = vcsRootElement.Attribute("href")?.Value;

                    try
                    {
                        var vcsRootResponse = await client.GetAsync(cicd.Endpoint.Trim('/') + uri);

                        var vscRootDocument = XDocument.Parse(await vcsRootResponse.Content.ReadAsStringAsync());
                        var projectElement  = vscRootDocument.Root.Element("project");
                        var vcsElement      = vscRootDocument.Root.Element("properties").Elements("property").FirstOrDefault(p => p.Attribute("name").Value == "url");
                        var name            = projectElement.Attribute("name").Value;
                        var webUri          = projectElement.Attribute("webUrl").Value;
                        var vcsUrl          = vcsElement?.Attribute("value").Value;

                        if (matcher.IsMatch(vcsUrl, repository.Url))
                        {
                            builds.Add(new CiCdBuildDto {
                                Name = name, WebUrl = webUri, CiCdId = cicd.CiCdId, Kind = cicd.Kind
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogTrace(e, $"Error finding build at {uri}");
                    }
                }
            }
            catch (Exception e)
            {
                logger.LogTrace(e, $"Error finding builds for {repository.WebUrl} at {cicd.Endpoint}");
            }

            return(builds);
        }
示例#8
0
 public void Encrypt(CiCdDto cicd)
 {
     cicd.Username = Encrypt(cicd.Username);
     cicd.ApiKey   = Encrypt(cicd.ApiKey);
     cicd.Password = Encrypt(cicd.Password);
 }
示例#9
0
        protected override async Task <List <CiCdBuildDto> > TryGetBuildsAsync(RepositoryDto repository, CiCdDto cicd)
        {
            var results = new List <CiCdBuildDto>();

            try
            {
                HttpClient httpClient = GetHttpClient(cicd);

                logger.LogTrace($"Checking {cicd.Endpoint} for builds related to {repository.WebUrl}");

                var jobs = await GetJobs(httpClient, new Uri(cicd.Endpoint));

                var builds = await GetBuilds(httpClient, jobs);

                foreach (var build in builds)
                {
                    try
                    {
                        var buildInfo = XDocument.Parse(await HttpClient.GetStringAsync(new Uri(new Uri(build, UriKind.Absolute), "api/xml")));
                        var vcsUrl    = buildInfo.XPathSelectElement("//remoteUrl").Value;
                        var name      = buildInfo.XPathSelectElement("//fullDisplayName").Value;
                        var webUrl    = buildInfo.XPathSelectElement("//url").Value;

                        if (matcher.IsMatch(vcsUrl, repository.Url))
                        {
                            results.Add(new CiCdBuildDto {
                                Name = name, WebUrl = webUrl, CiCdId = cicd.CiCdId, Kind = cicd.Kind
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        logger.LogTrace(e, $"Error finding build at {build}");
                    }
                }
            }
            catch (Exception e)
            {
                logger.LogTrace(e, $"Error finding builds for {repository.WebUrl} at {cicd.Endpoint}");
            }

            return(results);
        }
示例#10
0
 protected abstract Task <List <CiCdBuildDto> > TryGetBuildsAsync(RepositoryDto repository, CiCdDto cicd);
示例#11
0
 public async Task <List <CiCdBuildDto> > GetBuildsAsync(RepositoryDto repository, CiCdDto cicd)
 {
     using (var scope = logger.BeginScope($"[{repository.Url}]::[{cicd.Endpoint}]"))
     {
         try
         {
             return(await TryGetBuildsAsync(repository, cicd));
         }
         catch (Exception e)
         {
             logger.LogError(e, $"[{repository.Url}]::[{cicd.Endpoint}]::ERROR");
             return(null);
         }
     }
 }
示例#12
0
 protected abstract void AddAuthentication(HttpClient client, CiCdDto cicd);