Example #1
0
        public static Task PostForceEvent(
            IDictionary<string, object> environment,
            Func<IDictionary<string, object>, Task> next)
        {
            var fail = new Func<int, string, Task>((statusCode, message) =>
            {
                environment.SetStatusCode(statusCode);
                return environment.WriteAsync(message);
            });

            var form = environment.ReadForm();

            var payload = form["payload"];

            if (payload == null || payload.Count == 0)
            {
                fail(400, "Missing payload.");
                return environment.GetCompleted();
            }

            var @event = payload[0];

            var forceEvent = JsonConvert.DeserializeObject<ForceEvent>(@event);

            // TODO: validate the event's properties

            var buildConfiguration = fn.GetBuildConfiguration(forceEvent.Repository.Owner.Name, forceEvent.Repository.Name);
            if (buildConfiguration == null)
            {
                fail(422, "No build configuraion matches the identified build.");
                return environment.GetCompleted();
            }

            var newBuild = fn.CreateBuild(
                buildConfiguration.Command,
                buildConfiguration.CommandArguments,
                forceEvent.Repository.Name,
                forceEvent.Repository.Owner.Name,
                forceEvent.Ref,
                forceEvent.Revision,
                "rebuild",
                @event);

            var location = String.Format(
                "/{0}/{1}/builds/{2}",
                newBuild.RepositoryOwner,
                newBuild.RepositoryName,
                newBuild.Id);

            var absoluteLocation = String.Format(
                "http://{0}{1}",
                fn.GetHost(),
                location);

            environment.SetStatusCode(201);
            var responseHeaders = environment.GetResponseHeaders();
            responseHeaders.Add("Location", new [] { absoluteLocation });
            return environment.Render("queued", new { id = newBuild.Id, location});
        }
Example #2
0
        public static Task GetBuilds(
            IDictionary<string, object> environment,
            dynamic @params,
            Func<IDictionary<string, object>, Task> next)
        {
            var owner = @params.owner as string;
            var name = @params.name as string;

            var buildConfiguration = fn.GetBuildConfiguration(owner, name);
            if (buildConfiguration == null)
            {
                environment.SetStatusCode(400);
                return environment.GetCompleted();
            }

            var responseModel = new
            {
                owner = buildConfiguration.Owner,
                name = buildConfiguration.Name,
                builds = CreateBuildsResponseModel(buildConfiguration)
            };

            return environment.Render(
                "builds",
                responseModel);
        }
Example #3
0
        public static Task DeleteSession(
            IDictionary<string, object> environment,
            Func<IDictionary<string, object>, Task> next)
        {
            var owinContext = new OwinContext(environment);

            owinContext.Authentication.SignOut(new [] { "Session" });

            environment.SetRedirect("/");
            return environment.GetCompleted();
        }
Example #4
0
        public static Task PutSession(
            IDictionary<string, object> environment,
            Func<IDictionary<string, object>, Task> next)
        {
            var form = environment.ReadForm();

            var username = form["username"].FirstOrDefault();
            var password = form["password"].FirstOrDefault();

            var errors = new List<string>();

            var fail = new Func<Task>(() =>
            {
                environment.SetStatusCode(400);

                return environment.Render("sign-in-form", new
                {
                    errors = errors.Select(x => new {error = x}),
                    username
                });
            });

            if (String.IsNullOrEmpty(username))
                errors.Add("Username is required.");

            if (String.IsNullOrEmpty(password))
                errors.Add("Password is required.");

            if (errors.Count > 0)
                return fail();

            var user = fn.GetUserByUsername(username);
            if (user == null || !password.CompareToPasswordHash(user.PasswordHash))
            {
                errors.Add("Sign in failed.");
                return fail();
            }

            if (errors.Count > 0)
            {
                environment.SetStatusCode(400);

                return environment.Render("sign-in-form", new
                {
                    errors = errors.Select(x => new { error = x }),
                    username
                });
            }

            environment.SetUser(user);
            environment.SetRedirect("/");
            return environment.GetCompleted();
        }
Example #5
0
        public static Task PostUsers(
            IDictionary<string, object> environment,
            Func<IDictionary<string, object>, Task> next)
        {
            var form = environment.ReadForm();

            var username = form["username"].FirstOrDefault();
            var password = form["password"].FirstOrDefault();
            var emailAddress = form["email-address"].FirstOrDefault();

            var errors = new List<string>();

            if (String.IsNullOrEmpty(username))
                errors.Add("Username is required.");
            else
            {
                if (!Constants.UsernameRegex.IsMatch(username))
                    errors.Add("Username is invalid; must match regular expression '^[a-z][a-z0-9_-]+[a-z0-9]$'.");

                var exustingUser = fn.GetUserByUsername(username);
                if (exustingUser != null)
                    errors.Add("Username is not available; try another.");
            }

            if (String.IsNullOrEmpty(password))
                errors.Add("Password is required.");

            if (errors.Count > 0)
            {
                environment.SetStatusCode(400);

                return environment.Render("sign-up-form", new
                {
                    emailAddress,
                    errors = errors.Select(x => new { error = x }),
                    username
                });
            }

            var user = fn.CreateUser(username, password, emailAddress);

            var usersCount = fn.GetUsersCount();
            if (usersCount == 0)
            {
                fn.SetUserIsAdministrator(user, true);
            }

            environment.SetRedirect("/");

            return environment.GetCompleted();
        }
Example #6
0
        public static Task PatchUser(
            IDictionary<string, object> environment,
            dynamic @params,
            Func<IDictionary<string, object>, Task> next)
        {
            var username = @params.username as string;

            var user = fn.GetUserByUsername(username);
            if (user == null)
            {
                environment.SetStatusCode(404);
                return environment.GetCompleted();
            }

            var form = environment.ReadForm();

            var isAdministrator = form["isAdministrator"].FirstOrDefault() ?? "0";

            fn.SetUserIsAdministrator(user, isAdministrator == "1");

            environment.SetRedirect("/users");

            return environment.GetCompleted();
        }
