Example #1
0
        public void Query_ValidUserAndRepo_MapsReturnedUserFieldsCorrectly()
        {
            //  Arrange
            string            userUrl     = "https://api.github.com/users/robconery";
            GithubUser        githubUser  = ValidGithubUser();
            GithubRepo        githubRepo  = CreateRepo(1, 5);
            List <GithubRepo> githubRepos = new List <GithubRepo> {
                githubRepo
            };
            Mock <IWebClient> webClient = new Mock <IWebClient>();

            webClient.Setup(r => r.Query <GithubUser>(userUrl)).Returns(githubUser);
            webClient.Setup(r => r.Query <List <GithubRepo> >(githubUser.ReposUrl)).Returns(githubRepos);
            IRepository repository = new GithubRepository(webClient.Object);

            //  Act
            IUser userReturned = repository.SearchUsers(githubUser.Login);

            //  Assert
            Assert.Equal(githubUser.Login, userReturned.Login);
            Assert.Equal(githubUser.Name, userReturned.Name);
            Assert.Equal(githubUser.AvatarUrl, userReturned.AvatarUrl);
            Assert.Equal(githubUser.Location, userReturned.Location);
            Assert.Equal(githubUser.TopFiveMostPopularRepos[0].Name, userReturned.TopFiveMostPopularRepos[0].Name);
            Assert.Equal(githubUser.TopFiveMostPopularRepos[0].StargazersCount, userReturned.TopFiveMostPopularRepos[0].PopularityCount);
        }
        public IActionResult commits(string repo)
        {
            string        url  = "https://api.github.com/repos/" + _User + "/" + repo + "/commits";
            GithubRepo    rep  = new GithubRepo(url);
            List <Commit> info = rep.GetCommits("User");

            return(Json(info));
        }
        public IActionResult GetUser()
        {
            string     url  = "https://api.github.com/users/" + _User + "";
            GithubRepo rep  = new GithubRepo(url);
            UserInfo   info = rep.GetUserInfo();

            return(Json(info));
        }
        public IActionResult GetUserRepositories()
        {
            string                url   = "https://api.github.com/users/" + _User + "/repos";
            GithubRepo            rep   = new GithubRepo(url);
            List <UserRepository> repos = rep.GetAllRepositories().ToList();

            return(Json(repos));
        }
Example #5
0
        private void SetValueForContributerToRepo(GithubRepo repo)
        {
            IOrderedQueryable <ContributerToRepo> orderedQueryable =
                (IOrderedQueryable <ContributerToRepo>)db.ContributerToRepoes.Where(ctp => ctp.RepoId == repo.Id).OrderBy(ctp => ctp.Id);

            this.InterateChange <ContributerToRepo>(orderedQueryable, new Action <ContributerToRepo>(ctp =>
            {
                ctp.CacheCommitNumber = ctp.CalculatingCommitNumber;
            }));
        }
        public IHttpActionResult Post(GithubRepo githubRepo)
        {
            string     urlToAdd = githubRepo.Url.Trim();
            GithubRepo repo     = db.GithubRepoes.Where(gr => gr.Url == urlToAdd).FirstOrDefault();

            if (repo == null)
            {
                db.GithubRepoes.Add(githubRepo);
                db.SaveChanges();
                return(Created(githubRepo));
            }
            else
            {
                return(Conflict());
            }
        }
Example #7
0
        static void Main(string[] args)
        {
            var githubRepoId = Guid.NewGuid();
            var githubRepo   = new GithubRepo(githubRepoId, "eric.huang", "testrepo", DateTime.UtcNow);

            githubRepo.Commit("eric.huang", "init commit", DateTime.UtcNow);
            githubRepo.Commit("eric.huang", "2nd commit", DateTime.UtcNow);
            githubRepo.CreateBranch("eric.huang", "branch 1", DateTime.UtcNow);
            githubRepo.CreateBranch("eric.huang", "branch 2", DateTime.UtcNow);

            Console.WriteLine("LastCommitAtUtc: {0}", githubRepo.LastCommitAtUtc);
            Console.WriteLine("LastEventAppliedAt: {0}", githubRepo.LastEventAppliedAt);

            foreach (var uncommittedEvent in githubRepo.UncommittedEvents)
            {
                Console.WriteLine("UncommittedEvent: {0}", JsonConvert.SerializeObject(uncommittedEvent));
            }

            Console.ReadLine();
        }
