private boolean hookExist() throws IOException { GHRepository ghRepository = getGitHubRepo(); for (GHHook h : ghRepository.getHooks()) { if (!"web".equals(h.getName())) { continue; } if (!getHookUrl().equals(h.getConfig().get("url"))) { continue; } return true; } return false; }
private GHHook createWebHook(String webhookUrl) throws IOException { final GitHubRepositoryName gitHubRepoName = GitHubRepositoryName.create(gitHubRepositoryUrl); for (GHRepository repo : gitHubRepoName.resolve()) { // check if the webhook already exists for (GHHook hook : repo.getHooks()) { if ("web".equals(hook.getName()) && webhookUrl.equals(hook.getConfig().get("url"))) { LOGGER.info( MessageFormat.format( "Webhook {0} already exists for {1}", webhookUrl, gitHubRepositoryUrl)); return hook; } } try { Map<String, String> config = new HashMap<String, String>(); config.put("url", webhookUrl); config.put("insecure_ssl", "1"); GHHook hook = repo.createHook("web", config, WEBHOOK_EVENTS, true); LOGGER.info( MessageFormat.format( "Webhook {0} is added to GitHub repository {1}", webhookUrl, gitHubRepositoryUrl)); return hook; } catch (IOException e) { LOGGER.log( Level.FINEST, MessageFormat.format( "Failed to add webhook {0} to GitHub repository {1}", webhookUrl, gitHubRepositoryUrl), e); } } LOGGER.warning( MessageFormat.format( "Cannot add webhook {0} to GitHub repository {1}. " + "Make sure that you specified a valid GitHub credential for GitHub project {1} in Jenkins " + "configuration.", webhookUrl, gitHubRepositoryUrl)); return null; }