Java Code Examples for jenkins.scm.api.SCMFileSystem

The following examples show how to use jenkins.scm.api.SCMFileSystem. These examples are extracted from open source projects. 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
@Test
    public void readFileFromDir() throws Exception {
        assumeThat(revision, instanceOf(AbstractGitSCMSource.SCMRevisionImpl.class));
        assumeThat(((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash(),
                is("c0e024f89969b976da165eecaa71e09dc60c3da1"));
        SCMFileSystem fs = SCMFileSystem.of(source, master, revision);

        String expected = "Some text\n";
        // In previous versions of github-api, GHContent.read() (called by contentAsString())
        // would pull from the "raw" url of the GHContent instance.
        // Thus on windows, if somebody did not configure Git correctly,
        // the checkout may have "fixed" line endings that we needed to handle.
        // The problem with the raw url data is that it can get out of sync when from the actual content.
        // The GitHub API info stays sync'd and correct, so now GHContent.read() pulls from mime encoded data
        // in the GHContent record itself. Keeping this for reference in case it changes again.
//        try (InputStream inputStream = getClass().getResourceAsStream("/raw/__files/body-fu-bar.txt-b4k4I.txt")) {
//            if (inputStream != null) {
//                expected = IOUtils.toString(inputStream, StandardCharsets.US_ASCII);
//            }
//        } catch (IOException e) {
//            // ignore
//        }
        assertThat(fs.getRoot().child("fu/bar.txt").contentAsString(), is(expected));
    }
 
Example 2
@Test
public void resolveDirPRHead() throws Exception {
    assumeThat(revision, nullValue());

    assertThat(prHeadRevision.isMerge(), is(false));

    SCMFileSystem fs = SCMFileSystem.of(source, prHead, prHeadRevision);
    assertThat(fs, instanceOf(GitHubSCMFileSystem.class));

    // We can't check the sha, but we can check last modified
    // which are different for head or merge
    assertThat(((GitHubSCMFileSystem)fs).lastModified(),
        is(1480691047000L));

    assertThat(fs.getRoot().child("fu").getType(), is(SCMFile.Type.DIRECTORY));
}
 
Example 3
@Test
public void resolveDirPRMerge() throws Exception {
    assumeThat(revision, nullValue());

    assertThat(prMergeRevision.isMerge(), is(true));

    SCMFileSystem fs = SCMFileSystem.of(source, prMerge, prMergeRevision);
    assertThat(fs, instanceOf(GitHubSCMFileSystem.class));

    // We can't check the sha, but we can check last modified
    // which are different for head or merge
    assertThat(((GitHubSCMFileSystem)fs).lastModified(),
        is(1480777447000L));

    assertThat(fs.getRoot().child("fu").getType(), is(SCMFile.Type.DIRECTORY));
}
 
Example 4
Source Project: gitlab-branch-source-plugin   Source File: GitLabSCMFileSystem.java    License: MIT License 5 votes vote down vote up
@Override
public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head,
   @CheckForNull SCMRevision rev)
   throws IOException, InterruptedException {
    GitLabSCMSource gitlabScmSource = (GitLabSCMSource) source;
    GitLabApi gitLabApi = apiBuilder(gitlabScmSource.getServerName());
    String projectPath = gitlabScmSource.getProjectPath();
    return build(head, rev, gitLabApi, projectPath);
}
 
Example 5
Source Project: gitlab-branch-source-plugin   Source File: GitLabSCMFileSystem.java    License: MIT License 5 votes vote down vote up
public SCMFileSystem build(@NonNull SCMHead head, @CheckForNull SCMRevision rev,
    @NonNull GitLabApi gitLabApi, @NonNull String projectPath)
    throws IOException, InterruptedException {
    String ref;
    if (head instanceof MergeRequestSCMHead) {
        ref = ((MergeRequestSCMHead) head).getOriginName();
    } else if (head instanceof BranchSCMHead) {
        ref = head.getName();
    } else if (head instanceof GitLabTagSCMHead) {
        ref = head.getName();
    } else {
        return null;
    }
    return new GitLabSCMFileSystem(gitLabApi, projectPath, ref, rev);
}
 
Example 6
@Test
public void resolveDir() throws Exception {
    assumeThat(revision, instanceOf(AbstractGitSCMSource.SCMRevisionImpl.class));
    assumeThat(((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash(),
            is("c0e024f89969b976da165eecaa71e09dc60c3da1"));
    SCMFileSystem fs = SCMFileSystem.of(source, master, revision);
    assertThat(fs.getRoot().child("fu").getType(), is(SCMFile.Type.DIRECTORY));
}
 
Example 7
@Test
public void listDir() throws Exception {
    assumeThat(revision, instanceOf(AbstractGitSCMSource.SCMRevisionImpl.class));
    assumeThat(((AbstractGitSCMSource.SCMRevisionImpl) revision).getHash(),
            is("c0e024f89969b976da165eecaa71e09dc60c3da1"));
    SCMFileSystem fs = SCMFileSystem.of(source, master, revision);
    assertThat(fs.getRoot().child("fu").children(),
            hasItem(Matchers.<SCMFile>hasProperty("name", is("manchu.txt"))));
}
 
Example 8
@Test
public void resolveDirPRInvalidMerge() throws Exception {
    assumeThat(revision, nullValue());

    assertThat(prMergeInvalidRevision.isMerge(), is(true));

    SCMFileSystem fs = SCMFileSystem.of(source, prMerge, prMergeInvalidRevision);
    assertThat(fs, nullValue());
}
 
Example 9
@Test(expected = AbortException.class)
public void resolveDirPRNotMergeable() throws Exception {
    assumeThat(revision, nullValue());

    assertThat(prMergeNotMergeableRevision.isMerge(), is(true));

    SCMFileSystem fs = SCMFileSystem.of(source, prMerge, prMergeNotMergeableRevision);
    fail();
}
 
Example 10
@Override
public SCMFileSystem build(@Nonnull SCMSource source, @Nonnull SCMHead head, @CheckForNull SCMRevision rev)
        throws IOException, InterruptedException {
    if (source instanceof GitHubSCMSource) {
        GitHubSCMSource ghSCMSource = (GitHubSCMSource) source;
        GHRepository remoteRepo = ghSCMSource.getRemoteRepo();
        return new GitHubSCMFileSystem(remoteRepo, (GitHubSCMHead<?>) head, (GitHubSCMRevision) rev);
    }
    return null;
}
 
Example 11
Source Project: gitlab-branch-source-plugin   Source File: GitLabSCMFileSystem.java    License: MIT License 4 votes vote down vote up
@Override
public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm,
    @CheckForNull SCMRevision rev) {
    return null;
}
 
Example 12
@Override
public FlowExecution create(FlowExecutionOwner owner, TaskListener listener,
                            List<? extends Action> actions) throws Exception {
    Queue.Executable exec = owner.getExecutable();
    if (!(exec instanceof WorkflowRun)) {
        throw new IllegalStateException("inappropriate context");
    }

    WorkflowRun build = (WorkflowRun) exec;
    WorkflowJob job = build.getParent();
    BranchJobProperty property = job.getProperty(BranchJobProperty.class);

    Branch branch = property.getBranch();
    ItemGroup<?> parent = job.getParent();

    if (!(parent instanceof WorkflowMultiBranchProject)) {
        throw new IllegalStateException("inappropriate context");
    }

    SCMSource scmSource = ((WorkflowMultiBranchProject) parent).getSCMSource(branch.getSourceId());

    if (scmSource == null) {
        throw new IllegalStateException(branch.getSourceId() + " not found");
    }

    GitConfig gitConfig = new GitConfig();

    SCMHead head = branch.getHead();

    if ("Pull Request".equals(head.getPronoun())) {
        ChangeRequestSCMHead2 changeRequestSCMHead2 = (ChangeRequestSCMHead2) branch.getHead();
        head = changeRequestSCMHead2.getTarget();
    }

    SCMRevision tip = scmSource.fetch(head, listener);

    if (tip == null) {
        throw new IllegalStateException("Cannot determine the revision.");
    }

    SCMRevision rev = scmSource.getTrustedRevision(tip, listener);
    GitSCM gitSCM = (GitSCM) scmSource.build(head, rev);

    gitConfig.setGitUrl(gitSCM.getUserRemoteConfigs().get(0).getUrl());
    gitConfig.setCredentialsId(gitSCM.getUserRemoteConfigs().get(0).getCredentialsId());
    gitConfig.setGitBranch(head.getName());

    String script;
    try (SCMFileSystem fs = SCMFileSystem.of(scmSource, head, rev)) {
        if (fs != null) {
            InputStream yamlInputStream = fs.child(scriptPath).content();
            listener.getLogger().println("Path of yaml/yml config file: " + fs.child(scriptPath).getPath());
            YamlToPipeline y = new YamlToPipeline();
            script = y.generatePipeline(yamlInputStream, gitConfig, listener);
        } else {
            throw new IOException("SCM not supported");
            // FIXME implement full checkout
        }
    }

    listener.getLogger().println(script);
    return new CpsFlowExecution(script, false, owner);
}
 
Example 13
Source Project: gitea-plugin   Source File: GiteaSCMFileSystem.java    License: MIT License 4 votes vote down vote up
@Override
public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) {
    return null;
}
 
Example 14
Source Project: gitea-plugin   Source File: GiteaSCMFileSystem.java    License: MIT License 4 votes vote down vote up
@Override
public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @CheckForNull SCMRevision rev)
        throws IOException, InterruptedException {
    GiteaSCMSource src = (GiteaSCMSource) source;
    String repoOwner;
    String repository;
    String ref;
    if (head instanceof PullRequestSCMHead) {
        repoOwner = ((PullRequestSCMHead) head).getOriginOwner();
        repository = ((PullRequestSCMHead) head).getOriginRepository();
        ref = ((PullRequestSCMHead) head).getOriginName();
    } else if (head instanceof BranchSCMHead) {
        repoOwner = src.getRepoOwner();
        repository = src.getRepository();
        ref = head.getName();
    } else if (head instanceof TagSCMHead) {
        repoOwner = src.getRepoOwner();
        repository = src.getRepository();
        ref = head.getName();
    } else {
        return null;
    }
    SCMSourceOwner owner = source.getOwner();
    String serverUrl = src.getServerUrl();
    String credentialsId = src.getCredentialsId();
    StandardCredentials credentials = StringUtils.isBlank(credentialsId)
            ? null
            : CredentialsMatchers.firstOrNull(
            CredentialsProvider.lookupCredentials(
                    StandardCredentials.class,
                    owner,
                    Jenkins.getAuthentication(),
                    URIRequirementBuilder.fromUri(serverUrl).build()
            ),
            CredentialsMatchers.allOf(
                    AuthenticationTokens.matcher(GiteaAuth.class),
                    CredentialsMatchers.withId(credentialsId)
            )
    );
    if (owner != null) {
        CredentialsProvider.track(owner, credentials);
    }
    GiteaConnection connection = Gitea.server(serverUrl)
            .as(AuthenticationTokens.convert(GiteaAuth.class, credentials))
            .open();
    try {
        return new GiteaSCMFileSystem(connection, connection.fetchRepository(repoOwner, repository), ref, rev);
    } catch (IOException | InterruptedException e) {
        try {
            connection.close();
        } catch (IOException ioe) {
            e.addSuppressed(ioe);
        }
        throw e;
    }
}
 
