Skip to content

Commit 42469ba

Browse files
authored
Merge pull request #1260 from JLLeitschuh/fix/JLL/project_card_repository
Support getting repository from project card
2 parents 0b8c6e3 + c25068e commit 42469ba

File tree

58 files changed

+2565
-289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2565
-289
lines changed

src/main/java/org/kohsuke/github/GHIssue.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,32 @@ GHIssue wrap(GHRepository owner) {
7979
return this;
8080
}
8181

82+
private String getRepositoryUrlPath() {
83+
String url = getUrl().toString();
84+
int index = url.indexOf("/issues");
85+
if (index == -1) {
86+
index = url.indexOf("/pulls");
87+
}
88+
return url.substring(0, index);
89+
}
90+
8291
/**
8392
* Repository to which the issue belongs.
8493
*
8594
* @return the repository
8695
*/
8796
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
8897
public GHRepository getRepository() {
98+
try {
99+
synchronized (this) {
100+
if (owner == null) {
101+
String repositoryUrlPath = getRepositoryUrlPath();
102+
wrap(root().createRequest().withUrlPath(repositoryUrlPath).fetch(GHRepository.class));
103+
}
104+
}
105+
} catch (IOException e) {
106+
throw new GHException("Failed to fetch repository", e);
107+
}
89108
return owner;
90109
}
91110

@@ -595,7 +614,8 @@ protected String getIssuesApiRoute() {
595614
final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
596615
return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/");
597616
}
598-
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/issues/" + number;
617+
GHRepository repo = getRepository();
618+
return "/repos/" + repo.getOwnerName() + "/" + repo.getName() + "/issues/" + number;
599619
}
600620

601621
/**
@@ -740,7 +760,7 @@ protected static List<String> getLogins(Collection<GHUser> users) {
740760
*/
741761
public PagedIterable<GHIssueEvent> listEvents() throws IOException {
742762
return root().createRequest()
743-
.withUrlPath(owner.getApiTailUrl(String.format("/issues/%s/events", number)))
763+
.withUrlPath(getRepository().getApiTailUrl(String.format("/issues/%s/events", number)))
744764
.toIterable(GHIssueEvent[].class, item -> item.wrapUp(this));
745765
}
746766
}

src/main/java/org/kohsuke/github/GHProjectCard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public GHProjectColumn getColumn() throws IOException {
115115
}
116116

