jenkins.scm.api.SCMSourceOwner Java Examples

The following examples show how to use jenkins.scm.api.SCMSourceOwner. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PushBuildAction.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
public void run() {
    for (SCMSource scmSource : ((SCMSourceOwner) project).getSCMSources()) {
        if (scmSource instanceof GitSCMSource) {
            GitSCMSource gitSCMSource = (GitSCMSource) scmSource;
            try {
                if (new URIish(gitSCMSource.getRemote()).equals(new URIish(gitSCMSource.getRemote()))) {
                    if (!gitSCMSource.isIgnoreOnPushNotifications()) {
                        LOGGER.log(Level.FINE, "Notify scmSourceOwner {0} about changes for {1}",
                                   toArray(project.getName(), gitSCMSource.getRemote()));
                        ((SCMSourceOwner) project).onSCMSourceUpdated(scmSource);
                    } else {
                        LOGGER.log(Level.FINE, "Ignore on push notification for scmSourceOwner {0} about changes for {1}",
                                   toArray(project.getName(), gitSCMSource.getRemote()));
                    }
                }
            } catch (URISyntaxException e) {
                // nothing to do
            }
        }
    }
}
 
Example #2
Source File: GitHubSCMSourceRepositoryNameContributor.java    From github-branch-source-plugin with MIT License 6 votes vote down vote up
@Override
public void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
    if (item instanceof SCMSourceOwner) {
        SCMSourceOwner mp = (SCMSourceOwner) item;
        for (Object o : mp.getSCMSources()) {
            if (o instanceof GitHubSCMSource) {
                GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) o;
                result.add(new GitHubRepositoryName(
                        RepositoryUriResolver.hostnameFromApiUri(gitHubSCMSource.getApiUri()),
                        gitHubSCMSource.getRepoOwner(),
                        gitHubSCMSource.getRepository()));

            }
        }
    }
}
 
Example #3
Source File: GitLabSCMSourceSettings.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Restricted(NoExternalUse.class)
public ListBoxModel doFillCheckoutCredentialsIdItems(@AncestorInPath SCMSourceOwner context, @QueryParameter String connectionName, @QueryParameter String checkoutCredentialsId) {
    if (context == null && !Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER) ||
            context != null && !context.hasPermission(Item.EXTENDED_READ)) {
        return new StandardListBoxModel().includeCurrentValue(checkoutCredentialsId);
    }

    StandardListBoxModel result = new StandardListBoxModel();
    result.add("- anonymous -", CHECKOUT_CREDENTIALS_ANONYMOUS);
    return result.includeMatchingAs(
            context instanceof Queue.Task
                    ? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
                    : ACL.SYSTEM,
            context,
            StandardUsernameCredentials.class,
            SettingsUtils.gitLabConnectionRequirements(connectionName),
            GitClient.CREDENTIALS_MATCHER
    );
}
 
Example #4
Source File: GitHubPRCause.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Override
public void onAddedTo(@Nonnull Run run) {
    if (run.getParent().getParent() instanceof SCMSourceOwner) {
        // skip multibranch
        return;
    }

    // move polling log from cause to action
    try {
        GitHubPRPollingLogAction action = new GitHubPRPollingLogAction(run);
        FileUtils.writeStringToFile(action.getPollingLogFile(), getPollingLog());
        run.replaceAction(action);
    } catch (IOException e) {
        LOGGER.warn("Failed to persist the polling log", e);
    }
    setPollingLog(null);
}
 
Example #5
Source File: GitHubSourceContext.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private void forceScheduling(GitHubSCMRevision scmRevision) throws IOException {
    SCMSourceOwner owner = source.getOwner();
    if (owner instanceof MultiBranchProject) {
        MultiBranchProject mb = (MultiBranchProject) owner;
        BranchProjectFactory pf = mb.getProjectFactory();

        SCMHead scmHead = scmRevision.getHead();
        Job j = mb.getItemByBranchName(scmHead.getName());
        if (j != null) {
            SCMRevision rev = pf.getRevision(j);
            // set current rev to dummy to force scheduling
            if (rev != null && rev.equals(scmRevision)) {
                pf.setRevisionHash(j, new DummyRevision(scmHead));
            }
        }
    }
}
 
