Skip to content

Commit 7e1e5b4

Browse files
authored
Merge branch 'main' into feature/issue-1597-add-listCodeownersErrors
2 parents a4c05f9 + 1dcedba commit 7e1e5b4

29 files changed

+1239
-9
lines changed

.github/workflows/maven-build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
uses: actions/setup-java@v3
3232
with:
3333
java-version: ${{ matrix.java }}
34-
distribution: 'zulu'
34+
distribution: 'temurin'
3535
cache: 'maven'
3636
- name: Maven Install (skipTests)
3737
env:
@@ -50,7 +50,7 @@ jobs:
5050
uses: actions/setup-java@v3
5151
with:
5252
java-version: ${{ matrix.java }}
53-
distribution: 'adopt'
53+
distribution: 'temurin'
5454
cache: 'maven'
5555
- name: Maven Site
5656
env:
@@ -70,7 +70,7 @@ jobs:
7070
uses: actions/setup-java@v3
7171
with:
7272
java-version: ${{ matrix.java }}
73-
distribution: 'zulu'
73+
distribution: 'temurin'
7474
cache: 'maven'
7575
# JDK 8
7676
- name: Maven Install with Code Coverage
@@ -91,7 +91,7 @@ jobs:
9191
uses: actions/setup-java@v3
9292
with:
9393
java-version: ${{ matrix.java }}
94-
distribution: 'zulu'
94+
distribution: 'temurin'
9595
cache: 'maven'
9696
# JDK 11+
9797
- name: Maven Install without Code Coverage

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@
278278
<plugin>
279279
<groupId>org.apache.maven.plugins</groupId>
280280
<artifactId>maven-project-info-reports-plugin</artifactId>
281-
<version>3.4.1</version>
281+
<version>3.4.2</version>
282282
<dependencies>
283283
<dependency>
284284
<groupId>org.apache.bcel</groupId>
@@ -453,6 +453,12 @@
453453
<version>${hamcrest.version}</version>
454454
<scope>test</scope>
455455
</dependency>
456+
<dependency>
457+
<groupId>com.github.npathai</groupId>
458+
<artifactId>hamcrest-optional</artifactId>
459+
<version>2.0.0</version>
460+
<scope>test</scope>
461+
</dependency>
456462
<dependency>
457463
<groupId>junit</groupId>
458464
<artifactId>junit</artifactId>
@@ -485,7 +491,7 @@
485491
<dependency>
486492
<groupId>commons-fileupload</groupId>
487493
<artifactId>commons-fileupload</artifactId>
488-
<version>1.4</version>
494+
<version>1.5</version>
489495
<scope>test</scope>
490496
</dependency>
491497
<!-- for stapler-jetty -->

squashMerge

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.apache.commons.lang3.builder.ToStringBuilder;
44

55
import java.io.IOException;
6+
import java.util.Date;
67

78
// TODO: Auto-generated Javadoc
89
/**
@@ -20,6 +21,18 @@ public class GHDeployKey {
2021
protected long id;
2122
private GHRepository owner;
2223

24+
/** Creation date of the deploy key */
25+
private String created_at;
26+
27+
/** Last used date of the deploy key */
28+
private String last_used;
29+
30+
/** Name of user that added the deploy key */
31+
private String added_by;
32+
33+
/** Whether the deploykey has readonly permission or full access */
34+
private boolean read_only;
35+
2336
/**
2437
* Gets id.
2538
*
@@ -65,6 +78,42 @@ public boolean isVerified() {
6578
return verified;
6679
}
6780

81+
/**
82+
* Gets created_at.
83+
*
84+
* @return the created_at
85+
*/
86+
public Date getCreatedAt() {
87+
return GitHubClient.parseDate(created_at);
88+
}
89+
90+
/**
91+
* Gets last_used.
92+
*
93+
* @return the last_used
94+
*/
95+
public Date getLastUsedAt() {
96+
return GitHubClient.parseDate(last_used);
97+
}
98+
99+
/**
100+
* Gets added_by
101+
*
102+
* @return the added_by
103+
*/
104+
public String getAdded_by() {
105+
return added_by;
106+
}
107+
108+
/**
109+
* Is read_only
110+
*
111+
* @return true if the key can only read. False if the key has write permission as well.
112+
*/
113+
public boolean isRead_only() {
114+
return read_only;
115+
}
116+
68117
/**
69118
* Wrap gh deploy key.
70119
*
@@ -95,7 +144,14 @@ GHDeployKey lateBind(GHRepository repo) {
95144
* @return the string
96145
*/
97146
public String toString() {
98-
return new ToStringBuilder(this).append("title", title).append("id", id).append("key", key).toString();
147+
return new ToStringBuilder(this).append("title", title)
148+
.append("id", id)
149+
.append("key", key)
150+
.append("created_at", created_at)
151+
.append("last_used", last_used)
152+
.append("added_by", added_by)
153+
.append("read_only", read_only)
154+
.toString();
99155
}
100156