Example #8
0
        private JToken getAuthors(GithubRepo repo, GithubRelease release)
        {
            // Start with the user that published the release
            var authors = new HashSet <string>()
            {
                release.Author
            };

            for (GithubRepo r = repo; r != null;)
            {
                if (r.Owner?.Login != null)
                {
                    // Add repo owner
                    authors.Add(r.Owner.Login);
                }
                // Check parent repos
                r = r.ParentRepo == null
                    ? null
                    : _api.GetRepo(new GithubRef($"#/ckan/github/{r.ParentRepo.FullName}", false, _matchPreleases));
            }
            // Return a string if just one author, else an array
            return(authors.Count == 1 ? (JToken)authors.First() : new JArray(authors));
        }
Example #9
0
        private JToken getAuthors(GithubRepo repo, GithubRelease release)
        {
            // Start with the user that published the release
            var authors = new List <string>()
            {
                release.Author
            };

            for (GithubRepo r = repo; r != null;)
            {
                switch (r.Owner?.Type)
                {
                case userType:
                    // Prepend repo owner
                    if (!authors.Contains(r.Owner.Login))
                    {
                        authors.Insert(0, r.Owner.Login);
                    }
                    break;

                case orgType:
                    // Prepend org members
                    authors.InsertRange(0,
                                        _api.getOrgMembers(r.Owner)
                                        .Where(u => !authors.Contains(u.Login))
                                        .Select(u => u.Login)
                                        );
                    break;
                }
                // Check parent repos
                r = r.ParentRepo == null
                    ? null
                    : _api.GetRepo(new GithubRef($"#/ckan/github/{r.ParentRepo.FullName}", false, _matchPreleases));
            }
            // Return a string if just one author, else an array
            return(authors.Count == 1 ? (JToken)authors.First() : new JArray(authors));
        }
Example #10
0
        public void Query_ValidUserTenRepos_ReturnsTopFiveReposOrderedCorrectly()
        {
            //  Arrange
            string            userUrl     = "https://api.github.com/users/robconery";
            GithubUser        githubUser  = ValidGithubUser();
            GithubRepo        repo1       = CreateRepo(1, 5);
            GithubRepo        repo2       = CreateRepo(2, 500);
            GithubRepo        repo3       = CreateRepo(3, 51);
            GithubRepo        repo4       = CreateRepo(4, 43);
            GithubRepo        repo5       = CreateRepo(5, 56);
            GithubRepo        repo6       = CreateRepo(6, 999);
            GithubRepo        repo7       = CreateRepo(7, 0);
            GithubRepo        repo8       = CreateRepo(8, 14);
            GithubRepo        repo9       = CreateRepo(9, 23);
            GithubRepo        repo10      = CreateRepo(10, 34);
            List <GithubRepo> githubRepos = new List <GithubRepo>
            {
                repo1, repo2, repo3, repo4, repo5, repo6, repo7, repo8, repo9, repo10
            };
            Mock <IWebClient> webClient = new Mock <IWebClient>();

            webClient.Setup(r => r.Query <GithubUser>(userUrl)).Returns(githubUser);
            webClient.Setup(r => r.Query <List <GithubRepo> >(githubUser.ReposUrl)).Returns(githubRepos);
            IRepository repository = new GithubRepository(webClient.Object);

            //  Act
            IUser userReturned = repository.SearchUsers(githubUser.Login);

            //  Assert
            Assert.NotNull(userReturned.TopFiveMostPopularRepos);
            Assert.Equal(5, userReturned.TopFiveMostPopularRepos.Count);
            Assert.Equal(repo6.Name, userReturned.TopFiveMostPopularRepos[0].Name);
            Assert.Equal(repo2.Name, userReturned.TopFiveMostPopularRepos[1].Name);
            Assert.Equal(repo5.Name, userReturned.TopFiveMostPopularRepos[2].Name);
            Assert.Equal(repo3.Name, userReturned.TopFiveMostPopularRepos[3].Name);
            Assert.Equal(repo4.Name, userReturned.TopFiveMostPopularRepos[4].Name);
        }