Example #6
Source File: GHPRMultiBranchSubscriber.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Override
protected boolean isApplicable(@Nullable Item item) {
    if (item instanceof SCMSourceOwner) {
        SCMSourceOwner scmSourceOwner = (SCMSourceOwner) item;
        for (SCMSource source : scmSourceOwner.getSCMSources()) {
            if (source instanceof GitHubSCMSource) {
                GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
                for (GitHubHandler hubHandler : gitHubSCMSource.getHandlers()) {
                    if (hubHandler instanceof GitHubPRHandler) {
                        return true;
                    }
                }
            }
        }
    }

    return false;
}
 
Example #7
Source File: GHMultiBranchSubscriber.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Override
protected boolean isApplicable(@Nullable Item item) {
    if (item instanceof SCMSourceOwner) {
        SCMSourceOwner scmSourceOwner = (SCMSourceOwner) item;
        for (SCMSource source : scmSourceOwner.getSCMSources()) {
            if (source instanceof GitHubSCMSource) {
                GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
                for (GitHubHandler hubHandler : gitHubSCMSource.getHandlers()) {
                    if (hubHandler instanceof GitHubBranchHandler) {
                        return true;
                    }
                }
            }
        }
    }

    return false;
}
 
Example #8
Source File: Functions.java    From github-integration-plugin with MIT License 6 votes vote down vote up
public static <ITEM extends Item> Predicate<ITEM> withBranchHandler() {
    return item -> {
        if (item instanceof SCMSourceOwner) {
            SCMSourceOwner scmSourceOwner = (SCMSourceOwner) item;
            for (SCMSource source : scmSourceOwner.getSCMSources()) {
                if (source instanceof GitHubSCMSource) {
                    GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
                    for (GitHubHandler hubHandler : gitHubSCMSource.getHandlers()) {
                        if (hubHandler instanceof GitHubBranchHandler) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    };
}
 
Example #9
Source File: GitHubBranchCause.java    From github-integration-plugin with MIT License 6 votes vote down vote up
@Override
public void onAddedTo(@Nonnull Run run) {
    if (run.getParent().getParent() instanceof SCMSourceOwner) {
        // skip multibranch
        return;
    }

    // move polling log from cause to action
    try {
        GitHubBranchPollingLogAction action = new GitHubBranchPollingLogAction(run);
        writeStringToFile(action.getPollingLogFile(), getPollingLog());
        run.replaceAction(action);
    } catch (IOException ex) {
        LOG.warn("Failed to persist the polling log", ex);
    }
    setPollingLog(null);
}
 
Example #10
Source File: ActionResolver.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
private Item resolveProject(final String projectName, final Iterator<String> restOfPathParts) {
    return ACLUtil.impersonate(ACL.SYSTEM, new ACLUtil.Function<Item>() {
        public Item invoke() {
            final Jenkins jenkins = Jenkins.getInstance();
            if (jenkins != null) {
                Item item = jenkins.getItemByFullName(projectName);
                while (item instanceof ItemGroup<?> && !(item instanceof Job<?, ?> || item instanceof SCMSourceOwner) && restOfPathParts.hasNext()) {
                    item = jenkins.getItem(restOfPathParts.next(), (ItemGroup<?>) item);
                }
                if (item instanceof Job<?, ?> || item instanceof SCMSourceOwner) {
                    return item;
                }
            }
            LOGGER.log(Level.FINE, "No project found: {0}, {1}", toArray(projectName, Joiner.on('/').join(restOfPathParts)));
            return null;
        }
    });
}
 
Example #11
Source File: GitLabSCMSource.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner context,
        @QueryParameter String serverName, @QueryParameter String credentialsId) {
    StandardListBoxModel result = new StandardListBoxModel();
    if (context == null) {
        // must have admin if you want the list without a context
        if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
            result.includeCurrentValue(credentialsId);
            return result;
        }
    } else {
        if (!context.hasPermission(Item.EXTENDED_READ)
                && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
            // must be able to read the configuration or use the item credentials if you
            // want the list
            result.includeCurrentValue(credentialsId);
            return result;
        }
    }
    result.includeEmptyValue();
    result.includeMatchingAs(
            context instanceof Queue.Task ? ((Queue.Task) context).getDefaultAuthentication() : ACL.SYSTEM,
            context, StandardUsernameCredentials.class, fromUri(getServerUrlFromName(serverName)).build(),
            GitClient.CREDENTIALS_MATCHER);
    return result;
}
 
Example #12
Source File: PushBuildAction.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
public void execute() {
    if (pushHook.getRepository() != null && pushHook.getRepository().getUrl() == null) {
        LOGGER.log(Level.WARNING, "No repository url found.");
        return;
    }

    if (project instanceof Job<?, ?>) {
        ACL.impersonate(ACL.SYSTEM, new TriggerNotifier(project, secretToken, Jenkins.getAuthentication()) {
            @Override
            protected void performOnPost(GitLabPushTrigger trigger) {
                trigger.onPost(pushHook);
            }
        });
        throw HttpResponses.ok();
    }
    if (project instanceof SCMSourceOwner) {
        ACL.impersonate(ACL.SYSTEM, new SCMSourceOwnerNotifier());
        throw HttpResponses.ok();
    }
    throw HttpResponses.errorWithoutStack(409, "Push Hook is not supported for this project");
}
 
Example #13
Source File: GitLabSCMNavigator.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
public PersonalAccessToken credentials(SCMSourceOwner owner) {
    return CredentialsMatchers.firstOrNull(
        lookupCredentials(
            PersonalAccessToken.class,
            owner,
            Jenkins.getAuthentication(),
            fromUri(getServerUrlFromName(serverName)).build()
        ), credentials -> credentials instanceof PersonalAccessToken
    );
}
 
Example #14
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void onDeleted(SCMSourceOwner owner) {
    for (SCMSource source : owner.getSCMSources()) {
        if (source instanceof GitLabSCMSource) {
            unregister((GitLabSCMSource) source, owner);
        }
    }
}
 
Example #15
Source File: GitLabSCMWebHookItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onDeleted(Item item) {
    if (item instanceof SCMNavigatorOwner) {
        onDeleted((SCMNavigatorOwner) item);
    }

    if (item instanceof SCMSourceOwner) {
        onDeleted((SCMSourceOwner) item);
    }
}
 
Example #16
Source File: GitLabSCMItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private void onUpdated(Job<?, ?> job) {
    BranchJobProperty property = job.getProperty(BranchJobProperty.class);
    if (property != null && job.getParent() instanceof SCMSourceOwner) {
        // TODO: HACK ALERT! There must/should be a nicer way to do this!
        updateProperties(job, (SCMSourceOwner) job.getParent(), property.getBranch().getSourceId());
    }
}
 
Example #17
Source File: GitLabSCMItemListener.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
private boolean updateProperties(Job<?, ?> job, SCMSourceOwner sourceOwner, String sourceId) {
    SCMSource source = sourceOwner.getSCMSource(sourceId);
    if (source instanceof GitLabSCMSource) {
        String connectionName = ((GitLabSCMSource) source).getSourceSettings().getConnectionName();
        GitLabConnectionProperty property = job.getProperty(GitLabConnectionProperty.class);
        if (property == null || !connectionName.equals(property.getGitLabConnection())) {
            updateProperties(job, connectionName);
            return true;
        }
    }

    return false;
}
 
Example #18
Source File: GitHubStatusNotificationStep.java    From pipeline-githubnotify-step-plugin with MIT License 5 votes vote down vote up
private GitHubSCMSource getSource() {
    ItemGroup parent = run.getParent().getParent();
    if (parent instanceof SCMSourceOwner) {
        SCMSourceOwner owner = (SCMSourceOwner)parent;
        for (SCMSource source : owner.getSCMSources()) {
            if (source instanceof GitHubSCMSource) {
                return ((GitHubSCMSource) source);
            }
        }
        throw new IllegalArgumentException(UNABLE_TO_INFER_DATA);
    } else {
        throw new IllegalArgumentException(UNABLE_TO_INFER_DATA);
    }
}
 
Example #19
Source File: GitHubSCMSource.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void afterSave() {
    SCMSourceOwner owner = getOwner();
    if (owner != null) {
        GitHubWebHook.get().registerHookFor(owner);
    }
}
 
Example #20
Source File: GitHubSCMNavigatorTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
private SCMSourceObserver getObserver(Collection<String> names){
    return new SCMSourceObserver() {
        @NonNull
        @Override
        public SCMSourceOwner getContext() {
            return scmSourceOwner;
        }

        @NonNull
        @Override
        public TaskListener getListener() {
            return new LogTaskListener(Logger.getAnonymousLogger(), Level.INFO);
        }

        @NonNull
        @Override
        public ProjectObserver observe(@NonNull String projectName) throws IllegalArgumentException {
            names.add(projectName);
            return new NoOpProjectObserver();
        }

        @Override
        public void addAttribute(@NonNull String key, @Nullable Object value)
                throws IllegalArgumentException, ClassCastException {

        }
    };
}
 
Example #21
Source File: GitHubSCMSource.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Override
public void afterSave() {
    SCMSourceOwner owner = getOwner();
    if (nonNull(owner) && getRepoProvider().isManageHooks(this)) {
        getRepoProvider().registerHookFor(this);
    }
}
 
Example #22
Source File: GitHubScmSourceRepositoryNameContributor.java    From github-integration-plugin with MIT License 5 votes vote down vote up
@Override
public void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
    if (item instanceof SCMSourceOwner) {
        SCMSourceOwner sourceOwner = (SCMSourceOwner) item;
        for (SCMSource source : sourceOwner.getSCMSources()) {
            if (source instanceof GitHubSCMSource) {
                GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
                result.add(gitHubSCMSource.getRepoFullName());
            }
        }
    }
}
 
Example #23
Source File: GiteaSCMNavigator.java    From gitea-plugin with MIT License 5 votes vote down vote up
public StandardCredentials credentials(SCMSourceOwner owner) {
    return CredentialsMatchers.firstOrNull(
            CredentialsProvider.lookupCredentials(
                    StandardCredentials.class,
                    owner,
                    Jenkins.getAuthentication(),
                    URIRequirementBuilder.fromUri(serverUrl).build()
            ),
            CredentialsMatchers.allOf(
                    AuthenticationTokens.matcher(GiteaAuth.class),
                    CredentialsMatchers.withId(credentialsId)
            )
    );
}
 
Example #24
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 5 votes vote down vote up
public FormValidation doCheckCredentialsId(@AncestorInPath SCMSourceOwner context,
                                           @QueryParameter String serverUrl,
                                           @QueryParameter String value)
        throws IOException, InterruptedException {
    if (context == null) {
        if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
            return FormValidation.ok();
        }
    } else {
        if (!context.hasPermission(Item.EXTENDED_READ)
                && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
            return FormValidation.ok();
        }
    }
    GiteaServer server = GiteaServers.get().findServer(serverUrl);
    if (server == null) {
        return FormValidation.ok();
    }
    if (StringUtils.isBlank(value)) {
        return FormValidation.ok();
    }
    if (CredentialsProvider.listCredentials(
            StandardCredentials.class,
            context,
            context instanceof Queue.Task
                    ? ((Queue.Task) context).getDefaultAuthentication()
                    : ACL.SYSTEM,
            URIRequirementBuilder.fromUri(serverUrl).build(),
            CredentialsMatchers.allOf(
                    CredentialsMatchers.withId(value),
                    AuthenticationTokens.matcher(GiteaAuth.class)

            )).isEmpty()) {
        return FormValidation.error(Messages.GiteaSCMSource_selectedCredentialsMissing());
    }
    return FormValidation.ok();
}
 
