/*
 * Copyright 2012-2022 the original author or 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
 *
 *      https://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.
 */

package org.springframework.boot.build.bom.bomr.github;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * Standard implementation of {@link GitHubRepository}.
 *
 * @author Andy Wilkinson
 */
final class StandardGitHubRepository implements GitHubRepository {

	private final RestTemplate rest;

	StandardGitHubRepository(RestTemplate restTemplate) {
		this.rest = restTemplate;
	}

	@Override
	@SuppressWarnings("rawtypes")
	public int openIssue(String title, String body, List<String> labels, Milestone milestone) {
		Map<String, Object> requestBody = new HashMap<>();
		requestBody.put("title", title);
		if (milestone != null) {
			requestBody.put("milestone", milestone.getNumber());
		}
		if (!labels.isEmpty()) {
			requestBody.put("labels", labels);
		}
		requestBody.put("body", body);
		try {
			Thread.sleep(1000);
		}
		catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
		ResponseEntity<Map> response = this.rest.postForEntity("issues", requestBody, Map.class);
		return (Integer) response.getBody().get("number");
	}

	@Override
	public List<String> getLabels() {
		return get("labels?per_page=100", (label) -> (String) label.get("name"));
	}

	@Override
	public List<Milestone> getMilestones() {
		return get("milestones?per_page=100",
				(milestone) -> new Milestone((String) milestone.get("title"), (Integer) milestone.get("number")));
	}

	@Override
	public List<Issue> findIssues(List<String> labels, Milestone milestone) {
		return get(
				"issues?per_page=100&state=all&labels=" + String.join(",", labels) + "&milestone="
						+ milestone.getNumber(),
				(issue) -> new Issue(this.rest, (Integer) issue.get("number"), (String) issue.get("title")));
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private <T> List<T> get(String name, Function<Map<String, Object>, T> mapper) {
		ResponseEntity<List> response = this.rest.getForEntity(name, List.class);
		List<Map<String, Object>> body = response.getBody();
		return body.stream().map(mapper).collect(Collectors.toList());
	}

}

@@ -865,16 +865,149 @@
 Entity;%0A
+import org.springframework.web.client.HttpClientErrorException.Forbidden;%0Aimport org.springframework.web.client.RestClientException;%0A
 import o
@@ -1860,16 +1860,25 @@
 ();%0A%09%09%7D%0A
+%09%09try %7B%0A%09
 %09%09Respon
@@ -1959,24 +1959,25 @@
 p.class);%0A%09%09
+%09
 return (Inte
@@ -2015,16 +2015,214 @@
 mber%22);%0A
+%09%09%7D%0A%09%09catch (RestClientException ex) %7B%0A%09%09%09if (ex instanceof Forbidden) %7B%0A%09%09%09%09System.out.println(%22Received 403 response with headers %22 + ((Forbidden) ex).getResponseHeaders());%0A%09%09%09%7D%0A%09%09%09throw ex;%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

Improve diagnostics for 403 response to upgrade issue creation