Skip to content

Commit

Permalink
cloud config propagation url and tests adjusted
Browse files Browse the repository at this point in the history
  • Loading branch information
julioc-p committed Sep 12, 2024
1 parent 4be57f7 commit dfbb5bc
Show file tree
Hide file tree
Showing 11 changed files with 691 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import static java.util.Objects.nonNull;

public class BitBucketPPRPullRequestAction extends InvisibleAction implements BitBucketPPRAction {
private static final Logger logger = Logger.getLogger(BitBucketPPRPullRequestAction.class.getName());
private static final Logger logger =
Logger.getLogger(BitBucketPPRPullRequestAction.class.getName());
public static final String APPROVE = "/approve";
public static final String DECLINE = "/decline";
public static final String STATUSES = "/statuses";
Expand All @@ -61,8 +62,9 @@ public BitBucketPPRPullRequestAction(@Nonnull BitBucketPPRPayload payload) {

Map<String, String> workspaceRepo;
try {
workspaceRepo = BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl(
payload.getRepository().getLinks().getHtml().getHref());
workspaceRepo =
BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl(
payload.getRepository().getLinks().getHtml().getHref());
} catch (BitBucketPPRRepositoryNotParsedException e) {
throw new RuntimeException(e);
}
Expand All @@ -84,21 +86,28 @@ public String getTargetBranch() {
public String getLatestCommitFromRef() {
return payload.getPullRequest().getSource().getCommit().getHash();
}

@Override
public String getLatestCommitToRef() {
return payload.getPullRequest().getDestination().getCommit().getHash();
}

@Override
public String getPullRequestApiUrl() {
return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug,
PULL_REQUEST_API, pullRequestId);
return String.join(

Check warning on line 97 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 97 is not covered by tests
"/",
BITBUCKET_API_BASE_URL,
BITBUCKET_REPOSITORIES,
workspace,
repoSlug,
PULL_REQUEST_API,
pullRequestId);
}

@Override
public String getPullRequestUrl() {
return String.join("/", BITBUCKET_HTTP_BASE_URL, workspace, repoSlug, PULL_REQUEST_HTTP,
pullRequestId);
return String.join(
"/", BITBUCKET_HTTP_BASE_URL, workspace, repoSlug, PULL_REQUEST_HTTP, pullRequestId);
}

@Override
Expand Down Expand Up @@ -151,7 +160,8 @@ public String getPullRequestId() {

@Override
public String getComment() {
if (payload.getComment() == null || payload.getComment().getContent() == null
if (payload.getComment() == null
|| payload.getComment().getContent() == null
|| payload.getComment().getContent().getRaw() == null) {
return "";
}
Expand All @@ -170,20 +180,41 @@ public String getLinkSelf() {

@Override
public String getLinkApprove() {
return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug,
PULL_REQUEST_API, pullRequestId) + APPROVE;
return String.join(
"/",
BITBUCKET_API_BASE_URL,
BITBUCKET_REPOSITORIES,
workspace,
repoSlug,
PULL_REQUEST_API,
pullRequestId)
+ APPROVE;
}

@Override
public String getLinkDecline() {
return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug,
PULL_REQUEST_API, pullRequestId) + DECLINE;
return String.join(
"/",
BITBUCKET_API_BASE_URL,
BITBUCKET_REPOSITORIES,
workspace,
repoSlug,
PULL_REQUEST_API,
pullRequestId)
+ DECLINE;
}

@Override
public String getLinkStatuses() {
return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug,
PULL_REQUEST_API, pullRequestId) + STATUSES;
return String.join(
"/",
BITBUCKET_API_BASE_URL,
BITBUCKET_REPOSITORIES,
workspace,
repoSlug,
PULL_REQUEST_API,
pullRequestId)
+ STATUSES;
}

@Override
Expand All @@ -193,8 +224,10 @@ public String getLatestCommit() {

@Override
public String getCommitLink() {
return String.join("/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug,
COMMIT) + '/' + this.getLatestCommit();
return String.join(
"/", BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, COMMIT)
+ '/'
+ this.getLatestCommit();

Check warning on line 230 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 163-230 are not covered by tests
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.jenkins.plugins.bitbucketpushandpullrequest.model.cloud.BitBucketPPRChange;
import java.util.stream.Collectors;


public class BitBucketPPRRepositoryAction extends InvisibleAction implements BitBucketPPRAction {
private static final Logger logger = Logger.getLogger(BitBucketPPRAction.class.getName());
public static final String COMMIT = "commit";
Expand Down Expand Up @@ -66,16 +65,17 @@ public BitBucketPPRRepositoryAction(@Nonnull BitBucketPPRPayload payload) {

Map<String, String> workspaceRepo;
try {
workspaceRepo = BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl(
payload.getRepository().getLinks().getHtml().getHref());
workspaceRepo =
BitBucketPPRUtils.extractRepositoryNameFromHTTPSUrl(
payload.getRepository().getLinks().getHtml().getHref());
} catch (BitBucketPPRRepositoryNotParsedException e) {
throw new RuntimeException(e);
}
this.repoSlug = workspaceRepo.get(BitBucketPPRUtils.BB_REPOSITORY);
this.workspace = workspaceRepo.get(BitBucketPPRUtils.BB_WORKSPACE);

logger.log(Level.INFO,
() -> "Received commit hook notification for branch: " + this.targetBranchName);
logger.log(
Level.INFO, () -> "Received commit hook notification for branch: " + this.targetBranchName);
logger.log(Level.INFO, () -> "Received commit hook type: " + this.type);
}

Expand Down Expand Up @@ -128,7 +128,8 @@ public String getRepositoryId() {

@Override
public String getLatestCommit() {
// According to constructor `targetBranchName`, `type` and `repositoryUuid` will be set to first non-null change
// According to constructor `targetBranchName`, `type` and `repositoryUuid` will be set to first
// non-null change
// So lets hope it is not very destructive move to set latestCommit from first change.
for (BitBucketPPRChange change : payload.getPush().getChanges()) {
if (change.getNewChange() != null) {
Expand All @@ -141,9 +142,18 @@ public String getLatestCommit() {

@Override
public List<String> getCommitLinks() {
return payload.getPush().getChanges().stream().map(c -> String.join("/",
BITBUCKET_API_BASE_URL, BITBUCKET_REPOSITORIES, workspace, repoSlug, COMMIT,
c.getNewChange().getTarget().getHash())).collect(Collectors.toList());
return payload.getPush().getChanges().stream()
.map(
c ->
String.join(
"/",
BITBUCKET_API_BASE_URL,
BITBUCKET_REPOSITORIES,
workspace,
repoSlug,
COMMIT,
c.getNewChange().getTarget().getHash()))
.collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public boolean getUseJobNameAsBuildKey() {
@DataBoundSetter
public void setUseJobNameAsBuildKey(@CheckForNull boolean useJobNameAsBuildKey) {
this.useJobNameAsBuildKey = useJobNameAsBuildKey;
save();
}

@DataBoundSetter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2021, CloudBees, Inc.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Expand All @@ -20,6 +20,7 @@
******************************************************************************/
package io.jenkins.plugins.bitbucketpushandpullrequest.observer;

import java.net.MalformedURLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -34,7 +35,6 @@
import io.jenkins.plugins.bitbucketpushandpullrequest.event.BitBucketPPREventContext;
import io.jenkins.plugins.bitbucketpushandpullrequest.event.BitBucketPPREventType;


public abstract class BitBucketPPRHandlerTemplate {
static final Logger logger = Logger.getLogger(BitBucketPPRHandlerTemplate.class.getName());

Expand All @@ -61,13 +61,13 @@ public void run(BitBucketPPREventType eventType) throws Exception {
}

// @todo: do we need it also for pushs?
public void setApprovedOrDeclined() {
public void setApprovedOrDeclined() throws MalformedURLException {
return;
}

public abstract void setBuildStatusOnFinished();
public abstract void setBuildStatusOnFinished() throws MalformedURLException;

public abstract void setBuildStatusInProgress();
public abstract void setBuildStatusInProgress() throws MalformedURLException;

protected BitBucketPPRPluginConfig getGlobalConfig() {
return BitBucketPPRPluginConfig.getInstance();
Expand Down Expand Up @@ -96,8 +96,8 @@ protected void callClient(@Nonnull String url, @Nonnull Map<String, String> payl
}
}

protected void callClient(@Nonnull Verb verb, @Nonnull String url,
@Nonnull Map<String, String> payload) {
protected void callClient(
@Nonnull Verb verb, @Nonnull String url, @Nonnull Map<String, String> payload) {
ObjectMapper objectMapper = new ObjectMapper();

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2021, CloudBees, Inc.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Expand All @@ -20,6 +20,7 @@
******************************************************************************/
package io.jenkins.plugins.bitbucketpushandpullrequest.observer;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void getNotification(BitBucketPPREvent event) {
}

@Override
public void setApprovedOrDeclined() {
public void setApprovedOrDeclined() throws MalformedURLException {
if (!(context.getFilter().shouldSendApprove() || context.getFilter().shouldSendDecline())) {
return;
}
Expand Down Expand Up @@ -75,7 +76,7 @@ public void setApprovedOrDeclined() {
}

@Override
public void setBuildStatusOnFinished() {
public void setBuildStatusOnFinished() throws MalformedURLException {
BitBucketPPRAction bitbucketAction = context.getAction();
String url = bitbucketAction.getCommitLink() + "/statuses/build";
Result result = context.getRun().getResult();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*******************************************************************************
* The MIT License
*
*
* Copyright (C) 2021, CloudBees, Inc.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Expand All @@ -20,6 +20,7 @@
******************************************************************************/
package io.jenkins.plugins.bitbucketpushandpullrequest.observer;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
Expand Down Expand Up @@ -61,18 +62,18 @@ public void setApprovedOrDeclined() {
if (context.getFilter().shouldSendApprove()) {
url = bitbucketAction.getLinkApprove();
Verb verb = Verb.POST;

if (result == Result.FAILURE) {
verb = Verb.DELETE;
}

callClient(verb, url, map);
}

if (result == Result.FAILURE && context.getFilter().shouldSendDecline()) {
url = bitbucketAction.getLinkDecline();
callClient(Verb.POST, url, map);
}
}
}

@Override
Expand Down
Loading

0 comments on commit dfbb5bc

Please sign in to comment.