Example #25
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 5 votes vote down vote up
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner context,
                                             @QueryParameter String serverUrl,
                                             @QueryParameter String credentialsId) {
    StandardListBoxModel result = new StandardListBoxModel();
    if (context == null) {
        if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
            // must have admin if you want the list without a context
            result.includeCurrentValue(credentialsId);
            return result;
        }
    } else {
        if (!context.hasPermission(Item.EXTENDED_READ)
                && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
            // must be able to read the configuration or use the item credentials if you want the list
            result.includeCurrentValue(credentialsId);
            return result;
        }
    }
    result.includeEmptyValue();
    result.includeMatchingAs(
            context instanceof Queue.Task ?
                    ((Queue.Task) context).getDefaultAuthentication()
                    : ACL.SYSTEM,
            context,
            StandardCredentials.class,
            URIRequirementBuilder.fromUri(serverUrl).build(),
            AuthenticationTokens.matcher(GiteaAuth.class)
    );
    return result;
}
 
Example #26
Source File: GiteaSCMSource.java    From gitea-plugin with MIT License 5 votes vote down vote up
Gitea gitea() throws AbortException {
    GiteaServer server = GiteaServers.get().findServer(serverUrl);
    if (server == null) {
        throw new AbortException("Unknown server: " + serverUrl);
    }
    StandardCredentials credentials = credentials();
    SCMSourceOwner owner = getOwner();
    if (owner != null) {
        CredentialsProvider.track(owner, credentials);
    }
    return Gitea.server(serverUrl)
            .as(AuthenticationTokens.convert(GiteaAuth.class, credentials));
}
 
