Skip to content

fix: skip head merge commits on pull requests - #6253

Merged
grouville merged 10 commits into
dagger:mainfrom
grouville:skip-merge-commit
Dec 19, 2023
Merged

grouville merged 10 commits into
dagger:mainfrom
grouville:skip-merge-commit

Conversation

@grouville

@grouville grouville commented Dec 12, 2023 •

Copy link
Copy Markdown
Member

Fixes https://linear.app/dagger/issue/DEV-3086/pull-user-commit-message-in-a-context-pr

When collecting labels on the engine in a VCS context, sometimes, the commit head is an appended commit (from that VCS) [PR / MR]. This fix skips the merge request commit when it is the head.

Repro/test: https://gist.github.com/grouville/6cf442d86c41c0746bbef1812160e7c0

12/13 Advancements

Realization: only GitHub puts the PR in a detached HEAD with only the merge commit. GitLab keeps the git history of the branch being tested

After reviews + some testing, a few changes have been implemented:

  1. Make sure that the main branch doesn't skip the merge commits. Only pull_requests shall be be impacted
  2. Due to the realization, we need to fetch the branch and get the commits from there. However, this is an operation requiring authorization. To circumvent this, we rely on the fact that GitHub runners automatically add the GITHUB_TOKEN secrets to git CLI commands

12/15 Advancements

Previous implementation was introducing a bug by printing errors to stdout, which polluted the dagger listen connection string.

The error came from the fact that on Dagger, the PRs come from a fork and not the origin. However, after spending some time reverse engineering Github Actions' runner context, I did not find a 100% solution to retrieve the origin of a fork. This is a best try approach, where we build the fork's remote of a repo from available information. If we do not manage to do it, then we fallback on the current logic

Comment thread core/pipeline/label.go Resolved
@grouville
grouville force-pushed the skip-merge-commit branch 2 times, most recently from cee7515 to 0f3e3d8 Compare December 13, 2023 18:02
Comment thread core/pipeline/label.go Resolved
Comment thread core/pipeline/label.go Resolved
Comment thread core/pipeline/label.go Resolved
Comment thread core/pipeline/label.go Resolved
@grouville
grouville marked this pull request as draft December 13, 2023 19:04
@grouville
grouville force-pushed the skip-merge-commit branch 2 times, most recently from 347404b to 2713137 Compare December 14, 2023 00:38
@grouville
grouville marked this pull request as ready for review December 14, 2023 05:29
@grouville

grouville commented Dec 14, 2023 •

Copy link
Copy Markdown
Member Author

@marcosnils the failing test does not seem to come from me. I re-ran it a few times when in draft, and I kept having the same issue, I wonder if it is a cache thing ?
When running this test locally, it seems to work. All manual tests work perfectly on GitLab and GitHub

@grouville
grouville force-pushed the skip-merge-commit branch 2 times, most recently from f927ea6 to 0005721 Compare December 14, 2023 22:31
@grouville
grouville marked this pull request as draft December 14, 2023 23:24
@grouville
grouville force-pushed the skip-merge-commit branch 2 times, most recently from 7cab516 to 4aabb0e Compare December 16, 2023 00:04
@grouville
grouville marked this pull request as ready for review December 16, 2023 00:24
@grouville grouville changed the title fix: skip Head merge commits fix: skip Head merge commits on pull requests Dec 16, 2023
@grouville grouville changed the title fix: skip Head merge commits on pull requests fix: skip head merge commits on pull requests Dec 16, 2023
Comment thread core/pipeline/label.go Resolved
When collecting labels on the engine in a VCS context, sometimes, the commit head is an appended commit (from that VCS)

Signed-off-by: grouville <[email protected]>
…d HEAD state

The TestLoadGitLabels test was updated to account for the `dagger.io/git.branch` label in the expected output for the "detached HEAD state" scenario.

This change was necessary due to recent updates in the `LoadGitLabels` function in the `label.go` file. The function was modified to iterate over all references in the repository and check if any of them point to the commit hash. If a reference is found that matches the commit hash and it is a branch, the function adds the `dagger.io/git.branch` label.

This new behavior allows the function to correctly identify the branch association of a commit, even in a detached HEAD state. The test was updated to reflect this change and now expects the `dagger.io/git.branch` label in the output for the "detached HEAD state" scenario.

Signed-off-by: grouville <[email protected]>
Following Justin's comment, only escape merge commits in PR context

Signed-off-by: grouville <[email protected]>
This commit implements a few changes:
- After further checks, GitLab does not change the git history in a merge request context. It doesn't need to be handled
- GitHub automatically creates a detached merge commit containing only the merge commit.
To handle it, we need to fetch the origin branch and retrieve this commit. However, this requires authentication, which we bypass by leveraging the fact that GitHub Action automatically adds the GITHUB_TOKEN to any git commands inside their runner context

Signed-off-by: grouville <[email protected]>
Aim is to have a very readable implementation that handles the two separate logics:
- When the PR comes from a branch on the repo itself (origin)
- When the PR comes from a branch on a fork. Here, GitHub does not give us enough information to find with certainty the remote. So we try to construct the remote from the env var information. If it does not work, continue with current implementation

Signed-off-by: grouville <[email protected]>
In order to be as fast as possible, only fetch HEAD on either forked or origin remote

Signed-off-by: grouville <[email protected]>
Remove tmate for debugging purposes

Signed-off-by: grouville <[email protected]>
@grouville
grouville requested a review from jedevc December 18, 2023 20:53
@jedevc jedevc added this to the v0.9.5 milestone Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants