Ejemplo n.º 1
0
        protected override object Extract()
        {
            List <GitHubRepository> ret = new List <GitHubRepository>();

            string           BuildStorageConnectionString = configManager.GetConfig("BackendJobs", "OPSBuildStorageConnectionString");
            HashSet <string> OPGitRepoAccountWhitelist    = new HashSet <string>(configManager.GetConfig("BackendJobs", "OPGitRepoAccountWhitelist").Split(';'));

            var StorageAccount = CloudStorageAccount.Parse(BuildStorageConnectionString);
            var TableClient    = StorageAccount.CreateCloudTableClient();
            var repoTable      = TableClient.GetTableReference("RepositoryTableEntity");
            var userTable      = TableClient.GetTableReference("UserTableEntity");

            TableQuery <RepositoryTableEntity> query = new TableQuery <RepositoryTableEntity>();

            foreach (RepositoryTableEntity repo in repoTable.ExecuteQuery(query))
            {
                TableQuery <UserTableEntity> userQuery = new TableQuery <UserTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, repo.CreatedBy));
                UserTableEntity user = userTable.ExecuteQuery(userQuery).FirstOrDefault();

                if (user != null)
                {
                    ret.Add(new GitHubRepository()
                    {
                        Owner             = repo.GitRepositoryAccount,
                        RepositoryName    = repo.GitRepositoryName,
                        RepositoryUrl     = repo.GitRepositoryUrl,
                        AuthToken         = user.AccessToken,
                        PartitionKey      = repo.PartitionKey,
                        Timestamp         = repo.Timestamp.ToString("yyyy-MM-dd"),
                        GitRepositoryType = repo.GitRepositoryType
                    });
                }
            }
            return(ret);
        }
Ejemplo n.º 2
0
        public static object CallGitVSOAPI <T>(string url) where T : class
        {
            // Call build API to refresh token
            if (GenVSOTokenByCallingBuildAPI())
            {
                string filter1 = TableQuery.GenerateFilterCondition("Name", QueryComparisons.Equal, "Nanxuan Xu");
                string filter2 = TableQuery.GenerateFilterCondition("GitRepositoryType", QueryComparisons.Equal, "Vso");
                TableQuery <UserTableEntity> userQuery = new TableQuery <UserTableEntity>().Where(TableQuery.CombineFilters(filter1, TableOperators.And, filter2));

                ConfigManager   configManager = new ConfigManager();
                string          BuildStorageConnectionString = configManager.GetConfig("BackendJobs", "OPSBuildStorageConnectionString");
                var             StorageAccount = CloudStorageAccount.Parse(BuildStorageConnectionString);
                var             TableClient    = StorageAccount.CreateCloudTableClient();
                var             userTable      = TableClient.GetTableReference("UserTableEntity");
                UserTableEntity user           = userTable.ExecuteQuery(userQuery).FirstOrDefault();
                if (user == null)
                {
                    return(null);
                }
                string token = user.AccessToken; //RefreshToken?

                HttpWebRequest httpWebRequest = WebRequest.Create(new Uri(url)) as HttpWebRequest;
                httpWebRequest.Method      = "Get";
                httpWebRequest.Credentials = new NetworkCredential("Bearer", token);
                httpWebRequest.Headers.Add("Authorization", "Bearer " + token);
                httpWebRequest.Accept = "application/json";

                string contentInJson;
                JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
                try
                {
                    using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
                    {
                        if (response != null && response.StatusCode == HttpStatusCode.OK)
                        {
                            using (Stream stream = response.GetResponseStream())
                            {
                                if (stream != null)
                                {
                                    using (StreamReader sr = new StreamReader(stream))
                                    {
                                        contentInJson = sr.ReadToEnd();
                                        return(jsonSerializer.Deserialize <T>(contentInJson));
                                    }
                                }
                            };
                        }
                    }
                }
                catch (Exception e)
                {
                    if (e.Message.Contains("(404) Not Found."))
                    {
                        return(null);
                    }
                    throw e;
                }
            }
            return(null);
        }
Ejemplo n.º 3
0
        public static string GetGithubToken(string repositoryName)
        {
            ConfigManager configManager = new ConfigManager();

            string BuildStorageConnectionString = configManager.GetConfig("BackendJobs", "OPSBuildStorageConnectionString");

            var StorageAccount = CloudStorageAccount.Parse(BuildStorageConnectionString);
            var TableClient    = StorageAccount.CreateCloudTableClient();
            var userTable      = TableClient.GetTableReference("UserTableEntity");
            var repoTable      = TableClient.GetTableReference("RepositoryTableEntity");

            TableQuery <RepositoryTableEntity> query = new TableQuery <RepositoryTableEntity>().Where(TableQuery.GenerateFilterCondition("GitRepositoryName", QueryComparisons.Equal, repositoryName));;
            RepositoryTableEntity repo = repoTable.ExecuteQuery(query).FirstOrDefault();

            TableQuery <UserTableEntity> userQuery = new TableQuery <UserTableEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, repo.CreatedBy));
            UserTableEntity user = userTable.ExecuteQuery(userQuery).FirstOrDefault();

            if (user == null)
            {
                return(null);
            }
            return(user.AccessToken);
        }