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

Handy Uri Template #80

Merged
merged 8 commits into from Jan 10, 2018
Merged

Handy Uri Template #80

merged 8 commits into from Jan 10, 2018

Conversation

jetersen
Copy link
Member

@jetersen jetersen commented Nov 9, 2017

Just initial work, to get some feedback

cc @stephenc

Copy link
Member

@stephenc stephenc left a comment

Choose a reason for hiding this comment

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

Let’s see what switch the rest looks like

pom.xml Outdated
@@ -120,6 +120,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.damnhandy</groupId>
<artifactId>handy-uri-templates</artifactId>
<version>2.1.6</version>
Copy link
Member

Choose a reason for hiding this comment

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

We’ll need to either pull this into a library plugin or use maskClasses to shield ourselves from class conflicts

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at the deps: https://github.com/damnhandy/Handy-URI-Templates/blob/master/pom.xml#L473-L533
They are all optional or test scoped except for joda-time.
If we were to put into a library plugin, this is the way to go right: https://github.com/jenkinsci/apache-httpcomponents-client-4-api-plugin with some POM magic?

Copy link
Contributor

Choose a reason for hiding this comment

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

@Casz yes, thats the way to go about library plugin. joda-time libraries are used at other places too. It might cause conflicts.

Copy link
Member Author

@jetersen jetersen Dec 13, 2017

Choose a reason for hiding this comment

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

@stephenc if possible, please advice if I should be doing something special against joda-time or any of the optional dependencies. https://github.com/casz/handy-uri-templates-2-api-plugin/blob/master/pom.xml My maven knowledge is limited

Copy link
Member

Choose a reason for hiding this comment

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

It they are optional, then they will not be transitive.

I think joda-time may need its own api plugin, in which case you then have to add a jenkins plugin dependency to your api plugin... fun times

Copy link
Member Author

Choose a reason for hiding this comment

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

@stephenc I give it a shot for the joda-time too :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Joda-time should be safe as well. However I will gladly wrap it in a plugin :)

  <dependencies>
    <dependency>
      <groupId>org.joda</groupId>
      <artifactId>joda-convert</artifactId>
      <version>1.2</version>
      <scope>compile</scope>
      <optional>true</optional><!-- mandatory in Scala -->
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

Copy link
Member Author

@jetersen jetersen Dec 20, 2017

Choose a reason for hiding this comment

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

Currently waiting for approval: https://issues.jenkins-ci.org/browse/HOSTING-476

@jetersen
Copy link
Member Author

I'll have the next bit up later today

String url = UriTemplate.fromTemplate(REPO_URL_TEMPLATE + "/hooks/{uuid}")
.set("owner", owner)
.set("repo", repositoryName)
.set("uuid", Util.rawEncode(hook.getUuid()))
Copy link
Member Author

Choose a reason for hiding this comment

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

Don't think i need encoding here as Handy Uri should handle it

Copy link
Member

Choose a reason for hiding this comment

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

Yep, remove the rawEncode as it’s {uuid}. You’d need to use raw encode if the template was {+uuid}

