WIP, initial work to implement pipeline hooks:

I might be squashing this commit in the future.
This commit is contained in:
Lin Jen-Shin 2016-07-28 01:16:31 +08:00
parent 9b0e131b83
commit ae5f124e96
8 changed files with 107 additions and 0 deletions

View file

@ -56,6 +56,7 @@ class Projects::HooksController < Projects::ApplicationController
def hook_params
params.require(:hook).permit(
:build_events,
:pipeline_events,
:enable_ssl_verification,
:issues_events,
:merge_requests_events,

View file

@ -213,6 +213,12 @@ module Ci
]
end
def execute_hooks
pipeline_data = Gitlab::DataBuilder::PipelineDataBuilder.build(self)
project.execute_hooks(pipeline_data.dup, :pipeline_hooks)
project.execute_services(pipeline_data.dup, :pipeline_hooks)
end
private
def build_builds_for_stages(stages, user, status, trigger_request)

View file

@ -5,5 +5,6 @@ class ProjectHook < WebHook
scope :note_hooks, -> { where(note_events: true) }
scope :merge_request_hooks, -> { where(merge_requests_events: true) }
scope :build_hooks, -> { where(build_events: true) }
scope :pipeline_hooks, -> { where(pipeline_events: true) }
scope :wiki_page_hooks, -> { where(wiki_page_events: true) }
end

View file

@ -8,6 +8,7 @@ class WebHook < ActiveRecord::Base
default_value_for :merge_requests_events, false
default_value_for :tag_push_events, false
default_value_for :build_events, false
default_value_for :pipeline_events, false
default_value_for :enable_ssl_verification, true
scope :push_hooks, -> { where(push_events: true) }

View file

@ -36,6 +36,7 @@ class Service < ActiveRecord::Base
scope :merge_request_hooks, -> { where(merge_requests_events: true, active: true) }
scope :note_hooks, -> { where(note_events: true, active: true) }
scope :build_hooks, -> { where(build_events: true, active: true) }
scope :pipeline_hooks, -> { where(pipeline_events: true, active: true) }
scope :wiki_page_hooks, -> { where(wiki_page_events: true, active: true) }
scope :external_issue_trackers, -> { issue_trackers.active.without_defaults }

View file

@ -0,0 +1,16 @@
class AddPipelineEventsToWebHooks < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_column_with_default(:web_hooks, :pipeline_events, :boolean,
default: false, allow_null: false)
end
def down
remove_column(:web_hooks, :pipeline_events)
end
end

View file

@ -0,0 +1,16 @@
class AddPipelineEventsToServices < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_column_with_default(:services, :pipeline_events, :boolean,
default: false, allow_null: false)
end
def down
remove_column(:services, :pipeline_events)
end
end

View file

@ -0,0 +1,65 @@
module Gitlab
module DataBuilder
module PipelineDataBuilder
module_function
def build(pipeline)
{
object_kind: 'pipeline',
user: pipeline.user.hook_attrs,
project: pipeline.project.hook_attrs(backward: false),
commit: pipeline.commit.hook_attrs,
object_attributes: hook_attrs(pipeline),
builds: pipeline.builds.map(&method(:build_hook_attrs))
}
end
def hook_attrs(pipeline)
first_pending_build = pipeline.builds.first_pending
config_processor = pipeline.config_processor
{
id: pipeline.id,
ref: pipeline.ref,
tag: pipeline.tag,
sha: pipeline.sha,
before_sha: pipeline.before_sha,
status: pipeline.status,
stage: first_pending_build.try(:stage),
stages: config_processor.try(:stages),
created_at: pipeline.created_at,
finished_at: pipeline.finished_at
}
end
def build_hook_attrs(build)
{
id: build.id,
stage: build.stage,
name: build.name,
status: build.status,
created_at: build.created_at,
started_at: build.started_at,
finished_at: build.finished_at,
when: build.when,
manual: build.manual?,
user: build.user.hook_attrs,
runner: runner_hook_attrs(build.runner),
artifacts_file: {
filename: build.artifacts_file.filename,
size: build.artifacts_size
}
}
end
def runner_hook_attrs(runner)
{
id: runner.id,
description: runner.description,
active: runner.active?,
is_shared: runner.is_shared?
}
end
end
end
end