Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing gitlabUserUsername definition for MR and Note triggers #1469

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

ljackiewicz
Copy link
Contributor

It seems that gitlabUserUsername variable is not set in case of build triggered by MergeRequest event and Note (comments) event.

Additionally, gitlabUserName and gitlabUserEmail variables for these triggers (MR and Note) was changed to their real values from GitLab webhooks. In case of MR Event user is an author of specific MR, and in case of Note (Comments) Event user is an author of specific comment in MR, not author of last commit.

The proposed changes have been checked on a local GitLab instance (launched from src/docker).

@ljackiewicz ljackiewicz requested a review from a team as a code owner May 9, 2023 09:03
@ljackiewicz ljackiewicz force-pushed the fix/missing-defined-variables branch from 8c8d0da to 02cba11 Compare May 10, 2023 07:12
@krisstern krisstern changed the title Missing defined gitlabUserUsername variable for MR and Note triggers Fix missing gitlabUserUsername variable for MR and note triggers May 10, 2023
@krisstern krisstern added the bug For changelog: Minor bug. Will be listed after features label May 10, 2023
@krisstern krisstern self-assigned this May 10, 2023
@krisstern krisstern changed the title Fix missing gitlabUserUsername variable for MR and note triggers Fix missing gitlabUserUsername definition for MR and note triggers May 10, 2023
@ljackiewicz
Copy link
Contributor Author

It seems that the jenkins build didn't run for the last changes added, is there any way to trigger it manually?

@ljackiewicz ljackiewicz changed the title Fix missing gitlabUserUsername definition for MR and note triggers Fix missing gitlabUserUsername definition for MR and Note triggers May 22, 2023
@ljackiewicz ljackiewicz force-pushed the fix/missing-defined-variables branch from 472cd77 to 82a1838 Compare May 29, 2023 11:27
@krisstern
Copy link
Member

Thanks @ljackiewicz! Let me do a review of your PR now. Will get back to you within a few hours.

@krisstern krisstern force-pushed the fix/missing-defined-variables branch from 82a1838 to c39ff4d Compare May 29, 2023 12:49
Copy link
Member

@krisstern krisstern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay so far... @ljackiewicz If it looks okay to you too I will merge it as is.

Comment on lines 111 to 113
assertThat(pushHookArgumentCaptor.getValue().getUser(), is(notNullValue()));
assertThat(pushHookArgumentCaptor.getValue().getUser().getName(), containsString("Administrator"));
assertThat(pushHookArgumentCaptor.getValue().getUser().getUsername(), containsString("root"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this only checks for when the User's name contains "Administrator" and the User's username is "root" by design. But there should be no harm done by adding this code snippet.

Comment on lines +243 to +245
.withUserName("User")
.withUserUsername("username")
.withUserEmail("user@gitlab.com")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are the tests we need for this PR I guess, which I have added/updated.

@ljackiewicz
Copy link
Contributor Author

Thanks for the code review and your comments, but unfortunately, it's not look okay to me, because now this PR does not fix described problem and I'm not quiet sure why the proposed changes were overwritten.

I tried to explain it above, but though the User object has a username field defined and we can call the getUsername method on it, this value is not set for the author of the commit, the GitLab webhook passes only the name and email (if the email is set to public for the GitLab account) values.

Example request for MR Event: https://github.com/jenkinsci/gitlab-plugin/blob/master/src/test/resources/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestEvent.json#L63

Example request for Note Event: https://github.com/jenkinsci/gitlab-plugin/blob/master/src/test/resources/com/dabsquared/gitlabjenkins/webhook/build/NoteEvent.json#L102

So the hook.getObjectAttributes().getLastCommit().getAuthor().getUsername() call for MergeRequestTriggerHandlerImpl and the hook.getMergeRequest().getLastCommit().getAuthor().getUsername() call for NoteTriggerHandlerImpl will return Null value, so the gitlabUserUsername variable still won't be set for these triggers.

@@ -198,6 +198,7 @@ protected CauseData retrieveCauseData(MergeRequestHook hook) {
.withSourceBranch(hook.getObjectAttributes().getSourceBranch())
.withUserName(
hook.getObjectAttributes().getLastCommit().getAuthor().getName())
.withUserUsername(hook.getUser().getUsername())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the UserUsername cannot be returned here with hook.getObjectAttributes().getLastCommit().getAuthor().getUsername() then we will need some upstream changes. I don't think we are supposed to stray from the pattern prescribed, as the suggested hook.getUser().getUsername() would obviously return something not related to the Last Commit Author.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and I mentioned that it can be confusing that gitlabUserName and gitlabUserUsername can represent two different persons.
It was probably a mistake that the author of the last commit was set as the author of the merge request (in case of MR Event) or comment (in case of Note Event).

@krisstern
Copy link
Member

Then if we were to follow your logic the test cases below will need to be changed as well to include the username:

@ljackiewicz
Copy link
Contributor Author

It's not a mistake in existing test cases, these represent very well the real ones. I don't know why, but the GitLab webhook will never pass the username value of an author of the commit by design.

@krisstern
Copy link
Member

It's not a mistake in existing test cases, these represent very well the real ones. I don't know why, but the GitLab webhook will never pass the username value of an author of the commit by design.

It is exactly my point. I don't mean there are mistakes in the existing test cases, but if your code works we will need to update as well as pass the new tests as well. Maybe we could work together to figure something out.

@ljackiewicz
Copy link
Contributor Author

I misunderstand you then, and maybe I'm a bit stubborn, but I would like to ask do you completely rejecting an idea to make some breaking changes to the project and add gitlabLastCommiterName and gitlabLastCommiterEmail variables mentioned earlier and change the values of the gitlabUserName and gitlabUserEmail variables?

@krisstern
Copy link
Member

I misunderstand you then, and maybe I'm a bit stubborn, but I would like to ask do you completely rejecting an idea to make some breaking changes to the project and add gitlabLastCommiterName and gitlabLastCommiterEmail variables mentioned earlier and change the values of the gitlabUserName and gitlabUserEmail variables?

Hi @ljackiewicz Actually I think it would be good to have the both the gitlab last committer name and gitlab username as variables. I am not sure if this would be breaking though (it probably is) but will need to see how other parts of the codebase is impacted with this change.

@krisstern
Copy link
Member

Any updates? @ljackiewicz

@ljackiewicz
Copy link
Contributor Author

@krisstern Unfortunately, I don't currently have enough time to make these changes, but I think I will try to do it as soon as possible.

@zeteref
Copy link

zeteref commented Sep 17, 2023

Hi, i have encountered a small bug related to this change. If a MR is created from tag/commitid instead of a branch hook.getObjectAttributes().getLastCommit().getAuthor().getUsername() raises nullpointerexception.

@ljackiewicz
Copy link
Contributor Author

@zeteref Can you provide some more information about the problem you encountered, and are you sure that you describe it well? Because there are nowhere in code call of hook.getObjectAttributes().getLastCommit().getAuthor().getUsername(). It was just krisstern's suggestion, but since this call will always return null, it should never be used.

I understand that you manually built and installed the plugin based on changes provided in this PR?

@github-actions github-actions bot added the tests This PR adds/removes/updates test cases label Dec 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting feedback bug For changelog: Minor bug. Will be listed after features tests This PR adds/removes/updates test cases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants