Example #1
0
        // Start of the set up action
        public string[] SetUpGitHubEnvironment(string token)
        {
            string jsonFilePath = Server.MapPath("~") + @"\Json\";
            string baseAddress  = System.Configuration.ConfigurationManager.AppSettings["BaseAddress"];

            GitHubConfiguration con = new GitHubConfiguration {
                baseAddress = baseAddress, mediaType = "application/json", scheme = "Bearer", token = token
            };
            Users user = new Users(con);

            HttpResponseMessage res        = user.GetUserDetail();
            UserDetail          userDetail = new UserDetail();

            GitHubRepoResponse.RepoCreated GitHubRepo = new GitHubRepoResponse.RepoCreated();
            if (res.IsSuccessStatusCode)
            {
                userDetail = JsonConvert.DeserializeObject <UserDetail>(res.Content.ReadAsStringAsync().Result);
            }
            if (!string.IsNullOrEmpty(userDetail.login))
            {
                GitHubConfiguration repoCon = new GitHubConfiguration {
                    baseAddress = baseAddress, mediaType = "application/json", scheme = "Bearer", token = token, userName = userDetail.login
                };
                Repository repo = new Repository(repoCon);

                //Reading Create Repo JSON and Source code import JSON
                string createRepoJson    = accessDetails.ReadJsonFile(jsonFilePath + "CreateRepo.json");
                string srcCodeImportJson = accessDetails.ReadJsonFile(jsonFilePath + "SourceImport.json");

                //Deserializing Source code JSON into JObject to get the RepoName by splitting the "vcs_url"
                JObject jobj     = JsonConvert.DeserializeObject <JObject>(srcCodeImportJson);
                string  repoName = jobj["vcs_url"].ToString().Split('/')[jobj["vcs_url"].ToString().Split('/').Length - 1]; // splitting the "vcs_url" to get repoName
                createRepoJson = createRepoJson.Replace("$RepoName$", repoName);                                            // Replacing RepoName in create repo json, which creates repo with same name
                HttpResponseMessage response = repo.CreateRepository(createRepoJson);
                if (response.StatusCode.ToString() == "422")
                {
                    // if the Repo already exist, this part will be executed, will append the GUID and create the Repo
                    CreateRepo CreateRepo   = JsonConvert.DeserializeObject <CreateRepo>(createRepoJson);
                    string     guidToAppend = Guid.NewGuid().ToString();
                    guidToAppend = guidToAppend.Substring(0, 8);

                    string RepoName = CreateRepo.name + "_" + guidToAppend;
                    CreateRepo.name = RepoName;
                    string UpdatedJson = JsonConvert.SerializeObject(CreateRepo);
                    response = repo.CreateRepository(UpdatedJson);
                }
                if (response.IsSuccessStatusCode)
                {
                    GitHubRepo      = JsonConvert.DeserializeObject <GitHubRepoResponse.RepoCreated>(response.Content.ReadAsStringAsync().Result);
                    CreatedRepoName = GitHubRepo.name;
                }
                // Import the source code to user repo[to newly created repo], this is an async process which takes some time to import repo
                repo.ImportRepository(srcCodeImportJson, GitHubRepo.owner.login, GitHubRepo.name);
            }
            return(new string[] { });
        }