Skip to content

Commit

Permalink
github: log the response from GH when error occurs (#170)
Browse files Browse the repository at this point in the history
* github: log the response from GH when error occurs

* license up

* tests up, auto-close inputstream

---------

Co-authored-by: Benjamin Broadaway <[email protected]>
  • Loading branch information
brig and benbroadaway authored Sep 26, 2024
1 parent 643126b commit 50344d0
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -763,11 +765,6 @@ private Map<String, Object> getPRFiles(Map<String, Object> in, String gitHubUri)
}
}

private static GitHubClient createClient(String gitHubUri) {
URI uri = URI.create(gitHubUri);
return new GitHubClient(uri.getHost(), uri.getPort(), uri.getScheme());
}

private static Map<String, Object> createRepo(Map<String, Object> in, String gitHubUri) {
String gitHubAccessToken = assertString(in, GITHUB_ACCESSTOKEN);
String gitHubOrgName = assertString(in, GITHUB_ORGNAME);
Expand Down Expand Up @@ -1028,6 +1025,64 @@ private static Map<String, Object> makeResult(Object data) {
return m;
}

static GitHubClient createClient(String rawUrl) {
String host;
int port;
String scheme;

try {
URI uri = new URI(rawUrl);
host = uri.getHost();
if ("github.com".equals(host) || "gist.github.com".equals(host)) {
host = "api.github.com";
}

scheme = uri.getScheme();
port = uri.getPort();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}

return new GitHubClient(host, port, scheme) {
@Override
protected IOException createException(InputStream response, int code, String status) {
String responseBody = null;

if (this.isError(code)) {
RequestError error;
try {
error = this.parseError(response);
} catch (IOException e) {
return e;
}

if (error != null) {
return new RequestException(error, code);
}
} else {
try (BufferedInputStream reader = new BufferedInputStream(response)) {
responseBody = new String(reader.readAllBytes());
} catch (IOException e) {
// ignore
}
}

String message;
if (status != null && !status.isEmpty()) {
message = status + " (" + code + ')';
} else {
message = "Unknown error occurred (" + code + ')';
}

if (responseBody != null) {
message += "\n response: " + responseBody;
}

return new IOException(message);
}
};
}

public enum Action {
CREATEPR,
COMMENTPR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ class GitHubCommonTest {

@Test
void testCreateClient() {
GitHubClient client = GitHubClient.createClient("https://mock.github.local");
GitHubClient client = GitHubTask.createClient("https://mock.github.local");

assertNotNull(client);
}

@Test
void testCreateClientWithPort() {
GitHubClient client = GitHubTask.createClient("https://mock.github.local:8080");

assertNotNull(client);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.walmartlabs.concord.plugins.git.v2;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2024 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.walmartlabs.concord.plugins.git.GitHubTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.walmartlabs.concord.plugins.git.v2;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2024 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.walmartlabs.concord.plugins.git.GitHubTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.walmartlabs.concord.plugins.git.v2;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2024 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.walmartlabs.concord.plugins.git.GitHubTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
package com.walmartlabs.concord.plugins.git.v2;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2024 Walmart Inc., Concord Authors
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.walmartlabs.concord.plugins.git.GitHubTask;
Expand All @@ -18,6 +38,7 @@
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class RepoTest {
Expand Down Expand Up @@ -254,4 +275,22 @@ void testDeleteRepo() {
httpRule.verify(1, getRequestedFor(urlEqualTo("/api/v3/repos/octocat/repo-to-delete")));
httpRule.verify(1, deleteRequestedFor(urlEqualTo("/api/v3/repos/octocat/repo-to-delete")));
}

@Test
void testError() {
Map<String, Object> input = Map.of(
"action", "deleteRepo",
"accessToken", "mockToken",
"org", "octocat",
"repo", "error-repo",
"apiUrl", httpRule.baseUrl()
);

Exception ex = assertThrows(Exception.class, () -> new GitHubTask().execute(input, Map.of()));

assertTrue(ex.getCause().getMessage().contains("very unexpected error"));

httpRule.verify(1, getRequestedFor(urlEqualTo("/api/v3/repos/octocat/error-repo")));
httpRule.verify(0, deleteRequestedFor(urlEqualTo("/api/v3/repos/octocat/error-repo")));
}
}
29 changes: 29 additions & 0 deletions tasks/git/src/test/resources/wiremock/repo/mappings/get_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"id": "4bcaaca9-73fa-443d-a8ef-ebbe5b4e63fb",
"name": "github_task_test_repo_get_error",
"request": {
"url": "/api/v3/repos/octocat/error-repo",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "application/vnd.github.v3.full+json"
}
}
},
"response": {
"status": 999,
"body": "very unexpected error",
"headers": {
"Date": "Thu, 03 Oct 2019 21:18:41 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "999 fake error",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4907",
"X-RateLimit-Reset": "1570140015"
}
},
"uuid": "43cfd527-8dc8-4025-ab6f-41f6c45a86bd",
"persistent": true,
"insertionIndex": 4
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"Date": "Thu, 03 Oct 2019 21:18:41 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "201 Created",
"Status": "200 Ok",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4907",
"X-RateLimit-Reset": "1570140015",
Expand Down

0 comments on commit 50344d0

Please sign in to comment.