Example #27
Source File: GiteaSCMNavigator.java    From gitea-plugin with MIT License 5 votes vote down vote up
public FormValidation doCheckCredentialsId(@AncestorInPath SCMSourceOwner context,
                                           @QueryParameter String serverUrl,
                                           @QueryParameter String value)
        throws IOException, InterruptedException {
    if (context == null) {
        if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
            return FormValidation.ok();
        }
    } else {
        if (!context.hasPermission(Item.EXTENDED_READ)
                && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
            return FormValidation.ok();
        }
    }
    GiteaServer server = GiteaServers.get().findServer(serverUrl);
    if (server == null) {
        return FormValidation.ok();
    }
    if (StringUtils.isBlank(value)) {
        return FormValidation.ok();
    }
    if (CredentialsProvider.listCredentials(
            StandardCredentials.class,
            context,
            context instanceof Queue.Task ?
                    ((Queue.Task) context).getDefaultAuthentication()
                    : ACL.SYSTEM,
            URIRequirementBuilder.fromUri(serverUrl).build(),
            CredentialsMatchers.allOf(
                    CredentialsMatchers.withId(value),
                    AuthenticationTokens.matcher(GiteaAuth.class)

            )).isEmpty()) {
        return FormValidation.error(Messages.GiteaSCMNavigator_selectedCredentialsMissing());
    }
    return FormValidation.ok();
}
 
