Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@
<version>3.24.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.35.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-durable-task-step</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-stage-step</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.jenkins.plugins.checks.github;

import hudson.model.Action;
import hudson.model.Result;
import java.util.Collections;

import jenkins.model.ParameterizedJobMixIn;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Rule;
import org.junit.Test;

import static org.assertj.core.api.Assertions.*;

import hudson.model.FreeStyleProject;
import hudson.model.Run;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
import org.jvnet.hudson.test.JenkinsRule;

/**
* Integration tests for {@link GitSCMChecksContext}.
*/
public class GitSCMChecksContextITest {
private static final String EXISTING_HASH = "4ecc8623b06d99d5f029b66927438554fdd6a467";
private static final String HTTP_URL = "https://github.com/jenkinsci/github-checks-plugin.git";
private static final String CREDENTIALS_ID = "credentials";
private static final String URL_NAME = "url";

@Rule
public JenkinsRule j = new JenkinsRule();

/**
* Creates a FreeStyle job that uses {@link hudson.plugins.git.GitSCM} and runs a successful build.
* Then this build is used to create a new {@link GitSCMChecksContext}. So the build actually is not publishing
* the checks we just ensure that we can create the context with the successful build (otherwise we would need
* Wiremock to handle the requests to GitHub).
*/
@Test
public void shouldRetrieveContextFromFreeStyleBuild() throws Exception {
FreeStyleProject job = j.createFreeStyleProject();

BranchSpec branchSpec = new BranchSpec(EXISTING_HASH);
GitSCM scm = new GitSCM(GitSCM.createRepoList(HTTP_URL, CREDENTIALS_ID),
Collections.singletonList(branchSpec), false, Collections.emptyList(),
null, null, Collections.emptyList());
job.setScm(scm);

Run<?, ?> run = buildSuccessfully(job);

GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME);

assertThat(gitSCMChecksContext.getRepository()).isEqualTo("jenkinsci/github-checks-plugin");
assertThat(gitSCMChecksContext.getHeadSha()).isEqualTo(EXISTING_HASH);
assertThat(gitSCMChecksContext.getCredentialsId()).isEqualTo(CREDENTIALS_ID);
}

private Run<?, ?> buildSuccessfully(ParameterizedJobMixIn.ParameterizedJob<?, ?> job) throws Exception {
return j.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0, new Action[0]));
}

