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

2222: Remove JbsVault #1628

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bots/notify/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module {
test {
requires 'org.junit.jupiter.api'
requires 'org.openjdk.skara.test'
requires 'org.openjdk.skara.proxy'
opens 'org.openjdk.skara.bots.notify' to 'org.junit.platform.commons'
opens 'org.openjdk.skara.bots.notify.mailinglist' to 'org.junit.platform.commons'
opens 'org.openjdk.skara.bots.notify.json' to 'org.junit.platform.commons'
Expand Down Expand Up @@ -55,4 +56,5 @@ dependencies {
implementation project(':bots:common')

testImplementation project(':test')
testImplementation project(':proxy')
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -38,7 +38,6 @@ class IssueNotifierBuilder {
private boolean setFixVersion = false;
private LinkedHashMap<Pattern, String> fixVersions = null;
private LinkedHashMap<Pattern, List<Pattern>> altFixVersions = null;
private JbsVault vault = null;
private boolean prOnly = true;
private boolean repoOnly = false;
private String buildName = null;
Expand Down Expand Up @@ -93,11 +92,6 @@ public IssueNotifierBuilder altFixVersions(LinkedHashMap<Pattern, List<Pattern>>
return this;
}

public IssueNotifierBuilder vault(JbsVault vault) {
this.vault = vault;
return this;
}

public IssueNotifierBuilder prOnly(boolean prOnly) {
this.prOnly = prOnly;
return this;
Expand Down Expand Up @@ -167,7 +161,7 @@ public boolean resolve() {
}

IssueNotifier build() {
var jbsBackport = new JbsBackport(issueProject.issueTracker().uri(), vault);
var jbsBackport = new JbsBackport(issueProject.issueTracker());
return new IssueNotifier(issueProject, reviewLink, reviewIcon, commitLink, commitIcon,
setFixVersion, fixVersions, altFixVersions, jbsBackport, prOnly,
repoOnly, buildName, censusRepository, censusRef, namespace, useHeadVersion, originalRepository,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -85,19 +85,6 @@ public Notifier create(BotConfiguration botConfiguration, JSONObject notifierCon
builder.buildName(notifierConfiguration.get("buildname").asString());
}

if (notifierConfiguration.contains("vault")) {
var vaultConfiguration = notifierConfiguration.get("vault").asObject();
var credential = new Credential(vaultConfiguration.get("username").asString(), vaultConfiguration.get("password").asString());

if (credential.username().startsWith("https://")) {
var vaultUrl = URIBuilder.base(credential.username()).build();
var jbsVault = new JbsVault(vaultUrl, credential.password(), issueProject.issueTracker().uri());
builder.vault(jbsVault);
} else {
throw new RuntimeException("basic authentication not implemented yet");
}
}

if (notifierConfiguration.contains("pronly")) {
builder.prOnly(notifierConfiguration.get("pronly").asBoolean());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,69 +30,41 @@
import java.util.*;
import java.util.stream.Collectors;

import static org.openjdk.skara.issuetracker.jira.JiraProject.RESOLVED_IN_BUILD;

public class JbsBackport {
private final RestRequest backportRequest;
private final IssueTracker.CustomEndpoint backportEndpoint;

private static URI backportRequest(URI uri) {
return URIBuilder.base(uri)
.setPath("/rest/jbs/1.0/backport/")
.build();
JbsBackport(IssueTracker tracker) {
this.backportEndpoint = tracker.lookupCustomEndpoint("/rest/jbs/1.0/backport/").orElseThrow(() ->
new IllegalArgumentException("Issue tracker does not support backport endpoint")
);
}

JbsBackport(URI uri, JbsVault vault) {
if (vault != null) {
backportRequest = new RestRequest(backportRequest(uri), vault.authId(), (r) -> Arrays.asList("Cookie", vault.getCookie()));
} else {
backportRequest = null;
}
}
IssueTrackerIssue createBackport(IssueTrackerIssue primary, String fixVersion, String assignee, String defaultSecurity) {
var body = JSON.object()
.put("parentIssueKey", primary.id())
.put("fixVersion", fixVersion);

private IssueTrackerIssue createBackportIssue(IssueTrackerIssue primary, String fixVersion, String defaultSecurity) {
var finalProperties = new HashMap<>(primary.properties());
finalProperties.put("issuetype", JSON.of("Backport"));
finalProperties.put("fixVersion", JSON.of(fixVersion));
if (!primary.properties().containsKey("security") && defaultSecurity != null) {
finalProperties.put("security", JSON.of(defaultSecurity));
}
finalProperties.remove(RESOLVED_IN_BUILD);

var backport = primary.project().createIssue(primary.title(), primary.body().lines().collect(Collectors.toList()), finalProperties);

var backportLink = Link.create(backport, "backported by").build();
primary.addLink(backportLink);
return backport;
}

public IssueTrackerIssue createBackport(IssueTrackerIssue primary, String fixVersion, String assignee, String defaultSecurity) {
if (backportRequest == null) {
if (primary.project().webUrl().toString().contains("openjdk.org")) {
throw new RuntimeException("Backports on JBS require vault authentication");
} else {
return createBackportIssue(primary, fixVersion, defaultSecurity);
}
}

var request = backportRequest.post()
.body("parentIssueKey", primary.id())
.body("fixVersion", fixVersion);
if (assignee != null) {
request.body("assignee", assignee);
body = body.put("assignee", assignee);
}

if (primary.properties().containsKey("security")) {
request.body("level", primary.properties().get("security").asString());
body = body.put("level", primary.properties().get("security").asString());
} else if (defaultSecurity != null) {
request.body("level", defaultSecurity);
body = body.put("level", defaultSecurity);
}
var response = request.execute();

var response = backportEndpoint.post()
.body(body)
.execute();
var issue = primary.project().issue(response.get("key").asString()).orElseThrow();

// The backport should not have any labels set - if it does, clear them
var labels = issue.labelNames();
if (!labels.isEmpty()) {
issue.setLabels(List.of());
}

return issue;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.notify.issue;

import org.openjdk.skara.host.Credential;
import org.openjdk.skara.issuetracker.IssueTracker;
import org.openjdk.skara.issuetracker.IssueTrackerFactory;
import org.openjdk.skara.json.JSON;
import org.openjdk.skara.json.JSONObject;
import org.openjdk.skara.json.JSONValue;
import org.openjdk.skara.network.URIBuilder;
import org.openjdk.skara.proxy.HttpProxy;
import org.openjdk.skara.test.TestProperties;
import org.openjdk.skara.test.EnabledIfTestProperties;

import java.util.*;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;


class JbsBackportIntegrationTests {
private static TestProperties props;
private static IssueTracker tracker;

@BeforeAll
static void beforeAll() {
props = TestProperties.load();
if (props.contains("jira.uri", "jira.pat")) {
var factory = IssueTrackerFactory.getIssueTrackerFactories().stream().filter(f -> f.name().equals("jira")).findFirst();
if (factory.isEmpty()) {
throw new IllegalStateException("'jira.uri' and 'jira.pat' has been configured but could not find IssueTrackerFactory for 'jira'");
}
HttpProxy.setup();
var uri = URIBuilder.base(props.get("jira.uri")).build();
var credential = new Credential("", "Bearer " + props.get("jira.pat"));
tracker = factory.get().create(uri, credential, new JSONObject());
}
}

@Test
@EnabledIfTestProperties({"jira.uri", "jira.pat"})
void testBackportCreation() {
var project = tracker.project("SKARA");
var issue = project.createIssue("Issue to backport", List.of("This is just a test issue for testing backport"), new HashMap<String, JSONValue>());

var jbsBackport = new JbsBackport(tracker);
var backport = jbsBackport.createBackport(issue, "1.0", "duke", null);
assertNotEquals(issue.id(), backport.id());
var backportOfLink = backport.links().stream().filter(l -> l.relationship().equals(Optional.of("backport of"))).findFirst();
assertTrue(backportOfLink.isPresent());
assertTrue(backportOfLink.get().issue().isPresent());
assertEquals(issue.id(), backportOfLink.get().issue().get().id());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,14 +23,47 @@
package org.openjdk.skara.issuetracker;

import java.time.Duration;
import java.net.URI;
import java.util.Optional;

import org.openjdk.skara.host.*;
import org.openjdk.skara.json.JSONObject;
import org.openjdk.skara.network.RestRequest;
import org.openjdk.skara.json.*;

import java.net.URI;

public interface IssueTracker extends Host {
IssueProject project(String name);
public interface CustomEndpointRequest {
CustomEndpointRequest body(JSONValue json);
CustomEndpointRequest header(String value, String name);
CustomEndpointRequest onError(RestRequest.ErrorTransform transform);

JSONValue execute();
}

public interface CustomEndpoint {
default CustomEndpointRequest post() {
throw new UnsupportedOperationException("HTTP method POST is not supported");
}

default CustomEndpointRequest get() {
throw new UnsupportedOperationException("HTTP method GET is not supported");
}

default CustomEndpointRequest put() {
throw new UnsupportedOperationException("HTTP method PUT is not supported");
}

default CustomEndpointRequest patch() {
throw new UnsupportedOperationException("HTTP method PATCH is not supported");
}

default CustomEndpointRequest delete() {
throw new UnsupportedOperationException("HTTP method DELETE is not supported");
}
}

IssueProject project(String name);
Optional<CustomEndpoint> lookupCustomEndpoint(String path);
URI uri();

static IssueTracker from(String name, URI uri, Credential credential, JSONObject configuration) {
Expand Down
Loading