Example 15
Source Project: github-branch-source-plugin   Source File: GitHubSCMFileSystem.java    License: MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) {
    return null;
}
 
Example 16
@Test
public void haveFilesystem() throws Exception {
    assertThat(SCMFileSystem.of(source, master, revision), notNullValue());
}
 
Example 17
@Test
public void rootIsADirectory() throws Exception {
    SCMFileSystem fs = SCMFileSystem.of(source, master, revision);
    assertThat(fs.getRoot().getType(), is(SCMFile.Type.DIRECTORY));

}
 
Example 18
@Test
public void listFilesInRoot() throws Exception {
    SCMFileSystem fs = SCMFileSystem.of(source, master, revision);
    assertThat(fs.getRoot().children(), hasItem(Matchers.<SCMFile>hasProperty("name", is("README.md"))));
}
 
Example 19
@Test
public void readmeIsAFile() throws Exception {
    SCMFileSystem fs = SCMFileSystem.of(source, master, revision);
    assertThat(fs.getRoot().child("README.md").getType(), is(SCMFile.Type.REGULAR_FILE));
}
 
Example 20
@Test
public void readmeContents() throws Exception {
    SCMFileSystem fs = SCMFileSystem.of(source, master, revision);
    assertThat(fs.getRoot().child("README.md").contentAsString(), containsString("yolo"));
}
 
Example 21
@Override
public SCMFileSystem build(@Nonnull Item owner, @Nonnull SCM scm, @CheckForNull SCMRevision rev)
        throws IOException, InterruptedException {
    // I suppose we could inspect GitSCM for branch configuration
    return null;
}