Module: Gitlab::DataBuilder::Push

Extended by:
Push
Included in:
Push
Defined in:
lib/gitlab/data_builder/push.rb

Constant Summary collapse

SAMPLE_DATA =
{
  object_kind: "push",
  event_name: "push",
  before: "95790bf891e76fee5e1747ab589903a6a1f80f22",
  after: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  ref: "refs/heads/master",
  ref_protected: true,
  checkout_sha: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  message: "Hello World",
  user_id: 4,
  user_name: "John Smith",
  user_email: "[email protected]",
  user_avatar: "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
  project_id: 15,
  project: {
    id: 15,
    name: "gitlab",
    description: "",
    web_url: "http://test.example.com/gitlab/gitlab",
    avatar_url: "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80",
    git_ssh_url: "[email protected]:gitlab/gitlab.git",
    git_http_url: "http://test.example.com/gitlab/gitlab.git",
    namespace: "gitlab",
    visibility_level: 0,
    path_with_namespace: "gitlab/gitlab",
    default_branch: "master"
  },
  commits: [
    {
      id: "c5feabde2d8cd023215af4d2ceeb7a64839fc428",
      message: "Add simple search to projects in public area\n\ncommit message body",
      title: "Add simple search to projects in public area",
      timestamp: "2013-05-13T18:18:08+00:00",
      url: "https://test.example.com/gitlab/gitlab/-/commit/c5feabde2d8cd023215af4d2ceeb7a64839fc428",
      author: {
        name: "Test User",
        email: "[email protected]"
      }
    }
  ],
  total_commits_count: 1,
  push_options: { ci: { skip: true } }
}.freeze

Instance Method Summary collapse

Instance Method Details

#build(project:, user:, ref:, oldrev: nil, newrev: nil, commits: [], commits_count: nil, message: nil, push_options: {}, with_changed_files: true) ⇒ Object

Produce a hash of post-receive data

data = {

before: String,
after: String,
ref: String,
ref_protected: Boolean,
user_id: String,
user_name: String,
user_username: String,
user_email: String
project_id: Fixnum,
project: {
  id: Fixnum,
  name: String,
  description: String,
  web_url: String,
  avatar_url: String,
  git_ssh_url: String,
  git_http_url: String,
  namespace: String,
  visibility_level: Fixnum,
  path_with_namespace: String,
  default_branch: String
}
repository: {
  name: String,
  url: String,
  description: String,
  homepage: String,
},
commits: Array,
total_commits_count: Fixnum,
push_options: Hash

}

rubocop:disable Metrics/ParameterLists



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gitlab/data_builder/push.rb', line 90

def build(
  project:, user:, ref:, oldrev: nil, newrev: nil,
    commits: [], commits_count: nil, message: nil, push_options: {},
    with_changed_files: true)

  commits = Array(commits)

  # Total commits count
  commits_count ||= commits.size

  # Get latest 20 commits ASC
  commits_limited = commits.last(20)

  # For performance purposes maximum 20 latest commits
  # will be passed as post receive hook data.
  # n+1: https://gitlab.com/gitlab-org/gitlab-foss/issues/38259
  commit_attrs = Gitlab::GitalyClient.allow_n_plus_1_calls do
    commits_limited.map do |commit|
      commit.hook_attrs(with_changed_files: with_changed_files)
    end
  end

  type = Gitlab::Git.tag_ref?(ref) ? 'tag_push' : 'push'

  # Hash to be passed as post_receive_data
  {
    object_kind: type,
    event_name: type,
    before: oldrev,
    after: newrev,
    ref: ref,
    ref_protected: project.protected_for?(ref),
    checkout_sha: checkout_sha(project.repository, newrev, ref),
    message: message,
    user_id: user.id,
    user_name: user.name,
    user_username: user.username,
    user_email: user.public_email,
    user_avatar: user.avatar_url(only_path: false),
    project_id: project.id,
    project: project.hook_attrs,
    commits: commit_attrs,
    total_commits_count: commits_count,
    push_options: push_options,
    # DEPRECATED
    repository: project.hook_attrs.slice(:name, :url, :description, :homepage,
                                         :git_http_url, :git_ssh_url, :visibility_level)
  }
end

#build_bulk(action:, ref_type:, changes:) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/gitlab/data_builder/push.rb', line 140

def build_bulk(action:, ref_type:, changes:)
  {
    action: action,
    ref_count: changes.count,
    ref_type: ref_type
  }
end

#build_sample(project, user) ⇒ Object

This method provides a sample data generated with existing project and commits to test webhooks



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/gitlab/data_builder/push.rb', line 150

def build_sample(project, user)
  # Use sample data if repo has no commit
  # (expect the case of test service configuration settings)
  return sample_data if project.empty_repo?

  ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
  commits = project.repository.commits(project.default_branch.to_s, limit: 3)

  build(project: project,
        user: user,
        oldrev: commits.last&.id,
        newrev: commits.first&.id,
        ref: ref,
        commits: commits)
end

#checkout_sha(repository, newrev, ref) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/gitlab/data_builder/push.rb', line 170

def checkout_sha(repository, newrev, ref)
  # Checkout sha is nil when we remove branch or tag
  return if Gitlab::Git.blank_ref?(newrev)

  # Find sha for tag, except when it was deleted.
  if Gitlab::Git.tag_ref?(ref)
    tag_name = Gitlab::Git.ref_name(ref)
    tag = repository.find_tag(tag_name)

    if tag
      commit = repository.commit(tag.dereferenced_target)
      commit.try(:sha)
    end
  else
    newrev
  end
end

#sample_dataObject



166
167
168
# File 'lib/gitlab/data_builder/push.rb', line 166

def sample_data
  SAMPLE_DATA
end