コード例 #1
0
        // TODO: add support for GitLab
        public PublicImportController()
        {
            Post("/api/v1/repository/submit/post", _ => {
                var errors = ValidationProcessor.Process(Request, new IValidatorRule[] {
                    new ShouldHaveParameters(new[] { "url" }),
                });
                if (errors.Count > 0)
                {
                    return(HttpResponse.Errors(errors));
                }

                var url = GetRequestStr("url");

                Uri parsedUrl;
                try {
                    parsedUrl = new Uri(url);
                }
                catch (Exception e) {
                    return(HttpResponse.Error(HttpStatusCode.UnprocessableEntity, "Invalid url"));
                }

                string host = parsedUrl.Host;

                if (
                    !(new[] { "github.com" }.Contains(host)) || parsedUrl.Segments.Length < 3
                    )
                {
                    return(HttpResponse.Error(HttpStatusCode.UnprocessableEntity, "Invalid hostname"));
                }

                RepoServiceType serviceType = host == "github.com" ? RepoServiceType.GitHub : RepoServiceType.GitLab;

                (DL.Model.Project.Project project, DL.Model.Repo.Repo repo)result = (null, null);

                if (serviceType == RepoServiceType.GitHub)
                {
                    var githubClient = GitHubApi.Client();
                    Repository repo;
                    try {
                        repo = githubClient.Repository.Get(
                            parsedUrl.Segments[1].Replace("/", ""),
                            parsedUrl.Segments[2].Replace("/", "")
                            ).Result;
                    }
                    catch (Exception e) {
                        return(HttpResponse.Error(HttpStatusCode.NotFound, "GitHub repository does not exist"));
                    }

                    var existingRepo = RepoRepository.Find(repo.Id.ToString(), serviceType);

                    if (existingRepo != null)
                    {
                        return(HttpResponse.Error(HttpStatusCode.UnprocessableEntity, "Project is already imported",
                                                  new JObject()
                        {
                            ["project"] = new ProjectTransformer().Transform(existingRepo.Project()),
                        }));
                    }

                    result = GitHubRepositoriesUtils.ImportProject(null, repo.Id.ToString());
                }

                return(HttpResponse.Data(new JObject()
                {
                    ["project"] = new ProjectTransformer().Transform(result.project),
                    ["repository"] = new RepoTransformer().Transform(result.repo)
                }));
            });
コード例 #2
0
ファイル: Repo.cs プロジェクト: gitcomteam/gitcom-api
        public Octokit.Repository GithubRepo()
        {
            var originId = Convert.ToInt64(origin_id == "" ? "0" : origin_id);

            return(GitHubApi.Client().Repository.Get(originId).Result);
        }
コード例 #3
0
        public SyncIssues()
        {
            Post("/api/v1/schedule/issues/sync/start", _ => {
                var task = Task.Run(async() => {
                    var githubClient = GitHubApi.Client();

                    var projects = DL.Model.Project.Project.GetRandom(50);

                    foreach (var project in projects)
                    {
                        var board = project.Boards().First(x => x.name == "Development");
                        if (board == null)
                        {
                            continue;
                        }

                        var todoColumn = board.Columns().First(c => c.name == "TODO");
                        if (todoColumn == null)
                        {
                            continue;
                        }

                        try {
                            var originId = project.Repository().origin_id;
                            var issues   = githubClient.Issue.GetAllForRepository(
                                Convert.ToInt64(originId)
                                , new ApiOptions()
                            {
                                PageSize = 100
                            }
                                ).Result;
                            foreach (var issue in issues)
                            {
                                try {
                                    var existingCard = Card.FindBy("origin_id", issue.Id.ToString());
                                    if (existingCard != null)
                                    {
                                        continue;
                                    }
                                    var card = CardRepository.CreateAndGet(
                                        issue.Title, issue.Body ?? "", 1, todoColumn, null
                                        );
                                    card.UpdateCol("origin_id", issue.Id.ToString());
                                }
                                catch (Exception e) {
                                    Console.WriteLine(e.Message);
                                }
                            }
                        }
                        catch (AggregateException e) {
                            if (e.Message.Contains("API rate limit"))
                            {
                                Console.WriteLine("waiting");
                                await Task.Delay(GitHubApi.TimeUntilReset() * 1000);
                            }
                            else
                            {
                                SentrySdk.CaptureException(e);
                            }
                        }
                        catch (Exception e) {
                            Console.WriteLine(e.Message);
                            SentrySdk.CaptureException(e);
                        }
                    }

                    Console.WriteLine("Finished!");
                });
                JobsPool.Get().Push(task);
                return(HttpResponse.Data(new JObject()));
            });
        }