Example #11
0
        private Metadata TransformOne(Metadata metadata, JObject json, GithubRef ghRef, GithubRepo ghRepo, GithubRelease ghRelease)
        {
            // Make sure resources exist.
            if (json["resources"] == null)
            {
                json["resources"] = new JObject();
            }

            var resourcesJson = (JObject)json["resources"];

            if (!string.IsNullOrWhiteSpace(ghRepo.Description))
            {
                json.SafeAdd("abstract", ghRepo.Description);
            }

            // GitHub says NOASSERTION if it can't figure out the repo's license
            if (!string.IsNullOrWhiteSpace(ghRepo.License?.Id) &&
                ghRepo.License.Id != "NOASSERTION")
            {
                json.SafeAdd("license", ghRepo.License.Id);
            }

            if (!string.IsNullOrWhiteSpace(ghRepo.Homepage))
            {
                resourcesJson.SafeAdd("homepage", ghRepo.Homepage);
            }

            resourcesJson.SafeAdd("repository", ghRepo.HtmlUrl);

            if (ghRelease != null)
            {
                json.SafeAdd("version", ghRelease.Version.ToString());
                json.SafeAdd("author", ghRelease.Author);
                json.SafeAdd("download", ghRelease.Download.ToString());
                json.SafeAdd(Model.Metadata.UpdatedPropertyName, ghRelease.AssetUpdated);

                if (ghRef.Project.Contains("_"))
                {
                    json.SafeAdd("name", ghRef.Project.Replace("_", " "));
                }
                else if (ghRef.Project.Contains("-"))
                {
                    json.SafeAdd("name", ghRef.Project.Replace("-", " "));
                }
                else if (ghRef.Project.Contains("."))
                {
                    json.SafeAdd("name", ghRef.Project.Replace(".", " "));
                }
                else
                {
                    var repoName = ghRef.Project;
                    for (var i = 1; i < repoName.Length - 1; ++i)
                    {
                        if (char.IsLower(repoName[i - 1]) && char.IsUpper(repoName[i]) || repoName[i - 1] != ' ' && char.IsUpper(repoName[i]) && char.IsLower(repoName[i + 1]))
                        {
                            repoName = repoName.Insert(i, " ");
                        }
                    }

                    json.SafeAdd("name", repoName);
                }

                Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);

                return(new Metadata(json));
            }
            else
            {
                Log.WarnFormat("No releases found for {0}", ghRef.Repository);
                return(metadata);
            }
        }
Example #12
0
 public async void PostRepo(GithubRepo item)
 {
     item.Id = 0;
     _context.GithubRepoItems.Add(item);
     await _context.SaveChangesAsync();
 }
Example #13
0
        private Metadata TransformOne(
            Metadata metadata, JObject json, GithubRef ghRef, GithubRepo ghRepo, GithubRelease ghRelease,
            GithubReleaseAsset ghAsset, String version
            )
        {
            if (!string.IsNullOrWhiteSpace(ghRepo.Description))
            {
                json.SafeAdd("abstract", ghRepo.Description);
            }

            // GitHub says NOASSERTION if it can't figure out the repo's license
            if (!string.IsNullOrWhiteSpace(ghRepo.License?.Id) &&
                ghRepo.License.Id != "NOASSERTION")
            {
                json.SafeAdd("license", ghRepo.License.Id);
            }

            // Make sure resources exist.
            if (json["resources"] == null)
            {
                json["resources"] = new JObject();
            }

            var resourcesJson = (JObject)json["resources"];

            if (!string.IsNullOrWhiteSpace(ghRepo.Homepage))
            {
                resourcesJson.SafeAdd("homepage", ghRepo.Homepage);
            }

            resourcesJson.SafeAdd("repository", ghRepo.HtmlUrl);
            if (ghRepo.HasIssues)
            {
                resourcesJson.SafeAdd("bugtracker", $"{ghRepo.HtmlUrl}/issues");
            }

            if (ghRelease != null)
            {
                json.SafeAdd("version", version);
                json.SafeAdd("author", () => getAuthors(ghRepo, ghRelease));
                json.Remove("$kref");
                json.SafeAdd("download", ghAsset.Download.ToString());
                json.SafeAdd(Metadata.UpdatedPropertyName, ghAsset.Updated);

                if (ghRef.Project.Contains("_"))
                {
                    json.SafeAdd("name", ghRef.Project.Replace("_", " "));
                }
                else if (ghRef.Project.Contains("-"))
                {
                    json.SafeAdd("name", ghRef.Project.Replace("-", " "));
                }
                else if (ghRef.Project.Contains("."))
                {
                    json.SafeAdd("name", ghRef.Project.Replace(".", " "));
                }
                else
                {
                    var repoName = ghRef.Project;
                    for (var i = 1; i < repoName.Length - 1; ++i)
                    {
                        if (char.IsLower(repoName[i - 1]) && char.IsUpper(repoName[i]) || repoName[i - 1] != ' ' && char.IsUpper(repoName[i]) && char.IsLower(repoName[i + 1]))
                        {
                            repoName = repoName.Insert(i, " ");
                        }
                    }

                    json.SafeAdd("name", repoName);
                }

                json.SafeMerge(
                    "x_netkan_version_pieces",
                    JObject.FromObject(new Dictionary <string, string> {
                    { "tag", ghRelease.Tag.ToString() }
                })
                    );

                Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);

                return(new Metadata(json));
            }
            else
            {
                Log.WarnFormat("No releases found for {0}", ghRef.Repository);
                return(metadata);
            }
        }