Skip to content
Snippets Groups Projects

Persist source sha and target sha for merge pipelines

Merged Shinya Maeda requested to merge persist-source-sha-and-target-sha-for-pipelines into master
All threads resolved!

What does this MR do?

This MR let CreatePipelineService persist target_sha and source_sha. target_sha stores the SHA of the HEAD of the target branch of the merge request. source_sha stores the SHA of the HEAD of the source branch of the merge request.

This MR also introduces a few helper methods with these values.

Test in EE => https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/9739

Expected usage

## How to create a detached pipeline?
CreatePipelineService.new(project, user, ref: "refs/merge-requests/iid/head", checkout_sha: merge_request.source_branch_sha)
                     .execute(:merge_request, merge_request: merge_request)

## How to create a merge pipeline?
CreatePipelineService.new(project, user, ref: "refs/merge-requests/iid/merge", checkout_sha: merge_sha, source_sha: merge_request.diff_head_sha, target_sha: merge_request.target_branch_sha)
                     .execute(:merge_request, merge_request: merge_request)

## How to evaluate if it's a detached pipeline?
pipeline.merge_request? && pipeline.target_sha.nil?

## How to evaluate if it's a merge pipeline?
pipeline.merge_request? && pipeline.target_sha.present?

## How to evaluate if the merge pipeline uses HEAD of target branch (If false, don't merge)?
pipeline.merge_request? && pipeline.target_sha == merge_request.target_branch_sha

What are the relevant issue numbers?

Related https://gitlab.com/gitlab-org/gitlab-ee/issues/7380

Does this MR meet the acceptance criteria?