117117
/**
118-
* Gets content.
118+
* Gets content if present. Might be a {@link GHPullRequest} or a {@link GHIssue}.
119119
*
120120
* @return the content
121121
* @throws IOException

src/main/java/org/kohsuke/github/GHProjectColumn.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ public GHProjectCard createCard(String note) throws IOException {
186186
* the io exception
187187
*/
188188
public GHProjectCard createCard(GHIssue issue) throws IOException {
189+
String contentType = issue instanceof GHPullRequest ? "PullRequest" : "Issue";
189190
return root().createRequest()
190191
.method("POST")
191192
.withPreview(INERTIA)
192-
.with("content_type", issue instanceof GHPullRequest ? "PullRequest" : "Issue")
193+
.with("content_type", contentType)
193194
.with("content_id", issue.getId())
194195
.withUrlPath(String.format("/projects/columns/%d/cards", getId()))
195196
.fetch(GHProjectCard.class)

src/test/java/org/kohsuke/github/GHProjectCardTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,34 @@ public void testCreateCardFromIssue() throws IOException {
5555
GHIssue issue = repo.createIssue("new-issue").body("With body").create();
5656
GHProjectCard card = column.createCard(issue);
5757
assertThat(card.getContentUrl(), equalTo(issue.getUrl()));
58+
assertThat(card.getContent().getUrl(), equalTo(issue.getUrl()));
59+
assertThat(card.getContent().getRepository().getUrl(), equalTo(repo.getUrl()));
60+
} finally {
61+
repo.delete();
62+
}
63+
}
64+
65+
@Test
66+
public void testCreateCardFromPR() throws IOException {
67+
GHRepository repo = org.createRepository("repo-for-project-card").autoInit(true).create();
68+
69+
try {
70+
String mainHead = repo.getRef("heads/main").getObject().getSha();
71+
String branchName1 = "refs/heads/branch1";
72+
repo.createRef(branchName1, mainHead);
73+
repo.createContent()
74+
.content(branchName1)
75+
.message(branchName1)
76+
.path(branchName1)
77+
.branch(branchName1)
78+
.commit();
79+
GHPullRequest pr = repo.createPullRequest("new-PR", branchName1, "refs/heads/main", "Body Text");
80+
GHProjectCard card = column.createCard(pr);
81+
// For some reason, PRs are still treated as issues in project card urls
82+
assertThat(card.getContentUrl().toString(), equalTo(pr.getUrl().toString().replace("/pulls/", "/issues/")));
83+
assertThat(card.getContent().getUrl().toString(),
84+
equalTo(pr.getUrl().toString().replace("/pulls/", "/issues/")));
85+
assertThat(card.getContent().getRepository().getUrl(), equalTo(repo.getUrl()));
5886
} finally {
5987
repo.delete();
6088
}

src/test/resources/org/kohsuke/github/GHProjectCardTest/wiremock/testCreateCardFromIssue/__files/orgs_hub4j-test-org-2.json

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,46 @@
99
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
1010
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
1111
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12-
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13-
"description": null,
12+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
13+
"description": "Hub4j Test Org Description (this could be null or blank too)",
14+
"name": "Hub4j Test Org Name (this could be null or blank too)",
15+
"company": null,
16+
"blog": "https://hub4j.url.io/could/be/null",
17+
"location": "Hub4j Test Org Location (this could be null or blank too)",
18+
"email": "[email protected]",
19+
"twitter_username": null,
1420
"is_verified": false,
1521
"has_organization_projects": true,
1622
"has_repository_projects": true,
17-
"public_repos": 9,
23+
"public_repos": 20,
1824
"public_gists": 0,
1925
"followers": 0,
2026
"following": 0,
2127
"html_url": "https://github.com/hub4j-test-org",
2228
"created_at": "2014-05-10T19:39:11Z",
23-
"updated_at": "2015-04-20T00:42:30Z",
29+
"updated_at": "2020-06-04T05:56:10Z",
2430
"type": "Organization",
25-
"total_private_repos": 0,
26-
"owned_private_repos": 0,
31+
"total_private_repos": 2,
32+
"owned_private_repos": 2,
2733
"private_gists": 0,
28-
"disk_usage": 132,
34+
"disk_usage": 11979,
2935
"collaborators": 0,
3036
"billing_email": "[email protected]",
3137
"default_repository_permission": "none",
3238
"members_can_create_repositories": false,
3339
"two_factor_requirement_enabled": false,
40+
"members_allowed_repository_creation_type": "none",
41+
"members_can_create_public_repositories": false,
42+
"members_can_create_private_repositories": false,
43+
"members_can_create_internal_repositories": false,
44+
"members_can_create_pages": true,
45+
"members_can_create_public_pages": true,
46+
"members_can_create_private_pages": true,
3447
"plan": {
3548
"name": "free",
3649
"space": 976562499,
37-
"private_repos": 0,
38-
"filled_seats": 4,
39-
"seats": 0
50+
"private_repos": 10000,
51+
"filled_seats": 29,
52+
"seats": 3
4053
}
4154
}

src/test/resources/org/kohsuke/github/GHProjectCardTest/wiremock/testCreateCardFromIssue/__files/orgs_hub4j-test-org_projects-3.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"owner_url": "https://api.github.com/orgs/hub4j-test-org",
3-
"url": "https://api.github.com/projects/3312449",
4-
"html_url": "https://github.com/orgs/hub4j-test-org/projects/31",
5-
"columns_url": "https://api.github.com/projects/3312449/columns",
6-
"id": 3312449,
7-
"node_id": "MDc6UHJvamVjdDMzMTI0NDk=",
3+
"url": "https://api.github.com/projects/13495086",
4+
"html_url": "https://github.com/orgs/hub4j-test-org/projects/32",
5+
"columns_url": "https://api.github.com/projects/13495086/columns",
6+
"id": 13495086,
7+
"node_id": "PRO_kwDOAHMfo84Azesu",
88
"name": "test-project",
99
"body": "This is a test project",
10-
"number": 31,
10+
"number": 32,
1111
"state": "open",
1212
"creator": {
1313
"login": "bitwiseman",
@@ -29,8 +29,8 @@
2929
"type": "User",
3030
"site_admin": false
3131
},
32-
"created_at": "2019-10-04T17:25:11Z",
33-
"updated_at": "2019-10-04T17:25:11Z",
32+
"created_at": "2021-10-11T15:43:37Z",
33+
"updated_at": "2021-10-11T15:43:37Z",
3434
"organization_permission": "write",
3535
"private": true
36-
}
36+
}

