Exemplo n.º 1
0
        private static void ProcessGitHubAPIResponse(GitStubProject project, HttpResponseMessage response)
        {
            (string code, string msg)err = (null, null);
            switch (response.StatusCode)
            {
            case HttpStatusCode.Created:     //SUCCESS!! If only it was so easy in the rest of your life...
                Console.WriteLine($"****GitHub Repository Created: https://github.com/{project.User}/{project.RepoName}"); break;

            case HttpStatusCode.NotFound:
                err = ("404", "Not Found.  In this context, it probably means a problem with your username or password"); break;

            case HttpStatusCode.Forbidden:
                err = ("403", "Forbidden.  Too many failed login attempts"); break;

            case HttpStatusCode.Unauthorized:
                err = ("401", "Unauthorized. Check username and password"); break;

            default:
                err = (response.StatusCode.ToString(), "Not sure what happened here..."); break;
            }

            if (err.code != null)
            {
                Console.WriteLine($"GitHub API Error, returned ({err.code}) while trying to create the repository. {err.msg}");
                Environment.Exit(2);
            }
        }
Exemplo n.º 2
0
        private static void CreateProjectStructure(GitStubProject project)
        {
            if (!project.UseExisting)
            {
                Console.WriteLine("**Create Project Structure**");
                if (project.UseSolution) //if solution was specified, make a simple structure
                {
                    Console.WriteLine($"****Use Solution: {project.Solution}");
                    //Create the folder structure =>src/Proj/
                    var projSubDir = $"src\\{project.Project}";
                    Directory.CreateDirectory(projSubDir);
                    RunDotnetNew($"sln -n {project.Solution}"); //Create the solution file

                    //Create the project in src/project
                    Console.WriteLine($"****Create Project in src/: {project.Project}");
                    RunDotnetNew(string.Join(" ", project.NewArgs), $"\\{projSubDir}");
                    RunDotnetSln($"add {projSubDir}"); //Add the new project to the solution

                    //TODO add tests/testProject and any other structure scaffolding
                }
                else
                {
                    Console.WriteLine($"****Create Project: {project.Project}");
                    RunDotnetNew(string.Join(" ", project.NewArgs));
                }
            }

            Console.WriteLine("****Add .gitignore");
            //Add .gitignore
            //Thank you: https://github.com/dotnet/core/blob/master/.gitignore
            File.WriteAllText(".gitignore", gitIgnores(), System.Text.Encoding.ASCII);
        }
Exemplo n.º 3
0
 private static void AddCommitPush(GitStubProject project)
 {
     Console.WriteLine("**Add Files**");
     RunGit("add -A"); //Add all the files in the directory
     Console.WriteLine($"**Commit Local : {project.CommitMessage}**");
     RunGit($"commit -m \"{project.CommitMessage}\"");
     Console.WriteLine("**Push to Github**");
     RunGit("push --set-upstream origin master");
 }
Exemplo n.º 4
0
        private static GitStubProject ProcessArgsArray(string[] args)
        {
            GitStubProject project = new GitStubProject();

            try
            {
                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i].ToLower())
                    {
                    case "-gsu":
                        project.User = args[++i]; break;

                    case "-gsp":
                        project.Pass = args[++i]; break;

                    case "-gsr":
                        project.Project = args[++i]; break;

                    case "-gss":
                        project.Solution    = args[++i];
                        project.UseSolution = true;
                        break;

                    case "-gsd":
                        project.Desc = args[++i]; break;

                    case "-gsc":
                        project.CommitMessage = args[++i]; break;

                    case "--private":
                        project.AccessPrivate = true; break;

                    case "--existing":
                        project.UseExisting = true; break;

                    case "--sln":
                        project.UseSolution = true; break;

                    default:
                        project.NewArgs.Add(args[i]); break;
                    }
                }
            }
            catch (IndexOutOfRangeException outofRange)
            {
                Console.WriteLine("Error: Invalid Parameters.  It's not you baby, it's just the params.");
                Environment.Exit(2);
            }

#if DEBUG
            Console.WriteLine(project.ToString());
#endif

            return(project);
        }
Exemplo n.º 5
0
        private static void InitializeLocalRepository(GitStubProject project)
        {
            Console.WriteLine("**Git Init**");
            //Initialize the local git repo
            var gitArgs = "init";

            RunGit(gitArgs);

            Console.WriteLine("****Remote add origin");
            //Point the repository to github location
            RunGit($"remote add origin https://{project.Auth}@github.com/{project.User}/{project.RepoName}.git");
        }
Exemplo n.º 6
0
        private static HttpResponseMessage CreateGitHubRepository(GitStubProject project, StringContent apiContent, HttpClient client)
        {
            //Thank you: https://stackoverflow.com/questions/2423777/is-it-possible-to-create-a-remote-repo-on-github-from-the-cli-without-opening-br
            var gitHubApiUrl = "https://api.github.com/user/repos";

            //Thank you: https://stackoverflow.com/questions/50732772/best-curl-u-equivalence-in-c-sharp
            client.DefaultRequestHeaders.Add("Authorization", "Basic " + System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(project.Auth)));
            //Thank you: https://stackoverflow.com/questions/39907742/github-api-is-responding-with-a-403-when-using-requests-request-function
            client.DefaultRequestHeaders.Add("User-Agent", $"{project.User}/{project.RepoName}");

            var response = client.PostAsync(gitHubApiUrl, apiContent).Result;

            return(response);
        }
Exemplo n.º 7
0
        private static void ValidateParameters(GitStubProject project)
        {
            List <(string field, string param)> errs = new List <(string, string)>();

            if (string.IsNullOrWhiteSpace(project.User))
            {
                errs.Add(("your GitHub username", "-gsa <username>"));
            }
            if (string.IsNullOrWhiteSpace(project.Pass))
            {
                errs.Add(("your GitHub password", "-gsp <password>"));
            }

            if (errs.Count > 0)
            {
                errs.ForEach(err => Console.WriteLine($"Error: missing parameter, need {err.field}, please specify it with {err.param}"));
                Environment.Exit(1);
            }
        }
Exemplo n.º 8
0
        private static void CreateGitHubRepository(GitStubProject project)
        {
            Console.WriteLine("**Create Github Repository**");
            ////    This can be extended to provide more parameters to creation, in addition to name and description
            //Thank you: https://stackoverflow.com/questions/18971510/how-do-i-set-up-httpcontent-for-my-httpclient-postasync-second-parameter
            var apiValues  = $"{{\"name\":\"{project.RepoName}\", \"description\":\"{project.Desc}\"{(project.AccessPrivate ? $", \"private\":\"{project.AccessPrivate}\"": "")}}}";
            var apiContent = new StringContent(apiValues, System.Text.Encoding.UTF8, "application/json");

            using (var client = new HttpClient())
            {
                try
                {
                    Console.WriteLine("****Post to Github API");
                    HttpResponseMessage response = CreateGitHubRepository(project, apiContent, client);

                    ProcessGitHubAPIResponse(project, response);
                }
                catch (HttpRequestException reqEx)
                {
                    Console.WriteLine($"HttpRequestException while trying to create the repository on GitHub.  The exception says: {reqEx.Message}");
                }
            }
        }