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

JENKINS-48737 Adding lightweight checkouts for Bitbucket Pull Requests #117

Closed
wants to merge 2 commits into from

Conversation

benjaminfuchs
Copy link

Proposing a fix for https://issues.jenkins-ci.org/browse/JENKINS-48737

Tested on our Bitbucket -> Jenkins setup.

@tarioch
Copy link
Contributor

tarioch commented Mar 24, 2018

#114 seems to make more sense than this fix as it fixes the source issue

@benjaminfuchs
Copy link
Author

benjaminfuchs commented Mar 25, 2018

@tarioch I totally agree, but this fix will just affect the light weight checkout. So compared to #114 it just fixes this problem and doesn't change the environment variable CHANGE_BRANCH. But if #114 get merged this is not needed anymore.

@BrunoLavitForgerock
Copy link

BrunoLavitForgerock commented Apr 4, 2018

FYI, we have applied #117 and #111 on the master branch and the lightweight checkout is now working for our postcommit and PR pipeline jobs.
We are using Bitbucket datacenter 5.8.0 and Jenkins 2.46.3.

String ref;
if(file.getRef().matches("PR-\\d+")) { // because JENKINS-48737
String prId = file.getRef().replace("PR-", "");
ref = "refs/pull-requests/" + prId + "/from";
Copy link
Member

Choose a reason for hiding this comment

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

Could respect the pull request merge strategy set on the build configuration

Copy link

Choose a reason for hiding this comment

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

agree with @Casz, could we use pull-requests/id/merge as ref for merge strategy?

Copy link
Member

Choose a reason for hiding this comment

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

indeed, which I was hinting at 👍

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I will pick up your comments. Hopefully I have time this week. 👍

@benjaminfuchs benjaminfuchs force-pushed the master branch 7 times, most recently from 26b9f6a to 3bac95e Compare April 8, 2018 22:29
@honnel
Copy link

honnel commented Apr 24, 2018

We are affected by this issue, too. Any plans for next release?

@benjaminfuchs benjaminfuchs force-pushed the master branch 6 times, most recently from fcb4ed0 to 1e7dbf8 Compare June 9, 2018 14:05
@benjaminfuchs
Copy link
Author

@Casz @fengxx Finally I had time reworking this. I changed it, so the checkout strategy is taken into account.

@benjaminfuchs
Copy link
Author

So I tested it in our environment. It is working but this print is now wrong "Obtained Jenkinsfile from XXXX" since it should mention, that it used the merge ref.
Also if you choose "Merging the pull request with the current target branch revision" it is still using heavy weight checkout and doesn't even try leight weight version. Seems there have to be more changes to support both checkout strategies.

@jetersen
Copy link
Member

@benjaminfuchs see #114

@benjaminfuchs
Copy link
Author

benjaminfuchs commented Jun 11, 2018

@Casz If 114 also fixes this issue and gets merged I'm also fine with that. Is this done with the next release?
In the meantime this fix would be good as a workaround before 114 is merged and if it's merged it won't get triggers anymore.
As 114 is not solving the problem with the "merge" checkout strategy I would continue working on this in this PR?

@bjornmagnusson
Copy link

What is missing for getting this merged? This is blocker for us.

@amuniz
Copy link
Member

amuniz commented Jul 12, 2018

@tarioch said this issue is also fixed by #114, did someone check that?

@tarioch
Copy link
Contributor

tarioch commented Jul 13, 2018

As far as I can tell #114 fixes this issue for the "current pull request revision" strategy but it doesn't fix it for the "merge" strategy. Correct me please @benjaminfuchs

@benjaminfuchs
Copy link
Author

benjaminfuchs commented Jul 15, 2018

@tarioch Yes #114 will fix the "current pull request revision" but not the "merge" strategy. I think the "merge" strategy is the most common use-case, so I guess this pull request should be merged independent from #114.
We are using this in our productive environment and would be happy if this could be merged. Tests are implemented. If there is a reason for not merging let me know @Casz @fengxx

@tarioch
Copy link
Contributor

tarioch commented Jul 16, 2018

@benjaminfuchs I think after #114 is merged the first part

if (ref.matches("PR-\\d+")) { // because JENKINS-48737

would need to be changed because I think that would then be the branch name of the PR.

@bjornmagnusson
Copy link

Any updates?

@jetersen
Copy link
Member

I'll try and look into this and #114

@benjaminfuchs
Copy link
Author

@Casz Did you plan to finish this? After #114 is merged I would try to have a look in this one this weekend. Just want sync to avoid double work.

@jetersen
Copy link
Member

jetersen commented Oct 7, 2018

@benjaminfuchs please look into it 👍

@bjornmagnusson
Copy link

any ETA on this?

@jetersen
Copy link
Member

@benjaminfuchs I checked and PR still does full checkouts.

This needs to adapt:

PullRequestSCMHead pr = (PullRequestSCMHead) head;
if (!(pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) && pr.getRepository() != null) {
return new BitbucketSCMFileSystem(apiClient, pr.getOriginName(), rev);
}
return null; // TODO support merge revisions somehow

@jetersen
Copy link
Member

jetersen commented Oct 27, 2018

Actually most all of the logic can be moved to that spot.

@jetersen
Copy link
Member

jetersen commented Oct 27, 2018

@Override
public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @CheckForNull SCMRevision rev)
        throws IOException, InterruptedException {
    BitbucketSCMSource src = (BitbucketSCMSource) source;
    
    String credentialsId = src.getCredentialsId();
    String owner = src.getRepoOwner();
    String repository = src.getRepository();
    String serverUrl = src.getServerUrl();
    StandardUsernamePasswordCredentials credentials;
    credentials = lookupScanCredentials(src.getOwner(), credentialsId);
    
    BitbucketApi apiClient = BitbucketApiFactory.newInstance(serverUrl, credentials, owner, repository);
    String ref;
    if (head instanceof BranchSCMHead) {
        ref = head.getName();
    } else if (head instanceof PullRequestSCMHead) {
        PullRequestSCMHead pr = (PullRequestSCMHead) head;
        String checkoutStrat = "from";
        if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) {
            checkoutStrat = "merge";
        }
        ref = "refs/pull-requests/" + pr.getId() + "/" + checkoutStrat;
    } else if (head instanceof BitbucketTagSCMHead) {
         ref = "tags/" + head.getName();
    } else {
        return null;
    }

    return new BitbucketSCMFileSystem(apiClient, ref, rev);
}

