-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathGitHubBranch.java
59 lines (49 loc) · 1.91 KB
/
GitHubBranch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.cloudbees.jenkins;
import hudson.plugins.git.BranchSpec;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Identifies a GitHub branch and hides the SCM branch from its clients.
*/
public class GitHubBranch {
private static final String GITHUB_BRANCH_PREFIX = "refs/heads";
private static final int GITHUB_BRANCH_PREFIX_LENGTH = 10;
private final BranchSpec branch;
private GitHubBranch(final BranchSpec branch) {
this.branch = branch;
}
public boolean matches(GitHubRepositoryName repository, String ref) {
// ref (github) -> refs/heads/master
// branch (git SCM) -> REPO/remote/branch
// matching the meaningful part of the github branch name with
// the configured SCM branch expression
if (ref != null && !ref.equals("")) {
String branchExpression = "*";
if (repository != null && repository.repositoryName != null) {
branchExpression = repository.repositoryName;
}
if (ref.startsWith(GITHUB_BRANCH_PREFIX)) {
branchExpression += ref.substring(GITHUB_BRANCH_PREFIX_LENGTH);
} else {
branchExpression += "/" + ref;
}
LOGGER.log(Level.FINE, "Does SCM branch " + branch.getName()
+ " match GitHub branch " + branchExpression + "?");
return branch.matches(branchExpression);
}
return false;
}
public static GitHubBranch create(BranchSpec branch) {
if (isValidGitHubBranch(branch)) {
return new GitHubBranch(branch);
} else {
return null;
}
}
// TODO: implement logic to validate git SCM branches.
private static boolean isValidGitHubBranch(BranchSpec branch) {
return true;
}
private static final Logger LOGGER = Logger.getLogger(GitHubBranch.class
.getName());
}