src/test/resources/org/kohsuke/github/GHProjectCardTest/wiremock/testCreateCardFromIssue/__files/orgs_hub4j-test-org_repos-6.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
"id": 212868194,
3-
"node_id": "MDEwOlJlcG9zaXRvcnkyMTI4NjgxOTQ=",
2+
"id": 415990154,
3+
"node_id": "R_kgDOGMuBig",
44
"name": "repo-for-project-card",
55
"full_name": "hub4j-test-org/repo-for-project-card",
66
"private": false,
77
"owner": {
88
"login": "hub4j-test-org",
99
"id": 7544739,
1010
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11-
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
1212
"gravatar_id": "",
1313
"url": "https://api.github.com/users/hub4j-test-org",
1414
"html_url": "https://github.com/hub4j-test-org",
@@ -64,9 +64,9 @@
6464
"labels_url": "https://api.github.com/repos/hub4j-test-org/repo-for-project-card/labels{/name}",
6565
"releases_url": "https://api.github.com/repos/hub4j-test-org/repo-for-project-card/releases{/id}",
6666
"deployments_url": "https://api.github.com/repos/hub4j-test-org/repo-for-project-card/deployments",
67-
"created_at": "2019-10-04T17:25:12Z",
68-
"updated_at": "2019-10-04T17:25:12Z",
69-
"pushed_at": "2019-10-04T17:25:13Z",
67+
"created_at": "2021-10-11T15:43:39Z",
68+
"updated_at": "2021-10-11T15:43:39Z",
69+
"pushed_at": "2021-10-11T15:43:39Z",
7070
"git_url": "git://github.com/hub4j-test-org/repo-for-project-card.git",
7171
"ssh_url": "[email protected]:hub4j-test-org/repo-for-project-card.git",
7272
"clone_url": "https://github.com/hub4j-test-org/repo-for-project-card.git",
@@ -87,23 +87,31 @@
8787
"disabled": false,
8888
"open_issues_count": 0,
8989
"license": null,
90+
"allow_forking": true,
91+
"is_template": false,
92+
"topics": [],
93+
"visibility": "public",
9094
"forks": 0,
9195
"open_issues": 0,
9296
"watchers": 0,
9397
"default_branch": "main",
9498
"permissions": {
9599
"admin": true,
100+
"maintain": true,
96101
"push": true,
102+
"triage": true,
97103
"pull": true
98104
},
99105
"allow_squash_merge": true,
100106
"allow_merge_commit": true,
101107
"allow_rebase_merge": true,
108+
"allow_auto_merge": false,
109+
"delete_branch_on_merge": false,
102110
"organization": {
103111
"login": "hub4j-test-org",
104112
"id": 7544739,
105113
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
106-
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
114+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
107115
"gravatar_id": "",
108116
"url": "https://api.github.com/users/hub4j-test-org",
109117
"html_url": "https://github.com/hub4j-test-org",
@@ -120,5 +128,5 @@
120128
"site_admin": false
121129
},
122130
"network_count": 0,
123-
"subscribers_count": 2
131+
"subscribers_count": 0
124132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"url": "https://api.github.com/projects/columns/16361848",
3+
"project_url": "https://api.github.com/projects/13495086",
4+
"cards_url": "https://api.github.com/projects/columns/16361848/cards",
5+
"id": 16361848,
6+
"node_id": "PC_lAPOAHMfo84AzesuzgD5qXg",
7+
"name": "column-one",
8+
"created_at": "2021-10-11T15:43:38Z",
9+
"updated_at": "2021-10-11T15:43:38Z"
10+
}

src/test/resources/org/kohsuke/github/GHProjectCardTest/wiremock/testCreateCardFromIssue/__files/projects_3312449_columns-4.json

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"url": "https://api.github.com/projects/columns/cards/27353274",
3-
"project_url": "https://api.github.com/projects/3312449",
4-
"id": 27353274,
5-
"node_id": "MDExOlByb2plY3RDYXJkMjczNTMyNzQ=",
2+
"url": "https://api.github.com/projects/columns/cards/70575561",
3+
"project_url": "https://api.github.com/projects/13495086",
4+
"id": 70575561,
5+
"node_id": "PRC_lADOAHMfo84AzesuzgQ05ck",
66
"note": "This is a card",
77
"archived": false,
88
"creator": {
@@ -25,7 +25,7 @@
2525
"type": "User",
2626
"site_admin": false
2727
},
28-
"created_at": "2019-10-04T17:25:11Z",
29-
"updated_at": "2019-10-04T17:25:11Z",
30-
"column_url": "https://api.github.com/projects/columns/6706803"
31-
}
28+
"created_at": "2021-10-11T15:43:38Z",
29+
"updated_at": "2021-10-11T15:43:38Z",
30+
"column_url": "https://api.github.com/projects/columns/16361848"
31+
}

0 commit comments

Comments
 (0)