Example #7
0
        public static Task GetBuild(
            IDictionary<string, object> environment,
            dynamic @params,
            Func<IDictionary<string, object>, Task> next)
        {
            var owner = @params.owner as string;
            var name = @params.name as string;
            var id = int.Parse(@params.id as string);

            var buildConfiguration = fn.GetBuildConfiguration(owner, name);
            if (buildConfiguration == null)
            {
                environment.SetStatusCode(400);
                return environment.GetCompleted();
            }

            var build = fn.GetBuild(id);

            var sha = (string)null;
            if (build.Revision != null)
                sha = build.Revision.Substring(0, 7);

            return environment.Render("build", new
            {
                id = build.Id,
                description = fn.GetBuildDescription(build, includeRefDescription: true),
                name = build.RepositoryName,
                owner = build.RepositoryOwner,
                @ref = build.Ref,
                refDescription = fn.GetRefDescription(build),
                revision = build.Revision,
                sha,
                failed = !build.Succeeded,
                output = fn.Redact(build.Ouput),
                status = GetBuildStatus(build)
            });
        }
Example #8
0
        public static Task PostPushEvent(
            IDictionary<string, object> environment,
            Func<IDictionary<string, object>, Task> next)
        {
            var fail = new Func<int, string, Task>((statusCode, message) =>
            {
                environment.SetStatusCode(statusCode);
                return environment.WriteAsync(message);
            });

            var headers = environment.GetRequestHeaders();

            if (!headers.ContainsKey("X-GitHub-Event"))
            {
                fail(400, "Missing X-GitHub-Event header.");
                return environment.GetCompleted();
            }

            var eventTypes = headers["X-GitHub-Event"];
            if (eventTypes != null)
            {
                var eventType = eventTypes.FirstOrDefault();

                if (eventType == null || !eventType.Equals("push", StringComparison.OrdinalIgnoreCase))
                {
                    fail(422, String.Concat("Unexpected X-GitHub-Event: ", eventType, "."));
                    return environment.GetCompleted();
                }
            }

            var form = environment.ReadForm();

            var payload = form["payload"];

            if (payload == null || payload.Count == 0)
            {
                fail(400, "Missing payload.");
                return environment.GetCompleted();
            }

            var @event = payload[0];

            var pushEvent = JsonConvert.DeserializeObject<PushEvent>(@event);

            // When a branch is deleted, GitHub sends a push event where after is 0000000.
            // There's no need to do anything in this case.
            if (pushEvent.After.StartsWith("0000000"))
            {
                environment.SetStatusCode(204);
                return environment.GetCompleted();
            }

            var buildConfigurations = fn.GetBuildConfigurations();

            var buildConfiguration = buildConfigurations.FirstOrDefault(bc =>
                bc.Name.Equals(pushEvent.Repository.Name, StringComparison.OrdinalIgnoreCase) &&
                bc.Owner.Equals(pushEvent.Repository.Owner.Name, StringComparison.OrdinalIgnoreCase));

            if (buildConfiguration == null)
            {
                environment.SetStatusCode(204);
                return environment.GetCompleted();
            }

            fn.CreateBuild(
                buildConfiguration.Command,
                buildConfiguration.CommandArguments,
                pushEvent.Repository.Name,
                pushEvent.Repository.Owner.Name,
                pushEvent.Ref,
                pushEvent.After,
                "push",
                @event);

            environment.SetStatusCode(204);
            return environment.GetCompleted();
        }
Example #9
0
        public static Task PostBuild(
            IDictionary<string, object> environment,
            dynamic @params,
            Func<IDictionary<string, object>, Task> next)
        {
            var owner = @params.owner as string;
            var name = @params.name as string;

            var buildConfiguration = fn.GetBuildConfiguration(owner, name);
            if (buildConfiguration == null)
            {
                environment.SetStatusCode(400);
                return environment.GetCompleted();
            }

            var fail = new Func<int, string, Task>((statusCode, errorMessage) =>
            {
                environment.SetStatusCode(statusCode);

                var responseModel = new
                {
                    owner = buildConfiguration.Owner,
                    name = buildConfiguration.Name,
                    builds = CreateBuildsResponseModel(buildConfiguration),
                    errorMessage
                };

                return environment.Render(
                    "builds",
                    responseModel);
            });

            var form = environment.ReadForm();

            var branchOrPr = form["branch-or-pr"].FirstOrDefault();

            if (String.IsNullOrEmpty(branchOrPr))
            {
                fail(400, "A branch or pull request number is required.");
                return environment.GetCompleted();
            }

            string @ref;
            if (branchOrPr.StartsWith("#"))
                @ref = String.Concat("refs/pull/", branchOrPr.Substring(1), "/head");
            else
                @ref = String.Concat("refs/heads/", branchOrPr);

            var newBuild = fn.CreateBuild(
                buildConfiguration.Command,
                buildConfiguration.CommandArguments,
                name,
                owner,
                @ref,
                null,
                null,
                null);

            var location = String.Format(
                "/{0}/{1}/builds/{2}",
                newBuild.RepositoryOwner,
                newBuild.RepositoryName,
                newBuild.Id);

            var absoluteLocation = String.Format(
                "http://{0}{1}",
                fn.GetHost(),
                location);

            environment.SetStatusCode(201);
            var responseHeaders = environment.GetResponseHeaders();
            responseHeaders.Add("Location", new [] { absoluteLocation });
            return environment.Render("queued", new { id = newBuild.Id, location});
        }