Map<String,Object> content = JsonParser.mapper.readValue(response, new TypeReference<Map<String,Object>>(){});
Map page = (Map) content.get("children");
List<Map> values = (List<Map>) page.get("values");
collectFileAndDirectories(parent, path, values, files);
while (!(boolean)page.get("isLastPage")){
response = getRequest(url + String.format("&start=%s&limit=%s", start, 500));
start += (int) content.get("size");
Copy link
Member Author

@jetersen jetersen Nov 16, 2017

Choose a reason for hiding this comment

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

We were never counting up the pages 😕
Here we are

while(!(boolean)content.get("isLastPage")){
start += (int) content.get("size");
response = getRequest(String.format(url+"&start=%s&limit=&s", start, 500));
content = collectLines(response, lines);
}

However not here

while (!(boolean)page.get("isLastPage")){
response = getRequest(String.format(url+"&start=%s&limit=%s", start, 500));
content = JsonParser.mapper.readValue(response, new TypeReference<Map<String,Object>>(){});
page = (Map) content.get("children");
}

Copy link
Member

Choose a reason for hiding this comment

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

Can you create a JIRA and assign yourself (or let me know the JIRA and i’ll Assign it to you)

Good catch

Copy link
Member Author

Choose a reason for hiding this comment

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

@Hellspam
Copy link

Can you maybe make a larger limit for the getBranches function while you are at it? Something like 100 maybe instead of the current (25 I think by default).

@jetersen
Copy link
Member Author

I'll try and wrap it in a Jenkins plugin and put it up Jenkins repo tonight, and start the process of having it hosted 👍

@jetersen
Copy link
Member Author

jetersen commented Jan 2, 2018

@stephenc rebased onto master and using snapshot while I wait for hosting 👍 Seems like we are getting rid of all the ugly string format and encoding. Could you please take a look, if I missed anything.

If you still think Yoda as a dependency to handy templates, needs to be wrapped. All the dependencies of Yodatime are optional or as test scope.

Copy link
Member

@stephenc stephenc left a comment

Choose a reason for hiding this comment

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

@rsandell can you provide cross-review

@stephenc stephenc requested a review from rsandell January 3, 2018 14:14
pom.xml Outdated
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>handy-uri-templates-2-api</artifactId>
<version>2.1.6-1.0-20180102.164938-1</version>
Copy link
Member

Choose a reason for hiding this comment

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

Merging blocked until this is released

Copy link
Member Author

Choose a reason for hiding this comment

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

it just got hosted, so I'll upload it tonight 👍

@@ -460,13 +505,17 @@ private BitbucketRepositoryHooks parsePaginatedRepositoryHooks(String response)
@Override
@CheckForNull
public BitbucketTeam getTeam() throws IOException, InterruptedException {
String url = UriTemplate.fromTemplate("{+base}{/owner}")
.set("base", V2_TEAMS_API_BASE_URL)
Copy link
Member Author

Choose a reason for hiding this comment

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

perhaps we should change this template, I don't like the set "base"
WDYT?

@@ -39,8 +43,32 @@ public void smokes() {

@Test
public void getRepositoryUrl() {

Copy link
Member Author

Choose a reason for hiding this comment

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

👻 whitespace

Copy link
Member Author

@jetersen jetersen left a comment

Choose a reason for hiding this comment

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

@rsandell @stephenc WDYT? :)
This is benefitting from handy uri template 🚀 :

String branchUrl;
String title;
if (head instanceof PullRequestSCMHead) {
PullRequestSCMHead pr = (PullRequestSCMHead) head;
branchUrl = repoOwner + "/" + repository + "/pull-requests/" + pr.getId();
title = getPullRequestTitleCache().get(pr.getId());
ContributorMetadataAction contributor = getPullRequestContributorCache().get(pr.getId());
if (contributor != null) {
result.add(contributor);
}
} else {
branchUrl = repoOwner + "/" + repository + "/branch/" + Util.rawEncode(head.getName());
title = null;
}
result.add(new BitbucketLink("icon-bitbucket-branch", getServerUrl() + "/" + branchUrl));
result.add(new ObjectMetadataAction(title, null, getServerUrl() + "/" + branchUrl));
} else {
String branchUrl;
String title;
if (head instanceof PullRequestSCMHead) {
PullRequestSCMHead pr = (PullRequestSCMHead) head;
branchUrl = "projects/" + repoOwner + "/repos/" + repository + "/pull-requests/" +pr.getId()+"/overview";
title = getPullRequestTitleCache().get(pr.getId());
ContributorMetadataAction contributor = getPullRequestContributorCache().get(pr.getId());
if (contributor != null) {
result.add(contributor);
}
} else {
branchUrl = "projects/" + repoOwner + "/repos/" + repository + "/compare/commits"
+ "?sourceBranch=" + URLEncoder.encode(Constants.R_HEADS + head.getName(), "UTF-8");

@@ -726,13 +827,24 @@ public InputStream getFileContent(BitbucketSCMFile file) throws IOException, Int
String path = encodePath(file.getPath());
String ref = URLEncoder.encode(file.getRef(), "UTF-8");
Copy link
Member Author

Choose a reason for hiding this comment

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

This is no longer needed?

@@ -726,13 +827,24 @@ public InputStream getFileContent(BitbucketSCMFile file) throws IOException, Int
String path = encodePath(file.getPath());
Copy link
Member Author

Choose a reason for hiding this comment

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

This is no longer needed?

@@ -689,15 +779,26 @@ private String deleteRequest(String path) throws IOException {
String path = encodePath(directory.getPath());
Copy link
Member Author

Choose a reason for hiding this comment

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

This is no longer needed?

@@ -689,15 +779,26 @@ private String deleteRequest(String path) throws IOException {
String path = encodePath(directory.getPath());
String ref = URLEncoder.encode(directory.getRef(), "UTF-8");
Copy link
Member Author

Choose a reason for hiding this comment

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

This is no longer needed?

@jetersen
Copy link
Member Author

jetersen commented Jan 5, 2018

Tested with both bitbucket cloud and bitbucket server that URL's and scans still work 👍 🚀

image
image
https://bitbucket.org/caszum/testrepo/branch/bugfix%2Fmy%2Fsuper%2Fawesome%2Fbugfix

@stephenc
Copy link
Member

stephenc commented Jan 5, 2018

Can we get uri templates on

String branchUrl;
String title;
if (head instanceof PullRequestSCMHead) {
PullRequestSCMHead pr = (PullRequestSCMHead) head;
branchUrl = repoOwner + "/" + repository + "/pull-requests/" + pr.getId();
title = getPullRequestTitleCache().get(pr.getId());
ContributorMetadataAction contributor = getPullRequestContributorCache().get(pr.getId());
if (contributor != null) {
result.add(contributor);
}
} else {
branchUrl = repoOwner + "/" + repository + "/branch/" + Util.rawEncode(head.getName());
title = null;
}
result.add(new BitbucketLink("icon-bitbucket-branch", getServerUrl() + "/" + branchUrl));
result.add(new ObjectMetadataAction(title, null, getServerUrl() + "/" + branchUrl));
} else {
String branchUrl;
String title;
if (head instanceof PullRequestSCMHead) {
PullRequestSCMHead pr = (PullRequestSCMHead) head;
branchUrl = "projects/" + repoOwner + "/repos/" + repository + "/pull-requests/" +pr.getId()+"/overview";
title = getPullRequestTitleCache().get(pr.getId());
ContributorMetadataAction contributor = getPullRequestContributorCache().get(pr.getId());
if (contributor != null) {
result.add(contributor);
}
} else {
branchUrl = "projects/" + repoOwner + "/repos/" + repository + "/compare/commits"
+ "?sourceBranch=" + URLEncoder.encode(Constants.R_HEADS + head.getName(), "UTF-8");
too

@jetersen
Copy link
Member Author

jetersen commented Jan 5, 2018

@stephenc slightly improved version 803dbe4

Copy link
Member Author

@jetersen jetersen left a comment

Choose a reason for hiding this comment

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

I think this looks much cleaner

}
if (head instanceof PullRequestSCMHead) {
Copy link
Member Author

Choose a reason for hiding this comment

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

The if-else above is now only for URL template and this is now deduped.

}
String url = template.expand();
result.add(new BitbucketLink("icon-bitbucket-branch", url));
result.add(new ObjectMetadataAction(title, null, url));
Copy link
Member Author

Choose a reason for hiding this comment

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

More deduping

template.set("owner", repoOwner).set("repo", repository);
String url = template.expand();
result.add(new BitbucketLink("icon-bitbucket-repo", url));
result.add(new ObjectMetadataAction(r.getRepositoryName(), null, url));
Copy link
Member Author

@jetersen jetersen Jan 5, 2018

Choose a reason for hiding this comment

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

Some more needed deduping

@vivek
Copy link
Contributor

vivek commented Jan 9, 2018

Looks like its ready to be merged. @stephenc, looks good to you?

@stephenc
Copy link
Member

Let’s go

@stephenc stephenc merged commit fc07854 into jenkinsci:master Jan 10, 2018
@jetersen jetersen deleted the handyuri branch January 10, 2018 06:07
fengxx pushed a commit to fengxx/bitbucket-branch-source-plugin that referenced this pull request Mar 13, 2018
[FIXED JENKINS-41883] Append global log on open
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.

None yet

4 participants