Edited by Shinya Maeda

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Shinya Maeda marked the checklist item Tests added for this feature/bug as completed

    marked the checklist item Tests added for this feature/bug as completed

  • Shinya Maeda added 1 commit

    added 1 commit

    • be1c53c8 - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Author Maintainer

    @ayufan Would you mind doing an early review?

  • assigned to @ayufan

  • Shinya Maeda changed the description

    changed the description

  • Shinya Maeda added 1 commit

    added 1 commit

    • 8c5c39e9 - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • @dosuken123

    I think that this is good direction, but:

    1. I think we should pass source_sha and target_sha as params, instead of reading that from merge request,
    2. If we want to read that during call, it should rather be filled by reading the commit.id and filling the source_sha and target_sha from commit parents, as this allows us to efficiently store commit parents where commit is identified by ci_pipelines.sha,
    3. I don't think that we should store BLANK_SHA, it does not make sense. We should rather keep it nil,
    4. I have comments to your snippet, as for merge pipeline you keep using as ref source branch, maybe we should rather use ref that actually reflects used merge ref?

    I don't know what are the consequences of using that ref, but I assume that this is what we should be doing anyway. It will definitely break detached pipeline today for old runners, so maybe we could only require /merge to be used like that and transition to use /head with %12.0?

    ## How to create a detached pipeline?
    CreatePipelineService.new(project, user, ref: "refs/merge-requests/iid/head", checkout_sha: merge_request.source_branch_sha)
                         .execute(:merge_request, merge_request: merge_request)
    
    ## How to create a merge pipeline?
    CreatePipelineService.new(project, user, ref: "refs/merge-requests/iid/merge", checkout_sha: merge_sha, source_sha: merge_request.diff_head_sha, target_sha: merge_request.target_branch_sha)
                         .execute(:merge_request, merge_request: merge_request)
    
    ## How to evaluate if it's a detached pipeline?
    pipeline.merge_request? && pipeline.target_sha.nil?
    
    ## How to evaluate if it's a merge pipeline?
    pipeline.merge_request? && pipeline.target_sha.present?
    
    ## How to evaluate if the merge pipeline uses HEAD of target branch (If false, don't merge)?
    pipeline.merge_request? && pipeline.target_sha == merge_request.target_branch_sha
    Edited by Kamil Trzciński
  • Kamil Trzciński assigned to @dosuken123

    assigned to @dosuken123

  • Shinya Maeda added 1 commit

    added 1 commit

    Compare with previous version

  • Shinya Maeda added 1 commit

    added 1 commit

    • bfac467f - Allow nil source sha and target sha

    Compare with previous version

  • Shinya Maeda added 1 commit

    added 1 commit

    • 37a2f3ee - Allow nil source sha and target sha

    Compare with previous version

  • Shinya Maeda resolved all discussions

    resolved all discussions

  • Shinya Maeda added 1 commit

    added 1 commit

    Compare with previous version

  • Shinya Maeda added 161 commits

    added 161 commits

    Compare with previous version

  • Shinya Maeda changed the description

    changed the description

  • Shinya Maeda added 1 commit

    added 1 commit

    Compare with previous version

  • Author Maintainer

    @ayufan

    I don't know what are the consequences of using that ref, but I assume that this is what we should be doing anyway. It will definitely break detached pipeline today for old runners, so maybe we could only require /merge to be used like that and transition to use /head with %12.0?

    We'll try to implement this, however, we have to measure the impact on the potential refactoring at first. Also, storing full ref path is out of scope in this MR. This MR is simply focusing on allowing the system to store target_sha and source_sha in order for the detection of detached/merge MR pipelines.

  • Shinya Maeda added 1 commit

    added 1 commit

    • b5fcfd8c - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Author Maintainer

    @reprazent Would you mind taking a look at this? Thanks! We're planning to merge this today!

  • @dosuken123 Only nitpicks from my side.

  • Bob Van Landuyt assigned to @dosuken123

    assigned to @dosuken123

  • Shinya Maeda resolved all discussions

    resolved all discussions

  • Shinya Maeda added 1 commit

    added 1 commit

    • 433aeb88 - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Author Maintainer

    @reprazent Thank you for reviewing!

    @ayufan Would you mind taking a look at this MR?

  • assigned to @ayufan

  • Shinya Maeda changed the description

    changed the description

  • Shinya Maeda changed the description

    changed the description

  • Shinya Maeda added 1 commit

    added 1 commit

    • 1e1c8cf2 - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Shinya Maeda added 1 commit

    added 1 commit

    • e891be3e - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Author Maintainer

    @gl-database Hi DB specialists :bangbang: I'd like to ask you to review for database related changes.

    • db/migrate/20190220150130_add_extra_shas_to_ci_pipelines.rb
    • db/schema.rb

    It's simply adding two columns source_sha and target_sha to ci_pipelines table. So that I don't think there is a problematic code. Please let me know if you have any questions.

  • Author Maintainer

    We can compare bytea type column and varchar type column on PostgreSQL by using encode and decode.

    Here is a brief test on PostgreSQL instance. Both encoding the value to varchar and decoding the value to bytea correctly match the row.

    gitlabhq_development_ce=# select * from gpg_signatures where commit_sha = '6ac2afa3b2278cbf147a717894eb7643615d0884';
     id | created_at | updated_at | project_id | gpg_key_id | commit_sha | gpg_key_primary_keyid | gpg_key_user_name | gpg_key_user_email | verification_status | gpg_key_subkey_id
    ----+------------+------------+------------+------------+------------+-----------------------+-------------------+--------------------+---------------------+-------------------
    (0 rows)
    
    gitlabhq_development_ce=# select * from gpg_signatures where encode(commit_sha, 'hex') = '6ac2afa3b2278cbf147a717894eb7643615d0884';
     id |          created_at           |          updated_at           | project_id | gpg_key_id |                 commit_sha                 | gpg_key_primary_keyid | gpg_key_user_name | gpg_key_user_email | verification_status | gpg_key_subkey_id
    ----+-------------------------------+-------------------------------+------------+------------+--------------------------------------------+-----------------------+-------------------+--------------------+---------------------+-------------------
      1 | 2018-12-06 14:25:52.860447+09 | 2018-12-06 14:25:52.860447+09 |         50 |            | \x6ac2afa3b2278cbf147a717894eb7643615d0884 | \x7d7005a5732d1234    |                   |                    |                   5 |
    (1 row)
    
    gitlabhq_development_ce=# select * from gpg_signatures where commit_sha = decode('6ac2afa3b2278cbf147a717894eb7643615d0884', 'hex');
     id |          created_at           |          updated_at           | project_id | gpg_key_id |                 commit_sha                 | gpg_key_primary_keyid | gpg_key_user_name | gpg_key_user_email | verification_status | gpg_key_subkey_id
    ----+-------------------------------+-------------------------------+------------+------------+--------------------------------------------+-----------------------+-------------------+--------------------+---------------------+-------------------
      1 | 2018-12-06 14:25:52.860447+09 | 2018-12-06 14:25:52.860447+09 |         50 |            | \x6ac2afa3b2278cbf147a717894eb7643615d0884 | \x7d7005a5732d1234    |                   |                    |                   5 |
    (1 row)

    In MySQL, we need a different method CONVERT(column USING utf8).

    (Ref: https://gitlab.slack.com/archives/C3NBYFJ6N/p1551090922020000)

    Edited by Shinya Maeda
  • Kamil Trzciński assigned to @dosuken123

    assigned to @dosuken123

  • I think DB question. I think someone needs to make a final call: do we need or not need, otherwise comparision will be cumbersome :)

  • assigned to @abrandl

  • Shinya Maeda added 146 commits

    added 146 commits

    Compare with previous version

  • Shinya Maeda added 1 commit

    added 1 commit

    • d0b09bff - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Andreas Brandl assigned to @dosuken123

    assigned to @dosuken123

  • Shinya Maeda added 1 commit

    added 1 commit

    • bafa652b - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Shinya Maeda added 9 commits

    added 9 commits

    Compare with previous version

  • Shinya Maeda added 1 commit

    added 1 commit

    • 25146f70 - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Shinya Maeda added 1 commit

    added 1 commit

    • 24394eaa - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Author Maintainer

    @ayufan We've reached a conclusion about binary vs string https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/25417#note_145060335 Does it make sense to you?

  • assigned to @ayufan

  • Shinya Maeda added 32 commits

    added 32 commits

    Compare with previous version

  • Shinya Maeda added 1 commit

    added 1 commit

    • 5ac616b6 - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Kamil Trzciński approved this merge request

    approved this merge request

  • @dosuken123 Approved! Can you make it green? :)

    Edited by Kamil Trzciński
  • Kamil Trzciński assigned to @dosuken123

    assigned to @dosuken123

  • Shinya Maeda added 34 commits

    added 34 commits

    Compare with previous version

  • Shinya Maeda changed the description

    changed the description

  • Shinya Maeda added 1 commit

    added 1 commit

    • 0a9178d6 - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Shinya Maeda mentioned in merge request !25510 (closed)

    mentioned in merge request !25510 (closed)

  • Shinya Maeda added 1 commit

    added 1 commit

    • 314062fe - Persist source sha and target sha for merge pipelines

    Compare with previous version

  • Shinya Maeda mentioned in merge request !25504 (merged)

    mentioned in merge request !25504 (merged)

  • Shinya Maeda resolved all discussions

    resolved all discussions

  • Author Maintainer

    @ayufan Fixed pipeline. It's green now :thumbsup:

  • assigned to @ayufan

  • mentioned in commit d40a3809

  • mentioned in merge request gitlab!130467 (merged)

  • Please register or sign in to reply
    Loading