/**
* Creates a pipeline that uses {@link hudson.plugins.git.GitSCM} and runs a successful build.
* Then this build is used to create a new {@link GitSCMChecksContext}.
*/
@Test
public void shouldRetrieveContextFromPipeline() throws Exception {
WorkflowJob job = j.createProject(WorkflowJob.class);

job.setDefinition(new CpsFlowDefinition("node {\n"
+ " stage ('Checkout') {\n"
+ " checkout scm: ([\n"
+ " $class: 'GitSCM',\n"
+ " userRemoteConfigs: [[credentialsId: '" + CREDENTIALS_ID + "', url: '" + HTTP_URL + "']],\n"
+ " branches: [[name: '" + EXISTING_HASH + "']]\n"
+ " ])"
+ " }\n"
+ "}\n", true));

Run<?, ?> run = buildSuccessfully(job);

GitSCMChecksContext gitSCMChecksContext = new GitSCMChecksContext(run, URL_NAME);

assertThat(gitSCMChecksContext.getRepository()).isEqualTo("jenkinsci/github-checks-plugin");
assertThat(gitSCMChecksContext.getCredentialsId()).isEqualTo(CREDENTIALS_ID);
assertThat(gitSCMChecksContext.getHeadSha()).isEqualTo(EXISTING_HASH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.jenkins.plugins.checks.github.config;

import hudson.util.StreamTaskListener;
import io.jenkins.plugins.checks.github.GitHubChecksPublisherFactory;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import org.jvnet.hudson.test.JenkinsRule;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration test for {@link GitHubChecksConfig}.
*/
public class GitHubChecksConfigITest {

@Rule
public JenkinsRule j = new JenkinsRule();

/**
* When a job has not {@link org.jenkinsci.plugins.github_branch_source.GitHubSCMSource} or
* {@link hudson.plugins.git.GitSCM}, the default config should be used and no verbose log should be output.
*/
@Test
public void shouldUseDefaultConfigWhenNoSCM() throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
GitHubChecksPublisherFactory.fromJob(j.createFreeStyleProject(), new StreamTaskListener(os));

assertThat(os.toString()).doesNotContain("Causes for no suitable publisher found: ");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.jenkins.plugins.checks.github.status;

import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMSourceCriteria;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMSourceContext;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class GitHubSCMSourceStatusChecksTraitTest {
@Test
void shouldOnlyApplyTraitConfigurationsToGitHubBranchSourceNotificationsWhenItsNotDisabled() {
GitHubSCMSourceContext context = new GitHubSCMSourceContext(mock(SCMSourceCriteria.class),
mock(SCMHeadObserver.class));
GitHubSCMSourceStatusChecksTrait trait = new GitHubSCMSourceStatusChecksTrait();

// disable notifications, the trait configuration should be ignored
context.withNotificationsDisabled(true);
trait.setSkipNotifications(false);
trait.decorateContext(context);
assertThat(context.notificationsDisabled()).isTrue();

// enable notifications, the trait configuration should be applied
context.withNotificationsDisabled(false);
trait.setSkipNotifications(true);
trait.decorateContext(context);
assertThat(context.notificationsDisabled()).isTrue();
}
}
107 changes: 107 additions & 0 deletions src/test/resources/__files/create-access-token.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"token": "bogus",
"expires_at": "2019-08-10T05:54:58Z",
"permissions": {
"checks": "write",
"pull_requests": "write",
"contents": "read",
"metadata": "read"
},
"repository_selection": "selected",
"repositories": [
{
"id": 111111111,
"node_id": "asdfasdf",
"name": "bogus",
"full_name": "bogus/bogus",
"private": true,
"owner": {
"login": "bogus",
"id": 11111111,
"node_id": "asdfasdf",
"avatar_url": "https://avatars2.githubusercontent.com/u/11111111?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bogus",
"html_url": "https://github.com/bogus",
"followers_url": "https://api.github.com/users/bogus/followers",
"following_url": "https://api.github.com/users/bogus/following{/other_user}",
"gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
"organizations_url": "https://api.github.com/users/bogus/orgs",
"repos_url": "https://api.github.com/users/bogus/repos",
"events_url": "https://api.github.com/users/bogus/events{/privacy}",
"received_events_url": "https://api.github.com/users/bogus/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/bogus/bogus",
"description": null,
"fork": false,
"url": "https://api.github.com/repos/bogus/bogus",
"forks_url": "https://api.github.com/repos/bogus/bogus/forks",
"keys_url": "https://api.github.com/repos/bogus/bogus/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/bogus/bogus/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/bogus/bogus/teams",
"hooks_url": "https://api.github.com/repos/bogus/bogus/hooks",
"issue_events_url": "https://api.github.com/repos/bogus/bogus/issues/events{/number}",
"events_url": "https://api.github.com/repos/bogus/bogus/events",
"assignees_url": "https://api.github.com/repos/bogus/bogus/assignees{/user}",
"branches_url": "https://api.github.com/repos/bogus/bogus/branches{/branch}",
"tags_url": "https://api.github.com/repos/bogus/bogus/tags",
"blobs_url": "https://api.github.com/repos/bogus/bogus/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/bogus/bogus/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/bogus/bogus/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/bogus/bogus/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/bogus/bogus/statuses/{sha}",
"languages_url": "https://api.github.com/repos/bogus/bogus/languages",
"stargazers_url": "https://api.github.com/repos/bogus/bogus/stargazers",
"contributors_url": "https://api.github.com/repos/bogus/bogus/contributors",
"subscribers_url": "https://api.github.com/repos/bogus/bogus/subscribers",
"subscription_url": "https://api.github.com/repos/bogus/bogus/subscription",
"commits_url": "https://api.github.com/repos/bogus/bogus/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/bogus/bogus/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/bogus/bogus/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/bogus/bogus/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/bogus/bogus/contents/{+path}",
"compare_url": "https://api.github.com/repos/bogus/bogus/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/bogus/bogus/merges",
"archive_url": "https://api.github.com/repos/bogus/bogus/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/bogus/bogus/downloads",
"issues_url": "https://api.github.com/repos/bogus/bogus/issues{/number}",
"pulls_url": "https://api.github.com/repos/bogus/bogus/pulls{/number}",
"milestones_url": "https://api.github.com/repos/bogus/bogus/milestones{/number}",
"notifications_url": "https://api.github.com/repos/bogus/bogus/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/bogus/bogus/labels{/name}",
"releases_url": "https://api.github.com/repos/bogus/bogus/releases{/id}",
"deployments_url": "https://api.github.com/repos/bogus/bogus/deployments",
"created_at": "2018-09-06T03:25:38Z",
"updated_at": "2018-09-30T22:04:06Z",
"pushed_at": "2019-08-08T22:22:34Z",
"git_url": "git://github.com/bogus/bogus.git",
"ssh_url": "[email protected]:bogus/bogus.git",
"clone_url": "https://github.com/bogus/bogus.git",
"svn_url": "https://github.com/bogus/bogus",
"homepage": null,
"size": 618,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Java",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 5,
"license": null,
"forks": 0,
"open_issues": 5,
"watchers": 0,
"default_branch": "main"
}
]
}
41 changes: 41 additions & 0 deletions src/test/resources/__files/get-app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"id": 11111,
"node_id": "MDM6QXBwMzI2MTY=",
"owner": {
"login": "bogus",
"id": 111111111,
"node_id": "asdfasdfasdf",
"avatar_url": "https://avatars2.githubusercontent.com/u/111111111?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bogus",
"html_url": "https://github.com/bogus",
"followers_url": "https://api.github.com/users/bogus/followers",
"following_url": "https://api.github.com/users/bogus/following{/other_user}",
"gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
"organizations_url": "https://api.github.com/users/bogus/orgs",
"repos_url": "https://api.github.com/users/bogus/repos",
"events_url": "https://api.github.com/users/bogus/events{/privacy}",
"received_events_url": "https://api.github.com/users/bogus/received_events",
"type": "Organization",
"site_admin": false
},
"name": "Bogus-Development",
"description": "",
"external_url": "https://bogus.domain.com",
"html_url": "https://github.com/apps/bogus-development",
"created_at": "2019-06-10T04:21:41Z",
"updated_at": "2019-06-10T04:21:41Z",
"permissions": {
"checks": "write",
"contents": "read",
"metadata": "read",
"pull_requests": "write"
},
"events": [
"pull_request",
"push"
],
"installations_count": 1
}
45 changes: 45 additions & 0 deletions src/test/resources/__files/list-installations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[
{
"id": 11111111,
"account": {
"login": "bogus",
"id": 111111111,
"node_id": "asdfasdfasdf",
"avatar_url": "https://avatars2.githubusercontent.com/u/111111111?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bogus",
"html_url": "https://github.com/bogus",
"followers_url": "https://api.github.com/users/bogus/followers",
"following_url": "https://api.github.com/users/bogus/following{/other_user}",
"gists_url": "https://api.github.com/users/bogus/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bogus/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bogus/subscriptions",
"organizations_url": "https://api.github.com/users/bogus/orgs",
"repos_url": "https://api.github.com/users/bogus/repos",
"events_url": "https://api.github.com/users/bogus/events{/privacy}",
"received_events_url": "https://api.github.com/users/bogus/received_events",
"type": "Organization",
"site_admin": false
},
"repository_selection": "selected",
"access_tokens_url": "https://api.github.com/app/installations/11111111/access_tokens",
"repositories_url": "https://api.github.com/installation/repositories",
"html_url": "https://github.com/organizations/bogus/settings/installations/11111111",
"app_id": 11111,
"target_id": 111111111,
"target_type": "Organization",
"permissions": {
"checks": "write",
"pull_requests": "write",
"contents": "read",
"metadata": "read"
},
"events": [
"pull_request",
"push"
],
"created_at": "2019-07-04T01:19:36.000Z",
"updated_at": "2019-07-30T22:48:09.000Z",
"single_file_name": null
}
]
Loading