101157
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
44
import org.apache.commons.lang3.builder.ToStringBuilder;
55

6+
import java.io.IOException;
7+
68
// TODO: Auto-generated Javadoc
79
/**
810
* SSH public key.
@@ -74,4 +76,14 @@ public boolean isVerified() {
7476
public String toString() {
7577
return new ToStringBuilder(this).append("title", title).append("id", id).append("key", key).toString();
7678
}
79+
80+
/**
81+
* Delete the GHKey
82+
*
83+
* @throws IOException
84+
* the io exception
85+
*/
86+
public void delete() throws IOException {
87+
root().createRequest().method("DELETE").withUrlPath(String.format("/user/keys/%d", id)).send();
88+
}
7789
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ public List<GHKey> getPublicKeys() throws IOException {
8383
return root().createRequest().withUrlPath("/user/keys").toIterable(GHKey[].class, null).toList();
8484
}
8585

86+
/**
87+
* Add public SSH key for the user.
88+
* <p>
89+
* https://docs.github.com/en/rest/users/keys?apiVersion=2022-11-28#create-a-public-ssh-key-for-the-authenticated-user
90+
*
91+
* @param title
92+
* Title of the SSH key
93+
* @param key
94+
* the public key
95+
* @return the newly created Github key
96+
* @throws IOException
97+
* the io exception
98+
*/
99+
public GHKey addPublicKey(String title, String key) throws IOException {
100+
return root().createRequest()
101+
.withUrlPath("/user/keys")
102+
.method("POST")
103+
.with("title", title)
104+
.with("key", key)
105+
.fetch(GHKey.class);
106+
}
107+
86108
/**
87109
* Returns the read-only list of all the public verified keys of the current user.
88110
* <p>

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,29 @@ public PagedIterable<GHCheckRun> getCheckRuns(String ref) throws IOException {
22102210
return new GHCheckRunsIterable(this, request);
22112211
}
22122212

2213+
/**
2214+
* Gets check runs for given ref which validate provided parameters
2215+
*
2216+
* @param ref
2217+
* the Git reference
2218+
* @param params
2219+
* a map of parameters to filter check runs
2220+
* @return check runs for the given ref
2221+
* @throws IOException
2222+
* the io exception
2223+
* @see <a href="https://developer.github.com/v3/checks/runs/#list-check-runs-for-a-specific-ref">List check runs
2224+
* for a specific ref</a>
2225+
*/
2226+
@Preview(ANTIOPE)
2227+
public PagedIterable<GHCheckRun> getCheckRuns(String ref, Map<String, Object> params) throws IOException {
2228+
GitHubRequest request = root().createRequest()
2229+
.withUrlPath(String.format("/repos/%s/%s/commits/%s/check-runs", getOwnerName(), name, ref))
2230+
.with(params)
2231+
.withPreview(ANTIOPE)
2232+
.build();
2233+
return new GHCheckRunsIterable(this, request);
2234+
}
2235+
22132236
/**
22142237
* Creates a commit status.
22152238
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public void testRequireUseOfOnlySpecificApacheCommons() {
128128
targetMethodIs(ToStringBuilder.class, "append", String.class, Object.class),
129129
targetMethodIs(ToStringBuilder.class, "append", String.class, long.class),
130130
targetMethodIs(ToStringBuilder.class, "append", String.class, int.class),
131+
targetMethodIs(ToStringBuilder.class, "append", String.class, boolean.class),
131132
targetMethodIs(ToStringBuilder.class, "isEmpty"),
132133
targetMethodIs(ToStringBuilder.class, "equals"),
133134
targetMethodIs(ToStringBuilder.class, "capitalize"),
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
import java.time.Instant;
7+
import java.util.Date;
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent;
12+
import static org.hamcrest.Matchers.*;
13+
14+
/**
15+
* The Class GHDeployKeyTest.
16+
*
17+
* @author Jonas van Vliet
18+
*/
19+
public class GHDeployKeyTest extends AbstractGitHubWireMockTest {
20+
private static final String DEPLOY_KEY_TEST_REPO_NAME = "hub4j-test-org/GHDeployKeyTest";
21+
private static final String ED_25519_READONLY = "DeployKey - ed25519 - readonly";
22+
private static final String RSA_4096_READWRITE = "Deploykey - rsa4096 - readwrite";
23+
private static final String KEY_CREATOR_USERNAME = "van-vliet";
24+
25+
/**
26+
* Test get deploymentkeys.
27+
*
28+
* @throws IOException
29+
* Signals that an I/O exception has occurred.
30+
*/
31+
@Test
32+
public void testGetDeployKeys() throws IOException {
33+
final GHRepository repo = getRepository();
34+
final List<GHDeployKey> deployKeys = repo.getDeployKeys();
35+
assertThat("There should be 2 deploykeys in " + DEPLOY_KEY_TEST_REPO_NAME, deployKeys, hasSize(2));
36+
37+
Optional<GHDeployKey> ed25519Key = deployKeys.stream()
38+
.filter(key -> key.getTitle().equals(ED_25519_READONLY))
39+
.findAny();
40+
assertThat("The key exists", ed25519Key, isPresent());
41+
assertThat("The key was created at the specified date",
42+
ed25519Key.get().getCreatedAt(),
43+
is(Date.from(Instant.parse("2023-02-08T10:00:15.00Z"))));
44+
assertThat("The key is created by " + KEY_CREATOR_USERNAME,
45+
ed25519Key.get().getAdded_by(),
46+
is(KEY_CREATOR_USERNAME));
47+
assertThat("The key has a last_used value",
48+
ed25519Key.get().getLastUsedAt(),
49+
is(Date.from(Instant.parse("2023-02-08T10:02:11.00Z"))));
50+
assertThat("The key only has read access", ed25519Key.get().isRead_only(), is(true));
51+
assertThat("Object has a toString()", ed25519Key.get().toString(), is(notNullValue()));
52+
53+
Optional<GHDeployKey> rsa_4096Key = deployKeys.stream()
54+
.filter(key -> key.getTitle().equals(RSA_4096_READWRITE))
55+
.findAny();
56+
assertThat("The key exists", rsa_4096Key, isPresent());
57+
assertThat("The key was created at the specified date",
58+
rsa_4096Key.get().getCreatedAt(),
59+
is(Date.from(Instant.parse("2023-01-26T14:12:12.00Z"))));
60+
assertThat("The key is created by " + KEY_CREATOR_USERNAME,
61+
rsa_4096Key.get().getAdded_by(),
62+
is(KEY_CREATOR_USERNAME));
63+
assertThat("The key has never been used", rsa_4096Key.get().getLastUsedAt(), is(nullValue()));
64+
assertThat("The key only has read/write access", rsa_4096Key.get().isRead_only(), is(false));
65+
}
66+
67+
/**
68+
* Gets the repository.
69+
*
70+
* @return the repository
71+
* @throws IOException
72+
* Signals that an I/O exception has occurred.
73+
*/
74+
protected GHRepository getRepository() throws IOException {
75+
return getRepository(gitHub);
76+
}
77+
78+
private GHRepository getRepository(final GitHub gitHub) throws IOException {
79+
return gitHub.getRepository(DEPLOY_KEY_TEST_REPO_NAME);
80+
}
81+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* The Class GHPublicKeyTest.
7+
*
8+
* @author Jonas van Vliet
9+
*/
10+
public class GHPublicKeyTest extends AbstractGitHubWireMockTest {
11+
12+
private static final String TMP_KEY_NAME = "Temporary user key";
13+
private static final String WIREMOCK_SSH_PUBLIC_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDepW2/BSVFM2AfuGGsvi+vjQzC0EBD3R+/7PNEvP0/nvTWxiC/tthfvvCJR6TKrsprCir5tiJFm73gX+K18W0RKYpkyg8H6d1eZu3q/JOiGvoDPeN8Oe9hOGeeexw1WOiz7ESPHzZYXI981evzHAzxxn8zibr2EryopVNsXyoenw==";
14+
15+
/**
16+
* Test adding a public key to the user
17+
*
18+
* @throws Exception
19+
* the exception
20+
*/
21+
@Test
22+
public void testAddPublicKey() throws Exception {
23+
GHKey newPublicKey = null;
24+
try {
25+
GHMyself me = gitHub.getMyself();
26+
newPublicKey = me.addPublicKey(TMP_KEY_NAME, WIREMOCK_SSH_PUBLIC_KEY);
27+
} finally {
28+
if (newPublicKey != null) {
29+
newPublicKey.delete();
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)