Пример #1
0
        private async Task <GitHubRepository> CreateRepositoryInternal(string repositoryName, string organization, string description, bool isPrivate)
        {
            try
            {
                logger.Trace("Creating repository");

                var user = await GetCurrentUser();

                var keychainAdapter = keychain.Connect(OriginalUrl);

                var command = new StringBuilder("publish -r \"");
                command.Append(repositoryName);
                command.Append("\"");

                if (!string.IsNullOrEmpty(description))
                {
                    command.Append(" -d \"");
                    command.Append(description);
                    command.Append("\"");
                }

                if (!string.IsNullOrEmpty(organization))
                {
                    command.Append(" -o \"");
                    command.Append(organization);
                    command.Append("\"");
                }

                if (isPrivate)
                {
                    command.Append(" -p");
                }

                var octorunTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath, octorunScriptPath, command.ToString(),
                                                  user: user.Login, userToken: keychainAdapter.Credential.Token)
                                  .Configure(processManager);

                var ret = await octorunTask.StartAwait();

                if (ret.IsSuccess && ret.Output.Length == 2)
                {
                    return(new GitHubRepository
                    {
                        Name = ret.Output[0],
                        CloneUrl = ret.Output[1]
                    });
                }

                throw new ApiClientException(ret.GetApiErrorMessage() ?? "Publish failed");
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error Creating Repository");
                throw;
            }
        }
Пример #2
0
        private async Task <LoginResultData> TryLogin(
            UriString host,
            string username,
            string password,
            string code = null
            )
        {
            if (!nodeJsExecutablePath.HasValue)
            {
                throw new InvalidOperationException("nodeJsExecutablePath must be set");
            }

            if (!octorunScript.HasValue)
            {
                throw new InvalidOperationException("octorunScript must be set");
            }

            var hasTwoFactorCode = code != null;

            var arguments = hasTwoFactorCode ? "login --twoFactor" : "login";
            var loginTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath.Value, octorunScript.Value,
                                            arguments, ApplicationInfo.ClientId, ApplicationInfo.ClientSecret);

            loginTask.Configure(processManager, workingDirectory: octorunScript.Value.Parent.Parent, withInput: true);
            loginTask.OnStartProcess += proc =>
            {
                proc.StandardInput.WriteLine(username);
                proc.StandardInput.WriteLine(password);
                if (hasTwoFactorCode)
                {
                    proc.StandardInput.WriteLine(code);
                }
                proc.StandardInput.Close();
            };

            var ret = await loginTask.StartAwait();

            if (ret.IsSuccess)
            {
                return(new LoginResultData(LoginResultCodes.Success, null, host, ret.Output[0]));
            }

            if (ret.IsTwoFactorRequired)
            {
                var resultCodes = hasTwoFactorCode ? LoginResultCodes.CodeFailed : LoginResultCodes.CodeRequired;
                var message     = hasTwoFactorCode ? "Incorrect code. Two Factor Required." : "Two Factor Required.";

                return(new LoginResultData(resultCodes, message, host, ret.Output[0]));
            }

            return(new LoginResultData(LoginResultCodes.Failed, ret.GetApiErrorMessage() ?? "Failed.", host));
        }
Пример #3
0
        private async Task <GitHubRepository> CreateRepositoryInternal(string repositoryName, string organization, string description, bool isPrivate)
        {
            try
            {
                logger.Trace("Creating repository");

                await ValidateKeychain();
                await ValidateCurrentUserInternal();

                var uriString       = keychain.Connections.First().Host;
                var keychainAdapter = await keychain.Load(uriString);

                var command = new StringBuilder("publish -r \"");
                command.Append(repositoryName);
                command.Append("\"");

                if (!string.IsNullOrEmpty(description))
                {
                    command.Append(" -d \"");
                    command.Append(description);
                    command.Append("\"");
                }

                if (!string.IsNullOrEmpty(organization))
                {
                    command.Append(" -o \"");
                    command.Append(organization);
                    command.Append("\"");
                }

                if (isPrivate)
                {
                    command.Append(" -p");
                }

                var octorunTask = new OctorunTask(taskManager.Token, nodeJsExecutablePath, octorunScriptPath, command.ToString(),
                                                  user: keychainAdapter.Credential.Username, userToken: keychainAdapter.Credential.Token)
                                  .Configure(processManager);

                var ret = await octorunTask.StartAwait();

                if (ret.IsSuccess && ret.Output.Length == 2)
                {
                    return(new GitHubRepository
                    {
                        Name = ret.Output[0],
                        CloneUrl = ret.Output[1]
                    });
                }

                if (ret.Output.Any())
                {
                    throw new ApiClientException(string.Join(Environment.NewLine, ret.Output));
                }

                throw new ApiClientException("Publish failed");
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error Creating Repository");
                throw;
            }
        }