From 8ef76f5c6653ab3f869a4defe7c55296445513f6 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Tue, 9 Jan 2018 21:44:15 +0100 Subject: [PATCH] Removing state from GitHub Client The client was created once, with one API, and kept for all future invcations. So that if a changelog was created for one repo (A) and then for a repo (B), then B would use the API from A. Resulting in wrong issue information in B. --- CHANGELOG.md | 80 +++++++++---------- .../github/GitHubServiceFactory.java | 80 +++++++++---------- .../gitchangelog/api/GitChangelogApiTest.java | 4 +- .../bjurr/gitchangelog/api/TemplatesTest.java | 10 +-- 4 files changed, 82 insertions(+), 92 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ed2c7b..e71a845e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ Changelog for tomasbjerre git-changelog-lib. -## Next release +## 1.77 ### Jira JENKINS-19994 **Closing RevWalk** @@ -20,7 +20,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.76 -### GitHub #59 +### GitHub [#59](https://github.com/tomasbjerre/git-changelog-lib/pull/59) Adding support for Jira Issue Description **Updating Doc after merge of** @@ -66,7 +66,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.74 -### GitHub [#58](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/58) [feature] trigger button: reply message *enhancement* +### GitHub [#58](https://github.com/tomasbjerre/git-changelog-lib/issues/58) Avoid using integrations if fetched information is not used in the template *enhancement* **Avoid fetching from integrations if not used** @@ -94,7 +94,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.72 -### GitHub #51 +### GitHub [#51](https://github.com/tomasbjerre/git-changelog-lib/issues/51) Error while trying to create the changelog file. **Rewrite MediaWiki Client for Botuser** @@ -125,7 +125,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.70 -### GitHub #49 +### GitHub [#49](https://github.com/tomasbjerre/git-changelog-lib/issues/49) Ability to grab the url from the git config. *enhancement* **Gathering repo provider information** @@ -144,7 +144,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.69 -### GitHub #47 +### GitHub [#47](https://github.com/tomasbjerre/git-changelog-lib/pull/47) Add ignoreCommitsOlderThan date limiting *enhancement* **Adjustments after merge of** @@ -193,7 +193,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.67 -### GitHub #2 +### GitHub [#2](https://github.com/tomasbjerre/git-changelog-lib/issues/2) GitHub Integration *enhancement* **GitLab integration** @@ -203,7 +203,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.66 -### GitHub #42 +### GitHub [#42](https://github.com/tomasbjerre/git-changelog-lib/issues/42) GitLab integration *enhancement* **GitLab integration** @@ -226,7 +226,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.64 -### GitHub #40 +### GitHub [#40](https://github.com/tomasbjerre/git-changelog-lib/pull/40) add jira issue type *enhancement* **Adding issueType and labels attributes** @@ -282,7 +282,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.61 -### GitHub #38 +### GitHub [#38](https://github.com/tomasbjerre/git-changelog-lib/issues/38) Relocate packages to avoid classpath issues *enhancement* **Relocate packages to avoid classpath issues** @@ -291,7 +291,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.60 -### GitHub #38 +### GitHub [#38](https://github.com/tomasbjerre/git-changelog-lib/issues/38) Relocate packages to avoid classpath issues *enhancement* **Relocate packages to avoid classpath issues** @@ -300,7 +300,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.59 -### GitHub #38 +### GitHub [#38](https://github.com/tomasbjerre/git-changelog-lib/issues/38) Relocate packages to avoid classpath issues *enhancement* **Relocate packages to avoid classpath issues** @@ -309,7 +309,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.58 -### GitHub #36 +### GitHub [#36](https://github.com/tomasbjerre/git-changelog-lib/issues/36) Annotated tags support *enhancement* **Adding annotation to context of tag** @@ -318,7 +318,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.57 -### GitHub [#35](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/35) Encrypt authentication credentials *enhancement* +### GitHub [#35](https://github.com/tomasbjerre/git-changelog-lib/issues/35) Git Change log show only Merges or have Merge as its own Tag **Adding merge boolean to commits** @@ -327,7 +327,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.56 -### GitHub #31 +### GitHub [#31](https://github.com/tomasbjerre/git-changelog-lib/pull/31) issue key was missing in issue link **Fixing testcases after merge of** @@ -344,7 +344,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.55 -### GitHub #30 +### GitHub [#30](https://github.com/tomasbjerre/git-changelog-lib/issues/30) Getting full hash from Commit object *enhancement* **Adding {{hashFull}} variable with full commit hash** @@ -353,7 +353,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.54 -### GitHub #28 +### GitHub [#28](https://github.com/tomasbjerre/git-changelog-lib/issues/28) Crash when git repo has no master branch *bug* **Allowing master branch to be absent** @@ -379,7 +379,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.52 -### GitHub #29 +### GitHub [#29](https://github.com/tomasbjerre/git-changelog-lib/issues/29) Not, always, including merged in commits *bug* **Correcting how to find diffing commits #** @@ -415,7 +415,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.50 -### GitHub #29 +### GitHub [#29](https://github.com/tomasbjerre/git-changelog-lib/issues/29) Not, always, including merged in commits *bug* **Including commits frmo merges** @@ -424,7 +424,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.49 -### GitHub #28 +### GitHub [#28](https://github.com/tomasbjerre/git-changelog-lib/issues/28) Crash when git repo has no master branch *bug* **Finding first commit in repo as parents of HEAD** @@ -470,7 +470,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.45 -### GitHub #26 +### GitHub [#26](https://github.com/tomasbjerre/git-changelog-lib/issues/26) Excluded commits are included JENKINS-34156 *bug* **Including correct commits + performance** @@ -509,7 +509,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.42 -### GitHub [#23](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/23) Add the ability to be notified for commits *enhancement* +### GitHub [#23](https://github.com/tomasbjerre/git-changelog-lib/issues/23) changelog is generating incorrect order of commits/Issues *bug* **Parsing commits, oldest first** @@ -519,7 +519,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.41 -### GitHub [#23](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/23) Add the ability to be notified for commits *enhancement* +### GitHub [#23](https://github.com/tomasbjerre/git-changelog-lib/issues/23) changelog is generating incorrect order of commits/Issues *bug* **Traversing commit tree by parents** @@ -529,7 +529,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.40 -### GitHub [#23](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/23) Add the ability to be notified for commits *enhancement* +### GitHub [#23](https://github.com/tomasbjerre/git-changelog-lib/issues/23) changelog is generating incorrect order of commits/Issues *bug* **Adding feature to ignore tags by regexp** @@ -557,7 +557,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.38 -### GitHub #19 +### GitHub [#19](https://github.com/tomasbjerre/git-changelog-lib/issues/19) Feature-request (or question): Issues by category *enhancement* **Removing commits without issue, from tags** @@ -566,7 +566,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.37 -### GitHub #19 +### GitHub [#19](https://github.com/tomasbjerre/git-changelog-lib/issues/19) Feature-request (or question): Issues by category *enhancement* **Ignore commits without issue** @@ -579,7 +579,7 @@ Changelog for tomasbjerre git-changelog-lib. [a025adfc759313f](https://github.com/tomasbjerre/git-changelog-lib/commit/a025adfc759313f) Tomas Bjerre *2016-03-19 20:33:11* -### GitHub #21 +### GitHub [#21](https://github.com/tomasbjerre/git-changelog-lib/issues/21) Commits can be listed multiple times *bug* **Avoiding adding commit twice in same issue** @@ -626,7 +626,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.34 -### GitHub #18 +### GitHub [#18](https://github.com/tomasbjerre/git-changelog-lib/pull/18) Migrate GitHub to RetroFit , add pagination and token support **Logging error if error invoking GitHub API** @@ -643,7 +643,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.33 -### GitHub #10 +### GitHub [#10](https://github.com/tomasbjerre/git-changelog-lib/issues/10) Authentication with GitHub *enhancement* **Migrate GitHub REST-API to RetroFit library** @@ -653,7 +653,7 @@ Changelog for tomasbjerre git-changelog-lib. [d29029a38fad6a4](https://github.com/tomasbjerre/git-changelog-lib/commit/d29029a38fad6a4) Jonas Kalderstam *2016-03-15 00:12:45* -### GitHub #15 +### GitHub [#15](https://github.com/tomasbjerre/git-changelog-lib/issues/15) Github, support pagination *enhancement* **Migrate GitHub REST-API to RetroFit library** @@ -663,7 +663,7 @@ Changelog for tomasbjerre git-changelog-lib. [d29029a38fad6a4](https://github.com/tomasbjerre/git-changelog-lib/commit/d29029a38fad6a4) Jonas Kalderstam *2016-03-15 00:12:45* -### GitHub #18 +### GitHub [#18](https://github.com/tomasbjerre/git-changelog-lib/pull/18) Migrate GitHub to RetroFit , add pagination and token support **Introducing custom exceptions** @@ -681,7 +681,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.32 -### GitHub #16 +### GitHub [#16](https://github.com/tomasbjerre/git-changelog-lib/issues/16) Commit not available in all issues mentioned in commit comment *bug* **Supplying commit in each issue mentioned in message** @@ -750,7 +750,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.26 -### GitHub #13 +### GitHub [#13](https://github.com/tomasbjerre/git-changelog-lib/issues/13) Performance *enhancement* **Rewriting GitRepo to make it faster** @@ -765,7 +765,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.25 -### GitHub #13 +### GitHub [#13](https://github.com/tomasbjerre/git-changelog-lib/issues/13) Performance *enhancement* **Letting JGit determine new commits between refs** @@ -859,7 +859,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.19 -### GitHub #11 +### GitHub [#11](https://github.com/tomasbjerre/git-changelog-lib/issues/11) Move command line to its own repo *enhancement* **Removing command line code** @@ -937,7 +937,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.12 -### GitHub #2 +### GitHub [#2](https://github.com/tomasbjerre/git-changelog-lib/issues/2) GitHub Integration *enhancement* **Integrating with GitHub** @@ -945,7 +945,7 @@ Changelog for tomasbjerre git-changelog-lib. [45af766856ac703](https://github.com/tomasbjerre/git-changelog-lib/commit/45af766856ac703) Tomas Bjerre *2015-11-22 19:51:40* -### GitHub #3 +### GitHub [#3](https://github.com/tomasbjerre/git-changelog-lib/issues/3) Jira Integration *enhancement* **Integrating with Jira** @@ -1048,7 +1048,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.4 -### GitHub #7 +### GitHub [#7](https://github.com/tomasbjerre/git-changelog-lib/issues/7) Add booleans to enable if statements *enhancement* **Adding hasIssue hasLink to readme doc** @@ -1070,7 +1070,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.3 -### GitHub #7 +### GitHub [#7](https://github.com/tomasbjerre/git-changelog-lib/issues/7) Add booleans to enable if statements *enhancement* **Adding booleans to check if link and/or issue exists in issue** @@ -1097,7 +1097,7 @@ Changelog for tomasbjerre git-changelog-lib. ## 1.2 -### GitHub #4 +### GitHub [#4](https://github.com/tomasbjerre/git-changelog-lib/issues/4) Mediawiki integration *enhancement* **MediaWiki integration** diff --git a/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java b/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java index 8e42cf8f..bf80d17f 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubServiceFactory.java @@ -2,6 +2,7 @@ import static java.util.concurrent.TimeUnit.SECONDS; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import java.io.File; import java.io.IOException; @@ -14,61 +15,54 @@ import retrofit2.converter.gson.GsonConverterFactory; public class GitHubServiceFactory { + static Interceptor interceptor; - private static GitHubService gitHubService = null; - - public static void reset() { - gitHubService = null; - } - - public static GitHubService getGitHubService(String api, final Optional token) { - return getGitHubService(api, token, null); + @VisibleForTesting + public static void setInterceptor(Interceptor interceptor) { + GitHubServiceFactory.interceptor = interceptor; } public static synchronized GitHubService getGitHubService( - String api, final Optional token, final Interceptor interceptor) { + String api, final Optional token) { if (!api.endsWith("/")) { api += "/"; } - if (gitHubService == null) { - File cacheDir = new File(".okhttpcache"); - cacheDir.mkdir(); - Cache cache = new Cache(cacheDir, 1024 * 1024 * 10); + final File cacheDir = new File(".okhttpcache"); + cacheDir.mkdir(); + final Cache cache = new Cache(cacheDir, 1024 * 1024 * 10); - OkHttpClient.Builder builder = - new OkHttpClient.Builder().cache(cache).connectTimeout(10, SECONDS); + final OkHttpClient.Builder builder = + new OkHttpClient.Builder().cache(cache).connectTimeout(10, SECONDS); - if (token != null && token.isPresent() && !token.get().isEmpty()) { - builder.addInterceptor( - new Interceptor() { - @Override - public Response intercept(Chain chain) throws IOException { - Request original = chain.request(); + if (token != null && token.isPresent() && !token.get().isEmpty()) { + builder.addInterceptor( + new Interceptor() { + @Override + public Response intercept(Chain chain) throws IOException { + final Request original = chain.request(); - Request request = - original - .newBuilder() // - .addHeader("Authorization", "token " + token.get()) // - .method(original.method(), original.body()) // - .build(); - return chain.proceed(request); - } - }); - } + final Request request = + original + .newBuilder() // + .addHeader("Authorization", "token " + token.get()) // + .method(original.method(), original.body()) // + .build(); + return chain.proceed(request); + } + }); + } - if (interceptor != null) { - builder.addInterceptor(interceptor); - } + if (interceptor != null) { + builder.addInterceptor(interceptor); + } - Retrofit retrofit = - new Retrofit.Builder() // - .baseUrl(api) // - .client(builder.build()) // - .addConverterFactory(GsonConverterFactory.create()) // - .build(); + final Retrofit retrofit = + new Retrofit.Builder() // + .baseUrl(api) // + .client(builder.build()) // + .addConverterFactory(GsonConverterFactory.create()) // + .build(); - gitHubService = retrofit.create(GitHubService.class); - } - return gitHubService; + return retrofit.create(GitHubService.class); } } diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java index 8b855c4f..ab55b512 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java @@ -47,10 +47,8 @@ public void before() throws Exception { "https://api.github.com/repos/tomasbjerre/git-changelog-lib/issues?state=all&per_page=100&page=1", Resources.toString(getResource("github-issues.json"), UTF_8)); - GitHubServiceFactory.reset(); GitHubServiceFactory // - .getGitHubService( - "https://api.github.com/repos/tomasbjerre/git-changelog-lib", null, gitHubMockInterceptor); + .setInterceptor(gitHubMockInterceptor); } @Test diff --git a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.java b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.java index e1210195..ddee7d3a 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.java +++ b/src/test/java/se/bjurr/gitchangelog/api/TemplatesTest.java @@ -18,7 +18,7 @@ public class TemplatesTest { public void before() throws Exception { JiraClientFactory.reset(); - RestClientMock mockedRestClient = new RestClientMock(); + final RestClientMock mockedRestClient = new RestClientMock(); mockedRestClient // .addMockedResponse( "/repos/tomasbjerre/git-changelog-lib/issues?state=all", @@ -31,14 +31,12 @@ public void before() throws Exception { Resources.toString(getResource("jira-issue-jir-5262.json"), UTF_8)); mock(mockedRestClient); - GitHubMockInterceptor gitHubMockInterceptor = new GitHubMockInterceptor(); + final GitHubMockInterceptor gitHubMockInterceptor = new GitHubMockInterceptor(); gitHubMockInterceptor.addMockedResponse( "https://api.github.com/repos/tomasbjerre/git-changelog-lib/issues?state=all&per_page=100&page=1", Resources.toString(getResource("github-issues.json"), UTF_8)); - GitHubServiceFactory.reset(); - GitHubServiceFactory.getGitHubService( - "https://api.github.com/repos/tomasbjerre/git-changelog-lib", null, gitHubMockInterceptor); + GitHubServiceFactory.setInterceptor(gitHubMockInterceptor); } @Test @@ -88,7 +86,7 @@ public void testIssueTypesIssuesCommits() throws Exception { @Test public void testMerges() throws Exception { - String testcase = "testMerges"; + final String testcase = "testMerges"; assertThat(testcase + ".mustache") // .setIgnoreCommitsIfMessageMatches("") // .rendersTo(testcase + ".md");