Skip to content

Commit

Permalink
Merge pull request #1610 from michael-s-grant/feature/issue-1597-add-…
Browse files Browse the repository at this point in the history
…listCodeownersErrors

Issue 1597: Tested and implemented GHRepository.listCodeownersErrors()
  • Loading branch information
bitwiseman committed Feb 26, 2023
2 parents 44b184a + 7e1e5b4 commit 24832b1
Show file tree
Hide file tree
Showing 9 changed files with 689 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/main/java/org/kohsuke/github/GHCodeownersError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.kohsuke.github;

/**
* Represents an error in a {@code CODEOWNERS} file. See <a href=
* "https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners">the
* relevant documentation</a>.
*
* @author Michael Grant
*/
public class GHCodeownersError {
private int line, column;

private String kind, source, suggestion, message, path;

/**
* Gets line.
*
* @return the line
*/
public int getLine() {
return line;
}

/**
* Gets column.
*
* @return the column
*/
public int getColumn() {
return column;
}

/**
* Gets kind.
*
* @return the kind
*/
public String getKind() {
return kind;
}

/**
* Gets source.
*
* @return the source
*/
public String getSource() {
return source;
}

/**
* Gets suggestion.
*
* @return the suggestion
*/
public String getSuggestion() {
return suggestion;
}

/**
* Gets message.
*
* @return the message
*/
public String getMessage() {
return message;
}

/**
* Gets path.
*
* @return the path
*/
public String getPath() {
return path;
}
}
19 changes: 19 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2956,6 +2956,25 @@ public GHSubscription getSubscription() throws IOException {
}
}

// Only used within listCodeownersErrors().
private static class GHCodeownersErrors {
public List<GHCodeownersError> errors;
}

/**
* List errors in the {@code CODEOWNERS} file. Note that GitHub skips lines with incorrect syntax; these are
* reported in the web interface, but not in the API call which this library uses.
*
* @return the list of errors
* @throws IOException
* the io exception
*/
public List<GHCodeownersError> listCodeownersErrors() throws IOException {
return root().createRequest()
.withUrlPath(getApiTailUrl("codeowners/errors"))
.fetch(GHCodeownersErrors.class).errors;
}

/**
* List contributors paged iterable.
*
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/org/kohsuke/github/GHCodeownersErrorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.kohsuke.github;

import org.junit.Test;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.Matchers.*;

/**
* Test class for listing errors in CODEOWNERS files.
*
* @author Michael Grant
*/
public class GHCodeownersErrorTest extends AbstractGitHubWireMockTest {

/**
* Gets the {@code CODEOWNERS} errors.
*
* @throws IOException
* the exception
*/
@Test
public void testGetCodeownersErrors() throws IOException {
final GHRepository repo = getRepository(gitHub);
final List<GHCodeownersError> codeownersErrors = repo.listCodeownersErrors();
assertThat(codeownersErrors.size(), is(1));
final GHCodeownersError firstError = codeownersErrors.get(0);
assertThat(firstError.getLine(), is(1));
assertThat(firstError.getColumn(), is(3));
assertThat(firstError.getKind(), is("Unknown owner"));
assertThat(firstError.getSource(),
is("* @nonexistent-user # Deliberate error to test response to repo.listCodeownersErrors()\n"));
assertThat(firstError.getSuggestion(),
is("make sure @nonexistent-user exists and has write access to the repository"));
assertThat(firstError.getMessage(),
is("Unknown owner on line 1: make sure @nonexistent-user exists and has write access to the repository\n\n * @nonexistent-user # Deliberate error to test response to repo.listCodeownersErrors()\n ^"));
assertThat(firstError.getPath(), is(".github/CODEOWNERS"));
}

/**
* Gets the repository.
*
* @return the repository
* @throws IOException
* Signals that an I/O exception has occurred.
*/
protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}

private GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization(GITHUB_API_TEST_ORG).getRepository("github-api");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/hub4j-test-org",
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"description": "Hub4j Test Org Description (this could be null or blank too)",
"name": "Hub4j Test Org Name (this could be null or blank too)",
"company": null,
"blog": "https://hub4j.url.io/could/be/null",
"location": "Hub4j Test Org Location (this could be null or blank too)",
"email": "hub4jtestorgemail@could.be.null.com",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 54,
"public_gists": 0,
"followers": 1,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2020-06-04T05:56:10Z",
"type": "Organization"
}

0 comments on commit 24832b1

Please sign in to comment.