use of org.eclipse.jgit.transport.RemoteConfig in project gitea-plugin by jenkinsci.
the class GiteaPushSCMEvent method isMatch.
/**
* {@inheritDoc}
*/
@Override
public boolean isMatch(@NonNull SCM scm) {
URIish uri;
try {
uri = new URIish(getPayload().getRepository().getHtmlUrl());
} catch (URISyntaxException e) {
return false;
}
String ref = getPayload().getRef();
ref = ref.startsWith(Constants.R_HEADS) ? ref.substring(Constants.R_HEADS.length()) : ref;
if (scm instanceof GitSCM) {
GitSCM git = (GitSCM) scm;
if (git.getExtensions().get(IgnoreNotifyCommit.class) != null) {
return false;
}
for (RemoteConfig repository : git.getRepositories()) {
for (URIish remoteURL : repository.getURIs()) {
if (GitStatus.looselyMatches(uri, remoteURL)) {
for (BranchSpec branchSpec : git.getBranches()) {
if (branchSpec.getName().contains("$")) {
// If the branchspec is parametrized, always run the polling
return true;
} else {
if (branchSpec.matches(repository.getName() + "/" + ref)) {
return true;
}
}
}
}
}
}
}
return false;
}
use of org.eclipse.jgit.transport.RemoteConfig in project gitea-plugin by jenkinsci.
the class GiteaWebhookListener method register.
public static void register(SCMTriggerItem item, GitSCM scm) {
JenkinsLocationConfiguration locationConfiguration = JenkinsLocationConfiguration.get();
if (locationConfiguration == null) {
// Jenkins URL not configured, can't register hooks
return;
}
String rootUrl = locationConfiguration.getUrl();
if (StringUtils.isBlank(rootUrl) || rootUrl.startsWith("http://localhost:")) {
// Jenkins URL not configured, can't register hooks
return;
}
if (scm.getExtensions().get(IgnoreNotifyCommit.class) != null) {
// GitSCM special case to ignore commits, so no point registering hooks
return;
}
List<GiteaServer> serverList = new ArrayList<>(GiteaServers.get().getServers());
if (serverList.isEmpty()) {
// Not configured to register hooks, so no point registering.
return;
}
// track attempts to register in case there are multiple remotes for the same repo
Set<String> registered = new HashSet<>();
String hookUrl = UriTemplate.buildFromTemplate(rootUrl).literal("gitea-webhook").literal("/post").build().expand();
for (RemoteConfig repository : scm.getRepositories()) {
REMOTES: for (URIish remoteURL : repository.getURIs()) {
for (Iterator<GiteaServer> iterator = serverList.iterator(); iterator.hasNext(); ) {
GiteaServer server = iterator.next();
if (!server.isManageHooks()) {
// not allowed to manage hooks for this server, don't check it against any other remotes
iterator.remove();
continue;
}
URIish serverURL;
try {
serverURL = new URIish(server.getServerUrl());
} catch (URISyntaxException e) {
continue;
}
if (!StringUtils.equals(serverURL.getHost(), remoteURL.getHost())) {
// different hosts, so none of our business
continue;
}
if (serverURL.getPort() != -1 && remoteURL.getPort() != -1 && serverURL.getPort() != remoteURL.getPort()) {
// different explicit ports, so none of our business
continue;
}
String serverPath = serverURL.getPath();
if (!serverPath.startsWith("/")) {
serverPath = "/" + serverPath;
}
if (!serverPath.endsWith("/")) {
serverPath = serverPath + "/";
}
String remotePath = remoteURL.getPath();
if (!remotePath.startsWith("/")) {
remotePath = "/" + remotePath;
}
if (!remotePath.startsWith(serverPath)) {
// different context path, so none of our business
continue;
}
remotePath = remotePath.substring(serverPath.length());
int index = remotePath.indexOf('/');
if (index == -1) {
// not matching expected structure of repoOwner/repository[.git]
continue REMOTES;
}
String repoOwner = remotePath.substring(0, index);
String repoName = StringUtils.removeEnd(remotePath.substring(index + 1), ".git");
String registeredKey = server.getServerUrl() + "::" + repoOwner + "::" + repoName;
if (registered.contains(registeredKey)) {
// have already tried to register this repo during this method, so don't repeat
continue REMOTES;
}
registered.add(registeredKey);
try (GiteaConnection c = connect(server.getServerUrl(), server.credentials())) {
GiteaRepository repo = c.fetchRepository(repoOwner, repoName);
if (repo == null) {
continue REMOTES;
}
List<GiteaHook> hooks = c.fetchHooks(repo);
GiteaHook hook = null;
for (GiteaHook h : hooks) {
if (hookUrl.equals(h.getConfig().getUrl())) {
if (hook == null && h.getType() == GiteaHookType.GITEA && h.getConfig().getContentType() == GiteaPayloadType.JSON && h.isActive() && EnumSet.allOf(GiteaEventType.class).equals(h.getEvents())) {
hook = h;
} else {
c.deleteHook(repo, h);
}
}
}
if (hook == null) {
hook = new GiteaHook();
GiteaHook.Configuration configuration = new GiteaHook.Configuration();
configuration.setContentType(GiteaPayloadType.JSON);
configuration.setUrl(hookUrl);
hook.setType(GiteaHookType.GITEA);
hook.setConfig(configuration);
hook.setEvents(EnumSet.allOf(GiteaEventType.class));
hook.setActive(true);
c.createHook(repo, hook);
}
} catch (IOException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not manage repository hooks for " + repoOwner + "/" + repoName + " on " + server.getServerUrl(), e);
}
}
if (serverList.isEmpty()) {
// none of the servers are allowed manage hooks, no point checking the remaining remotes
return;
}
}
}
}
use of org.eclipse.jgit.transport.RemoteConfig in project jbosstools-openshift by jbosstools.
the class EGitUtils method getRemoteGitReposFilteredStream.
private static Stream<String> getRemoteGitReposFilteredStream(IProject project) throws CoreException {
if (project == null) {
return null;
}
Repository repo = getRepository(project);
if (repo == null) {
return null;
}
// Returned configurations are ordered lexicographically by names.
List<RemoteConfig> remoteConfigs = getAllRemoteConfigs(repo);
Collections.sort(remoteConfigs, new Comparator<RemoteConfig>() {
@Override
public int compare(RemoteConfig o1, RemoteConfig o2) {
if (ORIGIN.equals(o1.getName())) {
return -1;
}
if (ORIGIN.equals(o2.getName())) {
return 1;
}
return o1.getName().compareToIgnoreCase(o2.getName());
}
});
return remoteConfigs.stream().map(rc -> getFetchURI(rc)).filter(uri -> uri != null && uri.toString().startsWith("http")).map(URIish::toString);
}
use of org.eclipse.jgit.transport.RemoteConfig in project jbosstools-openshift by jbosstools.
the class EGitUtils method getAllRemoteURIs.
/**
* Returns all uris of alls remotes for the given project.
*
* @param project
* the project to get all remotes and all uris from
* @return all uris
* @throws CoreException
*/
public static List<URIish> getAllRemoteURIs(IProject project) throws CoreException {
List<RemoteConfig> remoteConfigs = getAllRemoteConfigs(getRepository(project));
List<URIish> uris = new ArrayList<>();
if (remoteConfigs != null) {
for (RemoteConfig remoteConfig : remoteConfigs) {
uris.addAll(remoteConfig.getURIs());
}
}
return uris;
}
use of org.eclipse.jgit.transport.RemoteConfig in project jbosstools-openshift by jbosstools.
the class ImportProjectOperation method checkoutBranch.
protected void checkoutBranch(String gitRef, String gitUrl, List<IProject> importedProjects, File repositoryFolder, IProgressMonitor monitor) throws CoreException {
if (!StringUtils.isEmpty(gitRef) && !CollectionUtils.isEmpty(importedProjects)) {
// all projects are in the same repo
IProject project = importedProjects.get(0);
Repository repository = EGitUtils.getRepository(project);
try {
if (!EGitUtils.hasBranch(gitRef, repository)) {
Pattern gitURIPattern = Pattern.compile(RegExUtils.escapeRegex(gitUrl));
RemoteConfig remote = EGitUtils.getRemoteByUrl(gitURIPattern, repository);
fetchBranch(gitRef, remote, repository, monitor);
}
CheckoutResult result = EGitUtils.checkoutBranch(gitRef, repository, monitor);
switch(result.getStatus()) {
case CONFLICTS:
case ERROR:
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not check out the branch {0} of the (reused) local repository at {1} because of {2}." + " Please resolve the problem and check out the branch manually so that it matches the code that's used in your OpenShift application.", new Object[] { gitRef, repositoryFolder, result.getStatus().toString().toLowerCase() })));
default:
}
} catch (IOException e) {
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could check that branch {0} exists within the (reused) local repository at {1}.", gitRef, repositoryFolder), e));
} catch (InvocationTargetException e) {
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not fetch branch {0} from check that branch {0} exists within the (reused) local repository at {1}.", gitRef, repositoryFolder), e));
}
}
}
Aggregations