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

Remove branch with escaped characters from pipeline.name #181

Merged
merged 2 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.datadog.jenkins.plugins.datadog.traces;

import org.apache.http.client.utils.URLEncodedUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -26,18 +31,28 @@ public class JobNameWrapper {
private final String traceJobName;
private final Map<String, String> configurations = new HashMap<>();

public JobNameWrapper(final String rawJobName, final String gitBranch) {
if(rawJobName == null) {
public JobNameWrapper(final String jobName, final String gitBranch) {
if(jobName == null) {
this.traceJobName = null;
return;
}

final String rawJobName = jobName.trim();
String jobNameNoBranch = rawJobName;
// First, the git branch is removed from the raw jobName
final String jobNameNoBranch;
if(gitBranch != null && !gitBranch.isEmpty()) {
jobNameNoBranch = rawJobName.trim().replace("/" + gitBranch, "");
} else {
jobNameNoBranch = rawJobName;
// First, we try to remove the non-encoded git branch.
jobNameNoBranch = rawJobName.replace("/" + gitBranch, "");

try {
// If the job name contains the git branch, that can have encoded characters.
// e.g. jobname: pipeline/feature%2F/one --> it corresponds with the real git branch feature/one
drodriguezhdez marked this conversation as resolved.
Show resolved Hide resolved
if(jobNameNoBranch.equals(rawJobName)) {
jobNameNoBranch = rawJobName.replace("/" + URLEncoder.encode(gitBranch, "UTF-8"), "");
}
} catch (UnsupportedEncodingException e){
jobNameNoBranch = rawJobName;
}
}

// Once the branch has been removed, we try to extract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static Collection<Object[]> data() {
{"jobName/master", "master", "jobName", EMPTY_MAP},
{"jobName/another", "master", "jobName/another", EMPTY_MAP},
{"jobName/another/branch", "another/branch", "jobName", EMPTY_MAP},
{"jobName/another%2Fbranch", "another/branch", "jobName", EMPTY_MAP},
{"jobName/KEY1=VALUE1,KEY2=VALUE2", "master", "jobName", sampleMap},
{"jobName/KEY1=VALUE1,KEY2=VALUE2/master", "master", "jobName", sampleMap},
{"jobName/KEY1=VALUE1,KEY2=VALUE2/another-branch", "master", "jobName", sampleMap}
Expand Down