Search in sources :

Example 1 with RepositoryDTO

use of io.fabric8.repo.git.RepositoryDTO in project fabric8 by fabric8io.

the class WebHooks method createGogsWebhook.

/**
 * Creates a webook in the given gogs repo for the user and password if the webhook does not already exist
 */
public static boolean createGogsWebhook(GitRepoClient repoClient, Logger log, String gogsUser, String repoName, String webhookUrl, String webhookSecret) throws JsonProcessingException {
    if (repoClient == null) {
        log.info("Cannot create Gogs webhooks as no Gogs service could be found or created");
        return false;
    }
    String gogsAddress = repoClient.getAddress();
    log.info("Querying webhooks in gogs at address: " + gogsAddress + " for user " + gogsUser + " repoName: " + repoName);
    RepositoryDTO repository = repoClient.getRepository(gogsUser, repoName);
    if (repository == null) {
        log.info("No repository found for user: " + gogsUser + " repo: " + repoName + " so cannot create any web hooks");
    // return false;
    }
    List<WebHookDTO> webhooks = repoClient.getWebhooks(gogsUser, repoName);
    for (WebHookDTO webhook : webhooks) {
        String url = null;
        WebhookConfig config = webhook.getConfig();
        if (config != null) {
            url = config.getUrl();
            if (Objects.equal(webhookUrl, url)) {
                log.info("Already has webhook for: " + url + " so not creating again");
                return false;
            }
            log.info("Ignoring webhook " + url + " from: " + toJson(config));
        }
    }
    CreateWebhookDTO createWebhook = new CreateWebhookDTO();
    createWebhook.setType("gogs");
    WebhookConfig config = createWebhook.getConfig();
    config.setUrl(webhookUrl);
    config.setSecret(webhookSecret);
    WebHookDTO webhook = repoClient.createWebhook(gogsUser, repoName, createWebhook);
    if (log.isDebugEnabled()) {
        log.debug("Got created web hook: " + toJson(webhook));
    }
    log.info("Created webhook for " + webhookUrl + " for user: " + gogsUser + " repoName: " + repoName + " on gogs URL: " + gogsAddress);
    return true;
}
Also used : WebhookConfig(io.fabric8.repo.git.WebhookConfig) WebHookDTO(io.fabric8.repo.git.WebHookDTO) RepositoryDTO(io.fabric8.repo.git.RepositoryDTO) CreateWebhookDTO(io.fabric8.repo.git.CreateWebhookDTO)

Example 2 with RepositoryDTO

use of io.fabric8.repo.git.RepositoryDTO in project fabric8 by fabric8io.

the class BuildConfigHelper method importNewGitProject.

public static CreateGitProjectResults importNewGitProject(KubernetesClient kubernetesClient, UserDetails userDetails, File basedir, String namespace, String projectName, String origin, String message, boolean apply, boolean useLocalGitAddress) throws GitAPIException, JsonProcessingException {
    GitUtils.disableSslCertificateChecks();
    InitCommand initCommand = Git.init();
    initCommand.setDirectory(basedir);
    Git git = initCommand.call();
    LOG.info("Initialised an empty git configuration repo at {}", basedir.getAbsolutePath());
    PersonIdent personIdent = userDetails.createPersonIdent();
    String user = userDetails.getUser();
    String address = userDetails.getAddress();
    String internalAddress = userDetails.getInternalAddress();
    String branch = userDetails.getBranch();
    // lets create the repository
    GitRepoClient repoClient = userDetails.createRepoClient();
    CreateRepositoryDTO createRepository = new CreateRepositoryDTO();
    createRepository.setName(projectName);
    String fullName = null;
    RepositoryDTO repository = repoClient.createRepository(createRepository);
    if (repository != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Got repository: " + toJson(repository));
        }
        fullName = repository.getFullName();
    }
    if (Strings.isNullOrBlank(fullName)) {
        fullName = user + "/" + projectName;
    }
    String htmlUrl = URLUtils.pathJoin(resolveToRoot(address), user, projectName);
    String localCloneUrl = URLUtils.pathJoin(resolveToRoot(internalAddress), user, projectName + ".git");
    String cloneUrl = htmlUrl + ".git";
    String defaultCloneUrl = cloneUrl;
    // lets default to using the local git clone URL
    if (useLocalGitAddress && Strings.isNotBlank(internalAddress)) {
        defaultCloneUrl = localCloneUrl;
    }
    // now lets import the code and publish
    GitUtils.configureBranch(git, branch, origin, defaultCloneUrl);
    GitUtils.addDummyFileToEmptyFolders(basedir);
    LOG.info("About to git commit and push to: " + defaultCloneUrl + " and remote name " + origin);
    GitUtils.doAddCommitAndPushFiles(git, userDetails, personIdent, branch, origin, message, true);
    Map<String, String> annotations = new HashMap<>();
    annotations.put(Annotations.Builds.GIT_CLONE_URL, cloneUrl);
    annotations.put(Annotations.Builds.LOCAL_GIT_CLONE_URL, localCloneUrl);
    BuildConfig buildConfig;
    if (apply) {
        buildConfig = createAndApplyBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
    } else {
        buildConfig = createBuildConfig(kubernetesClient, namespace, projectName, defaultCloneUrl, annotations);
    }
    return new CreateGitProjectResults(buildConfig, fullName, htmlUrl, localCloneUrl, cloneUrl);
}
Also used : Git(org.eclipse.jgit.api.Git) CreateRepositoryDTO(io.fabric8.repo.git.CreateRepositoryDTO) PersonIdent(org.eclipse.jgit.lib.PersonIdent) HashMap(java.util.HashMap) InitCommand(org.eclipse.jgit.api.InitCommand) GitRepoClient(io.fabric8.repo.git.GitRepoClient) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) RepositoryDTO(io.fabric8.repo.git.RepositoryDTO) CreateRepositoryDTO(io.fabric8.repo.git.CreateRepositoryDTO)

Aggregations

RepositoryDTO (io.fabric8.repo.git.RepositoryDTO)2 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)1 CreateRepositoryDTO (io.fabric8.repo.git.CreateRepositoryDTO)1 CreateWebhookDTO (io.fabric8.repo.git.CreateWebhookDTO)1 GitRepoClient (io.fabric8.repo.git.GitRepoClient)1 WebHookDTO (io.fabric8.repo.git.WebHookDTO)1 WebhookConfig (io.fabric8.repo.git.WebhookConfig)1 HashMap (java.util.HashMap)1 Git (org.eclipse.jgit.api.Git)1 InitCommand (org.eclipse.jgit.api.InitCommand)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1