package sagan.util.service.github;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.social.github.api.GitHub;
import org.springframework.social.github.api.impl.GitHubTemplate;
import org.springframework.social.github.connect.GitHubConnectionFactory;

@Configuration
public class GitHubConfiguration {

    @Value("${github.client.id}")
    private String githubClientId;

    @Value("${github.client.secret}")
    private String githubClientSecret;

    @Bean
    public GitHubConnectionFactory gitHubConnectionFactory() {
        GitHubConnectionFactory factory = new GitHubConnectionFactory(githubClientId, githubClientSecret);
        factory.setScope("read:org");
        return factory;
    }

    @Bean
    public GitHub gitHubTemplate() {
        return new GuideGitHubTemplate();
    }

    private static class GuideGitHubTemplate extends GitHubTemplate {

        @Override
        protected List<HttpMessageConverter<?>> getMessageConverters() {
            List<HttpMessageConverter<?>> converters = new ArrayList<>();
            converters.add(new JsonStringConverter());
            converters.add(new MarkdownHtmlConverter());
            converters.add(new DownloadConverter());
            converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
            return converters;
        }
    }
}

@@ -813,16 +813,150 @@
 ecret;%0A%0A
+    @Value(%22$%7BGITHUB_ACCESS_TOKEN:$%7Bgithub.access.token:5a0e089d267693b45926d7f620d85a2eb6a85da6%7D%7D%22)%0A    private String accessToken;%0A%0A
     @Bea
@@ -1282,16 +1282,27 @@
 emplate(
+accessToken
 );%0A    %7D
@@ -1370,24 +1370,125 @@
 Template %7B%0A%0A
+        private GuideGitHubTemplate(String accessToken) %7B%0A            super(accessToken);%0A        %7D%0A%0A
         @Ove

Revert "Remove obsolete GitHub token for guide access"