Example #28
Source File: GiteaSCMNavigator.java    From gitea-plugin with MIT License 5 votes vote down vote up
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner context,
                                             @QueryParameter String serverUrl,
                                             @QueryParameter String credentialsId) {
    StandardListBoxModel result = new StandardListBoxModel();
    if (context == null) {
        if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
            // must have admin if you want the list without a context
            result.includeCurrentValue(credentialsId);
            return result;
        }
    } else {
        if (!context.hasPermission(Item.EXTENDED_READ)
                && !context.hasPermission(CredentialsProvider.USE_ITEM)) {
            // must be able to read the configuration or use the item credentials if you want the list
            result.includeCurrentValue(credentialsId);
            return result;
        }
    }
    result.includeEmptyValue();
    result.includeMatchingAs(
            context instanceof Queue.Task ?
                    ((Queue.Task) context).getDefaultAuthentication()
                    : ACL.SYSTEM,
            context,
            StandardCredentials.class,
            URIRequirementBuilder.fromUri(serverUrl).build(),
            AuthenticationTokens.matcher(GiteaAuth.class)
    );
    return result;
}
 
Example #29
Source File: IssueCommentGHEventSubscriber.java    From github-pr-comment-build-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isApplicable(Item item) {
    if (item != null && item instanceof Job<?, ?>) {
        Job<?, ?> project = (Job<?, ?>) item;
        if (project.getParent() instanceof SCMSourceOwner) {
            SCMSourceOwner owner = (SCMSourceOwner) project.getParent();
            for (SCMSource source : owner.getSCMSources()) {
                if (source instanceof GitHubSCMSource) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example #30
Source File: PRUpdateGHEventSubscriber.java    From github-pr-comment-build-plugin with MIT License 5 votes vote down vote up
@Override
protected boolean isApplicable(Item item) {
    if (item != null && item instanceof Job<?, ?>) {
        Job<?, ?> project = (Job<?, ?>) item;
        if (project.getParent() instanceof SCMSourceOwner) {
            SCMSourceOwner owner = (SCMSourceOwner) project.getParent();
            for (SCMSource source : owner.getSCMSources()) {
                if (source instanceof GitHubSCMSource) {
                    return true;
                }
            }
        }
    }
    return false;
}