works out nicely 😅

@benjaminfuchs
Copy link
Author

@Casz We are currently using my patched plugin and it works fine. Your change also looks reasonable. How should we continue?

@jetersen
Copy link
Member

jetersen commented Oct 27, 2018

sadly does not work for cloud

java.io.FileNotFoundException: URL: https://api.bitbucket.org/2.0/repositories/caszum/testrepo/src/a84e84a71c6cc81c23bdda3fc216867a7e6fe031%2B4a3423ac3e042cfeb3b1a09e57280e7d8385ac86/Jenkinsfile
	at com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient.getRequestAsInputStream(BitbucketCloudApiClient.java:745)
	at com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketCloudApiClient.getFileContent(BitbucketCloudApiClient.java:927)
	at com.cloudbees.jenkins.plugins.bitbucket.filesystem.BitbucketSCMFile.content(BitbucketSCMFile.java:109)
	at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)
	at org.jenkinsci.plugins.workflow.multibranch.SCMBinder.create(SCMBinder.java:104)
	at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:303)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:421)

@benjaminfuchs
Copy link
Author

benjaminfuchs commented Oct 27, 2018

@Casz I guess my changes only work if you set "Discover pull requests from origin" to Strategy "The current pull requets revision". EDIT: Its not a guess, just downloaded the latest jenkins build result here and can confirm. Only works with that setting. Why, currently dont know.

@jetersen
Copy link
Member

I guess we have to check api client if server or cloud for now... until we can find a solution for cloud too

@jetersen
Copy link
Member

jetersen commented Oct 27, 2018

with the changes from #114 this did not work at least the current state 😅

@jetersen
Copy link
Member

jetersen commented Oct 27, 2018

@benjaminfuchs if you could adapt the PR and the tests to:

if (head instanceof BranchSCMHead) {
    ref = head.getName();
} else if (head instanceof PullRequestSCMHead) {
    PullRequestSCMHead pr = (PullRequestSCMHead) head;
    if (apiClient instanceof BitbucketCloudApiClient) {
        if (!(pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) && pr.getRepository() != null) {
            return new BitbucketSCMFileSystem(apiClient, pr.getOriginName(), rev);
        }
        return null; // TODO support merge revisions somehow for cloud
    }
    String checkoutStrategy = "from";
    if (pr.getCheckoutStrategy() == ChangeRequestCheckoutStrategy.MERGE) {
        checkoutStrategy = "merge";
    }
    ref = "refs/pull-requests/" + pr.getId() + "/" + checkoutStrategy;
} else if (head instanceof BitbucketTagSCMHead) {
     ref = "tags/" + head.getName();
} else {
    return null;
}

I would appreciate it 😇🙌

@CheckForNull
private ChangeRequestCheckoutStrategy getCheckoutStrategy() throws IOException, InterruptedException {
if (this.isDirectory()) {
SCMRevision revision = fileSystem.getRevision();
Copy link
Contributor

Choose a reason for hiding this comment

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

Having a look at the constructors in line 76 and line 80 fileSystem could be null.
I would also mark this variable final and maybe @CheckForNull.

@bjornmagnusson
Copy link

Any ETA for this very important feature?

@jetersen
Copy link
Member

@bjornmagnusson if you feel it is important, feel free to rework the PR.
I'll leave this here.
https://snarky.ca/setting-expectations-for-open-source-participation/

@stale
Copy link

stale bot commented Apr 6, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 6, 2019
@jetersen
Copy link
Member

jetersen commented Apr 7, 2019

Superseded by #174

@jetersen jetersen closed this Apr 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants