Exemplo n.º 1
0
        public void CreateOAuthToken(string code, Action <bool, string> result)
        {
            var command     = "token -h " + HostAddress.WebUri.Host;
            var octorunTask = new OctorunTask(taskManager.Token, environment, command, code)
                              .Configure(processManager);

            octorunTask
            .Then((b, octorunResult) =>
            {
                if (b && octorunResult.IsSuccess)
                {
                    var first = octorunResult.Output.FirstOrDefault();
                    if (first == null)
                    {
                        result(false, "Error validating token.");
                        return;
                    }

                    var match = accessTokenRegex.Match(first);
                    if (match.Success)
                    {
                        var token = match.Groups[1].Value;
                        LoginWithToken(token, b1 => result(b1, "Error validating token."));
                    }
                    else
                    {
                        result(false, octorunResult.Output.FirstOrDefault());
                    }
                }
                else
                {
                    result(false, octorunResult.Output.FirstOrDefault());
                }
            })
            .Catch(exception => result(false, exception.ToString()))
            .Start();
        }
Exemplo n.º 2
0
        public void CreateRepository(string name, string description, bool isPrivate,
                                     Action <GitHubRepository, Exception> callback, string organization = null)
        {
            Guard.ArgumentNotNull(callback, "callback");

            new FuncTask <GitHubRepository>(taskManager.Token, () =>
            {
                EnsureValidCredentials();

                var command = new StringBuilder("publish");

                if (!HostAddress.IsGitHubDotCom())
                {
                    command.Append(" -h ");
                    command.Append(HostAddress.ApiUri.Host);
                }

                command.Append(" -r \"");
                command.Append(name);
                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 adapter = EnsureKeychainAdapter();

                var octorunTask = new OctorunTask(taskManager.Token, environment, command.ToString(), adapter.Credential.Token)
                                  .Configure(processManager);

                var ret = octorunTask.RunSynchronously();
                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");
            })
            .FinallyInUI((success, ex, repository) =>
            {
                if (success)
                {
                    callback(repository, null);
                }
                else
                {
                    logger.Error(ex, "Error creating repository");
                    callback(null, ex);
                }
            })
            .Start();
        }
Exemplo n.º 3
0
        public void GetEnterpriseServerMeta(Action <GitHubHostMeta> onSuccess, Action <Exception> onError = null)
        {
            Guard.ArgumentNotNull(onSuccess, nameof(onSuccess));
            new FuncTask <GitHubHostMeta>(taskManager.Token, () =>
            {
                var octorunTask = new OctorunTask(taskManager.Token, environment, "meta -h " + HostAddress.ApiUri.Host)
                                  .Configure(processManager);

                var ret = octorunTask.RunSynchronously();
                if (ret.IsSuccess)
                {
                    var deserializeObject = SimpleJson.DeserializeObject <Dictionary <string, object> >(ret.Output[0]);

                    return(new GitHubHostMeta
                    {
                        InstalledVersion = (string)deserializeObject["installed_version"],
                        GithubServicesSha = (string)deserializeObject["github_services_sha"],
                        VerifiablePasswordAuthentication = (bool)deserializeObject["verifiable_password_authentication"]
                    });
                }

                var message = ret.GetApiErrorMessage();

                logger.Trace("Message: {0}", message);

                if (message != null)
                {
                    if (message.Contains("ETIMEDOUT", StringComparison.InvariantCulture))
                    {
                        message = "Connection timed out.";
                    }
                    else if (message.Contains("ECONNREFUSED", StringComparison.InvariantCulture))
                    {
                        message = "Connection refused.";
                    }
                    else if (message.Contains("ENOTFOUND", StringComparison.InvariantCulture))
                    {
                        message = "Address not found.";
                    }
                    else
                    {
                        int httpStatusCode;
                        if (int.TryParse(message, out httpStatusCode))
                        {
                            var httpStatus = ((HttpStatusCode)httpStatusCode).ToString();
                            message        = httpStatusErrorRegex.Replace(httpStatus, " $1");
                        }
                    }
                }
                else
                {
                    message = "Error getting server meta";
                }

                throw new ApiClientException(message);
            })
            .FinallyInUI((success, ex, meta) =>
            {
                if (success)
                {
                    onSuccess(meta);
                }
                else
                {
                    logger.Error(ex, "Error getting server meta");
                    onError?.Invoke(ex);
                }
            })
            .Start();
        }