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

$DRONE_PULL_REQUEST not defined for MRs from non-forks #1931

Closed
k0nserv opened this issue Feb 7, 2017 · 12 comments
Closed

$DRONE_PULL_REQUEST not defined for MRs from non-forks #1931

k0nserv opened this issue Feb 7, 2017 · 12 comments

Comments

@k0nserv
Copy link

k0nserv commented Feb 7, 2017

Hey 👋,

We are using Gitlab and Drone 0.4. I've found from testing that when drone runs for MRs that orginated in the same repo $DRONE_PULL_REQUEST is not defined whereas when a build is ran for an MR from a fork it is defined. Is this expected or do we have a problem with our setup somewhere?

@bradrydzewski
Copy link

bradrydzewski commented Feb 7, 2017

The DRONE_PULL_REQUEST is set IF DRONE_EVENT == pull_request AND IF the ref includes the pull request number. The pull request number is parsed from refs/merge-requests/%d/head using a regular expression: https://github.com/drone/drone/blob/master/agent/agent.go#L311

I should point out that in some cases pushing to a feature branch and opening a pull request will trigger two builds. One for the push to the feature branch, and a second for the pull request. The former will never include the pull request number, the latter should. Check DRONE_EVENT to confirm the event type ispull_request.

Once you have verified the event is correct you can check and see what ref is being used. The ref isn't displayed in the user interface, but you can easily check the value using the API. You can just type http://drone.mycompany.com/api/repos/{owner}/{name}/builds/{number} into your browser to check. Make sure the ref looks something like refs/merge-requests/%d/head or has a digit that can be extracted with the regexp.

If the reference does not look correct someone needs to look at the hook that GitLab sends to Drone and see what fields are available, and make sure Drone is using the correct field. If it is determined drone is sourcing the ref value from an incorrect fields, a PR should be issued to update Drone.

Please note that the GitLab implementation is community contributed and not something I personally use. So while I can definitely help point you in the right direction, it is not something I can easily debug of resolve. So community involvement here will be required.

@k0nserv
Copy link
Author

k0nserv commented Feb 7, 2017

Thanks for the response @bradrydzewski the DRONE_EVENT is indeed pull_request, but the ref is a branch ref not a merge request one. Could you point me in the direction of the Gitlab implementation and I'll have a look at that?

@bradrydzewski
Copy link

@k0nserv it looks like this might be the specific section in the code:
https://github.com/drone/drone/blob/master/remote/gitlab/gitlab.go#L506:L510

I recommend we check with @Bugagazavr who authored this code. The initial GitLab integration was created before merge refs existed. It is possible that, at the time of writing this code, merge refs were not available for intra-repository merge requests. Or maybe there is an edge case we don't know about.

Either way hopefully @Bugagazavr can shed some light :)

@kzaitsev
Copy link

kzaitsev commented Feb 7, 2017

Hello,

@k0nserv which version of gitlab you use?
@bradrydzewski i try to research this question, so maybe we can use merge-request for obj.SourceProjectId == obj.TargetProjectId condition, I do not remember why I did that, maybe at that time gitlab doesn't create merge-request ref when obj.SourceProjectId == obj.TargetProjectId

@k0nserv
Copy link
Author

k0nserv commented Feb 7, 2017

Hey @Bugagazavr we are on ee 8.11 currently

@k0nserv
Copy link
Author

k0nserv commented Feb 7, 2017

@connorshea do you have any idea why this condition might have been put in place?

@connorshea
Copy link

@k0nserv nope, but I've asked the CI team about it.

kzaitsev added a commit to kzaitsev/drone that referenced this issue Feb 8, 2017
@bradrydzewski
Copy link

just merged the update from @Bugagazavr , thanks!!

@k0nserv
Copy link
Author

k0nserv commented Feb 8, 2017

Brilliant thanks @Bugagazavr

@VojtechVitek
Copy link

@bradrydzewski pinging back about this.

	if build.Event == core.EventPullRequest {
		env["DRONE_PULL_REQUEST"] = re.FindString(build.Ref)
	}

Why is the event: pull_request strictly required?

For most of my Drone projects, I've disabled "Pull Requests" and I only spin up Drone pipelines against the "push" events.

If there is a Pull Request associated to a branch, I don't see why we wouldn't set up the DRONE_PULL_REQUEST env var, since it's available in the webhook payloads from Github.

Is there a specific reason why not to set this env var, even if possible? Thoughts?

@bradrydzewski
Copy link

a push hook will never contain the pull request number in the ref. A push ref contains the branch, and follows the pattern refs/heads/{branch}. For this reason, a pull request number can only exist for pull request events, which are triggered by pull request hooks coming from the source control management system (e.g. github).

@VojtechVitek
Copy link

you're right, thanks for the explanation

I'll have to stick to event: pull_request then

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

No branches or pull requests

5 participants