Compare commits

..

332 Commits

Author SHA1 Message Date
Liam Newman
2321dc50c5 [maven-release-plugin] prepare release github-api-1.111 2020-04-15 23:48:15 -07:00
Liam Newman
4efd2e8184 Merge pull request #786 from github-api/dependabot/maven/spotbugs.version-4.0.2
Bump spotbugs.version from 4.0.1 to 4.0.2
2020-04-15 23:43:50 -07:00
dependabot-preview[bot]
e30e153bfa Bump spotbugs.version from 4.0.1 to 4.0.2
Bumps `spotbugs.version` from 4.0.1 to 4.0.2.

Updates `spotbugs` from 4.0.1 to 4.0.2
- [Release notes](https://github.com/spotbugs/spotbugs/releases)
- [Changelog](https://github.com/spotbugs/spotbugs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/spotbugs/spotbugs/compare/4.0.1...4.0.2)

Updates `spotbugs-annotations` from 4.0.1 to 4.0.2
- [Release notes](https://github.com/spotbugs/spotbugs/releases)
- [Changelog](https://github.com/spotbugs/spotbugs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/spotbugs/spotbugs/compare/4.0.1...4.0.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-04-16 06:38:59 +00:00
Liam Newman
7d842175f7 Merge pull request #783 from bitwiseman/issue/760
Expose MappingReader and MappingWriter
2020-04-15 16:58:01 -07:00
Liam Newman
e0aee9f361 Expose MappingReader and MappingWriter
Jenkins Blue Ocean made interesting design choices relating github-api interactions.

They mostly reused the existing API and OM, but in a few places they chose to
implement their own object mapping independent of this project. This is fine
as long as nothing in this project ever changes, including internals such
as ObjectMapper configuration or behavior.

Recent release have made changes to those internals which break assumptions made
in Blue Ocean.

This change exposes this project's MappingReader and MappingWriter to allow
for a fix to Blue Ocean requiring only minimal changes.

This doesn't prevent future changes from breaking Blue Ocean but at least makes
them much less likely.

Fixes #780
2020-04-15 16:39:59 -07:00
Liam Newman
76c51922f1 [maven-release-plugin] prepare for next development iteration 2020-04-06 09:43:50 -07:00
Liam Newman
f95e89a136 [maven-release-plugin] prepare release github-api-1.110 2020-04-06 09:43:41 -07:00
Liam Newman
2dff60a23c Merge pull request #777 from bitwiseman/issue/775
Reverse removal of misnamed gitHttpTransportUrl
2020-04-06 09:38:47 -07:00
Liam Newman
95f83d1a29 Merge pull request #774 from martinvanzijl/issue_518_allow_getref_with_prefix
Allow "refs/" prefix in parameter to GHRepository.getRef()
2020-04-06 09:33:39 -07:00
Liam Newman
b875ccecc1 Reverse removal of misnamed gitHttpTransportUrl 2020-04-06 09:31:50 -07:00
Martin van Zijl
e4c3802f16 Revert accidental changes to addCollaborators test files. 2020-04-07 04:11:11 +12:00
Liam Newman
081e485f4f Merge pull request #773 from martinvanzijl/issue_444_unset_milestone
Add ability to unset the milestone of an issue
2020-04-05 12:57:10 -07:00
Liam Newman
4adf88da19 Merge pull request #767 from XiongKezhi/installation-repository-event
Installation repository event
2020-04-05 12:45:56 -07:00
Liam Newman
31e2b1b8d3 Merge branch 'master' into issue_444_unset_milestone 2020-04-05 12:41:02 -07:00
Martin van Zijl
bd28abd343 Allow "refs/" prefix in parameter to GHRepository.getRef().
Fixes #518.
2020-04-06 04:43:26 +12:00
August
955690b124 Rename refreash to populate and add node_id for GHRepository 2020-04-05 19:09:02 +08:00
Martin van Zijl
fa6f06ae15 Add ability to unset the milestone of an issue.
Fixes #444.
2020-04-05 16:16:04 +12:00
August
263de140c5 Removed InstallationRepository 2020-04-04 22:49:52 +08:00
Kezhi Xiong
ed85d06d69 Merge branch 'master' into installation-repository-event 2020-04-04 22:33:18 +08:00
August
4ff0870df8 Refresh repositories while warp up installation event 2020-04-04 22:30:05 +08:00
Liam Newman
410bac2040 [maven-release-plugin] prepare for next development iteration 2020-04-01 14:31:22 -07:00
Liam Newman
38b1e367b1 [maven-release-plugin] prepare release github-api-1.109 2020-04-01 14:31:14 -07:00
Liam Newman
3cddffa37f Merge pull request #768 from bitwiseman/task/jacoco-report
Add Jacoco report to site
2020-04-01 14:27:23 -07:00
Liam Newman
ea7a1a7175 More GHRateLimit test stabilization 2020-04-01 14:22:17 -07:00
Liam Newman
36b5601588 Add Jacoco report to site 2020-04-01 14:03:44 -07:00
Liam Newman
7fc68f2969 Merge branch 'master' into installation-repository-event 2020-04-01 12:29:13 -07:00
Liam Newman
c5ee07add4 Merge pull request #741 from sladyn98/add_has_projects
Add has_projects field to github repository
2020-04-01 08:33:59 -07:00
August
32ff315b6b Resolve spotbugs and format code 2020-04-01 16:58:02 +08:00
August
f919346f8f Add installation and installtion repositories event 2020-04-01 16:08:21 +08:00
Liam Newman
279df00404 Add checks for hasProjects() 2020-03-31 14:54:16 -07:00
Liam Newman
bfd4b17fa0 Merge remote-tracking branch 'upstream/master' into add_has_projects 2020-03-31 13:33:36 -07:00
Liam Newman
5fe2817164 Merge pull request #764 from bitwiseman/issue/valid_creds
Change credential check to use rate limit
2020-03-31 12:37:16 -07:00
Liam Newman
b337bb39bc Update PULL_REQUEST_TEMPLATE.md 2020-03-31 10:05:16 -07:00
Liam Newman
65ae41c5f1 Fix tests for isCredentialsValid 2020-03-31 09:36:26 -07:00
Liam Newman
796c644c4a Change credential check to use rate limit
Fixes #582
2020-03-31 08:28:57 -07:00
Liam Newman
bfd9023a27 Merge pull request #762 from bitwiseman/task/insensitive
Http header field names must be case-insensitive
2020-03-31 07:28:44 -07:00
Liam Newman
c9cdf5d03e Make rate limit tests tolerant of one second timing differences
Fixes #760
2020-03-30 19:55:19 -07:00
Liam Newman
f60bb41ad9 Move tests around for clarity 2020-03-30 19:15:00 -07:00
Liam Newman
c333903b4a Http header field names must be case-insensitive
GitHub has started changing their headers from capitalized words to all lowercase.
A recent change made the header fields querying case-senstive which broke gzip content detection.
This was not caught by tests because recorded files remain unchanged.
It is also possible that WireMock is auto-capitalizing.

This fixes the case-sensitivity issue and also extends that funcionality to anyone consuming the
headers generated by ResponseInfo.

Fixes #751
2020-03-30 17:31:42 -07:00
Liam Newman
dd55e8a22c Merge pull request #753 from jglick/createCheckRun
GHRepository.createCheckRun
2020-03-30 13:46:31 -07:00
Jesse Glick
3ab9381d0a createCheckRunErrMissingConclusion 2020-03-30 15:06:01 -04:00
Jesse Glick
58f1fe0671 createPendingCheckRun 2020-03-30 14:58:58 -04:00
Jesse Glick
82b9c05d0f Strengthened test a bit 2020-03-30 14:49:37 -04:00
Jesse Glick
7c9397f7f6 Switched from fluent style with .done() to accessible constructors 2020-03-30 14:46:56 -04:00
Jesse Glick
6214b6a3ff Regenerated WireMock metadata 2020-03-30 14:26:23 -04:00
Jesse Glick
883c8cc4c8 Merge branch 'master' of github.com:github-api/github-api into createCheckRun 2020-03-30 14:20:05 -04:00
Liam Newman
8d47c72913 Merge pull request #758 from XiongKezhi/add-check-suite-warp-up
Add wrapUp() for GHEventPayload.CheckSuite
2020-03-30 09:01:19 -07:00
Jesse Glick
89a6664e45 More links to GH docs from Javadoc 2020-03-27 14:07:40 -04:00
Jesse Glick
30d792d6e1 Remove ‘Draft’ from nested data class names 2020-03-27 14:04:42 -04:00
Liam Newman
3745bf3157 Merge branch 'master' into add_has_projects 2020-03-27 07:53:07 -07:00
Kezhi Xiong
a7fda3e50d Merge branch 'master' into add-check-suite-warp-up 2020-03-27 17:05:14 +08:00
XiongKezhi
7f07204fef Add warpUp() for GHEventPayload.CheckSuite 2020-03-27 16:57:06 +08:00
Jesse Glick
8b51a44b7c Merge branch 'createCheckRun' of github.com:jglick/github-api into createCheckRun 2020-03-26 19:10:29 -04:00
Jesse Glick
c499c73dcc Merge branch 'master' of github.com:github-api/github-api into createCheckRun 2020-03-26 19:09:13 -04:00
Jesse Glick
c01f3f5e8a NPE when there are no annotations 2020-03-26 19:07:35 -04:00
Liam Newman
2aef35655f Merge pull request #756 from bitwiseman/task/shorter
Shorten generated  test resource paths
2020-03-26 15:52:08 -07:00
Liam Newman
7ddf1f5830 Shorten generated test resource paths 2020-03-26 14:58:54 -07:00
Liam Newman
b2c513ea42 Merge branch 'master' into createCheckRun 2020-03-26 14:10:29 -07:00
Liam Newman
4c30f94355 Add Java 8 Site to CI 2020-03-26 14:06:46 -07:00
Liam Newman
e911e86c4c Merge pull request #755 from jglick/javadoc
#724 broke the site goal from .github/PULL_REQUEST_TEMPLATE.md
2020-03-26 13:57:51 -07:00
Liam Newman
ca640b3f64 Merge pull request #738 from sourabhsparkala/sign_verification
Get commit or tag signature verified flag
2020-03-26 13:56:03 -07:00
Jesse Glick
b4c4a05f3b Moving enums inside GHCheckRun 2020-03-26 16:44:01 -04:00
Jesse Glick
fd3c36a259 IMHO treating Javadoc warnings as fatal makes sources less legible and does not improve API comprehension at all 2020-03-26 16:33:57 -04:00
Jesse Glick
d8274ac2d4 Merge branch 'javadoc' into createCheckRun 2020-03-26 16:26:52 -04:00
Jesse Glick
9c7479f953 #724 broke the site goal from .github/PULL_REQUEST_TEMPLATE.md 2020-03-26 16:25:32 -04:00
Jesse Glick
b5dc3c4366 Added some Javadoc. 2020-03-26 16:22:04 -04:00
Jesse Glick
26b8082155 Handle >50 annotations 2020-03-26 16:14:39 -04:00
Liam Newman
418df15f7b Merge branch 'master' into sign_verification 2020-03-26 10:23:16 -07:00
Liam Newman
31ed0125b8 Shorten test paths for windows 2020-03-26 10:09:01 -07:00
Liam Newman
494318b879 Change method name to isVerified 2020-03-26 09:49:03 -07:00
Liam Newman
f554ddc372 Capitalize and Move GHVerification.Reason enum 2020-03-26 09:45:20 -07:00
Jesse Glick
03de12c221 SpotBugs 2020-03-25 20:03:18 -04:00
Jesse Glick
6c41f22b57 Coverage of DraftImage 2020-03-25 19:59:42 -04:00
Jesse Glick
7ae96388e3 Merge branch 'master' of github.com:github-api/github-api into createCheckRun 2020-03-25 19:21:46 -04:00
Jesse Glick
e8b4de00d2 WireMock coverage 2020-03-25 19:18:11 -04:00
Jesse Glick
cd7963b30d Acc to 7a650132c5/src/main/java/org/kohsuke/github/GitHubBuilder.java (L221) it is GITHUB_LOGIN not GITHUB_USER 2020-03-25 17:52:32 -04:00
Liam Newman
0dc44cffcf Merge branch 'master' into add_has_projects 2020-03-25 11:57:44 -07:00
Liam Newman
7a650132c5 Merge pull request #724 from bitwiseman/task/builder-updater
Add Builder/Creator/Updater for GHLabel
2020-03-25 11:56:41 -07:00
Jesse Glick
c7fb390c38 Introduced enums 2020-03-25 13:40:00 -04:00
Jesse Glick
572ff9df19 I guess nullability annotations can be omitted from internal members 2020-03-25 13:20:33 -04:00
Jesse Glick
b715e0cef7 Reformatted 2020-03-25 13:18:11 -04:00
Jesse Glick
36ab2a889f Redesigned using a fluent builder idiom 2020-03-25 13:14:06 -04:00
D067452
a78d2f28d7 Get commit or tag signature verified flag
This fixes #737

- A new entity GHVerification.java has been added which would be reflecting Verification flag
- Updating GHCommit.java and GHTagObject.java with GHVerification
- Altering few test cases AppTest.java and GHTagTest.java to verify if the Verification entity is being picked up
- A separate test class SignatureVerificationTest.java with the associated wiremock test resources
- Adding a new enum GHReason.java
- Updating tests to check the GHReason implementation, GHReasonTest.java with the associated wiremock test resources
2020-03-25 17:19:56 +01:00
Jesse Glick
7d5a39ed89 GHRepository.createCheckRun 2020-03-24 23:51:38 -04:00
Liam Newman
772272ff36 Re-record test for GHLabel 2020-03-24 12:32:13 -07:00
Liam Newman
2ab4eafee9 Tweaks and cleanup 2020-03-23 17:40:01 -07:00
Liam Newman
b15e0d4c45 Cleanup and tweaks 2020-03-23 17:40:01 -07:00
Liam Newman
b8180314d8 Change to Preview for new builder pattern 2020-03-23 17:40:01 -07:00
Liam Newman
fcb8d03a0f Ensure that Description is part of GHLabel comparision 2020-03-23 17:40:01 -07:00
Liam Newman
09ec89bc2e Remove Repository member from GHLabel
It turns out GHLabel instances do not need a reference to their repo, just to root.
2020-03-23 17:40:01 -07:00
Liam Newman
863ad0f486 Clarify behavior 2020-03-23 17:40:01 -07:00
Liam Newman
79a1bb3571 Update src/test/java/org/kohsuke/github/AppTest.java 2020-03-23 17:40:01 -07:00
Liam Newman
9f1d7323c7 Reverted getter changes to highlight the more important set/update changes 2020-03-23 17:40:01 -07:00
Liam Newman
64a82f4785 Clean up proposed API changes 2020-03-23 17:40:01 -07:00
Liam Newman
f37e4bd76e Private fields 2020-03-23 17:40:01 -07:00
Liam Newman
98ef2cc640 Update-in-place and safer single or batch calculation 2020-03-23 17:40:01 -07:00
Liam Newman
134222fd69 Minor cleanups 2020-03-23 17:40:01 -07:00
Liam Newman
0cb2371517 Third alternative proposal
This removes the  from the fields.  Functionally the behavior is unchanged but
it is no longer guaranteed at compile time.  This simplifies streamlines the code slightly,
but at the cost of only being able to assert immutability rather than know it.

However, as we move to using this structure through more of the library, this is may be a better choice.
There are so many places where the GitHub API itself returns partial records or updates them dynamically.
Trying to claim immutability where it doesn't exist is not great either.
2020-03-23 17:40:01 -07:00
Liam Newman
b7de4359fd Alternative proposal
The guts of this version are a bit ugly but they result reasonable API code without a ton of extra
code needed.
2020-03-23 17:40:01 -07:00
Liam Newman
2607d6a107 Make GHLabel example of proposed API design 2020-03-23 17:40:01 -07:00
Liam Newman
db46b1ce13 Merge pull request #752 from github-api/dependabot/maven/com.squareup.okio-okio-2.5.0
Bump okio from 2.4.3 to 2.5.0
2020-03-23 14:44:12 -07:00
dependabot-preview[bot]
d7b08d5207 Bump okio from 2.4.3 to 2.5.0
Bumps [okio](https://github.com/square/okio) from 2.4.3 to 2.5.0.
- [Release notes](https://github.com/square/okio/releases)
- [Changelog](https://github.com/square/okio/blob/master/CHANGELOG.md)
- [Commits](https://github.com/square/okio/compare/parent-2.4.3...parent-2.5.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-23 06:54:29 +00:00
Sladyn
29fbba832c Merge branch 'master' into add_has_projects 2020-03-21 23:03:10 +05:30
Liam Newman
fd621a442a Merge pull request #742 from sladyn98/addHtmlURL
Retrieve html_url from Pull Request Review Comment
2020-03-20 12:33:34 -07:00
Liam Newman
a1a73568ae Merge branch 'master' into addHtmlURL 2020-03-20 11:41:23 -07:00
Liam Newman
3daccbd6ec Merge pull request #740 from avano/list-checkruns-for-ref
[CheckRuns] Add method for listing checkruns for given ref
2020-03-20 11:39:40 -07:00
Liam Newman
293deadb48 Merge branch 'master' into addHtmlURL 2020-03-20 08:42:47 -07:00
Andrej Vano
452b56c47b [GHEvent] Add new enum for registry_package event 2020-03-20 11:47:04 +01:00
Andrej Vano
5cb6bfa633 [CheckRuns] Add method for listing checkruns for given ref 2020-03-20 11:47:04 +01:00
Sladyn Nunes
0515cee6f3 Merge branch 'addHtmlURL' of https://github.com/sladyn98/github-api into addHtmlURL 2020-03-20 11:57:58 +05:30
Sladyn Nunes
4247112539 Adjust format 2020-03-20 11:56:22 +05:30
Sladyn Nunes
8d3374f574 Added test 2020-03-20 11:27:29 +05:30
Liam Newman
26833e5f7c Merge branch 'master' into add_has_projects 2020-03-19 12:15:34 -07:00
Liam Newman
6752b46f67 Merge pull request #750 from github-api/dependabot/maven/spotbugs.version-4.0.1
Bump spotbugs.version from 4.0.0 to 4.0.1
2020-03-19 10:30:48 -07:00
dependabot-preview[bot]
b9429ffcaa Bump spotbugs.version from 4.0.0 to 4.0.1
Bumps `spotbugs.version` from 4.0.0 to 4.0.1.

Updates `spotbugs` from 4.0.0 to 4.0.1
- [Release notes](https://github.com/spotbugs/spotbugs/releases)
- [Changelog](https://github.com/spotbugs/spotbugs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/spotbugs/spotbugs/compare/4.0.0...4.0.1)

Updates `spotbugs-annotations` from 4.0.0 to 4.0.1
- [Release notes](https://github.com/spotbugs/spotbugs/releases)
- [Changelog](https://github.com/spotbugs/spotbugs/blob/master/CHANGELOG.md)
- [Commits](https://github.com/spotbugs/spotbugs/compare/4.0.0...4.0.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-19 06:35:16 +00:00
Liam Newman
10827c7e21 Merge branch 'master' into addHtmlURL 2020-03-18 08:43:43 -07:00
Liam Newman
23cb4a34a4 Merge branch 'master' into add_has_projects 2020-03-18 08:43:20 -07:00
Liam Newman
adfd09565f Merge pull request #733 from ingwarsw/add_get_team_by_slug
Make getTeambySlug faster
2020-03-17 15:23:14 -07:00
Karol Lassak
78b9ff49d4 Fix tests 2020-03-17 22:36:54 +01:00
Karol Lassak
fca425d25e Add test for old getteam method 2020-03-17 22:34:13 +01:00
Karol Lassak
1a4238156c Fix javadocs 2020-03-17 11:34:31 +01:00
Karol Lassak
f6210cc014 Change variable name to something meningfull 2020-03-17 11:21:58 +01:00
Karol Lassak
6c8b466e59 Add mocks for tests 2020-03-17 11:18:50 +01:00
Karol Lassak
2aebe97f9f Add/restore test for getTeam 2020-03-17 11:18:33 +01:00
Karol Lassak
157724bff8 Fix rest of tests 2020-03-17 10:20:42 +01:00
Karol Lassak
6cbb1a0bee Fix tests partially 2020-03-17 10:13:43 +01:00
Karol Lassak
960a13dd38 Merge branch 'master' into add_get_team_by_slug 2020-03-17 09:08:34 +01:00
Karol Lassak
9213f80435 Add comment to deprecation
Co-Authored-By: Liam Newman <bitwiseman@gmail.com>
2020-03-17 09:07:37 +01:00
Liam Newman
bccae94c7a Merge pull request #734 from github-api/dependabot/maven/org.apache.maven.plugins-maven-site-plugin-3.9.0
Bump maven-site-plugin from 3.8.2 to 3.9.0
2020-03-16 12:57:09 -07:00
Liam Newman
d71f77ce06 Merge pull request #746 from github-api/dependabot/maven/com.github.spotbugs-spotbugs-maven-plugin-4.0.0
Bump spotbugs-maven-plugin from 3.1.12.2 to 4.0.0
2020-03-16 12:56:54 -07:00
Liam Newman
2787f3dc71 Merge pull request #730 from github-api/dependabot/maven/com.squareup.okhttp3-okhttp-4.4.1
Bump okhttp from 4.4.0 to 4.4.1
2020-03-16 12:56:40 -07:00
dependabot-preview[bot]
fb00baab5b Bump maven-site-plugin from 3.8.2 to 3.9.0
Bumps [maven-site-plugin](https://github.com/apache/maven-site-plugin) from 3.8.2 to 3.9.0.
- [Release notes](https://github.com/apache/maven-site-plugin/releases)
- [Commits](https://github.com/apache/maven-site-plugin/compare/maven-site-plugin-3.8.2...maven-site-plugin-3.9.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-16 19:29:36 +00:00
dependabot-preview[bot]
9e22155d31 Bump okhttp from 4.4.0 to 4.4.1
Bumps [okhttp](https://github.com/square/okhttp) from 4.4.0 to 4.4.1.
- [Release notes](https://github.com/square/okhttp/releases)
- [Changelog](https://github.com/square/okhttp/blob/master/CHANGELOG.md)
- [Commits](https://github.com/square/okhttp/compare/parent-4.4.0...parent-4.4.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-16 19:29:35 +00:00
dependabot-preview[bot]
963373435d Bump spotbugs-maven-plugin from 3.1.12.2 to 4.0.0
Bumps [spotbugs-maven-plugin](https://github.com/spotbugs/spotbugs-maven-plugin) from 3.1.12.2 to 4.0.0.
- [Release notes](https://github.com/spotbugs/spotbugs-maven-plugin/releases)
- [Commits](https://github.com/spotbugs/spotbugs-maven-plugin/compare/spotbugs-maven-plugin-3.1.12.2...spotbugs-maven-plugin-4.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-16 19:29:30 +00:00
Liam Newman
377987fa92 Merge pull request #747 from sladyn98/add_ci_status_badge
Added github CI status badge
2020-03-16 12:28:18 -07:00
Liam Newman
0b6980639e Merge pull request #745 from github-api/dependabot/maven/org.mockito-mockito-core-3.3.3
Bump mockito-core from 3.3.1 to 3.3.3
2020-03-16 12:26:26 -07:00
Liam Newman
4f1cc9f94f Merge pull request #744 from github-api/dependabot/maven/org.apache.maven.plugins-maven-javadoc-plugin-3.2.0
Bump maven-javadoc-plugin from 3.1.1 to 3.2.0
2020-03-16 12:26:12 -07:00
dependabot-preview[bot]
6e5434a0ec Bump mockito-core from 3.3.1 to 3.3.3
Bumps [mockito-core](https://github.com/mockito/mockito) from 3.3.1 to 3.3.3.
- [Release notes](https://github.com/mockito/mockito/releases)
- [Commits](https://github.com/mockito/mockito/compare/v3.3.1...v3.3.3)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-16 19:25:57 +00:00
dependabot-preview[bot]
3244f7c38f Bump maven-javadoc-plugin from 3.1.1 to 3.2.0
Bumps [maven-javadoc-plugin](https://github.com/apache/maven-javadoc-plugin) from 3.1.1 to 3.2.0.
- [Release notes](https://github.com/apache/maven-javadoc-plugin/releases)
- [Commits](https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.1.1...maven-javadoc-plugin-3.2.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-16 19:25:56 +00:00
Liam Newman
f27b676e89 Merge pull request #743 from github-api/dependabot/maven/org.eclipse.jgit-org.eclipse.jgit-5.7.0.202003110725-r
Bump org.eclipse.jgit from 5.6.1.202002131546-r to 5.7.0.202003110725-r
2020-03-16 12:25:03 -07:00
Liam Newman
4f2a80a4a3 Merge pull request #732 from github-api/dependabot/maven/com.github.tomakehurst-wiremock-jre8-standalone-2.26.3
Bump wiremock-jre8-standalone from 2.26.2 to 2.26.3
2020-03-16 12:24:43 -07:00
Sladyn
a51bc27829 Merge branch 'master' into add_ci_status_badge 2020-03-16 23:35:17 +05:30
Sladyn Nunes
4fd321c93d Added github CI status badge 2020-03-16 23:32:23 +05:30
dependabot-preview[bot]
bbd62bdef5 Bump org.eclipse.jgit from 5.6.1.202002131546-r to 5.7.0.202003110725-r
Bumps org.eclipse.jgit from 5.6.1.202002131546-r to 5.7.0.202003110725-r.

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-16 06:40:34 +00:00
Sladyn
4bb1d78939 Merge branch 'master' into addHtmlURL 2020-03-15 18:41:22 +05:30
Sladyn Nunes
53c37ef413 Retrieve html_url from PRReviewComment 2020-03-15 18:30:29 +05:30
Sladyn
a6511b6c5a Merge branch 'master' into add_has_projects 2020-03-15 17:30:45 +05:30
Sladyn Nunes
829e96a2d0 Add has_projects field to github repository 2020-03-15 14:44:52 +05:30
Karol Lassak
2e25f37433 Format code 2020-03-09 16:39:22 +01:00
Karol Lassak
fbf6c73226 Make getTeambySlug fast enought 2020-03-09 16:04:01 +01:00
dependabot-preview[bot]
aab54e3f23 Bump wiremock-jre8-standalone from 2.26.2 to 2.26.3
Bumps [wiremock-jre8-standalone](https://github.com/tomakehurst/wiremock) from 2.26.2 to 2.26.3.
- [Release notes](https://github.com/tomakehurst/wiremock/releases)
- [Commits](https://github.com/tomakehurst/wiremock/compare/2.26.2...2.26.3)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-09 06:47:44 +00:00
Liam Newman
a6eff7fbfb [maven-release-plugin] prepare for next development iteration 2020-03-04 16:54:50 -08:00
Liam Newman
d5667d2473 [maven-release-plugin] prepare release github-api-1.108 2020-03-04 16:54:42 -08:00
Liam Newman
a42305dd59 Revert "Removed extraneous author javadocs"
This reverts commit 888abc9e2a.
2020-03-04 11:32:12 -08:00
Liam Newman
c22a718d14 Merge pull request #723 from XiongKezhi/complete-checks-api
Complete checks api
2020-03-03 23:03:10 -08:00
Liam Newman
d0b23c79e2 Merge branch 'master' into complete-checks-api 2020-03-03 19:59:21 -08:00
Liam Newman
76da04afd8 Merge pull request #725 from github-api/dependabot/maven/com.github.tomakehurst-wiremock-jre8-standalone-2.26.2
Bump wiremock-jre8-standalone from 2.26.1 to 2.26.2
2020-03-03 12:55:59 -08:00
XiongKezhi
768f60709f Add Output and more tests 2020-03-03 23:45:01 +08:00
dependabot-preview[bot]
8cd3acd318 Bump wiremock-jre8-standalone from 2.26.1 to 2.26.2
Bumps [wiremock-jre8-standalone](https://github.com/tomakehurst/wiremock) from 2.26.1 to 2.26.2.
- [Release notes](https://github.com/tomakehurst/wiremock/releases)
- [Commits](https://github.com/tomakehurst/wiremock/compare/2.26.1...2.26.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-03-03 06:35:55 +00:00
Kezhi Xiong
ce7cfc0648 Merge branch 'master' into complete-checks-api 2020-03-03 12:57:42 +08:00
Liam Newman
8b6cf55473 Merge pull request #719 from bitwiseman/task/deprecated
Remove a few long deprecated methods and fully annotate others
2020-03-02 14:06:50 -08:00
Liam Newman
75d95d844c Update src/test/java/org/kohsuke/github/AppTest.java
Co-Authored-By: Karl Shultz <kshultz@cloudbees.com>
2020-03-02 08:41:21 -08:00
XiongKezhi
f54bfd3fb5 Fixed JavaDoc warning reported by maven site 2020-03-01 22:18:36 +08:00
XiongKezhi
f8a8ee9b9d Format code 2020-03-01 22:14:53 +08:00
XiongKezhi
16faaae199 Add GHCheckSuite 2020-03-01 21:59:16 +08:00
XiongKezhi
375417527b Change timestamps property back to String 2020-03-01 15:17:24 +08:00
XiongKezhi
10b01ca6b3 using Date for timestamps and add more JavaDoc 2020-03-01 14:01:59 +08:00
Liam Newman
f9006af04c Merge branch 'master' into complete-checks-api 2020-02-29 15:39:38 -08:00
Kezhi Xiong
57f947576e Delete empty line in JavaDoc 2020-02-29 20:15:32 +08:00
XiongKezhi
5a8f8c345b parse completedAt and startedAt into Date in getters 2020-02-29 18:09:25 +08:00
XiongKezhi
e96067e3c8 Merge remote-tracking branch 'origin/complete-checks-api' into complete-checks-api 2020-02-29 13:27:02 +08:00
XiongKezhi
2242174515 add Output in GHCheckRun 2020-02-29 13:26:59 +08:00
Kezhi Xiong
73179c118b Fixed typo in JavaDoc of getApp 2020-02-29 13:19:14 +08:00
Liam Newman
5b575134fc Merge branch 'master' into task/deprecated 2020-02-28 14:38:30 -08:00
Liam Newman
c11c06b896 Merge pull request #720 from bitwiseman/task/inject-root
Enable injection of GitHub and other values into mapping
2020-02-28 14:38:09 -08:00
Liam Newman
ba8d2a251f Merge branch 'master' into task/deprecated 2020-02-28 08:37:51 -08:00
Liam Newman
c9589b73f4 Merge pull request #722 from github-api/dependabot/maven/org.mockito-mockito-core-3.3.1
Bump mockito-core from 3.3.0 to 3.3.1
2020-02-28 08:37:41 -08:00
Liam Newman
32f4425100 Merge pull request #721 from github-api/dependabot/maven/com.github.tomakehurst-wiremock-jre8-standalone-2.26.1
Bump wiremock-jre8-standalone from 2.26.0 to 2.26.1
2020-02-28 08:37:27 -08:00
XiongKezhi
05e81484f1 format code 2020-02-28 16:57:19 +08:00
XiongKezhi
10cc79f737 add more properties to GHCheckRun 2020-02-28 15:40:51 +08:00
dependabot-preview[bot]
957d9b180d Bump mockito-core from 3.3.0 to 3.3.1
Bumps [mockito-core](https://github.com/mockito/mockito) from 3.3.0 to 3.3.1.
- [Release notes](https://github.com/mockito/mockito/releases)
- [Commits](https://github.com/mockito/mockito/compare/v3.3.0...v3.3.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-28 06:31:41 +00:00
dependabot-preview[bot]
883204fc43 Bump wiremock-jre8-standalone from 2.26.0 to 2.26.1
Bumps [wiremock-jre8-standalone](https://github.com/tomakehurst/wiremock) from 2.26.0 to 2.26.1.
- [Release notes](https://github.com/tomakehurst/wiremock/releases)
- [Commits](https://github.com/tomakehurst/wiremock/compare/2.26.0...2.26.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-28 06:31:11 +00:00
Liam Newman
6d3904fbbd Change name of inject methods 2020-02-27 14:27:57 -08:00
Liam Newman
56a51f18e7 Update src/main/java/org/kohsuke/github/GHIssue.java 2020-02-27 14:20:03 -08:00
Liam Newman
307508b7a0 Move GHGist from late binding to inject bind 2020-02-27 10:23:54 -08:00
Liam Newman
66fce79427 Add ability to inject other local bindings 2020-02-27 10:23:31 -08:00
Liam Newman
5e5708d8d4 Clean up GHPerson deprecated a bit more 2020-02-27 10:16:48 -08:00
Liam Newman
944d92bbb4 Make more methods as deprecated 2020-02-27 09:40:03 -08:00
Liam Newman
0155d5aa39 Make HttpConnector a functional interface 2020-02-27 09:39:03 -08:00
Liam Newman
fe4f45c2b0 Remove DeleteToken class the was never in github v3 api 2020-02-27 09:38:50 -08:00
Liam Newman
1b63a58e63 Increase coverage 2020-02-27 09:38:24 -08:00
Liam Newman
46a141db9c Use consistent templating for all tests 2020-02-25 14:43:37 -08:00
Liam Newman
66de06956c Merge pull request #696 from nobe0716/feature/delete_branch_automatically
Add automatic deletion of branches that merged by PR
2020-02-25 13:44:01 -08:00
Liam Newman
713dd62bd1 Take new snapshot 2020-02-25 12:37:58 -08:00
Liam Newman
5ac65aafad Merge pull request #715 from Sage-Pierce/#714
[#714]Fix query parameter construction of org member filter
2020-02-25 12:17:30 -08:00
Liam Newman
4aef92e6fe Merge pull request #717 from bitwiseman/task/remove-fetch-array
Deprecate PagedIterable.asList() and PagedIterable.asSet()
2020-02-25 12:14:39 -08:00
Sage Pierce
a1b0e771e5 Merge branch 'master' into #714 2020-02-25 08:46:30 -06:00
spierce
5baeac4706 [#714]More specific assertions; Add test for members with filter 2020-02-25 08:42:10 -06:00
Noah J
87aa9bd673 Merge branch 'master' into feature/delete_branch_automatically 2020-02-25 14:22:14 +09:00
Liam Newman
2ec5ca56d5 Merge branch 'master' into task/remove-fetch-array 2020-02-24 20:34:26 -08:00
Liam Newman
b5c7f83ec8 Merge pull request #718 from bitwiseman/task/iterator-remove
Task/iterator remove
2020-02-24 20:30:35 -08:00
Liam Newman
eb3ebdbf52 Merge pull request #716 from bitwiseman/task/inject-response
Use InjectableValue to provide response info
2020-02-24 20:04:40 -08:00
Liam Newman
c60698ff7e Iterator.remove() has a default implementation 2020-02-24 20:03:50 -08:00
Liam Newman
f8c2cda257 Use InjectableValue to provide response info 2020-02-24 19:54:40 -08:00
Liam Newman
48f6c195e0 Remove toIterable from RequestBuilder 2020-02-24 19:31:53 -08:00
Liam Newman
804fa60317 Deprecate asList and asSet 2020-02-24 18:40:23 -08:00
Liam Newman
d77b99d3d4 Remove fetchArray method 2020-02-24 18:28:18 -08:00
spierce
006f1271d6 [#714]Add test for new method 2020-02-24 17:22:03 -06:00
spierce
0d14514712 [#714]Formatting fix 2020-02-24 16:47:35 -06:00
spierce
f25e5f9488 [#714]Fix query parameter construction of org member filter 2020-02-24 15:57:08 -06:00
Liam Newman
9e8bbfd175 Merge pull request #712 from github-api/dependabot/maven/org.mockito-mockito-core-3.3.0
Bump mockito-core from 3.2.4 to 3.3.0
2020-02-24 07:23:34 -08:00
Liam Newman
3d11c96e23 Merge pull request #713 from github-api/oleg-nenashev-patch-1
Fix scm.url in pom.xml so that Dependabot can pick up changelogs
2020-02-24 07:23:10 -08:00
Oleg Nenashev
a670737ca5 Fix scm.url in pom.xml so that Dependabot can pick up changelogs 2020-02-24 10:10:30 +01:00
dependabot-preview[bot]
9fdd982e73 Bump mockito-core from 3.2.4 to 3.3.0
Bumps [mockito-core](https://github.com/mockito/mockito) from 3.2.4 to 3.3.0.
- [Release notes](https://github.com/mockito/mockito/releases)
- [Commits](https://github.com/mockito/mockito/compare/v3.2.4...v3.3.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-24 06:44:28 +00:00
Liam Newman
8024918e08 [maven-release-plugin] prepare for next development iteration 2020-02-22 20:19:04 -08:00
Liam Newman
cda7607e1c [maven-release-plugin] prepare release github-api-1.107 2020-02-22 20:18:57 -08:00
Liam Newman
816c83c80a Merge branch 'master' into feature/delete_branch_automatically 2020-02-22 19:33:46 -08:00
Liam Newman
0c3c490d58 Merge pull request #707 from rdvdijk/add-rocket-eyes-reactions
Add support for rocket and eyes reactions.
2020-02-22 19:33:08 -08:00
Liam Newman
99da6fb66f Merge pull request #710 from avano/checks
CheckRun - Add ability to get the head sha + Implement GH Status Event
2020-02-22 19:17:06 -08:00
Liam Newman
fa2601386c Add tests for new reactions 2020-02-22 19:16:33 -08:00
Liam Newman
122833b0e3 Merge branch 'master' into checks 2020-02-22 18:48:50 -08:00
Liam Newman
8618dbf0d5 Merge pull request #709 from bitwiseman/issue/708
Fix for NullPointer in issue search results
2020-02-22 18:42:09 -08:00
Liam Newman
a0baf33459 Fix for NullPointer in issue search results
User reported #708 which made me realize that search tests were basically all disabled.
This terms on two basic search tests and also makes it so GHIssue can continue to work without
a GHRepository set on it.

Fixes #708
2020-02-22 17:01:12 -08:00
Andrej Vano
0ee66ea928 [Status] Add GHEventPayload.Status class 2020-02-22 18:14:03 +01:00
Andrej Vano
f68d4aaf5b [CheckRun] Add ability to access HEAD SHA 2020-02-22 18:10:27 +01:00
Liam Newman
888abc9e2a Removed extraneous author javadocs 2020-02-21 15:58:39 -08:00
Liam Newman
c8115b1c10 Remove shading 2020-02-21 15:58:39 -08:00
Liam Newman
137d4f591f Update maven-build.yml 2020-02-21 15:44:31 -08:00
Liam Newman
337d49770d Update maven-build.yml 2020-02-21 15:42:28 -08:00
Liam Newman
614c5578b0 Fix code coverage and GHRepositoryTraffic test 2020-02-21 15:35:45 -08:00
Liam Newman
d456e60800 Check code coverage on ubuntu 2020-02-21 13:17:31 -08:00
Liam Newman
064206fb9a Merge pull request #706 from bitwiseman/task/guard
Add basic rate limit checker
2020-02-21 13:06:51 -08:00
Liam Newman
a68fe3b39d More javadoc because words are hard 2020-02-21 12:04:25 -08:00
Liam Newman
1904c82941 PR feedback 2020-02-21 10:52:28 -08:00
Roel van Dijk
6fc9dd4b30 Add support for rocket and eyes reactions. 2020-02-21 10:59:17 +01:00
Liam Newman
158a31e924 Added javadocs and other cleanup
Whenever I submit a PR and then start looking at it as a reviewer, I immediately find a bunch of things that need changing.
2020-02-20 18:53:13 -08:00
Liam Newman
b70b924db4 Inital implmentation of RateLimitChecker
This is a stripped down rate limit checking implmentation that handles the infrastructure
of deciding how to get rate limit information and leaves it to other implementers to
decided what kind of checks they want to do and how long they want to wait.

The implmentation supports checkers that sleep less than the full time until the
rate limit is expected to reset, allowing for polling and notifying clients of why their query
is not returning.

A basic checker which sleeps until the rate limit is expected to reset is included as working example..
2020-02-20 16:03:46 -08:00
Liam Newman
9018d72e97 Merge pull request #697 from bitwiseman/tast/response-info
Refactor `Requester` into multiple smaller classes
2020-02-20 15:59:11 -08:00
Liam Newman
5c395138ed Merge pull request #705 from github-api/dependabot/maven/org.eclipse.jgit-org.eclipse.jgit-5.6.1.202002131546-r
Bump org.eclipse.jgit from 5.6.0.201912101111-r to 5.6.1.202002131546-r
2020-02-19 16:01:40 -08:00
Liam Newman
af157adc1b Merge pull request #704 from github-api/dependabot/maven/com.squareup.okhttp3-okhttp-4.4.0
Bump okhttp from 4.3.1 to 4.4.0
2020-02-19 16:01:21 -08:00
Liam Newman
1db4fca9db Comment tweaks 2020-02-18 09:02:52 -08:00
dependabot-preview[bot]
013f475859 Bump org.eclipse.jgit from 5.6.0.201912101111-r to 5.6.1.202002131546-r
Bumps org.eclipse.jgit from 5.6.0.201912101111-r to 5.6.1.202002131546-r.

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-18 06:36:30 +00:00
dependabot-preview[bot]
b5bc38fa52 Bump okhttp from 4.3.1 to 4.4.0
Bumps [okhttp](https://github.com/square/okhttp) from 4.3.1 to 4.4.0.
- [Release notes](https://github.com/square/okhttp/releases)
- [Changelog](https://github.com/square/okhttp/blob/master/CHANGELOG.md)
- [Commits](https://github.com/square/okhttp/compare/parent-4.3.1...parent-4.4.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-18 06:36:08 +00:00
Liam Newman
bd0e0cdfa4 Revert synchronization in iterators
These were not synchronized before we should leave them fix this in a future change
2020-02-17 20:08:52 -08:00
Liam Newman
dade4c4cc4 Bump spotbugs to 4.0.0 2020-02-17 13:11:00 -08:00
Liam Newman
acc5a89dff Class clean up
We don't need two layers of PageIterator just to get the final response.
Also made iterators thread-safe.
And added more detailed comments.
2020-02-17 12:55:44 -08:00
sunghyo-jung
d34881aa25 Add updated mapping files 2020-02-17 18:43:59 +09:00
sunghyo-jung
b8ad48997b Rollback field name, add mapping files
`delete_branch_on_merge` is field of github repository,
So use it as same literal (ref. https://developer.github.com/v3/repos/)

Use `getRepository` instead of `getTempRepository` at GHRepositoryTest
2020-02-17 18:42:30 +09:00
sunghyo-jung
77754b7246 Merge branch 'feature/delete_branch_automatically' of https://github.com/nobe0716/github-api into feature/delete_branch_automatically 2020-02-17 17:05:19 +09:00
sunghyo-jung
6d5bf49a51 Apply CamelCase on field delete_branch_on_merge 2020-02-17 17:03:43 +09:00
Liam Newman
b7af635a9a Address PR feedback 2020-02-16 21:33:19 -08:00
Noah J
f53b4e959c Merge branch 'master' into feature/delete_branch_automatically 2020-02-14 01:44:39 +09:00
nobe0716
6716d156bb Apply changes 2020-02-14 01:44:04 +09:00
Liam Newman
dc33e28452 Create GitHubHttpUrlConnectionClient to encapsulate interactions with HttpUrlConnection 2020-02-13 08:37:07 -08:00
Liam Newman
9da4781759 Update src/main/java/org/kohsuke/github/GitHub.java 2020-02-12 23:59:15 -08:00
Liam Newman
0c6959cb4a Merge remote-tracking branch 'github-api/master' into tast/response-info 2020-02-12 23:52:45 -08:00
Liam Newman
ff3136df70 Move a few static helpers to GitHubClient 2020-02-12 23:48:33 -08:00
Liam Newman
326c627221 Merge pull request #698 from github-api/dependabot/maven/org.apache.maven.plugins-maven-shade-plugin-3.2.2
Bump maven-shade-plugin from 3.2.1 to 3.2.2
2020-02-12 23:33:14 -08:00
dependabot-preview[bot]
075f382a8f Bump maven-shade-plugin from 3.2.1 to 3.2.2
Bumps [maven-shade-plugin](https://github.com/apache/maven-shade-plugin) from 3.2.1 to 3.2.2.
- [Release notes](https://github.com/apache/maven-shade-plugin/releases)
- [Commits](https://github.com/apache/maven-shade-plugin/compare/maven-shade-plugin-3.2.1...maven-shade-plugin-3.2.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-13 07:29:11 +00:00
Liam Newman
dabb8fe49e Merge pull request #675 from jimmysombrero/collaborator-permissions
Added ability to specify permissions for collaborators
2020-02-12 23:28:00 -08:00
Liam Newman
90489e4392 JavaDocs and refactoring 2020-02-12 22:42:12 -08:00
Liam Newman
ad45a74f87 Further clean up of refactored classes 2020-02-12 22:42:12 -08:00
Liam Newman
60c045a713 Delete mocking test that is just too brittle to live 2020-02-12 22:42:12 -08:00
Liam Newman
f6c75e1f99 More refactoring 2020-02-12 22:42:12 -08:00
Liam Newman
dd9245f6f2 Progress commit on moving to Client/Request/Response refactor 2020-02-12 22:42:12 -08:00
Liam Newman
7310a70743 Disable two tests due to spurious mocking failures 2020-02-12 22:42:12 -08:00
Liam Newman
82276837ac Created Client/Request/Response classes 2020-02-12 22:42:12 -08:00
Liam Newman
bd68252b44 Test cleanup 2020-02-12 22:42:12 -08:00
Liam Newman
6b1258e33a Major rewrite of Requester 2020-02-12 22:42:12 -08:00
Liam Newman
0400032923 Merge branch 'master' into collaborator-permissions 2020-02-12 22:32:50 -08:00
Liam Newman
d9563322f1 Merge pull request #695 from github-api/dependabot/maven/org.kohsuke.stapler-stapler-1.259
Bump stapler from 1.258 to 1.259
2020-02-12 22:32:31 -08:00
Liam Newman
ab965969dd Merge branch 'master' into collaborator-permissions 2020-02-12 22:31:03 -08:00
Liam Newman
2f32e034d8 Merge pull request #694 from bitwiseman/task/fetch
Streamline fetch and retry process
2020-02-12 22:29:22 -08:00
sunghyo-jung
d7bb171c1e Add automatic deletion of branches that merged by PR
Add method to Toggle `delete_branch_on_merge`
Locate new fields below to merge options
2020-02-12 19:02:31 +09:00
dependabot-preview[bot]
1cf7931f43 Bump stapler from 1.258 to 1.259
Bumps [stapler](https://github.com/stapler/stapler) from 1.258 to 1.259.
- [Release notes](https://github.com/stapler/stapler/releases)
- [Changelog](https://github.com/stapler/stapler/blob/master/CHANGELOG.md)
- [Commits](https://github.com/stapler/stapler/compare/stapler-parent-1.258...stapler-parent-1.259)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-11 06:30:12 +00:00
James Vaughn
edc697dd73 fixed javadoc isues 2020-02-07 21:27:00 -06:00
James Vaughn
54a059ff68 updated signatures for addCollaborators methods
updated javadoc comments for addCollaborators methods
combined both modifyCollaborators methods into one method
updated addColloaborators test to match new method signature
2020-02-07 21:13:23 -06:00
Liam Newman
289282e235 Move OTP detection earlier
Like other errors we've been waiting until later to catch, this one is detectable so
whe should do that before the downstream exception needs to be thrown.
2020-02-06 16:59:49 -08:00
Liam Newman
825c36c15e Tweaks for clarity 2020-02-06 14:11:16 -08:00
Liam Newman
1234c2e99e Add tests for rate limit 2020-02-06 13:33:29 -08:00
Liam Newman
b8fae1308d Streamline fetch and retry process 2020-02-06 08:01:17 -08:00
James Vaughn
dddcf624e6 finished suggested formatting changes 2020-02-05 21:30:52 -06:00
James Vaughn
b33fe9f7fe Merge branch 'collaborator-permissions' of https://github.com/jimmysombrero/github-api into collaborator-permissions 2020-02-04 21:10:39 -06:00
James Vaughn
5a799400a9 fixed minor formatting issues 2020-02-04 21:10:29 -06:00
Liam Newman
76919a819f Merge branch 'master' into collaborator-permissions 2020-02-03 12:46:17 -08:00
Liam Newman
9c30f846b2 Merge pull request #692 from github-api/dependabot/maven/com.github.tomakehurst-wiremock-jre8-standalone-2.26.0
Bump wiremock-jre8-standalone from 2.25.1 to 2.26.0
2020-02-03 12:45:46 -08:00
Liam Newman
9230f51988 Implement new methods on Options 2020-02-03 12:41:33 -08:00
dependabot-preview[bot]
712035dc9a Bump wiremock-jre8-standalone from 2.25.1 to 2.26.0
Bumps [wiremock-jre8-standalone](https://github.com/tomakehurst/wiremock) from 2.25.1 to 2.26.0.
- [Release notes](https://github.com/tomakehurst/wiremock/releases)
- [Commits](https://github.com/tomakehurst/wiremock/compare/2.25.1...2.26.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2020-02-03 12:34:01 -08:00
Liam Newman
32e5c5b4ad Merge pull request #693 from alexanderrtaylor/replaceAsserts
Replacing asserts with standard
2020-02-03 12:29:59 -08:00
Alex Taylor
134a6fab7e Merge branch 'master' into replaceAsserts 2020-02-03 14:47:11 -05:00
Alex Taylor
82e27cb962 Replacing asserts with standard
Beginning work to replace Asserts with assertThat to keep consistency
2020-02-03 11:43:16 -05:00
James Vaughn
8bcad7b3f9 Merge branch 'collaborator-permissions' of https://github.com/jimmysombrero/github-api into collaborator-permissions 2020-02-02 22:03:08 -06:00
James Vaughn
d767575f76 Added test for addCollaborators
Mark old addCollaborators methods depricated
2020-02-02 22:02:56 -06:00
jimmysombrero
7214c7d393 Merge branch 'master' into collaborator-permissions 2020-02-01 22:40:54 -06:00
James Vaughn
205e5ab03d Merge branch 'collaborator-permissions' of https://github.com/jimmysombrero/github-api into collaborator-permissions 2020-02-01 22:39:28 -06:00
James Vaughn
6576beae76 Added missing methods to keep the API from breaking
added tests for addCollaborators()
2020-02-01 22:39:14 -06:00
Liam Newman
001f8f1f50 Update PULL_REQUEST_TEMPLATE.md 2020-01-31 13:18:17 -08:00
Liam Newman
3b694a87ef Fix formatting 2020-01-31 13:17:16 -08:00
spierce
84dd06d769 [#690]Implement the ability to read total_private_repos for GHPerson 2020-01-31 13:44:43 -06:00
Liam Newman
c5cb16abfb Merge branch 'master' into collaborator-permissions 2020-01-31 08:32:35 -08:00
Liam Newman
79fb34324d Merge pull request #684 from alexanderrtaylor/assertThatIssues
Authentication and assertThat issues
2020-01-30 11:10:38 -08:00
Alex Taylor
303aef3548 Merge branch 'assertThatIssues' of https://github.com/alexanderrtaylor/github-api into assertThatIssues 2020-01-30 11:22:39 -05:00
Alex Taylor
fd278f8c32 Update AbstractGitHubWireMockTest.java
added return for Javadoc
2020-01-30 11:22:37 -05:00
Liam Newman
53041a4117 Merge branch 'master' into assertThatIssues 2020-01-29 18:32:47 -08:00
Liam Newman
9b3fe3b13a Merge pull request #687 from bitwiseman/task/windows-ci
Shorten file names for Windows
2020-01-29 18:06:48 -08:00
Liam Newman
5c6c5081e9 Improve naming of jobs 2020-01-29 18:00:30 -08:00
Liam Newman
e087ea0ac7 Split build jobs for clarity 2020-01-29 17:36:31 -08:00
Liam Newman
71c44dc805 Move CI to cache .m2 directory 2020-01-29 17:18:19 -08:00
Liam Newman
c5c8596664 Reduce output from connection retry 2020-01-29 17:04:24 -08:00
Liam Newman
92a86f4d1c No formatting checks on Windows CI build 2020-01-29 16:01:37 -08:00
Alex Taylor
8098b68b8e formatting changes
sorry forgot to run the formatting automation before committing the last
2020-01-29 16:56:49 -05:00
Alex Taylor
7356001723 Merge remote-tracking branch 'upstream/master' into assertThatIssues 2020-01-29 16:56:15 -05:00
Alex Taylor
aba60587ab Corrected massive change
Added back the extends Assert to the wiremock test base class so that I am not making a massive change and potentially  breaking inflight work people are doing
2020-01-29 16:49:33 -05:00
Liam Newman
936a6a04fb Reduce some code coverage bars on windows 2020-01-29 13:38:08 -08:00
Liam Newman
9675126298 Turn off some tests for windows 2020-01-29 13:30:01 -08:00
Liam Newman
6a5886ea1c Disable import sorting 2020-01-29 11:38:32 -08:00
Liam Newman
648c6a5a8f Shorten file names for Windows 2020-01-29 09:24:50 -08:00
Liam Newman
14b7bf4753 Add windows to CI matrix 2020-01-28 20:52:49 -08:00
jimmysombrero
0e310fa96a Merge branch 'master' into collaborator-permissions 2020-01-28 15:21:25 -06:00
Liam Newman
0f6c282c80 [maven-release-plugin] prepare for next development iteration 2020-01-27 19:07:15 -08:00
Alex Taylor
4c3a0d329b added Authentication Check
Added additional authentication checks on gitHubBeforeAfter so that cleanup is done with a user logged in
2020-01-27 15:24:28 -05:00
Alex Taylor
7c495c2177 Fixes after merge
Fixed some failing tests after merge
2020-01-27 14:45:02 -05:00
Alex Taylor
2f86a9e534 Merge remote-tracking branch 'upstream/master' into assertThatIssues 2020-01-27 14:37:46 -05:00
Alex Taylor
12c3a0b1fa Authentication and assertThat issues
Fixed some problems with tests trying to authenticate when you are not actually signed in. This hit rate API limiting which caused tests to hang/fail

Also fixed assertThat getting deprecated from junit
2020-01-27 14:29:46 -05:00
jimmysombrero
58c069ec5c Merge branch 'master' into collaborator-permissions 2020-01-24 18:26:54 -06:00
James Vaughn
7916600a7b updating local branch 2020-01-24 18:24:53 -06:00
James Vaughn
3e4f160c5d ran mvn clean install then the build commmand and build was successful 2020-01-24 18:23:59 -06:00
Liam Newman
ad683fee89 Merge branch 'master' into collaborator-permissions 2020-01-23 17:51:59 -08:00
James Vaughn
3bf8baee85 format fixed and validation passed 2020-01-23 18:32:37 -06:00
Vaughn
8792213594 tried formatting the file once again. 2020-01-23 15:16:36 -06:00
Vaughn
9ab8bdfe4a formatted file 2020-01-23 14:45:24 -06:00
Vaughn
90301ae9ee Added ability to specify permissions for collaborators 2020-01-23 13:37:35 -06:00
4296 changed files with 145547 additions and 78151 deletions

View File

@@ -7,4 +7,6 @@ We love getting PRs, but we hate asking people for the same basic changes every
- [ ] Push your changes to a branch other than `master`. Create your PR from that branch.
- [ ] Add JavaDocs and other comments
- [ ] Write tests that run and pass in CI. See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to capture snapshot data.
- [ ] Run `mvn -P ci install site` locally. This may reformat your code, commit those changes. If this command doesn't succeed, your change will not pass CI.
- [ ] Run `mvn clean compile` locally. This may reformat your code, commit those changes.
- [ ] Run `mvn -D enable-ci clean install site` locally. If this command doesn't succeed, your change will not pass CI.

View File

@@ -1,22 +1,72 @@
name: Java CI Build and Test
name: CI
on: [push, pull_request]
jobs:
build:
name: build-only (Java ${{ matrix.java }})
runs-on: ubuntu-latest
strategy:
matrix:
java: [ '1.8.0', '11.0.x', '13.0.x' ]
java: [ 11 ]
steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Cached .m2
uses: actions/cache@v1
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Maven Install (skipTests)
run: mvn -B install -DskipTests -D enable-ci --file pom.xml
site:
name: site (Java ${{ matrix.java }})
runs-on: ubuntu-latest
strategy:
matrix:
java: [ 8, 11 ]
steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- uses: actions/cache@v1
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Maven Site
run: mvn -B site -D enable-ci --file pom.xml
test:
name: test (${{ matrix.os }}, Java ${{ matrix.java }})
runs-on: ${{ matrix.os }}-latest
strategy:
matrix:
os: [ ubuntu, windows ]
java: [ 8, 11, 13 ]
steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: ${{ matrix.java }}
- name: Maven Download all dependencies
run: mvn -B org.apache.maven.plugins:maven-dependency-plugin:3.1.1:go-offline -P ci
- name: Maven Build
run: mvn -B install site -P ci --file pom.xml
- uses: actions/cache@v1
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Maven Install without Code Coverage
if: matrix.os == 'windows'
run: mvn -B install --file pom.xml
- name: Maven Install with Code Coverage
if: matrix.os != 'windows'
run: mvn -B install -D enable-ci --file pom.xml

2
.gitignore vendored
View File

@@ -9,3 +9,5 @@ target
.DS_Store
dependency-reduced-pom.xml
.factorypath
.vscode/settings.json

View File

@@ -2,6 +2,8 @@
[![Sonatype Nexus (Releases)](https://img.shields.io/nexus/r/org.kohsuke/github-api?server=https%3A%2F%2Foss.sonatype.org)](https://mvnrepository.com/artifact/org.kohsuke/github-api)
[![Join the chat at https://gitter.im/github-api/github-api](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/github-api/github-api?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
![CI](https://github.com/github-api/github-api/workflows/CI/badge.svg?branch=master)
See https://github-api.kohsuke.org/ for more details

134
pom.xml
View File

@@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<version>1.106</version>
<version>1.111</version>
<name>GitHub API for Java</name>
<url>https://github-api.kohsuke.org/</url>
<description>GitHub API for Java</description>
@@ -10,8 +10,8 @@
<scm>
<connection>scm:git:git@github.com/github-api/${project.artifactId}.git</connection>
<developerConnection>scm:git:ssh://git@github.com/github-api/${project.artifactId}.git</developerConnection>
<url>https://${project.artifactId}.kohsuke.org/</url>
<tag>github-api-1.106</tag>
<url>https://github.com/github-api/github-api/</url>
<tag>github-api-1.111</tag>
</scm>
<distributionManagement>
@@ -33,16 +33,16 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spotbugs-maven-plugin.version>3.1.12.2</spotbugs-maven-plugin.version>
<spotbugs.version>3.1.12</spotbugs.version>
<spotbugs-maven-plugin.version>4.0.0</spotbugs-maven-plugin.version>
<spotbugs.version>4.0.2</spotbugs.version>
<spotbugs-maven-plugin.failOnError>true</spotbugs-maven-plugin.failOnError>
<hamcrest.version>2.2</hamcrest.version>
<okhttp3.version>4.3.1</okhttp3.version>
<okio.version>2.4.3</okio.version>
<okhttp3.version>4.4.1</okhttp3.version>
<okio.version>2.5.0</okio.version>
<formatter-maven-plugin.goal>format</formatter-maven-plugin.goal>
<impsort-maven-plugin.goal>sort</impsort-maven-plugin.goal>
<!-- Using this as the minimum bar for code coverage. Adding methods without covering them will fail this. -->
<jacoco.coverage.target.bundle.method>0.556</jacoco.coverage.target.bundle.method>
<jacoco.coverage.target.bundle.method>0.60</jacoco.coverage.target.bundle.method>
<jacoco.coverage.target.class.method>0.25</jacoco.coverage.target.class.method>
<!-- For non-ci builds we'd like the build to still complete if jacoco metrics aren't met. -->
<jacoco.haltOnFailure>false</jacoco.haltOnFailure>
@@ -79,54 +79,6 @@
</testResources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>shaded-jar</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>hidden.com.fasterxml.jackson</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>hidden.org.apache</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<excludes>
<exclude>com.infradna.tool:bridge-method-annotation</exclude>
<exclude>org.jenkins-ci:annotation-indexer</exclude>
<exclude>com.squareup.*:*</exclude>
<exclude>org.jetbrains*:*</exclude>
<exclude>com.github.spotbugs:*</exclude>
<exclude>com.google.code.findbugs:*</exclude>
</excludes>
</artifactSet>
<shadedArtifactAttached>true</shadedArtifactAttached>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
@@ -194,25 +146,24 @@
<exclude>org.kohsuke.github.example.*</exclude>
<!-- No methods -->
<exclude>org.kohsuke.github.DeleteToken</exclude>
<exclude>org.kohsuke.github.Previews</exclude>
<!-- Deprecated -->
<exclude>org.kohsuke.github.extras.OkHttp3Connector</exclude>
<exclude>org.kohsuke.github.EnforcementLevel</exclude>
<exclude>org.kohsuke.github.GHPerson.1</exclude>
<exclude>org.kohsuke.github.GHPerson.1.1</exclude>
<!-- These fail coverage on windows because tests are disabled -->
<exclude>org.kohsuke.github.GHAsset</exclude>
<exclude>org.kohsuke.github.GHReleaseBuilder</exclude>
<exclude>org.kohsuke.github.GHRelease</exclude>
<!-- TODO: These still need test coverage -->
<exclude>org.kohsuke.github.GitHub.GHApiInfo</exclude>
<exclude>org.kohsuke.github.GHBranchProtection.RequiredSignatures</exclude>
<exclude>org.kohsuke.github.GHBranchProtectionBuilder.Restrictions</exclude>
<exclude>org.kohsuke.github.GHBranchProtection.Restrictions</exclude>
<exclude>org.kohsuke.github.GHCommentAuthorAssociation</exclude>
<exclude>org.kohsuke.github.GHCommitBuilder.UserInfo</exclude>
<exclude>org.kohsuke.github.GHCommitSearchBuilder.CommitSearchResult</exclude>
<exclude>org.kohsuke.github.GHCommitSearchBuilder.Sort</exclude>
<exclude>org.kohsuke.github.GHCommitSearchBuilder</exclude>
<exclude>org.kohsuke.github.GHCommitState</exclude>
<exclude>org.kohsuke.github.GHCompare.Commit</exclude>
<exclude>org.kohsuke.github.GHCompare.InnerCommit</exclude>
@@ -230,9 +181,6 @@
<exclude>org.kohsuke.github.GHHook</exclude>
<exclude>org.kohsuke.github.GHHooks.OrgContext</exclude>
<exclude>org.kohsuke.github.GHInvitation</exclude>
<exclude>org.kohsuke.github.GHIssueSearchBuilder.IssueSearchResult</exclude>
<exclude>org.kohsuke.github.GHIssueSearchBuilder.Sort</exclude>
<exclude>org.kohsuke.github.GHIssueSearchBuilder</exclude>
<exclude>org.kohsuke.github.GHMilestoneState</exclude>
<exclude>org.kohsuke.github.GHOrgHook</exclude>
<exclude>org.kohsuke.github.GHProject.ProjectStateFilter</exclude>
@@ -251,7 +199,6 @@
<exclude>org.kohsuke.github.GHTeam.Role</exclude>
<exclude>org.kohsuke.github.GHUserSearchBuilder.Sort</exclude>
<exclude>org.kohsuke.github.GHVerifiedKey</exclude>
<exclude>org.kohsuke.github.GitHubBuilder.1</exclude>
</excludes>
</rule>
</rules>
@@ -262,7 +209,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<version>3.2.0</version>
<configuration>
<source>8</source>
<failOnWarnings>true</failOnWarnings>
@@ -286,7 +233,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
<version>3.9.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -334,6 +281,10 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!-- SUREFIRE-1226 workaround -->
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
@@ -494,7 +445,7 @@
<dependency>
<groupId>org.kohsuke.stapler</groupId>
<artifactId>stapler</artifactId>
<version>1.258</version>
<version>1.259</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -506,7 +457,7 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.6.0.201912101111-r</version>
<version>5.7.0.202003110725-r</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -544,7 +495,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.2.4</version>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -556,7 +507,7 @@
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.25.1</version>
<version>2.26.3</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -580,23 +531,32 @@
</pluginRepositories>
<profiles>
<profile>
<id>ci</id>
<id>ci-non-windows</id>
<activation>
<property>
<name>enable-ci</name>
</property>
<os>
<family>!windows</family>
</os>
</activation>
<properties>
<formatter-maven-plugin.goal>validate</formatter-maven-plugin.goal>
<impsort-maven-plugin.goal>check</impsort-maven-plugin.goal>
</properties>
</profile>
<profile>
<id>ci-all</id>
<activation>
<property>
<name>enable-ci</name>
</property>
</activation>
<properties>
<formatter-maven-plugin.goal>validate</formatter-maven-plugin.goal>
<impsort-maven-plugin.goal>check</impsort-maven-plugin.goal>
<jacoco.haltOnFailure>true</jacoco.haltOnFailure>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
@@ -613,8 +573,8 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
@@ -665,6 +625,18 @@
</profiles>
<reporting>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>

View File

@@ -0,0 +1,165 @@
package org.kohsuke.github;
import java.io.IOException;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* An abstract data object builder/updater.
*
* This class can be use to make a Builder that supports both batch and single property changes.
* <p>
* Batching looks like this:
* </p>
*
* <pre>
* update().someName(value).otherName(value).done()
* </pre>
* <p>
* Single changes look like this:
* </p>
*
* <pre>
* set().someName(value);
* set().otherName(value);
* </pre>
* <p>
* If {@link S} is the same as {@link R}, {@link #with(String, Object)} will commit changes after the first value change
* and return a {@link R} from {@link #done()}.
* </p>
* <p>
* If {@link S} is not the same as {@link R}, {@link #with(String, Object)} will batch together multiple changes and let
* the user call {@link #done()} when they are ready.
*
* @param <R>
* Final return type built by this builder returned when {@link #done()}} is called.
* @param <S>
* Intermediate return type for this builder returned by calls to {@link #with(String, Object)}. If {@link S}
* the same as {@link R}, this builder will commit changes after each call to {@link #with(String, Object)}.
*/
abstract class AbstractBuilder<R, S> {
@Nonnull
private final Class<R> returnType;
private final boolean commitChangesImmediately;
@CheckForNull
private final R baseInstance;
@Nonnull
protected final Requester requester;
// TODO: Not sure how update-in-place behavior should be controlled
// However, it certainly can be controlled dynamically down to the instance level or inherited for all children of
// some
// connection.
protected boolean updateInPlace;
/**
* Creates a builder.
*
* @param root
* the GitHub instance to connect to.
* @param intermediateReturnType
* the intermediate return type of type {@link S} returned by calls to {@link #with(String, Object)}.
* Must either be equal to {@code builtReturnType} or this instance must be castable to this class. If
* not, the constructor will throw {@link IllegalArgumentException}.
* @param finalReturnType
* the final return type for built by this builder returned when {@link #done()}} is called.
* @param baseInstance
* optional instance on which to base this builder.
*/
protected AbstractBuilder(@Nonnull Class<R> finalReturnType,
@Nonnull Class<S> intermediateReturnType,
@Nonnull GitHub root,
@CheckForNull R baseInstance) {
this.requester = root.createRequest();
this.returnType = finalReturnType;
this.commitChangesImmediately = returnType.equals(intermediateReturnType);
if (!commitChangesImmediately && !intermediateReturnType.isInstance(this)) {
throw new IllegalArgumentException(
"Argument \"intermediateReturnType\": This instance must be castable to intermediateReturnType or finalReturnType must be equal to intermediateReturnType.");
}
this.baseInstance = baseInstance;
this.updateInPlace = false;
}
/**
* Finishes an update, committing changes.
*
* This method may update-in-place or not. Either way it returns the resulting instance.
*
* @return an instance with updated current data
* @throws IOException
* if there is an I/O Exception
*/
@Nonnull
@Preview
@Deprecated
public R done() throws IOException {
R result;
if (updateInPlace && baseInstance != null) {
result = requester.fetchInto(baseInstance);
} else {
result = requester.fetch(returnType);
}
return result;
}
/**
* Applies a value to a name for this builder.
*
* If {@link S} is the same as {@link R}, this method will commit changes after the first value change and return a
* {@link R} from {@link #done()}.
*
* If {@link S} is not the same as {@link R}, this method will return an {@link S} and letting the caller batch
* together multiple changes and call {@link #done()} when they are ready.
*
* @param name
* the name of the field
* @param value
* the value of the field
* @return either a continuing builder or an updated data record
* @throws IOException
* if an I/O error occurs
*/
@Nonnull
@Preview
@Deprecated
protected S with(@Nonnull String name, Object value) throws IOException {
requester.with(name, value);
return continueOrDone();
}
/**
* Chooses whether to return a continuing builder or an updated data record
*
* If {@link S} is the same as {@link R}, this method will commit changes after the first value change and return a
* {@link R} from {@link #done()}.
*
* If {@link S} is not the same as {@link R}, this method will return an {@link S} and letting the caller batch
* together multiple changes and call {@link #done()} when they are ready.
*
* @return either a continuing builder or an updated data record
* @throws IOException
* if an I/O error occurs
*/
@Nonnull
@Preview
@Deprecated
protected S continueOrDone() throws IOException {
// This little bit of roughness in this base class means all inheriting builders get to create Updater and
// Setter classes from almost identical code. Creator can often be implemented with significant code reuse as
// well.
if (commitChangesImmediately) {
// These casts look strange and risky, but they they're actually guaranteed safe due to the return path
// being based on the previous comparison of class instances passed to the constructor.
return (S) done();
} else {
return (S) this;
}
}
}

View File

@@ -29,6 +29,10 @@ public abstract class AbuseLimitHandler {
* @throws IOException
* on failure
* @see <a href="https://developer.github.com/v3/#abuse-rate-limits">API documentation from GitHub</a>
* @see <a href=
* "https://developer.github.com/v3/guides/best-practices-for-integrators/#dealing-with-abuse-rate-limits">Dealing
* with abuse rate limits</a>
*
*/
public abstract void onError(IOException e, HttpURLConnection uc) throws IOException;

View File

@@ -1,35 +0,0 @@
/*
* The MIT License
*
* Copyright (c) 2010, Kohsuke Kawaguchi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* @author Kohsuke Kawaguchi
*/
@SuppressFBWarnings(value = "UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD",
justification = "Being constructed by JSON deserialization")
class DeleteToken {
public String delete_token;
}

View File

@@ -140,7 +140,7 @@ public class GHApp extends GHObject {
}
public URL getHtmlUrl() {
return GitHub.parseURL(htmlUrl);
return GitHubClient.parseURL(htmlUrl);
}
/**

View File

@@ -63,7 +63,7 @@ public class GHAppCreateTokenBuilder {
public GHAppCreateTokenBuilder permissions(Map<String, GHPermissionType> permissions) {
Map<String, String> retMap = new HashMap<>();
for (Map.Entry<String, GHPermissionType> entry : permissions.entrySet()) {
retMap.put(entry.getKey(), Requester.transformEnum(entry.getValue()));
retMap.put(entry.getKey(), GitHubRequest.transformEnum(entry.getValue()));
}
builder.with("permissions", retMap);
return this;

View File

@@ -42,7 +42,7 @@ public class GHAppInstallation extends GHObject {
private String htmlUrl;
public URL getHtmlUrl() {
return GitHub.parseURL(htmlUrl);
return GitHubClient.parseURL(htmlUrl);
}
/**

View File

@@ -127,7 +127,7 @@ public class GHAppInstallationToken {
*/
@WithBridgeMethods(value = String.class, adapterMethod = "expiresAtStr")
public Date getExpiresAt() throws IOException {
return GitHub.parseDate(expires_at);
return GitHubClient.parseDate(expires_at);
}
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getExpiresAt")

View File

@@ -96,7 +96,7 @@ public class GHAuthorization extends GHObject {
* @return the app url
*/
public URL getAppUrl() {
return GitHub.parseURL(app.url);
return GitHubClient.parseURL(app.url);
}
/**
@@ -115,7 +115,7 @@ public class GHAuthorization extends GHObject {
*/
@SuppressFBWarnings(value = "NM_CONFUSING", justification = "It's a part of the library API, cannot be changed")
public URL getApiURL() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -141,7 +141,7 @@ public class GHAuthorization extends GHObject {
* @return the note url
*/
public URL getNoteUrl() {
return GitHub.parseURL(note_url);
return GitHubClient.parseURL(note_url);
}
/**

View File

@@ -24,7 +24,7 @@ public class GHBlob {
* @return API URL of this blob.
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**

View File

@@ -90,7 +90,7 @@ public class GHBranch {
@Preview
@Deprecated
public URL getProtectionUrl() {
return GitHub.parseURL(protection_url);
return GitHubClient.parseURL(protection_url);
}
/**

View File

@@ -4,13 +4,13 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
/**
* Represents a deployment
* Represents a check run.
*
* @see <a href="https://developer.github.com/v3/checks/runs/">documentation</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD" },
justification = "JSON API")
public class GHCheckRun extends GHObject {
@@ -20,7 +20,17 @@ public class GHCheckRun extends GHObject {
private String status;
private String conclusion;
private String name;
private String headSha;
private String nodeId;
private String externalId;
private String startedAt;
private String completedAt;
private URL htmlUrl;
private URL detailsUrl;
private Output output;
private GHApp app;
private GHPullRequest[] pullRequests;
private GHCheckSuite checkSuite;
GHCheckRun wrap(GHRepository owner) {
this.owner = owner;
@@ -40,18 +50,57 @@ public class GHCheckRun extends GHObject {
return pullRequests;
}
/**
* Gets status of the check run.
*
* @return Status of the check run
* @see Status
*/
public String getStatus() {
return status;
}
public static enum Status {
QUEUED, IN_PROGRESS, COMPLETED
}
/**
* Gets conclusion of a completed check run.
*
* @return Status of the check run
* @see Conclusion
*/
public String getConclusion() {
return conclusion;
}
public static enum Conclusion {
SUCCESS, FAILURE, NEUTRAL, CANCELLED, TIMED_OUT, ACTION_REQUIRED
}
/**
* Gets the custom name of this check run.
*
* @return Name of the check run
*/
public String getName() {
return name;
}
/**
* Gets the HEAD SHA.
*
* @return sha for the HEAD commit
*/
public String getHeadSha() {
return headSha;
}
/**
* Gets the pull requests participated in this check run.
*
* @return Pull requests of this check run
*/
GHPullRequest[] getPullRequests() throws IOException {
if (pullRequests != null && pullRequests.length != 0) {
for (GHPullRequest singlePull : pullRequests) {
@@ -62,11 +111,149 @@ public class GHCheckRun extends GHObject {
}
/**
* @deprecated This object has no HTML URL.
* Gets the HTML URL: https://github.com/[owner]/[repo-name]/runs/[check-run-id], usually an GitHub Action page of
* the check run.
*
* @return HTML URL
*/
@Override
public URL getHtmlUrl() {
return null;
return htmlUrl;
}
/**
* Gets the global node id to access most objects in GitHub.
*
* @see <a href="https://developer.github.com/v4/guides/using-global-node-ids/">documentation</a>
* @return Global node id
*/
public String getNodeId() {
return nodeId;
}
/**
* Gets a reference for the check run on the integrator's system.
*
* @return Reference id
*/
public String getExternalId() {
return externalId;
}
/**
* Gets the details URL from which to find full details of the check run on the integrator's site.
*
* @return Details URL
*/
public URL getDetailsUrl() {
return detailsUrl;
}
/**
* Gets the start time of the check run in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
*
* @return Timestamp of the start time
*/
public Date getStartedAt() {
return GitHubClient.parseDate(startedAt);
}
/**
* Gets the completed time of the check run in ISO 8601 format: YYYY-MM-DDTHH:MM:SSZ.
*
* @return Timestamp of the completed time
*/
public Date getCompletedAt() {
return GitHubClient.parseDate(completedAt);
}
/**
* Gets the GitHub app this check run belongs to, included in response.
*
* @return GitHub App
*/
public GHApp getApp() {
return app;
}
/**
* Gets the check suite this check run belongs to
*
* @return Check suite
*/
public GHCheckSuite getCheckSuite() {
return checkSuite;
}
/**
* Gets an output for a check run.
*
* @return Output of a check run
*/
public Output getOutput() {
return output;
}
/**
* Represents an output in a check run to include summary and other results.
*
* @see <a href="https://developer.github.com/v3/checks/runs/#output-object">documentation</a>
*/
public static class Output {
private String title;
private String summary;
private String text;
private int annotationsCount;
private URL annotationsUrl;
/**
* Gets the title of check run.
*
* @return title of check run
*/
public String getTitle() {
return title;
}
/**
* Gets the summary of the check run, note that it supports Markdown.
*
* @return summary of check run
*/
public String getSummary() {
return summary;
}
/**
* Gets the details of the check run, note that it supports Markdown.
*
* @return Details of the check run
*/
public String getText() {
return text;
}
/**
* Gets the annotation count of a check run.
*
* @return annotation count of a check run
*/
public int getAnnotationsCount() {
return annotationsCount;
}
/**
* Gets the URL of annotations.
*
* @return URL of annotations
*/
public URL getAnnotationsUrl() {
return annotationsUrl;
}
}
public static enum AnnotationLevel {
NOTICE, WARNING, FAILURE
}
}

View File

@@ -0,0 +1,291 @@
/*
* The MIT License
*
* Copyright 2020 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonInclude;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
/**
* Drafts a check run.
*
* @see GHCheckRun
* @see GHRepository#createCheckRun
* @see <a href="https://developer.github.com/v3/checks/runs/#create-a-check-run">documentation</a>
*/
@SuppressFBWarnings(value = "URF_UNREAD_FIELD", justification = "Jackson serializes these even without a getter")
@Preview
@Deprecated
public final class GHCheckRunBuilder {
private final GHRepository repo;
private final Requester requester;
private Output output;
private List<Action> actions;
GHCheckRunBuilder(GHRepository repo, String name, String headSHA) {
this.repo = repo;
requester = repo.root.createRequest()
.withPreview(Previews.ANTIOPE)
.method("POST")
.with("name", name)
.with("head_sha", headSHA)
.withUrlPath(repo.getApiTailUrl("check-runs"));
}
public @NonNull GHCheckRunBuilder withDetailsURL(@CheckForNull String detailsURL) {
requester.with("details_url", detailsURL);
return this;
}
public @NonNull GHCheckRunBuilder withExternalID(@CheckForNull String externalID) {
requester.with("external_id", externalID);
return this;
}
public @NonNull GHCheckRunBuilder withStatus(@CheckForNull GHCheckRun.Status status) {
if (status != null) {
// Do *not* use the overload taking Enum, as that s/_/-/g which would be wrong here.
requester.with("status", status.toString().toLowerCase(Locale.ROOT));
}
return this;
}
public @NonNull GHCheckRunBuilder withConclusion(@CheckForNull GHCheckRun.Conclusion conclusion) {
if (conclusion != null) {
requester.with("conclusion", conclusion.toString().toLowerCase(Locale.ROOT));
}
return this;
}
public @NonNull GHCheckRunBuilder withStartedAt(@CheckForNull Date startedAt) {
if (startedAt != null) {
requester.with("started_at", GitHubClient.printDate(startedAt));
}
return this;
}
public @NonNull GHCheckRunBuilder withCompletedAt(@CheckForNull Date completedAt) {
if (completedAt != null) {
requester.with("completed_at", GitHubClient.printDate(completedAt));
}
return this;
}
public @NonNull GHCheckRunBuilder add(@NonNull Output output) {
if (this.output != null) {
throw new IllegalStateException("cannot add Output twice");
}
this.output = output;
return this;
}
public @NonNull GHCheckRunBuilder add(@NonNull Action action) {
if (actions == null) {
actions = new LinkedList<>();
}
actions.add(action);
return this;
}
private static final int MAX_ANNOTATIONS = 50;
/**
* Actually creates the check run. (If more than fifty annotations were requested, this is done in batches.)
*
* @return the resulting run
* @throws IOException
* for the usual reasons
*/
public @NonNull GHCheckRun create() throws IOException {
List<Annotation> extraAnnotations;
if (output != null && output.annotations != null && output.annotations.size() > MAX_ANNOTATIONS) {
extraAnnotations = output.annotations.subList(MAX_ANNOTATIONS, output.annotations.size());
output.annotations = output.annotations.subList(0, MAX_ANNOTATIONS);
} else {
extraAnnotations = Collections.emptyList();
}
GHCheckRun run = requester.with("output", output).with("actions", actions).fetch(GHCheckRun.class).wrap(repo);
while (!extraAnnotations.isEmpty()) {
Output output2 = new Output(output.title, output.summary);
int i = Math.min(extraAnnotations.size(), MAX_ANNOTATIONS);
output2.annotations = extraAnnotations.subList(0, i);
extraAnnotations = extraAnnotations.subList(i, extraAnnotations.size());
run = repo.root.createRequest()
.withPreview(Previews.ANTIOPE)
.method("PATCH")
.with("output", output2)
.withUrlPath(repo.getApiTailUrl("check-runs/" + run.id))
.fetch(GHCheckRun.class)
.wrap(repo);
}
return run;
}
/**
* @see <a href="https://developer.github.com/v3/checks/runs/#output-object">documentation</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Output {
private final String title;
private final String summary;
private String text;
private List<Annotation> annotations;
private List<Image> images;
public Output(@NonNull String title, @NonNull String summary) {
this.title = title;
this.summary = summary;
}
public @NonNull Output withText(@CheckForNull String text) {
this.text = text;
return this;
}
public @NonNull Output add(@NonNull Annotation annotation) {
if (annotations == null) {
annotations = new LinkedList<>();
}
annotations.add(annotation);
return this;
}
public @NonNull Output add(@NonNull Image image) {
if (images == null) {
images = new LinkedList<>();
}
images.add(image);
return this;
}
}
/**
* @see <a href="https://developer.github.com/v3/checks/runs/#annotations-object">documentation</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Annotation {
private final String path;
private final int start_line;
private final int end_line;
private final String annotation_level;
private final String message;
private Integer start_column;
private Integer end_column;
private String title;
private String raw_details;
public Annotation(@NonNull String path,
int line,
@NonNull GHCheckRun.AnnotationLevel annotationLevel,
@NonNull String message) {
this(path, line, line, annotationLevel, message);
}
public Annotation(@NonNull String path,
int startLine,
int endLine,
@NonNull GHCheckRun.AnnotationLevel annotationLevel,
@NonNull String message) {
this.path = path;
start_line = startLine;
end_line = endLine;
annotation_level = annotationLevel.toString().toLowerCase(Locale.ROOT);
this.message = message;
}
public @NonNull Annotation withStartColumn(@CheckForNull Integer startColumn) {
start_column = startColumn;
return this;
}
public @NonNull Annotation withEndColumn(@CheckForNull Integer endColumn) {
end_column = endColumn;
return this;
}
public @NonNull Annotation withTitle(@CheckForNull String title) {
this.title = title;
return this;
}
public @NonNull Annotation withRawDetails(@CheckForNull String rawDetails) {
raw_details = rawDetails;
return this;
}
}
/**
* @see <a href="https://developer.github.com/v3/checks/runs/#images-object">documentation</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Image {
private final String alt;
private final String image_url;
private String caption;
public Image(@NonNull String alt, @NonNull String imageURL) {
this.alt = alt;
image_url = imageURL;
}
public @NonNull Image withCaption(@CheckForNull String caption) {
this.caption = caption;
return this;
}
}
/**
* @see <a href="https://developer.github.com/v3/checks/runs/#actions-object">documentation</a>
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
public static final class Action {
private final String label;
private final String description;
private final String identifier;
public Action(@NonNull String label, @NonNull String description, @NonNull String identifier) {
this.label = label;
this.description = description;
this.identifier = identifier;
}
}
}

View File

@@ -0,0 +1,44 @@
package org.kohsuke.github;
import java.util.Iterator;
import javax.annotation.Nonnull;
/**
* Iterable for check-runs listing.
*/
class GHCheckRunsIterable extends PagedIterable<GHCheckRun> {
private GitHub root;
private final GitHubRequest request;
private GHCheckRunsPage result;
public GHCheckRunsIterable(GitHub root, GitHubRequest request) {
this.root = root;
this.request = request;
}
@Nonnull
@Override
public PagedIterator<GHCheckRun> _iterator(int pageSize) {
return new PagedIterator<>(
adapt(GitHubPageIterator.create(root.getClient(), GHCheckRunsPage.class, request, pageSize)),
null);
}
protected Iterator<GHCheckRun[]> adapt(final Iterator<GHCheckRunsPage> base) {
return new Iterator<GHCheckRun[]>() {
public boolean hasNext() {
return base.hasNext();
}
public GHCheckRun[] next() {
GHCheckRunsPage v = base.next();
if (result == null) {
result = v;
}
return v.getCheckRuns(root);
}
};
}
}

View File

@@ -0,0 +1,20 @@
package org.kohsuke.github;
/**
* Represents the one page of check-runs result when listing check-runs.
*/
class GHCheckRunsPage {
private int total_count;
private GHCheckRun[] check_runs;
public int getTotalCount() {
return total_count;
}
GHCheckRun[] getCheckRuns(GitHub root) {
for (GHCheckRun check_run : check_runs) {
check_run.wrap(root);
}
return check_runs;
}
}

View File

@@ -0,0 +1,239 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
/**
* Represents a check suite.
*
* @see <a href="https://developer.github.com/v3/checks/suites/">documentation</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD" },
justification = "JSON API")
public class GHCheckSuite extends GHObject {
GHRepository owner;
GitHub root;
private String nodeId;
private String headBranch;
private String headSha;
private String status;
private String conclusion;
private String before;
private String after;
private int latestCheckRunsCount;
private URL checkRunsUrl;
private HeadCommit headCommit;
private GHApp app;
private GHPullRequest[] pullRequests;
GHCheckSuite wrap(GHRepository owner) {
this.owner = owner;
this.root = owner.root;
return this;
}
GHCheckSuite wrap(GitHub root) {
this.root = root;
if (owner != null) {
owner.wrap(root);
}
return this;
}
GHPullRequest[] wrap() {
return pullRequests;
}
/**
* Gets the global node id to access most objects in GitHub.
*
* @see <a href="https://developer.github.com/v4/guides/using-global-node-ids/">documentation</a>
* @return global node id
*/
public String getNodeId() {
return nodeId;
}
/**
* The head branch name the changes are on.
*
* @return head branch name
*/
public String getHeadBranch() {
return headBranch;
}
/**
* Gets the HEAD SHA.
*
* @return sha for the HEAD commit
*/
public String getHeadSha() {
return headSha;
}
/**
* Gets status of the check suite. It can be one of request, in_progress, or completed.
*
* @return status of the check suite
*/
public String getStatus() {
return status;
}
/**
* Gets conclusion of a completed check suite. It can be one of success, failure, neutral, cancelled, time_out,
* action_required, or stale. The check suite will report the highest priority check run conclusion in the check
* suite's conclusion.
*
* @return conclusion of the check suite
*/
public String getConclusion() {
return conclusion;
}
/**
* The SHA of the most recent commit on ref before the push.
*
* @return sha of a commit
*/
public String getBefore() {
return before;
}
/**
* The SHA of the most recent commit on ref after the push.
*
* @return sha of a commit
*/
public String getAfter() {
return after;
}
/**
* The quantity of check runs that had run as part of the latest push.
*
* @return sha of the most recent commit
*/
public int getLatestCheckRunsCount() {
return latestCheckRunsCount;
}
/**
* The url used to list all the check runs belonged to this suite.
*
* @return url containing all check runs
*/
public URL getCheckRunsUrl() {
return checkRunsUrl;
}
/**
* The commit of current head.
*
* @return head commit
*/
public HeadCommit getHeadCommit() {
return headCommit;
}
/**
* Gets the GitHub app this check suite belongs to, included in response.
*
* @return GitHub App
*/
public GHApp getApp() {
return app;
}
/**
* Gets the pull requests participated in this check suite.
*
* @return Pull requests
*/
GHPullRequest[] getPullRequests() throws IOException {
if (pullRequests != null && pullRequests.length != 0) {
for (GHPullRequest singlePull : pullRequests) {
singlePull.refresh();
}
}
return pullRequests;
}
/**
* Check suite doesn't have a HTML URL.
*
* @return null
*/
@Override
public URL getHtmlUrl() {
return null;
}
public static class HeadCommit {
private String id;
private String treeId;
private String message;
private String timestamp;
private GitUser author;
private GitUser committer;
/**
* Gets id of the commit, used by {@link GHCheckSuite} when a {@link GHEvent#CHECK_SUITE} comes
*
* @return id of the commit
*/
public String getId() {
return id;
}
/**
* Gets id of the tree.
*
* @return id of the tree
*/
public String getTreeId() {
return treeId;
}
/**
* Gets message.
*
* @return commit message.
*/
public String getMessage() {
return message;
}
/**
* Gets timestamp of the commit.
*
* @return timestamp of the commit
*/
public Date getTimestamp() {
return GitHubClient.parseDate(timestamp);
}
/**
* Gets author.
*
* @return the author
*/
public GitUser getAuthor() {
return author;
}
/**
* Gets committer.
*
* @return the committer
*/
public GitUser getCommitter() {
return committer;
}
}
}

View File

@@ -39,6 +39,8 @@ public class GHCommit {
private int comment_count;
private GHVerification verification;
static class Tree {
String sha;
}
@@ -61,7 +63,7 @@ public class GHCommit {
* @return the authored date
*/
public Date getAuthoredDate() {
return GitHub.parseDate(author.date);
return GitHubClient.parseDate(author.date);
}
/**
@@ -80,7 +82,7 @@ public class GHCommit {
* @return the commit date
*/
public Date getCommitDate() {
return GitHub.parseDate(committer.date);
return GitHubClient.parseDate(committer.date);
}
/**
@@ -100,6 +102,15 @@ public class GHCommit {
public int getCommentCount() {
return comment_count;
}
/**
* Gets Verification Status.
*
* @return the Verification status
*/
public GHVerification getVerification() {
return verification;
}
}
/**
@@ -201,7 +212,7 @@ public class GHCommit {
* resolves to the actual content of the file.
*/
public URL getRawUrl() {
return GitHub.parseURL(raw_url);
return GitHubClient.parseURL(raw_url);
}
/**
@@ -212,7 +223,7 @@ public class GHCommit {
* that resolves to the HTML page that describes this file.
*/
public URL getBlobUrl() {
return GitHub.parseURL(blob_url);
return GitHubClient.parseURL(blob_url);
}
/**
@@ -326,7 +337,7 @@ public class GHCommit {
* "https://github.com/kohsuke/sandbox-ant/commit/8ae38db0ea5837313ab5f39d43a6f73de3bd9000"
*/
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -512,6 +523,19 @@ public class GHCommit {
return owner.getLastCommitStatus(sha);
}
/**
* Gets check-runs for given sha.
*
* @return check runs for given sha.
* @throws IOException
* on error
*/
@Preview
@Deprecated
public PagedIterable<GHCheckRun> getCheckRuns() throws IOException {
return owner.getCheckRuns(sha);
}
/**
* Some of the fields are not always filled in when this object is retrieved as a part of another API call.
*

View File

@@ -41,7 +41,7 @@ public class GHCommitComment extends GHObject implements Reactable {
* show this commit comment in a browser.
*/
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**

View File

@@ -83,7 +83,7 @@ public class GHCommitQueryBuilder {
* @return the gh commit query builder
*/
public GHCommitQueryBuilder since(Date dt) {
req.with("since", GitHub.printDate(dt));
req.with("since", GitHubClient.printDate(dt));
return this;
}
@@ -106,7 +106,7 @@ public class GHCommitQueryBuilder {
* @return the gh commit query builder
*/
public GHCommitQueryBuilder until(Date dt) {
req.with("until", GitHub.printDate(dt));
req.with("until", GitHubClient.printDate(dt));
return this;
}

View File

@@ -259,7 +259,7 @@ public class GHCommitSearchBuilder extends GHSearchBuilder<GHCommit> {
if (StringUtils.isBlank(commitUrl)) {
return null;
}
int indexOfUsername = (GitHub.GITHUB_URL + "/repos/").length();
int indexOfUsername = (GitHubClient.GITHUB_URL + "/repos/").length();
String[] tokens = commitUrl.substring(indexOfUsername).split("/", 3);
return tokens[0] + '/' + tokens[1];
}

View File

@@ -27,7 +27,7 @@ public class GHCompare {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -36,7 +36,7 @@ public class GHCompare {
* @return the html url
*/
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -45,7 +45,7 @@ public class GHCompare {
* @return the permalink url
*/
public URL getPermalinkUrl() {
return GitHub.parseURL(permalink_url);
return GitHubClient.parseURL(permalink_url);
}
/**
@@ -54,7 +54,7 @@ public class GHCompare {
* @return the diff url
*/
public URL getDiffUrl() {
return GitHub.parseURL(diff_url);
return GitHubClient.parseURL(diff_url);
}
/**
@@ -63,7 +63,7 @@ public class GHCompare {
* @return the patch url
*/
public URL getPatchUrl() {
return GitHub.parseURL(patch_url);
return GitHubClient.parseURL(patch_url);
}
/**

View File

@@ -388,22 +388,6 @@ public class GHContent implements Refreshable {
return this;
}
/**
* Wrap gh content [ ].
*
* @param contents
* the contents
* @param repository
* the repository
* @return the gh content [ ]
*/
public static GHContent[] wrap(GHContent[] contents, GHRepository repository) {
for (GHContent unwrappedContent : contents) {
unwrappedContent.wrap(repository);
}
return contents;
}
/**
* Fully populate the data by retrieving missing data.
*

View File

@@ -79,6 +79,18 @@ public class GHCreateRepositoryBuilder {
return this;
}
/**
* Enables projects
*
* @param enabled
* true if enabled
* @return a builder to continue with building
*/
public GHCreateRepositoryBuilder projects(boolean enabled) {
this.builder.with("has_projects", enabled);
return this;
}
/**
* Enables wiki
*

View File

@@ -38,7 +38,7 @@ public class GHDeployment extends GHObject {
* @return the statuses url
*/
public URL getStatusesUrl() {
return GitHub.parseURL(statuses_url);
return GitHubClient.parseURL(statuses_url);
}
/**
@@ -47,7 +47,7 @@ public class GHDeployment extends GHObject {
* @return the repository url
*/
public URL getRepositoryUrl() {
return GitHub.parseURL(repository_url);
return GitHubClient.parseURL(repository_url);
}
/**

View File

@@ -37,7 +37,7 @@ public class GHDeploymentStatus extends GHObject {
* @return the target url
*/
public URL getTargetUrl() {
return GitHub.parseURL(target_url);
return GitHubClient.parseURL(target_url);
}
/**
@@ -46,7 +46,7 @@ public class GHDeploymentStatus extends GHObject {
* @return the deployment url
*/
public URL getDeploymentUrl() {
return GitHub.parseURL(deployment_url);
return GitHubClient.parseURL(deployment_url);
}
/**
@@ -55,7 +55,7 @@ public class GHDeploymentStatus extends GHObject {
* @return the repository url
*/
public URL getRepositoryUrl() {
return GitHub.parseURL(repository_url);
return GitHubClient.parseURL(repository_url);
}
/**

View File

@@ -23,6 +23,7 @@ public class GHDeploymentStatusBuilder {
* the state
* @deprecated Use {@link GHDeployment#createStatus(GHDeploymentState)}
*/
@Deprecated
public GHDeploymentStatusBuilder(GHRepository repo, int deploymentId, GHDeploymentState state) {
this(repo, (long) deploymentId, state);
}

View File

@@ -50,6 +50,7 @@ public enum GHEvent {
PULL_REQUEST_REVIEW,
PULL_REQUEST_REVIEW_COMMENT,
PUSH,
REGISTRY_PACKAGE,
RELEASE,
REPOSITORY_DISPATCH, // only valid for org hooks
REPOSITORY,

View File

@@ -78,7 +78,7 @@ public class GHEventInfo {
* @return the created at
*/
public Date getCreatedAt() {
return GitHub.parseDate(created_at);
return GitHubClient.parseDate(created_at);
}
/**
@@ -144,7 +144,7 @@ public class GHEventInfo {
* if payload cannot be parsed
*/
public <T extends GHEventPayload> T getPayload(Class<T> type) throws IOException {
T v = GitHub.MAPPER.readValue(payload.traverse(), type);
T v = GitHubClient.getMappingObjectReader(root).readValue(payload.traverse(), type);
v.wrapUp(root);
return v;
}

View File

@@ -3,6 +3,7 @@ package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonSetter;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
@@ -48,7 +49,7 @@ public abstract class GHEventPayload {
}
// List of events that still need to be added:
// CheckRunEvent CheckSuiteEvent ContentReferenceEvent
// ContentReferenceEvent
// DeployKeyEvent DownloadEvent FollowEvent ForkApplyEvent GitHubAppAuthorizationEvent GistEvent GollumEvent
// InstallationEvent InstallationRepositoriesEvent IssuesEvent LabelEvent MarketplacePurchaseEvent MemberEvent
// MembershipEvent MetaEvent MilestoneEvent OrganizationEvent OrgBlockEvent PackageEvent PageBuildEvent
@@ -132,6 +133,7 @@ public abstract class GHEventPayload {
* @return the repository
*/
public GHRepository getRepository() {
repository.root = root;
return repository;
}
@@ -150,6 +152,209 @@ public abstract class GHEventPayload {
}
}
/**
* A check suite event has been requested, rerequested or completed.
*
* @see <a href="https://developer.github.com/v3/activity/events/types/#checkrunevent">authoritative source</a>
*/
@SuppressFBWarnings(
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public static class CheckSuite extends GHEventPayload {
private String action;
private GHCheckSuite checkSuite;
private GHRepository repository;
/**
* Gets action.
*
* @return the action
*/
public String getAction() {
return action;
}
/**
* Gets the Check Suite object
*
* @return the Check Suite object
*/
public GHCheckSuite getCheckSuite() {
return checkSuite;
}
/**
* Gets repository.
*
* @return the repository
*/
public GHRepository getRepository() {
repository.root = root;
return repository;
}
@Override
void wrapUp(GitHub root) {
super.wrapUp(root);
if (checkSuite == null)
throw new IllegalStateException(
"Expected check_suite payload, but got something else. Maybe we've got another type of event?");
if (repository != null) {
repository.wrap(root);
checkSuite.wrap(repository);
} else {
checkSuite.wrap(root);
}
}
}
/**
* An installation has been installed, uninstalled, or its permissions have been changed.
*
* @see <a href="https://developer.github.com/v3/activity/events/types/#installationevent">authoritative source</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
public static class Installation extends GHEventPayload {
private String action;
private GHAppInstallation installation;
private List<GHRepository> repositories;
/**
* Gets action
*
* @return the action
*/
public String getAction() {
return action;
}
/**
* Gets installation
*
* @return the installation
*/
public GHAppInstallation getInstallation() {
return installation;
}
/**
* Gets repositories
*
* @return the repositories
*/
public List<GHRepository> getRepositories() {
return repositories;
};
@Override
void wrapUp(GitHub root) {
super.wrapUp(root);
if (installation == null)
throw new IllegalStateException(
"Expected check_suite payload, but got something else. Maybe we've got another type of event?");
else
installation.wrapUp(root);
if (repositories != null && !repositories.isEmpty()) {
try {
for (GHRepository singleRepo : repositories) { // warp each of the repository
singleRepo.wrap(root);
singleRepo.populate();
}
} catch (IOException e) {
throw new GHException("Failed to refresh repositories", e);
}
}
}
}
/**
* A repository has been added or removed from an installation.
*
* @see <a href="https://developer.github.com/v3/activity/events/types/#installationrepositoriesevent">authoritative
* source</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
public static class InstallationRepositories extends GHEventPayload {
private String action;
private GHAppInstallation installation;
private String repositorySelection;
private List<GHRepository> repositoriesAdded;
private List<GHRepository> repositoriesRemoved;
/**
* Gets action
*
* @return the action
*/
public String getAction() {
return action;
}
/**
* Gets installation
*
* @return the installation
*/
public GHAppInstallation getInstallation() {
return installation;
}
/**
* Gets installation selection
*
* @return the installation selection
*/
public String getRepositorySelection() {
return repositorySelection;
}
/**
* Gets repositories added
*
* @return the repositories
*/
public List<GHRepository> getRepositoriesAdded() {
return repositoriesAdded;
}
/**
* Gets repositories removed
*
* @return the repositories
*/
public List<GHRepository> getRepositoriesRemoved() {
return repositoriesRemoved;
}
@Override
void wrapUp(GitHub root) {
super.wrapUp(root);
if (installation == null)
throw new IllegalStateException(
"Expected check_suite payload, but got something else. Maybe we've got another type of event?");
else
installation.wrapUp(root);
List<GHRepository> repositories;
if (action == "added")
repositories = repositoriesAdded;
else // action == "removed"
repositories = repositoriesRemoved;
if (repositories != null && !repositories.isEmpty()) {
try {
for (GHRepository singleRepo : repositories) { // warp each of the repository
singleRepo.wrap(root);
singleRepo.populate();
}
} catch (IOException e) {
throw new GHException("Failed to refresh repositories", e);
}
}
}
}
/**
* A pull request status has changed.
*
@@ -1428,6 +1633,107 @@ public abstract class GHEventPayload {
organization.wrapUp(root);
}
}
}
/**
* A git commit status was changed.
*
* @see <a href="https://developer.github.com/v3/activity/events/types/#statusevent">authoritative source</a>
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "Constructed by JSON deserialization")
public static class Status extends GHEventPayload {
private String context;
private String description;
private GHCommitState state;
private GHCommit commit;
private GHRepository repository;
/**
* Gets the status content.
*
* @return status content
*/
public String getContext() {
return context;
}
/**
* Gets the status description.
*
* @return status description
*/
public String getDescription() {
return description;
}
/**
* Gets the status state.
*
* @return status state
*/
public GHCommitState getState() {
return state;
}
/**
* Sets the status stage.
*
* @param state
* status state
*/
public void setState(GHCommitState state) {
this.state = state;
}
/**
* Gets the commit associated with the status event.
*
* @return commit
*/
public GHCommit getCommit() {
return commit;
}
/**
* Sets the commit associated with the status event.
*
* @param commit
* commit
*/
public void setCommit(GHCommit commit) {
this.commit = commit;
}
/**
* Gets the repository associated with the status event.
*
* @return repository
*/
public GHRepository getRepository() {
return repository;
}
/**
* Sets the repository associated with the status event.
*
* @param repository
* repository
*/
public void setRepository(GHRepository repository) {
this.repository = repository;
}
@Override
void wrapUp(GitHub root) {
super.wrapUp(root);
if (state == null) {
throw new IllegalStateException(
"Expected status payload, but got something else. Maybe we've got another type of event?");
}
if (repository != null) {
repository.wrap(root);
commit.wrapUp(repository);
}
}
}
}

View File

@@ -1,11 +1,11 @@
package org.kohsuke.github;
import java.io.FileNotFoundException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* Request/responce contains useful metadata. Custom exception allows store info for next diagnostics.
@@ -24,11 +24,24 @@ public class GHFileNotFoundException extends FileNotFoundException {
/**
* Instantiates a new Gh file not found exception.
*
* @param s
* the s
* @param message
* the message
*/
public GHFileNotFoundException(String s) {
super(s);
public GHFileNotFoundException(String message) {
super(message);
}
/**
* Instantiates a new Gh file not found exception.
*
* @param message
* the message
* @param cause
* the cause
*/
public GHFileNotFoundException(String message, Throwable cause) {
super(message);
this.initCause(cause);
}
/**
@@ -41,8 +54,8 @@ public class GHFileNotFoundException extends FileNotFoundException {
return responseHeaderFields;
}
GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
GHFileNotFoundException withResponseHeaderFields(@Nonnull Map<String, List<String>> headerFields) {
this.responseHeaderFields = headerFields;
return this;
}
}

View File

@@ -1,12 +1,13 @@
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
@@ -20,8 +21,9 @@ import java.util.Map.Entry;
* @see <a href="https://developer.github.com/v3/gists/">documentation</a>
*/
public class GHGist extends GHObject {
/* package almost final */ GHUser owner;
/* package almost final */ GitHub root;
final GHUser owner;
final GitHub root;
private String forks_url, commits_url, id, git_pull_url, git_push_url, html_url;
@@ -34,7 +36,19 @@ public class GHGist extends GHObject {
private String comments_url;
private Map<String, GHGistFile> files = new HashMap<String, GHGistFile>();
private final Map<String, GHGistFile> files;
@JsonCreator
private GHGist(@JacksonInject GitHub root,
@JsonProperty("owner") GHUser owner,
@JsonProperty("files") Map<String, GHGistFile> files) {
this.root = root;
for (Entry<String, GHGistFile> e : files.entrySet()) {
e.getValue().fileName = e.getKey();
}
this.files = Collections.unmodifiableMap(files);
this.owner = root.getUser(owner);
}
/**
* Gets owner.
@@ -44,7 +58,7 @@ public class GHGist extends GHObject {
* the io exception
*/
public GHUser getOwner() throws IOException {
return root.intern(owner);
return owner;
}
/**
@@ -84,7 +98,7 @@ public class GHGist extends GHObject {
}
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -140,31 +154,7 @@ public class GHGist extends GHObject {
* @return the files
*/
public Map<String, GHGistFile> getFiles() {
return Collections.unmodifiableMap(files);
}
GHGist wrapUp(GHUser owner) {
this.owner = owner;
this.root = owner.root;
wrapUp();
return this;
}
/**
* Used when caller obtains {@link GHGist} without knowing its owner. A partially constructed owner object is
* interned.
*/
GHGist wrapUp(GitHub root) {
this.owner = root.getUser(owner);
this.root = root;
wrapUp();
return this;
}
private void wrapUp() {
for (Entry<String, GHGistFile> e : files.entrySet()) {
e.getValue().fileName = e.getKey();
}
return files;
}
String getApiTailUrl(String tail) {
@@ -214,7 +204,7 @@ public class GHGist extends GHObject {
* the io exception
*/
public GHGist fork() throws IOException {
return root.createRequest().method("POST").withUrlPath(getApiTailUrl("forks")).fetch(GHGist.class).wrapUp(root);
return root.createRequest().method("POST").withUrlPath(getApiTailUrl("forks")).fetch(GHGist.class);
}
/**
@@ -223,9 +213,7 @@ public class GHGist extends GHObject {
* @return the paged iterable
*/
public PagedIterable<GHGist> listForks() {
return root.createRequest()
.withUrlPath(getApiTailUrl("forks"))
.toIterable(GHGist[].class, item -> item.wrapUp(root));
return root.createRequest().withUrlPath(getApiTailUrl("forks")).toIterable(GHGist[].class, null);
}
/**
@@ -264,10 +252,4 @@ public class GHGist extends GHObject {
public int hashCode() {
return id.hashCode();
}
GHGist wrap(GHUser owner) {
this.owner = owner;
this.root = owner.root;
return this;
}
}

View File

@@ -73,6 +73,6 @@ public class GHGistBuilder {
*/
public GHGist create() throws IOException {
req.with("files", files);
return req.withUrlPath("/gists").fetch(GHGist.class).wrapUp(root);
return req.withUrlPath("/gists").fetch(GHGist.class);
}
}

View File

@@ -96,6 +96,6 @@ public class GHGistUpdater {
*/
public GHGist update() throws IOException {
builder.with("files", files);
return builder.method("PATCH").withUrlPath(base.getApiTailUrl("")).fetch(GHGist.class).wrap(base.owner);
return builder.method("PATCH").withUrlPath(base.getApiTailUrl("")).fetch(GHGist.class);
}
}

View File

@@ -1,11 +1,11 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* Request/responce contains useful metadata. Custom exception allows store info for next diagnostics.
@@ -31,6 +31,20 @@ public class GHIOException extends IOException {
super(message);
}
/**
* Constructs a {@code GHIOException} with the specified detail message and cause.
*
* @param message
* The detail message (which is saved for later retrieval by the {@link #getMessage()} method)
*
* @param cause
* The cause (which is saved for later retrieval by the {@link #getCause()} method). (A null value is
* permitted, and indicates that the cause is nonexistent or unknown.)
*/
public GHIOException(String message, Throwable cause) {
super(message, cause);
}
/**
* Gets response header fields.
*
@@ -41,8 +55,8 @@ public class GHIOException extends IOException {
return responseHeaderFields;
}
GHIOException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
GHIOException withResponseHeaderFields(@Nonnull Map<String, List<String>> headerFields) {
this.responseHeaderFields = headerFields;
return this;
}
}

View File

@@ -51,6 +51,6 @@ public class GHInvitation extends GHObject {
@Override
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
}

View File

@@ -26,6 +26,7 @@ package org.kohsuke.github;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.URL;
@@ -63,8 +64,7 @@ public class GHIssue extends GHObject implements Reactable {
protected int comments;
@SkipFromToString
protected String body;
// for backward compatibility with < 1.63, this collection needs to hold instances of Label, not GHLabel
protected List<Label> labels;
protected List<GHLabel> labels;
protected GHUser user;
protected String title, html_url;
protected GHIssue.PullRequest pull_request;
@@ -72,14 +72,6 @@ public class GHIssue extends GHObject implements Reactable {
protected GHUser closed_by;
protected boolean locked;
/**
* The type Label.
*
* @deprecated use {@link GHLabel}
*/
public static class Label extends GHLabel {
}
GHIssue wrap(GHRepository owner) {
this.owner = owner;
if (milestone != null)
@@ -100,12 +92,6 @@ public class GHIssue extends GHObject implements Reactable {
return this;
}
static GHIssue[] wrap(GHIssue[] issues, GHRepository owner) {
for (GHIssue i : issues)
i.wrap(owner);
return issues;
}
/**
* Repository to which the issue belongs.
*
@@ -137,7 +123,7 @@ public class GHIssue extends GHObject implements Reactable {
* The HTML page of this issue, like https://github.com/jenkinsci/jenkins/issues/100
*/
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -178,7 +164,7 @@ public class GHIssue extends GHObject implements Reactable {
if (labels == null) {
return Collections.emptyList();
}
return Collections.<GHLabel>unmodifiableList(labels);
return Collections.unmodifiableList(labels);
}
/**
@@ -187,7 +173,7 @@ public class GHIssue extends GHObject implements Reactable {
* @return the closed at
*/
public Date getClosedAt() {
return GitHub.parseDate(closed_at);
return GitHubClient.parseDate(closed_at);
}
/**
@@ -196,7 +182,7 @@ public class GHIssue extends GHObject implements Reactable {
* @return the api url
*/
public URL getApiURL() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -242,6 +228,13 @@ public class GHIssue extends GHObject implements Reactable {
root.createRequest().with(key, value).method("PATCH").withUrlPath(getApiRoute()).send();
}
/**
* Identical to edit(), but allows null for the value.
*/
private void editNullable(String key, Object value) throws IOException {
root.createRequest().withNullable(key, value).method("PATCH").withUrlPath(getApiRoute()).send();
}
private void editIssue(String key, Object value) throws IOException {
root.createRequest().with(key, value).method("PATCH").withUrlPath(getIssuesApiRoute()).send();
}
@@ -291,15 +284,19 @@ public class GHIssue extends GHObject implements Reactable {
}
/**
* Sets milestone.
* Sets the milestone for this issue.
*
* @param milestone
* the milestone
* The milestone to assign this issue to. Use null to remove the milestone for this issue.
* @throws IOException
* the io exception
* The io exception
*/
public void setMilestone(GHMilestone milestone) throws IOException {
edit("milestone", milestone.getNumber());
if (milestone == null) {
editNullable("milestone", null);
} else {
edit("milestone", milestone.getNumber());
}
}
/**
@@ -434,7 +431,7 @@ public class GHIssue extends GHObject implements Reactable {
* @see #listComments() #listComments()
*/
public List<GHIssueComment> getComments() throws IOException {
return listComments().asList();
return listComments().toList();
}
/**
@@ -453,7 +450,7 @@ public class GHIssue extends GHObject implements Reactable {
@Preview
@Deprecated
public GHReaction createReaction(ReactionContent content) throws IOException {
return owner.root.createRequest()
return root.createRequest()
.method("POST")
.withPreview(SQUIRREL_GIRL)
.with("content", content.getContent())
@@ -465,10 +462,10 @@ public class GHIssue extends GHObject implements Reactable {
@Preview
@Deprecated
public PagedIterable<GHReaction> listReactions() {
return owner.root.createRequest()
return root.createRequest()
.withPreview(SQUIRREL_GIRL)
.withUrlPath(getApiRoute() + "/reactions")
.toIterable(GHReaction[].class, item -> item.wrap(owner.root));
.toIterable(GHReaction[].class, item -> item.wrap(root));
}
/**
@@ -571,6 +568,10 @@ public class GHIssue extends GHObject implements Reactable {
* @return the issues api route
*/
protected String getIssuesApiRoute() {
if (owner == null) {
// Issues returned from search to do not have an owner. Attempt to use url.
return StringUtils.prependIfMissing(getUrl().toString().replace(root.getApiUrl(), ""), "/");
}
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/issues/" + number;
}
@@ -677,7 +678,7 @@ public class GHIssue extends GHObject implements Reactable {
* @return the diff url
*/
public URL getDiffUrl() {
return GitHub.parseURL(diff_url);
return GitHubClient.parseURL(diff_url);
}
/**
@@ -686,7 +687,7 @@ public class GHIssue extends GHObject implements Reactable {
* @return the patch url
*/
public URL getPatchUrl() {
return GitHub.parseURL(patch_url);
return GitHubClient.parseURL(patch_url);
}
/**
@@ -695,7 +696,7 @@ public class GHIssue extends GHObject implements Reactable {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
}

View File

@@ -87,7 +87,7 @@ public class GHIssueComment extends GHObject implements Reactable {
@Override
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**

View File

@@ -90,7 +90,7 @@ public class GHIssueEvent {
* @return the created at
*/
public Date getCreatedAt() {
return GitHub.parseDate(created_at);
return GitHubClient.parseDate(created_at);
}
/**

View File

@@ -1,27 +1,56 @@
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* The type GHLabel.
*
* @author Kohsuke Kawaguchi
* @see <a href="https://developer.github.com/v3/issues/labels/">Labels</a>
* @see GHIssue#getLabels() GHIssue#getLabels()
* @see GHRepository#listLabels() GHRepository#listLabels()
*/
public class GHLabel {
private String url, name, color, description;
private GHRepository repo;
@Nonnull
private String url, name, color;
@CheckForNull
private String description;
@Nonnull
private final GitHub root;
@JsonCreator
private GHLabel(@JacksonInject @Nonnull GitHub root) {
this.root = root;
url = "";
name = "";
color = "";
description = null;
}
@Nonnull
GitHub getApiRoot() {
return Objects.requireNonNull(root);
}
/**
* Gets url.
*
* @return the url
*/
@Nonnull
public String getUrl() {
return url;
}
@@ -31,6 +60,7 @@ public class GHLabel {
*
* @return the name
*/
@Nonnull
public String getName() {
return name;
}
@@ -40,6 +70,7 @@ public class GHLabel {
*
* @return the color
*/
@Nonnull
public String getColor() {
return color;
}
@@ -49,25 +80,11 @@ public class GHLabel {
*
* @return the description
*/
@CheckForNull
public String getDescription() {
return description;
}
GHLabel wrapUp(GHRepository repo) {
this.repo = repo;
return this;
}
/**
* Delete.
*
* @throws IOException
* the io exception
*/
public void delete() throws IOException {
repo.root.createRequest().method("DELETE").setRawUrlPath(url).send();
}
/**
* Sets color.
*
@@ -75,15 +92,11 @@ public class GHLabel {
* 6-letter hex color code, like "f29513"
* @throws IOException
* the io exception
* @deprecated use {@link #set()} or {@link #update()} instead
*/
@Deprecated
public void setColor(String newColor) throws IOException {
repo.root.createRequest()
.method("PATCH")
.with("name", name)
.with("color", newColor)
.with("description", description)
.setRawUrlPath(url)
.send();
set().color(newColor);
}
/**
@@ -93,25 +106,106 @@ public class GHLabel {
* Description of label
* @throws IOException
* the io exception
* @deprecated use {@link #set()} or {@link #update()} instead
*/
@Deprecated
public void setDescription(String newDescription) throws IOException {
repo.root.createRequest()
.method("PATCH")
.with("name", name)
.with("color", color)
.with("description", newDescription)
.setRawUrlPath(url)
.send();
set().description(newDescription);
}
static Collection<String> toNames(Collection<GHLabel> labels) {
List<String> r = new ArrayList<String>();
List<String> r = new ArrayList<>();
for (GHLabel l : labels) {
r.add(l.getName());
}
return r;
}
/**
* Begins the creation of a new instance.
*
* Consumer must call {@link Creator#done()} to commit changes.
*
* @param repository
* the repository in which the label will be created.
* @return a {@link Creator}
* @throws IOException
* the io exception
*/
@Preview
@Deprecated
static Creator create(GHRepository repository) throws IOException {
return new Creator(repository);
}
/**
* Reads a label from a repository.
*
* @param repository
* the repository to read from
* @param name
* the name of the label
* @return a label
* @throws IOException
* the io exception
*/
static GHLabel read(@Nonnull GHRepository repository, @Nonnull String name) throws IOException {
return repository.root.createRequest()
.withUrlPath(repository.getApiTailUrl("labels"), name)
.fetch(GHLabel.class);
}
/**
* Reads all labels from a repository.
*
* @param repository
* the repository to read from
* @return iterable of all labels
* @throws IOException
* the io exception
*/
static PagedIterable<GHLabel> readAll(@Nonnull final GHRepository repository) throws IOException {
return repository.root.createRequest()
.withUrlPath(repository.getApiTailUrl("labels"))
.toIterable(GHLabel[].class, null);
}
/**
* Begins a batch update
*
* Consumer must call {@link Updater#done()} to commit changes.
*
* @return a {@link Updater}
*/
@Preview
@Deprecated
public Updater update() {
return new Updater(this);
}
/**
* Begins a single property update.
*
* @return a {@link Setter}
*/
@Preview
@Deprecated
public Setter set() {
return new Setter(this);
}
/**
* Delete this label from the repository.
*
* @throws IOException
* the io exception
*/
public void delete() throws IOException {
root.createRequest().method("DELETE").setRawUrlPath(getUrl()).send();
}
@Override
public boolean equals(final Object o) {
if (this == o)
@@ -120,11 +214,54 @@ public class GHLabel {
return false;
final GHLabel ghLabel = (GHLabel) o;
return Objects.equals(url, ghLabel.url) && Objects.equals(name, ghLabel.name)
&& Objects.equals(color, ghLabel.color) && Objects.equals(repo, ghLabel.repo);
&& Objects.equals(color, ghLabel.color) && Objects.equals(description, ghLabel.description);
}
@Override
public int hashCode() {
return Objects.hash(url, name, color, repo);
return Objects.hash(url, name, color, description);
}
/**
* A {@link GHLabelBuilder} that updates a single property per request
*
* {@link #done()} is called automatically after the property is set.
*/
@Preview
@Deprecated
public static class Setter extends GHLabelBuilder<GHLabel> {
private Setter(@Nonnull GHLabel base) {
super(GHLabel.class, base.getApiRoot(), base);
requester.method("PATCH").setRawUrlPath(base.getUrl());
}
}
/**
* A {@link GHLabelBuilder} that allows multiple properties to be updated per request.
*
* Consumer must call {@link #done()} to commit changes.
*/
@Preview
@Deprecated
public static class Updater extends GHLabelBuilder<Updater> {
private Updater(@Nonnull GHLabel base) {
super(Updater.class, base.getApiRoot(), base);
requester.method("PATCH").setRawUrlPath(base.getUrl());
}
}
/**
* A {@link GHLabelBuilder} that creates a new {@link GHLabel}
*
* Consumer must call {@link #done()} to create the new instance.
*/
@Preview
@Deprecated
public static class Creator extends GHLabelBuilder<Creator> {
private Creator(@Nonnull GHRepository repository) {
super(Creator.class, repository.root, null);
requester.method("POST").withUrlPath(repository.getApiTailUrl("labels"));
}
}
}

View File

@@ -0,0 +1,60 @@
package org.kohsuke.github;
import java.io.IOException;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
*
* @param <S>
* Intermediate return type for this builder returned by calls to {@link #with(String, Object)}. If {@link S}
* the same as {@link GHLabel}, this builder will commit changes after each call to
* {@link #with(String, Object)}.
*/
class GHLabelBuilder<S> extends AbstractBuilder<GHLabel, S> {
/**
*
* @param intermediateReturnType
* Intermediate return type for this builder returned by calls to {@link #with(String, Object)}. If
* {@link S} the same as {@link GHLabel}, this builder will commit changes after each call to
* {@link #with(String, Object)}.
* @param root
* the GitHub instance to which updates will be sent
* @param baseInstance
* instance on which to base this builder. If {@code null} a new instance will be created.
*/
protected GHLabelBuilder(@Nonnull Class<S> intermediateReturnType,
@Nonnull GitHub root,
@CheckForNull GHLabel baseInstance) {
super(GHLabel.class, intermediateReturnType, root, baseInstance);
if (baseInstance != null) {
requester.with("name", baseInstance.getName());
requester.with("color", baseInstance.getColor());
requester.with("description", baseInstance.getDescription());
}
}
@Nonnull
@Preview
@Deprecated
public S name(String value) throws IOException {
return with("name", value);
}
@Nonnull
@Preview
@Deprecated
public S color(String value) throws IOException {
return with("color", value);
}
@Nonnull
@Preview
@Deprecated
public S description(String value) throws IOException {
return with("description", value);
}
}

View File

@@ -83,7 +83,7 @@ public class GHLicense extends GHObject {
*/
@WithBridgeMethods(value = String.class, adapterMethod = "urlToString")
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -100,7 +100,7 @@ public class GHLicense extends GHObject {
public URL getHtmlUrl() throws IOException {
populate();
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**

View File

@@ -37,7 +37,7 @@ public class GHMarketplaceAccount {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**

View File

@@ -68,7 +68,7 @@ public class GHMarketplacePendingChange {
* @return the effective date
*/
public Date getEffectiveDate() {
return GitHub.parseDate(effectiveDate);
return GitHubClient.parseDate(effectiveDate);
}
}

View File

@@ -47,7 +47,7 @@ public class GHMarketplacePlan {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**

View File

@@ -52,7 +52,7 @@ public class GHMarketplacePurchase {
* @return the next billing date
*/
public Date getNextBillingDate() {
return GitHub.parseDate(nextBillingDate);
return GitHubClient.parseDate(nextBillingDate);
}
/**
@@ -70,7 +70,7 @@ public class GHMarketplacePurchase {
* @return the free trial ends on
*/
public Date getFreeTrialEndsOn() {
return GitHub.parseDate(freeTrialEndsOn);
return GitHubClient.parseDate(freeTrialEndsOn);
}
/**
@@ -88,7 +88,7 @@ public class GHMarketplacePurchase {
* @return the updated at
*/
public Date getUpdatedAt() {
return GitHub.parseDate(updatedAt);
return GitHubClient.parseDate(updatedAt);
}
/**

View File

@@ -54,7 +54,7 @@ public class GHMarketplaceUserPurchase {
* @return the next billing date
*/
public Date getNextBillingDate() {
return GitHub.parseDate(nextBillingDate);
return GitHubClient.parseDate(nextBillingDate);
}
/**
@@ -72,7 +72,7 @@ public class GHMarketplaceUserPurchase {
* @return the free trial ends on
*/
public Date getFreeTrialEndsOn() {
return GitHub.parseDate(freeTrialEndsOn);
return GitHubClient.parseDate(freeTrialEndsOn);
}
/**
@@ -90,7 +90,7 @@ public class GHMarketplaceUserPurchase {
* @return the updated at
*/
public Date getUpdatedAt() {
return GitHub.parseDate(updatedAt);
return GitHubClient.parseDate(updatedAt);
}
/**

View File

@@ -25,7 +25,7 @@ public class GHMembership /* extends GHObject --- but it doesn't have id, create
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -84,11 +84,6 @@ public class GHMembership /* extends GHObject --- but it doesn't have id, create
return this;
}
static void wrap(GHMembership[] page, GitHub root) {
for (GHMembership m : page)
m.wrap(root);
}
/**
* Role of a user in an organization.
*/

View File

@@ -56,7 +56,7 @@ public class GHMilestone extends GHObject {
public Date getDueOn() {
if (due_on == null)
return null;
return GitHub.parseDate(due_on);
return GitHubClient.parseDate(due_on);
}
/**
@@ -67,7 +67,7 @@ public class GHMilestone extends GHObject {
* the io exception
*/
public Date getClosedAt() throws IOException {
return GitHub.parseDate(closed_at);
return GitHubClient.parseDate(closed_at);
}
/**
@@ -116,7 +116,7 @@ public class GHMilestone extends GHObject {
}
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -195,7 +195,7 @@ public class GHMilestone extends GHObject {
* the io exception
*/
public void setDueOn(Date dueOn) throws IOException {
edit("due_on", GitHub.printDate(dueOn));
edit("due_on", GitHubClient.printDate(dueOn));
}
/**

View File

@@ -2,7 +2,6 @@ package org.kohsuke.github;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
@@ -71,8 +70,7 @@ public class GHMyself extends GHUser {
* the io exception
*/
public List<GHEmail> getEmails2() throws IOException {
GHEmail[] addresses = root.createRequest().withUrlPath("/user/emails").fetchArray(GHEmail[].class);
return Collections.unmodifiableList(Arrays.asList(addresses));
return root.createRequest().withUrlPath("/user/emails").toIterable(GHEmail[].class, null).toList();
}
/**
@@ -86,8 +84,7 @@ public class GHMyself extends GHUser {
* the io exception
*/
public List<GHKey> getPublicKeys() throws IOException {
return Collections.unmodifiableList(
Arrays.asList(root.createRequest().withUrlPath("/user/keys").fetchArray(GHKey[].class)));
return root.createRequest().withUrlPath("/user/keys").toIterable(GHKey[].class, null).toList();
}
/**
@@ -101,8 +98,10 @@ public class GHMyself extends GHUser {
* the io exception
*/
public List<GHVerifiedKey> getPublicVerifiedKeys() throws IOException {
return Collections.unmodifiableList(Arrays.asList(
root.createRequest().withUrlPath("/users/" + getLogin() + "/keys").fetchArray(GHVerifiedKey[].class)));
return root.createRequest()
.withUrlPath("/users/" + getLogin() + "/keys")
.toIterable(GHVerifiedKey[].class, null)
.toList();
}
/**
@@ -115,7 +114,10 @@ public class GHMyself extends GHUser {
public GHPersonSet<GHOrganization> getAllOrganizations() throws IOException {
GHPersonSet<GHOrganization> orgs = new GHPersonSet<GHOrganization>();
Set<String> names = new HashSet<String>();
for (GHOrganization o : root.createRequest().withUrlPath("/user/orgs").fetchArray(GHOrganization[].class)) {
for (GHOrganization o : root.createRequest()
.withUrlPath("/user/orgs")
.toIterable(GHOrganization[].class, null)
.toArray()) {
if (names.add(o.getLogin())) // in case of rumoured duplicates in the data
orgs.add(root.getOrganization(o.getLogin()));
}
@@ -189,6 +191,7 @@ public class GHMyself extends GHUser {
* @return the paged iterable
* @deprecated Use {@link #listRepositories()}
*/
@Deprecated
public PagedIterable<GHRepository> listAllRepositories() {
return listRepositories();
}

View File

@@ -79,7 +79,7 @@ public class GHNotificationStream implements Iterable<GHThread> {
* @return the gh notification stream
*/
public GHNotificationStream since(Date dt) {
since = GitHub.printDate(dt);
since = GitHubClient.printDate(dt);
return this;
}
@@ -180,7 +180,11 @@ public class GHNotificationStream implements Iterable<GHThread> {
req.setHeader("If-Modified-Since", lastModified);
threads = req.withUrlPath(apiUrl).fetchArray(GHThread[].class);
Requester requester = req.withUrlPath(apiUrl);
GitHubResponse<GHThread[]> response = ((GitHubPageContentsIterable<GHThread>) requester
.toIterable(GHThread[].class, null)).toResponse();
threads = response.body();
if (threads == null) {
threads = EMPTY_ARRAY; // if unmodified, we get empty array
} else {
@@ -189,27 +193,21 @@ public class GHNotificationStream implements Iterable<GHThread> {
}
idx = threads.length - 1;
nextCheckTime = calcNextCheckTime();
lastModified = req.getResponseHeader("Last-Modified");
nextCheckTime = calcNextCheckTime(response);
lastModified = response.headerField("Last-Modified");
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
private long calcNextCheckTime() {
String v = req.getResponseHeader("X-Poll-Interval");
private long calcNextCheckTime(GitHubResponse<GHThread[]> response) {
String v = response.headerField("X-Poll-Interval");
if (v == null)
v = "60";
long seconds = Integer.parseInt(v);
return System.currentTimeMillis() + seconds * 1000;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@@ -234,7 +232,7 @@ public class GHNotificationStream implements Iterable<GHThread> {
public void markAsRead(long timestamp) throws IOException {
final Requester req = root.createRequest();
if (timestamp >= 0)
req.with("last_read_at", GitHub.printDate(new Date(timestamp)));
req.with("last_read_at", GitHubClient.printDate(new Date(timestamp)));
req.withUrlPath(apiUrl).fetchHttpStatusCode();
}

View File

@@ -1,5 +1,6 @@
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
@@ -23,7 +24,7 @@ public abstract class GHObject {
/**
* Capture response HTTP headers on the state object.
*/
protected Map<String, List<String>> responseHeaderFields;
protected transient Map<String, List<String>> responseHeaderFields;
protected String url;
protected long id;
@@ -33,6 +34,19 @@ public abstract class GHObject {
GHObject() {
}
/**
* Called by Jackson
*
* @param responseInfo
* the {@link GitHubResponse.ResponseInfo} to get headers from.
*/
@JacksonInject
protected void setResponseHeaderFields(@CheckForNull GitHubResponse.ResponseInfo responseInfo) {
if (responseInfo != null) {
responseHeaderFields = responseInfo.headers();
}
}
/**
* Returns the HTTP response headers given along with the state of this object.
*
@@ -60,7 +74,7 @@ public abstract class GHObject {
*/
@WithBridgeMethods(value = String.class, adapterMethod = "createdAtStr")
public Date getCreatedAt() throws IOException {
return GitHub.parseDate(created_at);
return GitHubClient.parseDate(created_at);
}
@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getCreatedAt")
@@ -75,7 +89,7 @@ public abstract class GHObject {
*/
@WithBridgeMethods(value = String.class, adapterMethod = "urlToString")
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -96,7 +110,7 @@ public abstract class GHObject {
* on error
*/
public Date getUpdatedAt() throws IOException {
return GitHub.parseDate(updated_at);
return GitHubClient.parseDate(updated_at);
}
/**

View File

@@ -40,6 +40,7 @@ public class GHOrganization extends GHPerson {
* the io exception
* @deprecated Use {@link #createRepository(String)} that uses a builder pattern to let you control every aspect.
*/
@Deprecated
public GHRepository createRepository(String name,
String description,
String homepage,
@@ -69,6 +70,7 @@ public class GHOrganization extends GHPerson {
* the io exception
* @deprecated Use {@link #createRepository(String)} that uses a builder pattern to let you control every aspect.
*/
@Deprecated
public GHRepository createRepository(String name,
String description,
String homepage,
@@ -126,6 +128,24 @@ public class GHOrganization extends GHPerson {
.toIterable(GHTeam[].class, item -> item.wrapUp(this));
}
/**
* Gets a single team by ID.
*
* @param teamId
* id of the team that we want to query for
* @return the team
* @throws IOException
* the io exception
*
* @see <a href= "https://developer.github.com/v3/teams/#get-team-by-name">documentation</a>
*/
public GHTeam getTeam(int teamId) throws IOException {
return root.createRequest()
.withUrlPath(String.format("/organizations/%d/team/%d", id, teamId))
.fetch(GHTeam.class)
.wrapUp(this);
}
/**
* Finds a team that has the given name in its {@link GHTeam#getName()}
*
@@ -145,19 +165,19 @@ public class GHOrganization extends GHPerson {
/**
* Finds a team that has the given slug in its {@link GHTeam#getSlug()}
*
*
* @param slug
* the slug
* @return the team by slug
* @throws IOException
* the io exception
* @see <a href= "https://developer.github.com/v3/teams/#get-team-by-name">documentation</a>
*/
public GHTeam getTeamBySlug(String slug) throws IOException {
for (GHTeam t : listTeams()) {
if (t.getSlug().equals(slug))
return t;
}
return null;
return root.createRequest()
.withUrlPath(String.format("/orgs/%s/teams/%s", login, slug))
.fetch(GHTeam.class)
.wrapUp(this);
}
/**
@@ -255,7 +275,7 @@ public class GHOrganization extends GHPerson {
* @deprecated use {@link #listMembers()}
*/
public List<GHUser> getMembers() throws IOException {
return listMembers().asList();
return listMembers().toList();
}
/**
@@ -281,7 +301,7 @@ public class GHOrganization extends GHPerson {
}
private PagedIterable<GHUser> listMembers(String suffix) throws IOException {
return listMembers(suffix, null);
return listMembers(suffix, null, null);
}
/**
@@ -294,13 +314,28 @@ public class GHOrganization extends GHPerson {
* the io exception
*/
public PagedIterable<GHUser> listMembersWithFilter(String filter) throws IOException {
return listMembers("members", filter);
return listMembers("members", filter, null);
}
private PagedIterable<GHUser> listMembers(final String suffix, final String filter) throws IOException {
String filterParams = (filter == null) ? "" : ("?filter=" + filter);
/**
* List members with specified role paged iterable.
*
* @param role
* the role
* @return the paged iterable
* @throws IOException
* the io exception
*/
public PagedIterable<GHUser> listMembersWithRole(String role) throws IOException {
return listMembers("members", null, role);
}
private PagedIterable<GHUser> listMembers(final String suffix, final String filter, String role)
throws IOException {
return root.createRequest()
.withUrlPath(String.format("/orgs/%s/%s%s", login, suffix, filterParams))
.withUrlPath(String.format("/orgs/%s/%s", login, suffix))
.with("filter", filter)
.with("role", role)
.toIterable(GHUser[].class, item -> item.wrapUp(root));
}

View File

@@ -2,13 +2,14 @@ package org.kohsuke.github;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
/**
@@ -20,7 +21,7 @@ public abstract class GHPerson extends GHObject {
/* package almost final */ GitHub root;
// core data fields that exist even for "small" user data (such as the user info in pull request)
protected String login, avatar_url, gravatar_id;
protected String login, avatar_url;
// other fields (that only show up in full data)
protected String location, blog, email, name, company, type;
@@ -28,6 +29,9 @@ public abstract class GHPerson extends GHObject {
protected int followers, following, public_repos, public_gists;
protected boolean site_admin;
// other fields (that only show up in full data) that require privileged scope
protected Integer total_private_repos;
GHPerson wrapUp(GitHub root) {
this.root = root;
return this;
@@ -112,29 +116,27 @@ public abstract class GHPerson extends GHObject {
*/
@Deprecated
public synchronized Iterable<List<GHRepository>> iterateRepositories(final int pageSize) {
return new Iterable<List<GHRepository>>() {
public Iterator<List<GHRepository>> iterator() {
final Iterator<GHRepository[]> pager = root.createRequest()
.withUrlPath("users", login, "repos")
.asIterator(GHRepository[].class, pageSize);
return new Iterator<List<GHRepository>>() {
public boolean hasNext() {
return pager.hasNext();
}
public List<GHRepository> next() {
GHRepository[] batch = pager.next();
for (GHRepository r : batch)
r.root = root;
return Arrays.asList(batch);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
return () -> {
final PagedIterator<GHRepository> pager;
try {
GitHubPageIterator<GHRepository[]> iterator = GitHubPageIterator.create(root.getClient(),
GHRepository[].class,
root.createRequest().withUrlPath("users", login, "repos").build(),
pageSize);
pager = new PagedIterator<>(iterator, item -> item.wrap(root));
} catch (MalformedURLException e) {
throw new GHException("Unable to build GitHub API URL", e);
}
return new Iterator<List<GHRepository>>() {
public boolean hasNext() {
return pager.hasNext();
}
public List<GHRepository> next() {
return pager.nextPage();
}
};
};
}
@@ -173,22 +175,18 @@ public abstract class GHPerson extends GHObject {
* @return the gravatar id
* @deprecated No longer available in the v3 API.
*/
@Deprecated
public String getGravatarId() {
return gravatar_id;
return "";
}
/**
* Returns a string like 'https://secure.gravatar.com/avatar/0cb9832a01c22c083390f3c5dcb64105' that indicates the
* avatar image URL.
* Returns a string of the avatar image URL.
*
* @return the avatar url
*/
public String getAvatarUrl() {
if (avatar_url != null)
return avatar_url;
if (gravatar_id != null)
return "https://secure.gravatar.com/avatar/" + gravatar_id;
return null;
return avatar_url;
}
/**
@@ -260,7 +258,7 @@ public abstract class GHPerson extends GHObject {
@Override
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -346,4 +344,16 @@ public abstract class GHPerson extends GHObject {
populate();
return site_admin;
}
/**
* Gets total private repo count.
*
* @return the total private repo count
* @throws IOException
* the io exception
*/
public Optional<Integer> getTotalPrivateRepoCount() throws IOException {
populate();
return Optional.ofNullable(total_private_repos);
}
}

View File

@@ -51,7 +51,7 @@ public class GHProject extends GHObject {
@Override
public URL getHtmlUrl() throws IOException {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -99,7 +99,7 @@ public class GHProject extends GHObject {
* @return the owner url
*/
public URL getOwnerUrl() {
return GitHub.parseURL(owner_url);
return GitHubClient.parseURL(owner_url);
}
/**

View File

@@ -149,7 +149,7 @@ public class GHProjectCard extends GHObject {
* @return the content url
*/
public URL getContentUrl() {
return GitHub.parseURL(content_url);
return GitHubClient.parseURL(content_url);
}
/**
@@ -158,7 +158,7 @@ public class GHProjectCard extends GHObject {
* @return the project url
*/
public URL getProjectUrl() {
return GitHub.parseURL(project_url);
return GitHubClient.parseURL(project_url);
}
/**
@@ -167,7 +167,7 @@ public class GHProjectCard extends GHObject {
* @return the column url
*/
public URL getColumnUrl() {
return GitHub.parseURL(column_url);
return GitHubClient.parseURL(column_url);
}
/**

View File

@@ -90,7 +90,7 @@ public class GHProjectColumn extends GHObject {
* @return the project url
*/
public URL getProjectUrl() {
return GitHub.parseURL(project_url);
return GitHubClient.parseURL(project_url);
}
/**

View File

@@ -23,6 +23,8 @@
*/
package org.kohsuke.github;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
@@ -99,6 +101,10 @@ public class GHPullRequest extends GHIssue implements Refreshable {
@Override
protected String getApiRoute() {
if (owner == null) {
// Issues returned from search to do not have an owner. Attempt to use url.
return StringUtils.prependIfMissing(getUrl().toString().replace(root.getApiUrl(), ""), "/");
}
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/pulls/" + number;
}
@@ -108,7 +114,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
* @return the patch url
*/
public URL getPatchUrl() {
return GitHub.parseURL(patch_url);
return GitHubClient.parseURL(patch_url);
}
/**
@@ -117,7 +123,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
* @return the issue url
*/
public URL getIssueUrl() {
return GitHub.parseURL(issue_url);
return GitHubClient.parseURL(issue_url);
}
/**
@@ -156,7 +162,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
* @return the diff url
*/
public URL getDiffUrl() {
return GitHub.parseURL(diff_url);
return GitHubClient.parseURL(diff_url);
}
/**
@@ -165,7 +171,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
* @return the merged at
*/
public Date getMergedAt() {
return GitHub.parseDate(merged_at);
return GitHubClient.parseDate(merged_at);
}
@Override
@@ -447,6 +453,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
* the io exception
* @deprecated Use {@link #createReview()}
*/
@Deprecated
public GHPullRequestReview createReview(String body,
@CheckForNull GHPullRequestReviewState event,
GHPullRequestReviewComment... comments) throws IOException {
@@ -467,6 +474,7 @@ public class GHPullRequest extends GHIssue implements Refreshable {
* the io exception
* @deprecated Use {@link #createReview()}
*/
@Deprecated
public GHPullRequestReview createReview(String body,
@CheckForNull GHPullRequestReviewState event,
List<GHPullRequestReviewComment> comments) throws IOException {

View File

@@ -75,7 +75,7 @@ public class GHPullRequestCommitDetail {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
}
@@ -125,7 +125,7 @@ public class GHPullRequestCommitDetail {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -161,7 +161,7 @@ public class GHPullRequestCommitDetail {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -170,7 +170,7 @@ public class GHPullRequestCommitDetail {
* @return the html url
*/
public URL getHtml_url() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -214,7 +214,7 @@ public class GHPullRequestCommitDetail {
* @return the api url
*/
public URL getApiUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -223,7 +223,7 @@ public class GHPullRequestCommitDetail {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -232,7 +232,7 @@ public class GHPullRequestCommitDetail {
* @return the comments url
*/
public URL getCommentsUrl() {
return GitHub.parseURL(comments_url);
return GitHubClient.parseURL(comments_url);
}
/**

View File

@@ -105,7 +105,7 @@ public class GHPullRequestFileDetail {
* @return the blob url
*/
public URL getBlobUrl() {
return GitHub.parseURL(blob_url);
return GitHubClient.parseURL(blob_url);
}
/**
@@ -114,7 +114,7 @@ public class GHPullRequestFileDetail {
* @return the raw url
*/
public URL getRawUrl() {
return GitHub.parseURL(raw_url);
return GitHubClient.parseURL(raw_url);
}
/**
@@ -123,7 +123,7 @@ public class GHPullRequestFileDetail {
* @return the contents url
*/
public URL getContentsUrl() {
return GitHub.parseURL(contents_url);
return GitHubClient.parseURL(contents_url);
}
/**

View File

@@ -122,7 +122,7 @@ public class GHPullRequestReview extends GHObject {
* the io exception
*/
public Date getSubmittedAt() throws IOException {
return GitHub.parseDate(submitted_at);
return GitHubClient.parseDate(submitted_at);
}
/**
@@ -145,6 +145,7 @@ public class GHPullRequestReview extends GHObject {
* @deprecated Former preview method that changed when it got public. Left here for backward compatibility. Use
* {@link #submit(String, GHPullRequestReviewEvent)}
*/
@Deprecated
public void submit(String body, GHPullRequestReviewState state) throws IOException {
submit(body, state.toEvent());
}

View File

@@ -44,6 +44,7 @@ public class GHPullRequestReviewComment extends GHObject implements Reactable {
private String body;
private GHUser user;
private String path;
private String html_url;
private int position = -1;
private int original_position = -1;
private long in_reply_to_id = -1L;
@@ -60,6 +61,7 @@ public class GHPullRequestReviewComment extends GHObject implements Reactable {
* @return the gh pull request review comment
* @deprecated You should be using {@link GHPullRequestReviewBuilder#comment(String, String, int)}
*/
@Deprecated
public static GHPullRequestReviewComment draft(String body, String path, int position) {
GHPullRequestReviewComment result = new GHPullRequestReviewComment();
result.body = body;
@@ -142,7 +144,7 @@ public class GHPullRequestReviewComment extends GHObject implements Reactable {
@Override
public URL getHtmlUrl() {
return null;
return GitHubClient.parseURL(html_url);
}
/**

View File

@@ -1,5 +1,6 @@
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -219,6 +220,28 @@ public class GHRateLimit {
return Objects.hash(getCore(), getSearch(), getGraphQL(), getIntegrationManifest());
}
/**
* Gets the appropriate {@link Record} for a particular url path.
*
* @param urlPath
* the url path of the request
* @return the {@link Record} for a url path.
*/
@Nonnull
Record getRecordForUrlPath(@Nonnull String urlPath) {
if (urlPath.equals("/rate_limit")) {
return new UnknownLimitRecord();
} else if (urlPath.startsWith("/search")) {
return getSearch();
} else if (urlPath.startsWith("/graphql")) {
return getGraphQL();
} else if (urlPath.startsWith("/app-manifests")) {
return getIntegrationManifest();
} else {
return getCore();
}
}
/**
* A limit record used as a placeholder when the the actual limit is not known.
* <p>
@@ -257,6 +280,8 @@ public class GHRateLimit {
/**
* The time at which the current rate limit window resets in UTC epoch seconds.
*
* This is the raw value returned by the server.
*/
private final long resetEpochSeconds;
@@ -266,11 +291,12 @@ public class GHRateLimit {
private final long createdAtEpochSeconds = System.currentTimeMillis() / 1000;
/**
* The calculated time at which the rate limit will reset. Recalculated if {@link #recalculateResetDate} is
* called.
* The time at which the rate limit will reset. This value is calculated based on
* {@link #getResetEpochSeconds()} by calling {@link #calculateResetDate}. If the clock on the local machine not
* synchronized with the server clock, this time value will be adjusted to match the local machine's clock.
*/
@Nonnull
private Date resetDate;
private final Date resetDate;
/**
* Instantiates a new Record.
@@ -282,7 +308,6 @@ public class GHRateLimit {
* @param resetEpochSeconds
* the reset epoch seconds
*/
@JsonCreator
public Record(@JsonProperty(value = "limit", required = true) int limit,
@JsonProperty(value = "remaining", required = true) int remaining,
@JsonProperty(value = "reset", required = true) long resetEpochSeconds) {
@@ -290,7 +315,7 @@ public class GHRateLimit {
}
/**
* Instantiates a new Record.
* Instantiates a new Record. Called by Jackson data binding or during header parsing.
*
* @param limit
* the limit
@@ -298,26 +323,53 @@ public class GHRateLimit {
* the remaining
* @param resetEpochSeconds
* the reset epoch seconds
* @param updatedAt
* the updated at
* @param responseInfo
* the response info
*/
@SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD", justification = "Deprecated")
public Record(int limit, int remaining, long resetEpochSeconds, @CheckForNull String updatedAt) {
@JsonCreator
Record(@JsonProperty(value = "limit", required = true) int limit,
@JsonProperty(value = "remaining", required = true) int remaining,
@JsonProperty(value = "reset", required = true) long resetEpochSeconds,
@JacksonInject @CheckForNull GitHubResponse.ResponseInfo responseInfo) {
this.limit = limit;
this.remaining = remaining;
this.resetEpochSeconds = resetEpochSeconds;
this.resetDate = recalculateResetDate(updatedAt);
String updatedAt = null;
if (responseInfo != null) {
updatedAt = responseInfo.headerField("Date");
}
this.resetDate = calculateResetDate(updatedAt);
}
/**
* Recalculates the reset date using the server response date to calculate a time duration and then add that to
* the local created time for this record.
* Recalculates the {@link #resetDate} relative to the local machine clock.
* <p>
* {@link RateLimitChecker}s and {@link RateLimitHandler}s use {@link #getResetDate()} to make decisions about
* how long to wait for until for the rate limit to reset. That means that {@link #getResetDate()} needs to be
* accurate to the local machine.
* </p>
* <p>
* When we say that the clock on two machines is "synchronized", we mean that the UTC time returned from
* {@link System#currentTimeMillis()} on each machine is basically the same. For the purposes of rate limits an
* differences of up to a second can be ignored.
* </p>
* <p>
* When the clock on the local machine is synchronized to the same time as the clock on the GitHub server (via a
* time service for example), the {@link #resetDate} generated directly from {@link #resetEpochSeconds} will be
* accurate for the local machine as well.
* </p>
* <p>
* When the clock on the local machine is not synchronized with the server, the {@link #resetDate} must be
* recalculated relative to the local machine clock. This is done by taking the number of seconds between the
* response "Date" header and {@link #resetEpochSeconds} and then adding that to this record's
* {@link #createdAtEpochSeconds}.
*
* @param updatedAt
* a string date in RFC 1123
* @return reset date based on the passed date
*/
Date recalculateResetDate(@CheckForNull String updatedAt) {
@Nonnull
private Date calculateResetDate(@CheckForNull String updatedAt) {
long updatedAtEpochSeconds = createdAtEpochSeconds;
if (!StringUtils.isBlank(updatedAt)) {
try {
@@ -334,7 +386,7 @@ public class GHRateLimit {
// This may seem odd but it results in an accurate or slightly pessimistic reset date
// based on system time rather than assuming the system time synchronized with the server
long calculatedSecondsUntilReset = resetEpochSeconds - updatedAtEpochSeconds;
return resetDate = new Date((createdAtEpochSeconds + calculatedSecondsUntilReset) * 1000);
return new Date((createdAtEpochSeconds + calculatedSecondsUntilReset) * 1000);
}
/**
@@ -358,7 +410,12 @@ public class GHRateLimit {
/**
* Gets the time in epoch seconds when the rate limit will reset.
*
* @return a long
* This is the raw value returned by the server. This value is not adjusted if local machine time is not
* synchronized with server time. If attempting to check when the rate limit will reset, use
* {@link #getResetDate()} or implement a {@link RateLimitChecker} instead.
*
* @return a long representing the time in epoch seconds when the rate limit will reset
* @see #getResetDate() #getResetDate()
*/
public long getResetEpochSeconds() {
return resetEpochSeconds;
@@ -374,7 +431,10 @@ public class GHRateLimit {
}
/**
* Returns the date at which the rate limit will reset.
* Returns the date at which the rate limit will reset, adjusted to local machine time if the local machine's
* clock not synchronized with to the same clock as the GitHub server.
*
* If attempting to wait for the rate limit to reset, consider implementing a {@link RateLimitChecker} instead.
*
* @return the calculated date at which the rate limit has or will reset.
*/

View File

@@ -31,7 +31,7 @@ public class GHRef {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**
@@ -90,13 +90,6 @@ public class GHRef {
return this;
}
static GHRef[] wrap(GHRef[] in, GitHub root) {
for (GHRef r : in) {
r.wrap(root);
}
return in;
}
/**
* The type GHObject.
*/
@@ -131,7 +124,7 @@ public class GHRef {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
}
}

View File

@@ -6,7 +6,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@@ -72,12 +71,13 @@ public class GHRelease extends GHObject {
* the io exception
* @deprecated Use {@link #update()}
*/
@Deprecated
public GHRelease setDraft(boolean draft) throws IOException {
return update().draft(draft).update();
}
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -258,8 +258,9 @@ public class GHRelease extends GHObject {
public List<GHAsset> getAssets() throws IOException {
Requester builder = owner.root.createRequest();
GHAsset[] assets = builder.withUrlPath(getApiTailUrl("assets")).fetchArray(GHAsset[].class);
return Arrays.asList(GHAsset.wrap(assets, this));
return builder.withUrlPath(getApiTailUrl("assets"))
.toIterable(GHAsset[].class, item -> item.wrap(this))
.toList();
}
/**

View File

@@ -25,6 +25,8 @@ package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils;
@@ -37,7 +39,6 @@ import java.io.Reader;
import java.net.URL;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@@ -62,9 +63,9 @@ import static org.kohsuke.github.Previews.*;
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public class GHRepository extends GHObject {
/* package almost final */ GitHub root;
/* package almost final */ transient GitHub root;
private String description, homepage, name, full_name;
private String nodeId, description, homepage, name, full_name;
private String html_url; // this is the UI
/*
* The license information makes use of the preview API.
@@ -75,12 +76,14 @@ public class GHRepository extends GHObject {
private String git_url, ssh_url, clone_url, svn_url, mirror_url;
private GHUser owner; // not fully populated. beware.
private boolean has_issues, has_wiki, fork, has_downloads, has_pages, archived;
private boolean has_issues, has_wiki, fork, has_downloads, has_pages, archived, has_projects;
private boolean allow_squash_merge;
private boolean allow_merge_commit;
private boolean allow_rebase_merge;
private boolean delete_branch_on_merge;
@JsonProperty("private")
private boolean _private;
private int forks_count, stargazers_count, watchers_count, size, open_issues_count, subscribers_count;
@@ -116,6 +119,7 @@ public class GHRepository extends GHObject {
* the io exception
* @deprecated Use {@code getDeployment(id).listStatuses()}
*/
@Deprecated
public PagedIterable<GHDeploymentStatus> getDeploymentStatuses(final int id) throws IOException {
return getDeployment(id).listStatuses();
}
@@ -171,6 +175,7 @@ public class GHRepository extends GHObject {
* the io exception
* @deprecated Use {@code getDeployment(deploymentId).createStatus(ghDeploymentState)}
*/
@Deprecated
public GHDeploymentStatusBuilder createDeployStatus(int deploymentId, GHDeploymentState ghDeploymentState)
throws IOException {
return getDeployment(deploymentId).createStatus(ghDeploymentState);
@@ -180,6 +185,15 @@ public class GHRepository extends GHObject {
boolean pull, push, admin;
}
/**
* Gets node id
*
* @return the node id
*/
public String getNodeId() {
return nodeId;
}
/**
* Gets description.
*
@@ -222,6 +236,7 @@ public class GHRepository extends GHObject {
* @return the string
* @deprecated Typo of {@link #getHttpTransportUrl()}
*/
@Deprecated
public String gitHttpTransportUrl() {
return clone_url;
}
@@ -255,7 +270,7 @@ public class GHRepository extends GHObject {
}
public URL getHtmlUrl() {
return GitHub.parseURL(html_url);
return GitHubClient.parseURL(html_url);
}
/**
@@ -358,7 +373,7 @@ public class GHRepository extends GHObject {
* the io exception
*/
public List<GHIssue> getIssues(GHIssueState state) throws IOException {
return listIssues(state).asList();
return listIssues(state).toList();
}
/**
@@ -376,8 +391,9 @@ public class GHRepository extends GHObject {
Requester requester = root.createRequest()
.with("state", state)
.with("milestone", milestone == null ? "none" : "" + milestone.getNumber());
return Arrays
.asList(GHIssue.wrap(requester.withUrlPath(getApiTailUrl("issues")).fetchArray(GHIssue[].class), this));
return requester.withUrlPath(getApiTailUrl("issues"))
.toIterable(GHIssue[].class, item -> item.wrap(this))
.toList();
}
/**
@@ -436,7 +452,7 @@ public class GHRepository extends GHObject {
* @deprecated use {@link #listReleases()}
*/
public List<GHRelease> getReleases() throws IOException {
return listReleases().asList();
return listReleases().toList();
}
/**
@@ -551,6 +567,15 @@ public class GHRepository extends GHObject {
return has_issues;
}
/**
* Has projects boolean.
*
* @return the boolean
*/
public boolean hasProjects() {
return has_projects;
}
/**
* Has wiki boolean.
*
@@ -605,13 +630,34 @@ public class GHRepository extends GHObject {
return allow_rebase_merge;
}
/**
* Automatically deleting head branches when pull requests are merged
*
* @return the boolean
*/
public boolean isDeleteBranchOnMerge() {
return delete_branch_on_merge;
}
/**
* Returns the number of all forks of this repository. This not only counts direct forks, but also forks of forks,
* and so on.
*
* @return the forks
* @deprecated use {@link #getForksCount()} instead
*/
@Deprecated
public int getForks() {
return getForksCount();
}
/**
* Returns the number of all forks of this repository. This not only counts direct forks, but also forks of forks,
* and so on.
*
* @return the forks
*/
public int getForks() {
public int getForksCount() {
return forks_count;
}
@@ -655,8 +701,19 @@ public class GHRepository extends GHObject {
* Gets watchers.
*
* @return the watchers
* @deprecated use {@link #getWatchersCount()} instead
*/
@Deprecated
public int getWatchers() {
return getWatchersCount();
}
/**
* Gets the count of watchers.
*
* @return the watchers
*/
public int getWatchersCount() {
return watchers_count;
}
@@ -669,16 +726,6 @@ public class GHRepository extends GHObject {
return open_issues_count;
}
/**
* Gets network count.
*
* @return the network count
* @deprecated This no longer exists in the official API documentation. Use {@link #getForks()}
*/
public int getNetworkCount() {
return forks_count;
}
/**
* Gets subscribers count.
*
@@ -694,7 +741,7 @@ public class GHRepository extends GHObject {
* @return null if the repository was never pushed at.
*/
public Date getPushedAt() {
return GitHub.parseDate(pushed_at);
return GitHubClient.parseDate(pushed_at);
}
/**
@@ -712,6 +759,7 @@ public class GHRepository extends GHObject {
* @return the master branch
* @deprecated Renamed to {@link #getDefaultBranch()}
*/
@Deprecated
public String getMasterBranch() {
return default_branch;
}
@@ -734,7 +782,7 @@ public class GHRepository extends GHObject {
*/
@WithBridgeMethods(Set.class)
public GHPersonSet<GHUser> getCollaborators() throws IOException {
return new GHPersonSet<GHUser>(listCollaborators().asList());
return new GHPersonSet<GHUser>(listCollaborators().toList());
}
/**
@@ -784,11 +832,14 @@ public class GHRepository extends GHObject {
* the io exception
*/
public Set<String> getCollaboratorNames() throws IOException {
Set<String> r = new HashSet<String>();
for (GHUser u : GHUser.wrap(
root.createRequest().withUrlPath(getApiTailUrl("collaborators")).fetchArray(GHUser[].class),
root))
Set<String> r = new HashSet<>();
// no initializer - we just want to the logins
PagedIterable<GHUser> users = root.createRequest()
.withUrlPath(getApiTailUrl("collaborators"))
.toIterable(GHUser[].class, null);
for (GHUser u : users.toArray()) {
r.add(u.login);
}
return r;
}
@@ -813,7 +864,7 @@ public class GHRepository extends GHObject {
* Obtain permission for a given user in this repository.
*
* @param u
* the u
* the user
* @return the permission
* @throws IOException
* the io exception
@@ -830,9 +881,25 @@ public class GHRepository extends GHObject {
* the io exception
*/
public Set<GHTeam> getTeams() throws IOException {
return Collections.unmodifiableSet(new HashSet<GHTeam>(Arrays.asList(
GHTeam.wrapUp(root.createRequest().withUrlPath(getApiTailUrl("teams")).fetchArray(GHTeam[].class),
root.getOrganization(getOwnerName())))));
GHOrganization org = root.getOrganization(getOwnerName());
return root.createRequest()
.withUrlPath(getApiTailUrl("teams"))
.toIterable(GHTeam[].class, item -> item.wrapUp(org))
.toSet();
}
/**
* Add collaborators.
*
* @param users
* the users
* @param permission
* the permission level
* @throws IOException
* the io exception
*/
public void addCollaborators(GHOrganization.Permission permission, GHUser... users) throws IOException {
addCollaborators(asList(users), permission);
}
/**
@@ -856,7 +923,21 @@ public class GHRepository extends GHObject {
* the io exception
*/
public void addCollaborators(Collection<GHUser> users) throws IOException {
modifyCollaborators(users, "PUT");
modifyCollaborators(users, "PUT", null);
}
/**
* Add collaborators.
*
* @param users
* the users
* @param permission
* the permission level
* @throws IOException
* the io exception
*/
public void addCollaborators(Collection<GHUser> users, GHOrganization.Permission permission) throws IOException {
modifyCollaborators(users, "PUT", permission);
}
/**
@@ -880,12 +961,20 @@ public class GHRepository extends GHObject {
* the io exception
*/
public void removeCollaborators(Collection<GHUser> users) throws IOException {
modifyCollaborators(users, "DELETE");
modifyCollaborators(users, "DELETE", null);
}
private void modifyCollaborators(Collection<GHUser> users, String method) throws IOException {
private void modifyCollaborators(@NonNull Collection<GHUser> users,
@NonNull String method,
@CheckForNull GHOrganization.Permission permission) throws IOException {
Requester requester = root.createRequest().method(method);
if (permission != null) {
requester = requester.with("permission", permission).inBody();
}
for (GHUser user : users) {
root.createRequest().method(method).withUrlPath(getApiTailUrl("collaborators/" + user.getLogin())).send();
requester.withUrlPath(getApiTailUrl("collaborators/" + user.getLogin())).send();
}
}
@@ -928,6 +1017,18 @@ public class GHRepository extends GHObject {
edit("has_issues", String.valueOf(v));
}
/**
* Enables or disables projects for this repository.
*
* @param v
* the v
* @throws IOException
* the io exception
*/
public void enableProjects(boolean v) throws IOException {
edit("has_projects", String.valueOf(v));
}
/**
* Enables or disables Wiki for this repository.
*
@@ -1048,6 +1149,18 @@ public class GHRepository extends GHObject {
edit("allow_rebase_merge", Boolean.toString(value));
}
/**
* After pull requests are merged, you can have head branches deleted automatically.
*
* @param value
* the value
* @throws IOException
* the io exception
*/
public void deleteBranchOnMerge(boolean value) throws IOException {
edit("delete_branch_on_merge", Boolean.toString(value));
}
/**
* Deletes this repository.
*
@@ -1202,7 +1315,7 @@ public class GHRepository extends GHObject {
* @see #listPullRequests(GHIssueState) #listPullRequests(GHIssueState)
*/
public List<GHPullRequest> getPullRequests(GHIssueState state) throws IOException {
return queryPullRequests().state(state).list().asList();
return queryPullRequests().state(state).list().toList();
}
/**
@@ -1213,6 +1326,7 @@ public class GHRepository extends GHObject {
* @return the paged iterable
* @deprecated Use {@link #queryPullRequests()}
*/
@Deprecated
public PagedIterable<GHPullRequest> listPullRequests(GHIssueState state) {
return queryPullRequests().state(state).list();
}
@@ -1413,9 +1527,7 @@ public class GHRepository extends GHObject {
* on failure communicating with GitHub
*/
public GHRef[] getRefs() throws IOException {
return GHRef.wrap(root.createRequest()
.withUrlPath(String.format("/repos/%s/%s/git/refs", getOwnerName(), name))
.fetchArray(GHRef[].class), root);
return listRefs().toArray();
}
/**
@@ -1440,9 +1552,7 @@ public class GHRepository extends GHObject {
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
public GHRef[] getRefs(String refType) throws IOException {
return GHRef.wrap(root.createRequest()
.withUrlPath(String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType))
.fetchArray(GHRef[].class), root);
return listRefs(refType).toArray();
}
/**
@@ -1469,6 +1579,11 @@ public class GHRepository extends GHObject {
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
public GHRef getRef(String refName) throws IOException {
// Also accept e.g. "refs/heads/branch" for consistency with createRef().
if (refName.startsWith("refs/")) {
refName = refName.replaceFirst("refs/", "");
}
return root.createRequest()
.withUrlPath(getApiTailUrl(String.format("git/refs/%s", refName)))
.fetch(GHRef.class)
@@ -1676,7 +1791,7 @@ public class GHRepository extends GHObject {
}
/**
* /** Lists all the commit statues attached to the given commit, newer ones first.
* /** Lists all the commit statuses attached to the given commit, newer ones first.
*
* @param sha1
* the sha 1
@@ -1700,10 +1815,31 @@ public class GHRepository extends GHObject {
* the io exception
*/
public GHCommitStatus getLastCommitStatus(String sha1) throws IOException {
List<GHCommitStatus> v = listCommitStatuses(sha1).asList();
List<GHCommitStatus> v = listCommitStatuses(sha1).toList();
return v.isEmpty() ? null : v.get(0);
}
/**
* Gets check runs for given ref.
*
* @param ref
* ref
* @return check runs for given ref
* @throws IOException
* the io exception
* @see <a href="https://developer.github.com/v3/checks/runs/#list-check-runs-for-a-specific-ref">List check runs
* for a specific ref</a>
*/
@Preview
@Deprecated
public PagedIterable<GHCheckRun> getCheckRuns(String ref) throws IOException {
GitHubRequest request = root.createRequest()
.withUrlPath(String.format("/repos/%s/%s/commits/%s/check-runs", getOwnerName(), name, ref))
.withPreview(ANTIOPE)
.build();
return new GHCheckRunsIterable(root, request);
}
/**
* Creates a commit status
*
@@ -1759,6 +1895,21 @@ public class GHRepository extends GHObject {
return createCommitStatus(sha1, state, targetUrl, description, null);
}
/**
* Creates a check run for a commit.
*
* @param name
* an identifier for the run
* @param headSHA
* the commit hash
* @return a builder which you should customize, then call {@link GHCheckRunBuilder#create}
*/
@Preview
@Deprecated
public @NonNull GHCheckRunBuilder createCheckRun(@NonNull String name, @NonNull String headSHA) {
return new GHCheckRunBuilder(this, name, headSHA);
}
/**
* Lists repository events.
*
@@ -1782,9 +1933,7 @@ public class GHRepository extends GHObject {
* the io exception
*/
public PagedIterable<GHLabel> listLabels() throws IOException {
return root.createRequest()
.withUrlPath(getApiTailUrl("labels"))
.toIterable(GHLabel[].class, item -> item.wrapUp(this));
return GHLabel.readAll(this);
}
/**
@@ -1797,7 +1946,7 @@ public class GHRepository extends GHObject {
* the io exception
*/
public GHLabel getLabel(String name) throws IOException {
return root.createRequest().withUrlPath(getApiTailUrl("labels/" + name)).fetch(GHLabel.class).wrapUp(this);
return GHLabel.read(this, name);
}
/**
@@ -1812,7 +1961,7 @@ public class GHRepository extends GHObject {
* the io exception
*/
public GHLabel createLabel(String name, String color) throws IOException {
return createLabel(name, color, "");
return GHLabel.create(this).name(name).color(color).description("").done();
}
/**
@@ -1829,14 +1978,7 @@ public class GHRepository extends GHObject {
* the io exception
*/
public GHLabel createLabel(String name, String color, String description) throws IOException {
return root.createRequest()
.method("POST")
.with("name", name)
.with("color", color)
.with("description", description)
.withUrlPath(getApiTailUrl("labels"))
.fetch(GHLabel.class)
.wrapUp(this);
return GHLabel.create(this).name(name).color(color).description(description).done();
}
/**
@@ -1939,14 +2081,6 @@ public class GHRepository extends GHObject {
return createWebHook(url, null);
}
// this is no different from getPullRequests(OPEN)
// /**
// * Retrieves all the pull requests.
// */
// public List<GHPullRequest> getPullRequests() throws IOException {
// return root.retrieveWithAuth("/pulls/"+owner+'/'+name,JsonPullRequests.class).wrap(root);
// }
/**
* Returns a set that represents the post-commit hook URLs. The returned set is live, and changes made to them are
* reflected to GitHub.
@@ -1956,6 +2090,7 @@ public class GHRepository extends GHObject {
*/
@SuppressFBWarnings(value = "DMI_COLLECTION_OF_URLS",
justification = "It causes a performance degradation, but we have already exposed it to the API")
@Deprecated
public Set<URL> getPostCommitHooks() {
return postCommitHooks;
}
@@ -1969,7 +2104,7 @@ public class GHRepository extends GHObject {
private final Set<URL> postCommitHooks = new AbstractSet<URL>() {
private List<URL> getPostCommitHooks() {
try {
List<URL> r = new ArrayList<URL>();
List<URL> r = new ArrayList<>();
for (GHHook h : getHooks()) {
if (h.getName().equals("web")) {
r.add(new URL(h.getConfig().get("url")));
@@ -2020,7 +2155,7 @@ public class GHRepository extends GHObject {
GHRepository wrap(GitHub root) {
this.root = root;
if (root.isOffline()) {
if (root.isOffline() && owner != null) {
owner.wrapUp(root);
}
return this;
@@ -2035,8 +2170,10 @@ public class GHRepository extends GHObject {
*/
public Map<String, GHBranch> getBranches() throws IOException {
Map<String, GHBranch> r = new TreeMap<String, GHBranch>();
for (GHBranch p : root.createRequest().withUrlPath(getApiTailUrl("branches")).fetchArray(GHBranch[].class)) {
p.wrap(this);
for (GHBranch p : root.createRequest()
.withUrlPath(getApiTailUrl("branches"))
.toIterable(GHBranch[].class, item -> item.wrap(this))
.toArray()) {
r.put(p.getName(), p);
}
return r;
@@ -2167,11 +2304,10 @@ public class GHRepository extends GHObject {
}
String target = getApiTailUrl("contents/" + path);
GHContent[] files = requester.with("ref", ref).withUrlPath(target).fetchArray(GHContent[].class);
GHContent.wrap(files, this);
return Arrays.asList(files);
return requester.with("ref", ref)
.withUrlPath(target)
.toIterable(GHContent[].class, item -> item.wrap(this))
.toList();
}
/**
@@ -2325,11 +2461,10 @@ public class GHRepository extends GHObject {
* the io exception
*/
public List<GHDeployKey> getDeployKeys() throws IOException {
List<GHDeployKey> list = new ArrayList<GHDeployKey>(
Arrays.asList(root.createRequest().withUrlPath(getApiTailUrl("keys")).fetchArray(GHDeployKey[].class)));
for (GHDeployKey h : list)
h.wrap(this);
return list;
return root.createRequest()
.withUrlPath(getApiTailUrl("keys"))
.toIterable(GHDeployKey[].class, item -> item.wrap(this))
.toList();
}
/**
@@ -2682,4 +2817,17 @@ public class GHRepository extends GHObject {
.fetch(GHTagObject.class)
.wrap(this);
}
/**
* Populate this object.
*
* @throws java.io.IOException
* The IO exception
*/
void populate() throws IOException {
if (root.isOffline())
return; // can't populate if the root is offline
root.createRequest().withApiUrl(root.getApiUrl() + full_name).fetchInto(this).wrap(root);
}
}

View File

@@ -47,7 +47,7 @@ public abstract class GHRepositoryTraffic implements TrafficInfo {
* @return the timestamp
*/
public Date getTimestamp() {
return GitHub.parseDate(timestamp);
return GitHubClient.parseDate(timestamp);
}
public int getCount() {

View File

@@ -2,6 +2,7 @@ package org.kohsuke.github;
import org.apache.commons.lang3.StringUtils;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
@@ -23,6 +24,7 @@ public abstract class GHSearchBuilder<T> extends GHQueryBuilder<T> {
GHSearchBuilder(GitHub root, Class<? extends SearchResult<T>> receiverType) {
super(root);
this.receiverType = receiverType;
req.withUrlPath(getApiUrl());
}
/**
@@ -42,16 +44,13 @@ public abstract class GHSearchBuilder<T> extends GHQueryBuilder<T> {
*/
@Override
public PagedSearchIterable<T> list() {
return new PagedSearchIterable<T>(root) {
public PagedIterator<T> _iterator(int pageSize) {
req.set("q", StringUtils.join(terms, " "));
return new PagedIterator<T>(adapt(req.withUrlPath(getApiUrl()).asIterator(receiverType, pageSize))) {
protected void wrapUp(T[] page) {
// SearchResult.getItems() should do it
}
};
}
};
req.set("q", StringUtils.join(terms, " "));
try {
return new PagedSearchIterable<>(root, req.build(), receiverType);
} catch (MalformedURLException e) {
throw new GHException("", e);
}
}
/**

View File

@@ -32,7 +32,7 @@ public class GHStargazer {
* @return the date the stargazer was added
*/
public Date getStarredAt() {
return GitHub.parseDate(starred_at);
return GitHubClient.parseDate(starred_at);
}
/**

View File

@@ -23,7 +23,7 @@ public class GHSubscription {
* @return the created at
*/
public Date getCreatedAt() {
return GitHub.parseDate(created_at);
return GitHubClient.parseDate(created_at);
}
/**

View File

@@ -19,6 +19,7 @@ public class GHTagObject {
private String message;
private GitUser tagger;
private GHRef.GHObject object;
private GHVerification verification;
GHTagObject wrap(GHRepository owner) {
this.owner = owner;
@@ -97,4 +98,13 @@ public class GHTagObject {
public GHRef.GHObject getObject() {
return object;
}
/**
* Gets Verification Status.
*
* @return the Verification status
*/
public GHVerification getVerification() {
return verification;
}
}

View File

@@ -1,7 +1,6 @@
package org.kohsuke.github;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@@ -54,13 +53,6 @@ public class GHTeam implements Refreshable {
return wrapUp(organization);
}
static GHTeam[] wrapUp(GHTeam[] teams, GHOrganization owner) {
for (GHTeam t : teams) {
t.wrapUp(owner);
}
return teams;
}
static GHTeam[] wrapUp(GHTeam[] teams, GHPullRequest owner) {
for (GHTeam t : teams) {
t.root = owner.root;
@@ -165,7 +157,7 @@ public class GHTeam implements Refreshable {
* the io exception
*/
public Set<GHUser> getMembers() throws IOException {
return Collections.unmodifiableSet(listMembers().asSet());
return listMembers().toSet();
}
/**

View File

@@ -41,7 +41,7 @@ public class GHThread extends GHObject {
* @return the last read at
*/
public Date getLastReadAt() {
return GitHub.parseDate(last_read_at);
return GitHubClient.parseDate(last_read_at);
}
/**

View File

@@ -71,7 +71,7 @@ public class GHTree {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
GHTree wrap(GHRepository repo) {

View File

@@ -68,7 +68,7 @@ public class GHTreeEntry {
* @return the url
*/
public URL getUrl() {
return GitHub.parseURL(url);
return GitHubClient.parseURL(url);
}
/**

View File

@@ -43,8 +43,7 @@ public class GHUser extends GHPerson {
* the io exception
*/
public List<GHKey> getKeys() throws IOException {
return Collections.unmodifiableList(
Arrays.asList(root.createRequest().withUrlPath(getApiTailUrl("keys")).fetchArray(GHKey[].class)));
return root.createRequest().withUrlPath(getApiTailUrl("keys")).toIterable(GHKey[].class, null).toList();
}
/**
@@ -76,7 +75,7 @@ public class GHUser extends GHPerson {
*/
@WithBridgeMethods(Set.class)
public GHPersonSet<GHUser> getFollows() throws IOException {
return new GHPersonSet<GHUser>(listFollows().asList());
return new GHPersonSet<GHUser>(listFollows().toList());
}
/**
@@ -97,7 +96,7 @@ public class GHUser extends GHPerson {
*/
@WithBridgeMethods(Set.class)
public GHPersonSet<GHUser> getFollowers() throws IOException {
return new GHPersonSet<GHUser>(listFollowers().asList());
return new GHPersonSet<GHUser>(listFollowers().toList());
}
/**
@@ -193,7 +192,8 @@ public class GHUser extends GHPerson {
Set<String> names = new HashSet<String>();
for (GHOrganization o : root.createRequest()
.withUrlPath("/users/" + login + "/orgs")
.fetchArray(GHOrganization[].class)) {
.toIterable(GHOrganization[].class, null)
.toArray()) {
if (names.add(o.getLogin())) // I've seen some duplicates in the data
orgs.add(root.getOrganization(o.getLogin()));
}
@@ -219,7 +219,7 @@ public class GHUser extends GHPerson {
public PagedIterable<GHGist> listGists() throws IOException {
return root.createRequest()
.withUrlPath(String.format("/users/%s/gists", login))
.toIterable(GHGist[].class, item -> item.wrapUp(this));
.toIterable(GHGist[].class, null);
}
@Override

View File

@@ -0,0 +1,82 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* The commit/tag can be signed by user. This object holds the verification status. Whether the Commit/Tag is signed or
* not.
*
* @see <a href="https://developer.github.com/v3/git/tags/#signature-verification-object">tags signature
* verificatiion</a>
* @see <a href="https://developer.github.com/v3/git/commits/#signature-verification-object">commits signature
* verificatiion</a>
*
* @author Sourabh Sarvotham Parkala
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public class GHVerification {
private String signature, payload;
private boolean verified;
private Reason reason;
/**
* Indicates whether GitHub considers the signature in this commit to be verified.
*
* @return true if the signature is valid else returns false.
*/
public boolean isVerified() {
return verified;
}
/**
* Gets reason for verification value.
*
* @return return reason of type {@link Reason}, such as "valid" or "unsigned". The possible values can be found in
* {@link Reason}}
*/
public Reason getReason() {
return reason;
}
/**
* Gets signature used for the verification.
*
* @return null if not signed else encoded signature.
*/
public String getSignature() {
return signature;
}
/**
* Gets the payload that was signed.
*
* @return null if not signed else encoded signature.
*/
public String getPayload() {
return payload;
}
/**
* The possible values for reason in verification object from github.
*
* @see <a href="https://developer.github.com/v3/repos/commits/#signature-verification-object">List of possible
* reason values</a>
* @author Sourabh Sarvotham Parkala
*/
public enum Reason {
EXPIRED_KEY,
NOT_SIGNING_KEY,
GPGVERIFY_ERROR,
GPGVERIFY_UNAVAILABLE,
UNSIGNED,
UNKNOWN_SIGNATURE_TYPE,
NO_USER,
UNVERIFIED_EMAIL,
BAD_EMAIL,
UNKNOWN_KEY,
MALFORMED_SIGNATURE,
INVALID,
VALID
}
}

View File

@@ -23,23 +23,12 @@
*/
package org.kohsuke.github;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker.Std;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Base64;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
@@ -48,10 +37,6 @@ import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.util.logging.Level.FINE;
import static org.kohsuke.github.Previews.INERTIA;
import static org.kohsuke.github.Previews.MACHINE_MAN;
@@ -67,27 +52,15 @@ import static org.kohsuke.github.Previews.MACHINE_MAN;
* @author Kohsuke Kawaguchi
*/
public class GitHub {
final String login;
/**
* Value of the authorization header to be sent with the request.
*/
final String encodedAuthorization;
@Nonnull
private final GitHubClient client;
@CheckForNull
private GHMyself myself;
private final ConcurrentMap<String, GHUser> users;
private final ConcurrentMap<String, GHOrganization> orgs;
// Cache of myself object.
private GHMyself myself;
private final String apiUrl;
final RateLimitHandler rateLimitHandler;
final AbuseLimitHandler abuseLimitHandler;
private HttpConnector connector = HttpConnector.DEFAULT;
private final Object headerRateLimitLock = new Object();
private GHRateLimit headerRateLimit = null;
private volatile GHRateLimit rateLimit = null;
/**
* Creates a client API root object.
@@ -137,36 +110,20 @@ public class GitHub {
String password,
HttpConnector connector,
RateLimitHandler rateLimitHandler,
AbuseLimitHandler abuseLimitHandler) throws IOException {
if (apiUrl.endsWith("/"))
apiUrl = apiUrl.substring(0, apiUrl.length() - 1); // normalize
this.apiUrl = apiUrl;
if (null != connector)
this.connector = connector;
if (oauthAccessToken != null) {
encodedAuthorization = "token " + oauthAccessToken;
} else {
if (jwtToken != null) {
encodedAuthorization = "Bearer " + jwtToken;
} else if (password != null) {
String authorization = (login + ':' + password);
String charsetName = StandardCharsets.UTF_8.name();
encodedAuthorization = "Basic "
+ Base64.getEncoder().encodeToString(authorization.getBytes(charsetName));
} else {// anonymous access
encodedAuthorization = null;
}
}
users = new ConcurrentHashMap<String, GHUser>();
orgs = new ConcurrentHashMap<String, GHOrganization>();
this.rateLimitHandler = rateLimitHandler;
this.abuseLimitHandler = abuseLimitHandler;
if (login == null && encodedAuthorization != null && jwtToken == null)
login = getMyself().getLogin();
this.login = login;
AbuseLimitHandler abuseLimitHandler,
GitHubRateLimitChecker rateLimitChecker) throws IOException {
this.client = new GitHubHttpUrlConnectionClient(apiUrl,
login,
oauthAccessToken,
jwtToken,
password,
connector,
rateLimitHandler,
abuseLimitHandler,
rateLimitChecker,
(myself) -> setMyself(myself));
users = new ConcurrentHashMap<>();
orgs = new ConcurrentHashMap<>();
}
/**
@@ -184,7 +141,10 @@ public class GitHub {
* Version that connects to GitHub Enterprise.
*
* @param apiUrl
* the api url
* The URL of GitHub (or GitHub Enterprise) API endpoint, such as "https://api.github.com" or
* "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has <code>/api/v3</code> in the URL. For
* historical reasons, this parameter still accepts the bare domain name, but that's considered
* deprecated.
* @param oauthAccessToken
* the oauth access token
* @return the git hub
@@ -264,8 +224,7 @@ public class GitHub {
* @return the git hub
* @throws IOException
* the io exception
* @deprecated Either OAuth token or password is sufficient, so there's no point in passing both. Use
* {@link #connectUsingPassword(String, String)} or {@link #connectUsingOAuth(String)}.
* @deprecated Use {@link #connectUsingOAuth(String)}.
*/
@Deprecated
public static GitHub connect(String login, String oauthAccessToken, String password) throws IOException {
@@ -282,7 +241,12 @@ public class GitHub {
* @return the git hub
* @throws IOException
* the io exception
* @deprecated Use {@link #connectUsingOAuth(String)} instead.
* @see <a href=
* "https://developer.github.com/changes/2020-02-14-deprecating-password-auth/#changes-to-make">Deprecating
* password authentication and OAuth authorizations API</a>
*/
@Deprecated
public static GitHub connectUsingPassword(String login, String password) throws IOException {
return new GitHubBuilder().withPassword(login, password).build();
}
@@ -366,7 +330,7 @@ public class GitHub {
* @return {@code true} if operations that require authentication will fail.
*/
public boolean isAnonymous() {
return login == null && encodedAuthorization == null;
return client.isAnonymous();
}
/**
@@ -375,7 +339,7 @@ public class GitHub {
* @return {@code true} if this is an always offline "connection".
*/
public boolean isOffline() {
return connector == HttpConnector.OFFLINE;
return client.isOffline();
}
/**
@@ -384,7 +348,19 @@ public class GitHub {
* @return the connector
*/
public HttpConnector getConnector() {
return connector;
return client.getConnector();
}
/**
* Sets the custom connector used to make requests to GitHub.
*
* @param connector
* the connector
* @deprecated HttpConnector should not be changed. If you find yourself needing to do this, file an issue.
*/
@Deprecated
public void setConnector(HttpConnector connector) {
client.setConnector(connector);
}
/**
@@ -393,39 +369,7 @@ public class GitHub {
* @return the api url
*/
public String getApiUrl() {
return apiUrl;
}
/**
* Sets the custom connector used to make requests to GitHub.
*
* @param connector
* the connector
*/
public void setConnector(HttpConnector connector) {
this.connector = connector;
}
void requireCredential() {
if (isAnonymous())
throw new IllegalStateException(
"This operation requires a credential but none is given to the GitHub constructor");
}
URL getApiURL(String tailApiUrl) throws IOException {
if (tailApiUrl.startsWith("/")) {
if ("github.com".equals(apiUrl)) {// backward compatibility
return new URL(GITHUB_URL + tailApiUrl);
} else {
return new URL(apiUrl + tailApiUrl);
}
} else {
return new URL(tailApiUrl);
}
}
Requester createRequest() {
return new Requester(this);
return client.getApiUrl();
}
/**
@@ -436,64 +380,7 @@ public class GitHub {
* the io exception
*/
public GHRateLimit getRateLimit() throws IOException {
GHRateLimit rateLimit;
try {
rateLimit = createRequest().withUrlPath("/rate_limit").fetch(JsonRateLimit.class).resources;
} catch (FileNotFoundException e) {
// GitHub Enterprise doesn't have the rate limit
// return a default rate limit that
rateLimit = GHRateLimit.Unknown();
}
return this.rateLimit = rateLimit;
}
/**
* Update the Rate Limit with the latest info from response header. Due to multi-threading requests might complete
* out of order, we want to pick the one with the most recent info from the server.
*
* @param observed
* {@link GHRateLimit.Record} constructed from the response header information
*/
void updateCoreRateLimit(@Nonnull GHRateLimit.Record observed) {
synchronized (headerRateLimitLock) {
if (headerRateLimit == null || shouldReplace(observed, headerRateLimit.getCore())) {
headerRateLimit = GHRateLimit.fromHeaderRecord(observed);
LOGGER.log(FINE, "Rate limit now: {0}", headerRateLimit);
}
}
}
/**
* Update the Rate Limit with the latest info from response header. Due to multi-threading requests might complete
* out of order, we want to pick the one with the most recent info from the server. Header date is only accurate to
* the second, so we look at the information in the record itself.
*
* {@link GHRateLimit.UnknownLimitRecord}s are always replaced by regular {@link GHRateLimit.Record}s. Regular
* {@link GHRateLimit.Record}s are never replaced by {@link GHRateLimit.UnknownLimitRecord}s. Candidates with
* resetEpochSeconds later than current record are more recent. Candidates with the same reset and a lower remaining
* count are more recent. Candidates with an earlier reset are older.
*
* @param candidate
* {@link GHRateLimit.Record} constructed from the response header information
* @param current
* the current {@link GHRateLimit.Record} record
*/
static boolean shouldReplace(@Nonnull GHRateLimit.Record candidate, @Nonnull GHRateLimit.Record current) {
if (candidate instanceof GHRateLimit.UnknownLimitRecord
&& !(current instanceof GHRateLimit.UnknownLimitRecord)) {
// Unknown candidate never replaces a regular record
return false;
} else if (current instanceof GHRateLimit.UnknownLimitRecord
&& !(candidate instanceof GHRateLimit.UnknownLimitRecord)) {
// Any real record should replace an unknown Record.
return true;
} else {
// records of the same type compare to each other as normal.
return current.getResetEpochSeconds() < candidate.getResetEpochSeconds()
|| (current.getResetEpochSeconds() == candidate.getResetEpochSeconds()
&& current.getRemaining() > candidate.getRemaining());
}
return client.getRateLimit();
}
/**
@@ -504,9 +391,7 @@ public class GitHub {
*/
@CheckForNull
public GHRateLimit lastRateLimit() {
synchronized (headerRateLimitLock) {
return headerRateLimit;
}
return client.lastRateLimit();
}
/**
@@ -518,16 +403,7 @@ public class GitHub {
*/
@Nonnull
public GHRateLimit rateLimit() throws IOException {
synchronized (headerRateLimitLock) {
if (headerRateLimit != null && !headerRateLimit.isExpired()) {
return headerRateLimit;
}
}
GHRateLimit rateLimit = this.rateLimit;
if (rateLimit == null || rateLimit.isExpired()) {
rateLimit = getRateLimit();
}
return rateLimit;
return client.rateLimit();
}
/**
@@ -539,16 +415,20 @@ public class GitHub {
*/
@WithBridgeMethods(GHUser.class)
public GHMyself getMyself() throws IOException {
requireCredential();
client.requireCredential();
synchronized (this) {
if (this.myself != null)
return myself;
if (this.myself == null) {
GHMyself u = createRequest().withUrlPath("/user").fetch(GHMyself.class);
setMyself(u);
}
return myself;
}
}
GHMyself u = createRequest().withUrlPath("/user").fetch(GHMyself.class);
u.root = this;
this.myself = u;
return u;
private void setMyself(GHMyself myself) {
synchronized (this) {
myself.wrapUp(this);
this.myself = myself;
}
}
@@ -730,12 +610,9 @@ public class GitHub {
* the io exception
*/
public List<GHInvitation> getMyInvitations() throws IOException {
GHInvitation[] invitations = createRequest().withUrlPath("/user/repository_invitations")
.fetchArray(GHInvitation[].class);
for (GHInvitation i : invitations) {
i.wrapUp(this);
}
return Arrays.asList(invitations);
return createRequest().withUrlPath("/user/repository_invitations")
.toIterable(GHInvitation[].class, item -> item.wrapUp(this))
.toList();
}
/**
@@ -749,11 +626,13 @@ public class GitHub {
* the io exception
*/
public Map<String, GHOrganization> getMyOrganizations() throws IOException {
GHOrganization[] orgs = createRequest().withUrlPath("/user/orgs").fetchArray(GHOrganization[].class);
Map<String, GHOrganization> r = new HashMap<String, GHOrganization>();
GHOrganization[] orgs = createRequest().withUrlPath("/user/orgs")
.toIterable(GHOrganization[].class, item -> item.wrapUp(this))
.toArray();
Map<String, GHOrganization> r = new HashMap<>();
for (GHOrganization o : orgs) {
// don't put 'o' into orgs because they are shallow
r.put(o.getLogin(), o.wrapUp(this));
r.put(o.getLogin(), o);
}
return r;
}
@@ -803,11 +682,12 @@ public class GitHub {
*/
public Map<String, GHOrganization> getUserPublicOrganizations(String login) throws IOException {
GHOrganization[] orgs = createRequest().withUrlPath("/users/" + login + "/orgs")
.fetchArray(GHOrganization[].class);
Map<String, GHOrganization> r = new HashMap<String, GHOrganization>();
.toIterable(GHOrganization[].class, item -> item.wrapUp(this))
.toArray();
Map<String, GHOrganization> r = new HashMap<>();
for (GHOrganization o : orgs) {
// don't put 'o' into orgs because they are shallow
r.put(o.getLogin(), o.wrapUp(this));
// don't put 'o' into orgs cache because they are shallow records
r.put(o.getLogin(), o);
}
return r;
}
@@ -823,13 +703,14 @@ public class GitHub {
* the io exception
*/
public Map<String, Set<GHTeam>> getMyTeams() throws IOException {
Map<String, Set<GHTeam>> allMyTeams = new HashMap<String, Set<GHTeam>>();
for (GHTeam team : createRequest().withUrlPath("/user/teams").fetchArray(GHTeam[].class)) {
team.wrapUp(this);
Map<String, Set<GHTeam>> allMyTeams = new HashMap<>();
for (GHTeam team : createRequest().withUrlPath("/user/teams")
.toIterable(GHTeam[].class, item -> item.wrapUp(this))
.toArray()) {
String orgLogin = team.getOrganization().getLogin();
Set<GHTeam> teamsPerOrg = allMyTeams.get(orgLogin);
if (teamsPerOrg == null) {
teamsPerOrg = new HashSet<GHTeam>();
teamsPerOrg = new HashSet<>();
}
teamsPerOrg.add(team);
allMyTeams.put(orgLogin, teamsPerOrg);
@@ -845,7 +726,11 @@ public class GitHub {
* @return the team
* @throws IOException
* the io exception
*
* @deprecated Use {@link GHOrganization#getTeam(int)}
* @see <a href= "https://developer.github.com/v3/teams/#get-team-legacy">deprecation notice</a>
*/
@Deprecated
public GHTeam getTeam(int id) throws IOException {
return createRequest().withUrlPath("/teams/" + id).fetch(GHTeam.class).wrapUp(this);
}
@@ -858,10 +743,9 @@ public class GitHub {
* the io exception
*/
public List<GHEventInfo> getEvents() throws IOException {
GHEventInfo[] events = createRequest().withUrlPath("/events").fetchArray(GHEventInfo[].class);
for (GHEventInfo e : events)
e.wrapUp(this);
return Arrays.asList(events);
return createRequest().withUrlPath("/events")
.toIterable(GHEventInfo[].class, item -> item.wrapUp(this))
.toList();
}
/**
@@ -874,7 +758,7 @@ public class GitHub {
* the io exception
*/
public GHGist getGist(String id) throws IOException {
return createRequest().withUrlPath("/gists/" + id).fetch(GHGist.class).wrapUp(this);
return createRequest().withUrlPath("/gists/" + id).fetch(GHGist.class);
}
/**
@@ -903,7 +787,7 @@ public class GitHub {
* the io exception
*/
public <T extends GHEventPayload> T parseEventPayload(Reader r, Class<T> type) throws IOException {
T t = MAPPER.readValue(r, type);
T t = GitHubClient.getMappingObjectReader(this).forType(type).readValue(r);
t.wrapUp(this);
return t;
}
@@ -1126,16 +1010,7 @@ public class GitHub {
* @return the boolean
*/
public boolean isCredentialValid() {
try {
createRequest().withUrlPath("/user").fetch(GHUser.class);
return true;
} catch (IOException e) {
if (LOGGER.isLoggable(FINE))
LOGGER.log(FINE,
"Exception validating credentials on " + this.apiUrl + " with login '" + this.login + "' " + e,
e);
return false;
}
return client.isCredentialValid();
}
/**
@@ -1151,20 +1026,6 @@ public class GitHub {
return createRequest().withUrlPath("/meta").fetch(GHMeta.class);
}
GHUser intern(GHUser user) throws IOException {
if (user == null)
return user;
// if we already have this user in our map, use it
GHUser u = users.get(user.getLogin());
if (u != null)
return u;
// if not, remember this new user
users.putIfAbsent(user.getLogin(), user);
return user;
}
/**
* Gets project.
*
@@ -1210,18 +1071,6 @@ public class GitHub {
.wrap(this);
}
private static class GHApiInfo {
private String rate_limit_url;
void check(String apiUrl) throws IOException {
if (rate_limit_url == null)
throw new IOException(apiUrl + " doesn't look like GitHub API URL");
// make sure that the URL is legitimate
new URL(rate_limit_url);
}
}
/**
* Tests the connection.
*
@@ -1236,62 +1085,7 @@ public class GitHub {
* the io exception
*/
public void checkApiUrlValidity() throws IOException {
try {
createRequest().withUrlPath("/").fetch(GHApiInfo.class).check(apiUrl);
} catch (IOException e) {
if (isPrivateModeEnabled()) {
throw (IOException) new IOException(
"GitHub Enterprise server (" + apiUrl + ") with private mode enabled").initCause(e);
}
throw e;
}
}
/**
* Checks if a GitHub Enterprise server is configured in private mode.
*
* In private mode response looks like:
*
* <pre>
* $ curl -i https://github.mycompany.com/api/v3/
* HTTP/1.1 401 Unauthorized
* Server: GitHub.com
* Date: Sat, 05 Mar 2016 19:45:01 GMT
* Content-Type: application/json; charset=utf-8
* Content-Length: 130
* Status: 401 Unauthorized
* X-GitHub-Media-Type: github.v3
* X-XSS-Protection: 1; mode=block
* X-Frame-Options: deny
* Content-Security-Policy: default-src 'none'
* Access-Control-Allow-Credentials: true
* Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
* Access-Control-Allow-Origin: *
* X-GitHub-Request-Id: dbc70361-b11d-4131-9a7f-674b8edd0411
* Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
* X-Content-Type-Options: nosniff
* </pre>
*
* @return {@code true} if private mode is enabled. If it tries to use this method with GitHub, returns {@code
* false}.
*/
private boolean isPrivateModeEnabled() {
try {
HttpURLConnection uc = getConnector().connect(getApiURL("/"));
try {
return uc.getResponseCode() == HTTP_UNAUTHORIZED && uc.getHeaderField("X-GitHub-Media-Type") != null;
} finally {
// ensure that the connection opened by getResponseCode gets closed
try {
IOUtils.closeQuietly(uc.getInputStream());
} catch (IOException ignore) {
// ignore
}
IOUtils.closeQuietly(uc.getErrorStream());
}
} catch (IOException e) {
return false;
}
client.checkApiUrlValidity();
}
/**
@@ -1398,49 +1192,55 @@ public class GitHub {
"UTF-8");
}
static URL parseURL(String s) {
try {
return s == null ? null : new URL(s);
} catch (MalformedURLException e) {
throw new IllegalStateException("Invalid URL: " + s);
}
/**
* Do not use this method. This method will be removed and should never have been needed in the first place.
*
* @return an {@link ObjectWriter} instance that can be further configured.
* @deprecated DO NOT USE THIS METHOD. Provided for backward compatibility with projects that did their own jackson
* mapping of this project's data objects, such as Jenkins Blue Ocean.
*/
@Deprecated
@Nonnull
public static ObjectWriter getMappingObjectWriter() {
return GitHubClient.getMappingObjectWriter();
}
static Date parseDate(String timestamp) {
if (timestamp == null)
return null;
for (String f : TIME_FORMATS) {
try {
SimpleDateFormat df = new SimpleDateFormat(f);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.parse(timestamp);
} catch (ParseException e) {
// try next
}
}
throw new IllegalStateException("Unable to parse the timestamp: " + timestamp);
/**
* Do not use this method. This method will be removed and should never have been needed in the first place.
*
* @return an {@link ObjectReader} instance that can be further configured.
* @deprecated DO NOT USE THIS METHOD. Provided for backward compatibility with projects that did their own jackson
* mapping of this project's data objects, such as Jenkins Blue Ocean.
*/
@Deprecated
@Nonnull
public static ObjectReader getMappingObjectReader() {
return GitHubClient.getMappingObjectReader(GitHub.offline());
}
static String printDate(Date dt) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(dt);
@Nonnull
GitHubClient getClient() {
return client;
}
static final ObjectMapper MAPPER = new ObjectMapper();
private static final String[] TIME_FORMATS = { "yyyy/MM/dd HH:mm:ss ZZZZ", "yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:ss.S'Z'" // GitHub App endpoints return a different date format
};
static {
MAPPER.setVisibility(new Std(NONE, NONE, NONE, NONE, ANY));
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
@Nonnull
Requester createRequest() {
return new Requester(client).injectMappingValue(this);
}
static final String GITHUB_URL = "https://api.github.com";
GHUser intern(GHUser user) throws IOException {
if (user == null)
return user;
// if we already have this user in our map, use it
GHUser u = users.get(user.getLogin());
if (u != null)
return u;
// if not, remember this new user
users.putIfAbsent(user.getLogin(), user);
return user;
}
private static final Logger LOGGER = Logger.getLogger(GitHub.class.getName());
}

View File

@@ -14,6 +14,8 @@ import java.util.Locale;
import java.util.Map.Entry;
import java.util.Properties;
import javax.annotation.Nonnull;
/**
* Configures connection details and produces {@link GitHub}.
*
@@ -22,7 +24,7 @@ import java.util.Properties;
public class GitHubBuilder implements Cloneable {
// default scoped so unit tests can read them.
/* private */ String endpoint = GitHub.GITHUB_URL;
/* private */ String endpoint = GitHubClient.GITHUB_URL;
/* private */ String user;
/* private */ String password;
/* private */ String oauthToken;
@@ -32,6 +34,7 @@ public class GitHubBuilder implements Cloneable {
private RateLimitHandler rateLimitHandler = RateLimitHandler.WAIT;
private AbuseLimitHandler abuseLimitHandler = AbuseLimitHandler.WAIT;
private GitHubRateLimitChecker rateLimitChecker = new GitHubRateLimitChecker();
/**
* Instantiates a new Git hub builder.
@@ -90,6 +93,7 @@ public class GitHubBuilder implements Cloneable {
* @deprecated Use {@link #fromEnvironment()} to pick up standard set of environment variables, so that different
* clients of this library will all recognize one consistent set of coordinates.
*/
@Deprecated
public static GitHubBuilder fromEnvironment(String loginVariableName,
String passwordVariableName,
String oauthVariableName) throws IOException {
@@ -119,6 +123,7 @@ public class GitHubBuilder implements Cloneable {
* @deprecated Use {@link #fromEnvironment()} to pick up standard set of environment variables, so that different
* clients of this library will all recognize one consistent set of coordinates.
*/
@Deprecated
public static GitHubBuilder fromEnvironment(String loginVariableName,
String passwordVariableName,
String oauthVariableName,
@@ -214,7 +219,7 @@ public class GitHubBuilder implements Cloneable {
self.withOAuthToken(props.getProperty("oauth"), props.getProperty("login"));
self.withJwtToken(props.getProperty("jwt"));
self.withPassword(props.getProperty("login"), props.getProperty("password"));
self.withEndpoint(props.getProperty("endpoint", GitHub.GITHUB_URL));
self.withEndpoint(props.getProperty("endpoint", GitHubClient.GITHUB_URL));
return self;
}
@@ -311,11 +316,26 @@ public class GitHubBuilder implements Cloneable {
}
/**
* With rate limit handler git hub builder.
* Adds a {@link RateLimitHandler} to this {@link GitHubBuilder}.
* <p>
* GitHub allots a certain number of requests to each user or application per period of time (usually per hour). The
* number of requests remaining is returned in the response header and can also be requested using
* {@link GitHub#getRateLimit()}. This requests per interval is referred to as the "rate limit".
* </p>
* <p>
* When the remaining number of requests reaches zero, the next request will return an error. If this happens,
* {@link RateLimitHandler#onError(IOException, HttpURLConnection)} will be called.
* </p>
* <p>
* NOTE: GitHub treats clients that exceed their rate limit very harshly. If possible, clients should avoid
* exceeding their rate limit. Consider adding a {@link RateLimitChecker} to automatically check the rate limit for
* each request and wait if needed.
* </p>
*
* @param handler
* the handler
* @return the git hub builder
* @see #withRateLimitChecker(RateLimitChecker)
*/
public GitHubBuilder withRateLimitHandler(RateLimitHandler handler) {
this.rateLimitHandler = handler;
@@ -323,7 +343,12 @@ public class GitHubBuilder implements Cloneable {
}
/**
* With abuse limit handler git hub builder.
* Adds a {@link AbuseLimitHandler} to this {@link GitHubBuilder}.
* <p>
* When a client sends too many requests in a short time span, GitHub may return an error and set a header telling
* the client to not make any more request for some period of time. If this happens,
* {@link AbuseLimitHandler#onError(IOException, HttpURLConnection)} will be called.
* </p>
*
* @param handler
* the handler
@@ -334,6 +359,36 @@ public class GitHubBuilder implements Cloneable {
return this;
}
/**
* Adds a {@link RateLimitChecker} to this {@link GitHubBuilder}.
* <p>
* GitHub allots a certain number of requests to each user or application per period of time (usually per hour). The
* number of requests remaining is returned in the response header and can also be requested using
* {@link GitHub#getRateLimit()}. This requests per interval is referred to as the "rate limit".
* </p>
* <p>
* GitHub prefers that clients stop before exceeding their rate limit rather than stopping after they exceed it. The
* {@link RateLimitChecker} is called before each request to check the rate limit and wait if the checker criteria
* are met.
* </p>
* <p>
* Checking your rate limit using {@link GitHub#getRateLimit()} does not effect your rate limit, but each
* {@link GitHub} instance will attempt to cache and reuse the last seen rate limit rather than making a new
* request.
* </p>
*
* @param coreRateLimitChecker
* the {@link RateLimitChecker} for core GitHub API requests
* @return the git hub builder
*/
public GitHubBuilder withRateLimitChecker(@Nonnull RateLimitChecker coreRateLimitChecker) {
this.rateLimitChecker = new GitHubRateLimitChecker(coreRateLimitChecker,
RateLimitChecker.NONE,
RateLimitChecker.NONE,
RateLimitChecker.NONE);
return this;
}
/**
* Configures {@linkplain #withConnector(HttpConnector) connector} that uses HTTP library in JRE but use a specific
* proxy, instead of the system default one.
@@ -343,15 +398,11 @@ public class GitHubBuilder implements Cloneable {
* @return the git hub builder
*/
public GitHubBuilder withProxy(final Proxy p) {
return withConnector(new ImpatientHttpConnector(new HttpConnector() {
public HttpURLConnection connect(URL url) throws IOException {
return (HttpURLConnection) url.openConnection(p);
}
}));
return withConnector(new ImpatientHttpConnector(url -> (HttpURLConnection) url.openConnection(p)));
}
/**
* Build git hub.
* Builds a {@link GitHub} instance.
*
* @return the git hub
* @throws IOException
@@ -365,7 +416,8 @@ public class GitHubBuilder implements Cloneable {
password,
connector,
rateLimitHandler,
abuseLimitHandler);
abuseLimitHandler,
rateLimitChecker);
}
@Override

View File

@@ -0,0 +1,751 @@
package org.kohsuke.github;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import org.apache.commons.lang3.StringUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
import java.util.function.Consumer;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.net.ssl.SSLHandshakeException;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static java.util.logging.Level.*;
/**
* A GitHub API Client
* <p>
* A GitHubClient can be used to send requests and retrieve their responses. GitHubClient is thread-safe and can be used
* to send multiple requests. GitHubClient also track some GitHub API information such as {@link #rateLimit()}.
* </p>
*/
abstract class GitHubClient {
static final int CONNECTION_ERROR_RETRIES = 2;
/**
* If timeout issues let's retry after milliseconds.
*/
static final int retryTimeoutMillis = 100;
/* private */ final String login;
/**
* Value of the authorization header to be sent with the request.
*/
/* private */ final String encodedAuthorization;
// Cache of myself object.
private final String apiUrl;
protected final RateLimitHandler rateLimitHandler;
protected final AbuseLimitHandler abuseLimitHandler;
private final GitHubRateLimitChecker rateLimitChecker;
private HttpConnector connector;
private final Object headerRateLimitLock = new Object();
private GHRateLimit headerRateLimit = null;
private volatile GHRateLimit rateLimit = null;
private static final Logger LOGGER = Logger.getLogger(GitHubClient.class.getName());
private static final ObjectMapper MAPPER = new ObjectMapper();
static final String GITHUB_URL = "https://api.github.com";
private static final String[] TIME_FORMATS = { "yyyy/MM/dd HH:mm:ss ZZZZ", "yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:ss.S'Z'" // GitHub App endpoints return a different date format
};
static {
MAPPER.setVisibility(new VisibilityChecker.Std(NONE, NONE, NONE, NONE, ANY));
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
}
GitHubClient(String apiUrl,
String login,
String oauthAccessToken,
String jwtToken,
String password,
HttpConnector connector,
RateLimitHandler rateLimitHandler,
AbuseLimitHandler abuseLimitHandler,
GitHubRateLimitChecker rateLimitChecker,
Consumer<GHMyself> myselfConsumer) throws IOException {
if (apiUrl.endsWith("/")) {
apiUrl = apiUrl.substring(0, apiUrl.length() - 1); // normalize
}
if (null == connector) {
connector = HttpConnector.DEFAULT;
}
this.apiUrl = apiUrl;
this.connector = connector;
if (oauthAccessToken != null) {
encodedAuthorization = "token " + oauthAccessToken;
} else {
if (jwtToken != null) {
encodedAuthorization = "Bearer " + jwtToken;
} else if (password != null) {
String authorization = (login + ':' + password);
String charsetName = StandardCharsets.UTF_8.name();
encodedAuthorization = "Basic "
+ Base64.getEncoder().encodeToString(authorization.getBytes(charsetName));
} else {// anonymous access
encodedAuthorization = null;
}
}
this.rateLimitHandler = rateLimitHandler;
this.abuseLimitHandler = abuseLimitHandler;
this.rateLimitChecker = rateLimitChecker;
if (login == null && encodedAuthorization != null && jwtToken == null) {
GHMyself myself = fetch(GHMyself.class, "/user");
login = myself.getLogin();
if (myselfConsumer != null) {
myselfConsumer.accept(myself);
}
}
this.login = login;
}
private <T> T fetch(Class<T> type, String urlPath) throws IOException {
return this
.sendRequest(GitHubRequest.newBuilder().withApiUrl(getApiUrl()).withUrlPath(urlPath).build(),
(responseInfo) -> GitHubResponse.parseBody(responseInfo, type))
.body();
}
/**
* Ensures that the credential for this client is valid.
*
* @return the boolean
*/
public boolean isCredentialValid() {
try {
// If 404, ratelimit returns a default value.
// This works as credential test because invalid credentials returns 401, not 404
getRateLimit();
return true;
} catch (IOException e) {
if (LOGGER.isLoggable(FINE))
LOGGER.log(FINE,
"Exception validating credentials on " + getApiUrl() + " with login '" + login + "' " + e,
e);
return false;
}
}
/**
* Is this an always offline "connection".
*
* @return {@code true} if this is an always offline "connection".
*/
public boolean isOffline() {
return getConnector() == HttpConnector.OFFLINE;
}
/**
* Gets connector.
*
* @return the connector
*/
public HttpConnector getConnector() {
return connector;
}
/**
* Sets the custom connector used to make requests to GitHub.
*
* @param connector
* the connector
* @deprecated HttpConnector should not be changed.
*/
@Deprecated
public void setConnector(HttpConnector connector) {
LOGGER.warning("Connector should not be changed. Please file an issue describing your use case.");
this.connector = connector;
}
/**
* Is this an anonymous connection
*
* @return {@code true} if operations that require authentication will fail.
*/
public boolean isAnonymous() {
return login == null && encodedAuthorization == null;
}
/**
* Gets the current rate limit from the server.
*
* @return the rate limit
* @throws IOException
* the io exception
*/
public GHRateLimit getRateLimit() throws IOException {
GHRateLimit rateLimit;
try {
rateLimit = fetch(JsonRateLimit.class, "/rate_limit").resources;
} catch (FileNotFoundException e) {
// GitHub Enterprise doesn't have the rate limit
// return a default rate limit that
rateLimit = GHRateLimit.Unknown();
}
return this.rateLimit = rateLimit;
}
/**
* Returns the most recently observed rate limit data or {@code null} if either there is no rate limit (for example
* GitHub Enterprise) or if no requests have been made.
*
* @return the most recently observed rate limit data or {@code null}.
*/
@CheckForNull
public GHRateLimit lastRateLimit() {
synchronized (headerRateLimitLock) {
return headerRateLimit;
}
}
/**
* Gets the current rate limit while trying not to actually make any remote requests unless absolutely necessary.
*
* @return the current rate limit data.
* @throws IOException
* if we couldn't get the current rate limit data.
*/
@Nonnull
public GHRateLimit rateLimit() throws IOException {
synchronized (headerRateLimitLock) {
if (headerRateLimit != null && !headerRateLimit.isExpired()) {
return headerRateLimit;
}
}
GHRateLimit rateLimit = this.rateLimit;
if (rateLimit == null || rateLimit.isExpired()) {
rateLimit = getRateLimit();
}
return rateLimit;
}
/**
* Tests the connection.
*
* <p>
* Verify that the API URL and credentials are valid to access this GitHub.
*
* <p>
* This method returns normally if the endpoint is reachable and verified to be GitHub API URL. Otherwise this
* method throws {@link IOException} to indicate the problem.
*
* @throws IOException
* the io exception
*/
public void checkApiUrlValidity() throws IOException {
try {
fetch(GHApiInfo.class, "/").check(getApiUrl());
} catch (IOException e) {
if (isPrivateModeEnabled()) {
throw (IOException) new IOException(
"GitHub Enterprise server (" + getApiUrl() + ") with private mode enabled").initCause(e);
}
throw e;
}
}
public String getApiUrl() {
return apiUrl;
}
/**
* Builds a {@link GitHubRequest}, sends the {@link GitHubRequest} to the server, and uses the
* {@link GitHubResponse.BodyHandler} to parse the response info and response body data into an instance of
* {@link T}.
*
* @param builder
* used to build the request that will be sent to the server.
* @param handler
* parse the response info and body data into a instance of {@link T}. If null, no parsing occurs and
* {@link GitHubResponse#body()} will return null.
* @param <T>
* the type of the parse body data.
* @return a {@link GitHubResponse} containing the parsed body data as a {@link T}. Parsed instance may be null.
* @throws IOException
* if an I/O Exception occurs
*/
@Nonnull
public <T> GitHubResponse<T> sendRequest(@Nonnull GitHubRequest.Builder<?> builder,
@CheckForNull GitHubResponse.BodyHandler<T> handler) throws IOException {
return sendRequest(builder.build(), handler);
}
/**
* Sends the {@link GitHubRequest} to the server, and uses the {@link GitHubResponse.BodyHandler} to parse the
* response info and response body data into an instance of {@link T}.
*
* @param request
* the request that will be sent to the server.
* @param handler
* parse the response info and body data into a instance of {@link T}. If null, no parsing occurs and
* {@link GitHubResponse#body()} will return null.
* @param <T>
* the type of the parse body data.
* @return a {@link GitHubResponse} containing the parsed body data as a {@link T}. Parsed instance may be null.
* @throws IOException
* if an I/O Exception occurs
*/
@Nonnull
public <T> GitHubResponse<T> sendRequest(GitHubRequest request, @CheckForNull GitHubResponse.BodyHandler<T> handler)
throws IOException {
int retries = CONNECTION_ERROR_RETRIES;
do {
// if we fail to create a connection we do not retry and we do not wrap
GitHubResponse.ResponseInfo responseInfo = null;
try {
if (LOGGER.isLoggable(FINE)) {
LOGGER.log(FINE,
"GitHub API request [" + (login == null ? "anonymous" : login) + "]: " + request.method()
+ " " + request.url().toString());
}
rateLimitChecker.checkRateLimit(this, request);
responseInfo = getResponseInfo(request);
noteRateLimit(responseInfo);
detectOTPRequired(responseInfo);
if (isInvalidCached404Response(responseInfo)) {
// Setting "Cache-Control" to "no-cache" stops the cache from supplying
// "If-Modified-Since" or "If-None-Match" values.
// This makes GitHub give us current data (not incorrectly cached data)
request = request.toBuilder().withHeader("Cache-Control", "no-cache").build();
continue;
}
if (!(isRateLimitResponse(responseInfo) || isAbuseLimitResponse(responseInfo))) {
return createResponse(responseInfo, handler);
}
} catch (IOException e) {
// For transient errors, retry
if (retryConnectionError(e, request.url(), retries)) {
continue;
}
throw interpretApiError(e, request, responseInfo);
}
handleLimitingErrors(responseInfo);
} while (--retries >= 0);
throw new GHIOException("Ran out of retries for URL: " + request.url().toString());
}
@Nonnull
protected abstract GitHubResponse.ResponseInfo getResponseInfo(GitHubRequest request) throws IOException;
protected abstract void handleLimitingErrors(@Nonnull GitHubResponse.ResponseInfo responseInfo) throws IOException;
@Nonnull
private static <T> GitHubResponse<T> createResponse(@Nonnull GitHubResponse.ResponseInfo responseInfo,
@CheckForNull GitHubResponse.BodyHandler<T> handler) throws IOException {
T body = null;
if (responseInfo.statusCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
// special case handling for 304 unmodified, as the content will be ""
} else if (responseInfo.statusCode() == HttpURLConnection.HTTP_ACCEPTED) {
// Response code 202 means data is being generated.
// This happens in specific cases:
// statistics - See https://developer.github.com/v3/repos/statistics/#a-word-about-caching
// fork creation - See https://developer.github.com/v3/repos/forks/#create-a-fork
if (responseInfo.url().toString().endsWith("/forks")) {
LOGGER.log(INFO, "The fork is being created. Please try again in 5 seconds.");
} else if (responseInfo.url().toString().endsWith("/statistics")) {
LOGGER.log(INFO, "The statistics are being generated. Please try again in 5 seconds.");
} else {
LOGGER.log(INFO,
"Received 202 from " + responseInfo.url().toString() + " . Please try again in 5 seconds.");
}
// Maybe throw an exception instead?
} else if (handler != null) {
body = handler.apply(responseInfo);
}
return new GitHubResponse<>(responseInfo, body);
}
/**
* Handle API error by either throwing it or by returning normally to retry.
*/
private static IOException interpretApiError(IOException e,
@Nonnull GitHubRequest request,
@CheckForNull GitHubResponse.ResponseInfo responseInfo) throws IOException {
// If we're already throwing a GHIOException, pass through
if (e instanceof GHIOException) {
return e;
}
int statusCode = -1;
String message = null;
Map<String, List<String>> headers = new HashMap<>();
String errorMessage = null;
if (responseInfo != null) {
statusCode = responseInfo.statusCode();
message = responseInfo.headerField("Status");
headers = responseInfo.headers();
errorMessage = responseInfo.errorMessage();
}
if (errorMessage != null) {
if (e instanceof FileNotFoundException) {
// pass through 404 Not Found to allow the caller to handle it intelligently
e = new GHFileNotFoundException(e.getMessage() + " " + errorMessage, e)
.withResponseHeaderFields(headers);
} else if (statusCode >= 0) {
e = new HttpException(errorMessage, statusCode, message, request.url().toString(), e);
} else {
e = new GHIOException(errorMessage).withResponseHeaderFields(headers);
}
} else if (!(e instanceof FileNotFoundException)) {
e = new HttpException(statusCode, message, request.url().toString(), e);
}
return e;
}
protected static boolean isRateLimitResponse(@Nonnull GitHubResponse.ResponseInfo responseInfo) {
return responseInfo.statusCode() == HttpURLConnection.HTTP_FORBIDDEN
&& "0".equals(responseInfo.headerField("X-RateLimit-Remaining"));
}
protected static boolean isAbuseLimitResponse(@Nonnull GitHubResponse.ResponseInfo responseInfo) {
return responseInfo.statusCode() == HttpURLConnection.HTTP_FORBIDDEN
&& responseInfo.headerField("Retry-After") != null;
}
private static boolean retryConnectionError(IOException e, URL url, int retries) throws IOException {
// There are a range of connection errors where we want to wait a moment and just automatically retry
boolean connectionError = e instanceof SocketException || e instanceof SocketTimeoutException
|| e instanceof SSLHandshakeException;
if (connectionError && retries > 0) {
LOGGER.log(INFO,
e.getMessage() + " while connecting to " + url + ". Sleeping " + GitHubClient.retryTimeoutMillis
+ " milliseconds before retrying... ; will try " + retries + " more time(s)");
try {
Thread.sleep(GitHubClient.retryTimeoutMillis);
} catch (InterruptedException ie) {
throw (IOException) new InterruptedIOException().initCause(e);
}
return true;
}
return false;
}
private static boolean isInvalidCached404Response(GitHubResponse.ResponseInfo responseInfo) {
// WORKAROUND FOR ISSUE #669:
// When the Requester detects a 404 response with an ETag (only happpens when the server's 304
// is bogus and would cause cache corruption), try the query again with new request header
// that forces the server to not return 304 and return new data instead.
//
// This solution is transparent to users of this library and automatically handles a
// situation that was cause insidious and hard to debug bad responses in caching
// scenarios. If GitHub ever fixes their issue and/or begins providing accurate ETags to
// their 404 responses, this will result in at worst two requests being made for each 404
// responses. However, only the second request will count against rate limit.
if (responseInfo.statusCode() == 404 && Objects.equals(responseInfo.request().method(), "GET")
&& responseInfo.headerField("ETag") != null
&& !Objects.equals(responseInfo.request().headers().get("Cache-Control"), "no-cache")) {
LOGGER.log(FINE,
"Encountered GitHub invalid cached 404 from " + responseInfo.url()
+ ". Retrying with \"Cache-Control\"=\"no-cache\"...");
return true;
}
return false;
}
private void noteRateLimit(@Nonnull GitHubResponse.ResponseInfo responseInfo) {
if (responseInfo.request().urlPath().startsWith("/search")) {
// the search API uses a different rate limit
return;
}
String limitString = responseInfo.headerField("X-RateLimit-Limit");
if (StringUtils.isBlank(limitString)) {
// if we are missing a header, return fast
return;
}
String remainingString = responseInfo.headerField("X-RateLimit-Remaining");
if (StringUtils.isBlank(remainingString)) {
// if we are missing a header, return fast
return;
}
String resetString = responseInfo.headerField("X-RateLimit-Reset");
if (StringUtils.isBlank(resetString)) {
// if we are missing a header, return fast
return;
}
int limit, remaining;
long reset;
try {
limit = Integer.parseInt(limitString);
} catch (NumberFormatException e) {
if (LOGGER.isLoggable(FINEST)) {
LOGGER.log(FINEST, "Malformed X-RateLimit-Limit header value " + limitString, e);
}
return;
}
try {
remaining = Integer.parseInt(remainingString);
} catch (NumberFormatException e) {
if (LOGGER.isLoggable(FINEST)) {
LOGGER.log(FINEST, "Malformed X-RateLimit-Remaining header value " + remainingString, e);
}
return;
}
try {
reset = Long.parseLong(resetString);
} catch (NumberFormatException e) {
if (LOGGER.isLoggable(FINEST)) {
LOGGER.log(FINEST, "Malformed X-RateLimit-Reset header value " + resetString, e);
}
return;
}
GHRateLimit.Record observed = new GHRateLimit.Record(limit, remaining, reset, responseInfo);
updateCoreRateLimit(observed);
}
private static void detectOTPRequired(@Nonnull GitHubResponse.ResponseInfo responseInfo) throws GHIOException {
// 401 Unauthorized == bad creds or OTP request
if (responseInfo.statusCode() == HTTP_UNAUTHORIZED) {
// In the case of a user with 2fa enabled, a header with X-GitHub-OTP
// will be returned indicating the user needs to respond with an otp
if (responseInfo.headerField("X-GitHub-OTP") != null) {
throw new GHOTPRequiredException().withResponseHeaderFields(responseInfo.headers());
}
}
}
void requireCredential() {
if (isAnonymous())
throw new IllegalStateException(
"This operation requires a credential but none is given to the GitHub constructor");
}
/**
* Update the Rate Limit with the latest info from response header. Due to multi-threading requests might complete
* out of order, we want to pick the one with the most recent info from the server. Calls
* {@link #shouldReplace(GHRateLimit.Record, GHRateLimit.Record)}
*
* @param observed
* {@link GHRateLimit.Record} constructed from the response header information
*/
private void updateCoreRateLimit(@Nonnull GHRateLimit.Record observed) {
synchronized (headerRateLimitLock) {
if (headerRateLimit == null || shouldReplace(observed, headerRateLimit.getCore())) {
headerRateLimit = GHRateLimit.fromHeaderRecord(observed);
LOGGER.log(FINE, "Rate limit now: {0}", headerRateLimit);
}
}
}
private static class GHApiInfo {
private String rate_limit_url;
void check(String apiUrl) throws IOException {
if (rate_limit_url == null)
throw new IOException(apiUrl + " doesn't look like GitHub API URL");
// make sure that the URL is legitimate
new URL(rate_limit_url);
}
}
/**
* Checks if a GitHub Enterprise server is configured in private mode.
*
* In private mode response looks like:
*
* <pre>
* $ curl -i https://github.mycompany.com/api/v3/
* HTTP/1.1 401 Unauthorized
* Server: GitHub.com
* Date: Sat, 05 Mar 2016 19:45:01 GMT
* Content-Type: application/json; charset=utf-8
* Content-Length: 130
* Status: 401 Unauthorized
* X-GitHub-Media-Type: github.v3
* X-XSS-Protection: 1; mode=block
* X-Frame-Options: deny
* Content-Security-Policy: default-src 'none'
* Access-Control-Allow-Credentials: true
* Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
* Access-Control-Allow-Origin: *
* X-GitHub-Request-Id: dbc70361-b11d-4131-9a7f-674b8edd0411
* Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
* X-Content-Type-Options: nosniff
* </pre>
*
* @return {@code true} if private mode is enabled. If it tries to use this method with GitHub, returns {@code
* false}.
*/
private boolean isPrivateModeEnabled() {
try {
GitHubResponse<?> response = sendRequest(GitHubRequest.newBuilder().withApiUrl(getApiUrl()), null);
return response.statusCode() == HTTP_UNAUTHORIZED && response.headerField("X-GitHub-Media-Type") != null;
} catch (IOException e) {
return false;
}
}
/**
* Determine if one {@link GHRateLimit.Record} should replace another. Header date is only accurate to the second,
* so we look at the information in the record itself.
*
* {@link GHRateLimit.UnknownLimitRecord}s are always replaced by regular {@link GHRateLimit.Record}s. Regular
* {@link GHRateLimit.Record}s are never replaced by {@link GHRateLimit.UnknownLimitRecord}s. Candidates with
* resetEpochSeconds later than current record are more recent. Candidates with the same reset and a lower remaining
* count are more recent. Candidates with an earlier reset are older.
*
* @param candidate
* {@link GHRateLimit.Record} constructed from the response header information
* @param current
* the current {@link GHRateLimit.Record} record
*/
static boolean shouldReplace(@Nonnull GHRateLimit.Record candidate, @Nonnull GHRateLimit.Record current) {
if (candidate instanceof GHRateLimit.UnknownLimitRecord
&& !(current instanceof GHRateLimit.UnknownLimitRecord)) {
// Unknown candidate never replaces a regular record
return false;
} else if (current instanceof GHRateLimit.UnknownLimitRecord
&& !(candidate instanceof GHRateLimit.UnknownLimitRecord)) {
// Any real record should replace an unknown Record.
return true;
} else {
// records of the same type compare to each other as normal.
return current.getResetEpochSeconds() < candidate.getResetEpochSeconds()
|| (current.getResetEpochSeconds() == candidate.getResetEpochSeconds()
&& current.getRemaining() > candidate.getRemaining());
}
}
static URL parseURL(String s) {
try {
return s == null ? null : new URL(s);
} catch (MalformedURLException e) {
throw new IllegalStateException("Invalid URL: " + s);
}
}
static Date parseDate(String timestamp) {
if (timestamp == null)
return null;
for (String f : TIME_FORMATS) {
try {
SimpleDateFormat df = new SimpleDateFormat(f);
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.parse(timestamp);
} catch (ParseException e) {
// try next
}
}
throw new IllegalStateException("Unable to parse the timestamp: " + timestamp);
}
static String printDate(Date dt) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(dt);
}
/**
* Gets an {@link ObjectWriter}.
*
* @return an {@link ObjectWriter} instance that can be further configured.
*/
@Nonnull
static ObjectWriter getMappingObjectWriter() {
return MAPPER.writer();
}
/**
* Helper for {@link #getMappingObjectReader(GitHubResponse.ResponseInfo)}
*
* @param root
* the root GitHub object for this reader
*
* @return an {@link ObjectReader} instance that can be further configured.
*/
@Nonnull
static ObjectReader getMappingObjectReader(@Nonnull GitHub root) {
ObjectReader reader = getMappingObjectReader((GitHubResponse.ResponseInfo) null);
((InjectableValues.Std) reader.getInjectableValues()).addValue(GitHub.class, root);
return reader;
}
/**
* Gets an {@link ObjectReader}.
*
* Members of {@link InjectableValues} must be present even if {@code null}, otherwise classes expecting those
* values will fail to read. This differs from regular JSONProperties which provide defaults instead of failing.
*
* Having one spot to create readers and having it take all injectable values is not a great long term solution but
* it is sufficient for this first cut.
*
* @param responseInfo
* the {@link GitHubResponse.ResponseInfo} to inject for this reader.
*
* @return an {@link ObjectReader} instance that can be further configured.
*/
@Nonnull
static ObjectReader getMappingObjectReader(@CheckForNull GitHubResponse.ResponseInfo responseInfo) {
Map<String, Object> injected = new HashMap<>();
// Required or many things break
injected.put(GitHubResponse.ResponseInfo.class.getName(), null);
injected.put(GitHub.class.getName(), null);
if (responseInfo != null) {
injected.put(GitHubResponse.ResponseInfo.class.getName(), responseInfo);
injected.putAll(responseInfo.request().injectedMappingValues());
}
return MAPPER.reader(new InjectableValues.Std(injected));
}
}

View File

@@ -0,0 +1,240 @@
package org.kohsuke.github;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import javax.annotation.Nonnull;
import static java.util.logging.Level.*;
import static org.apache.commons.lang3.StringUtils.defaultString;
/**
* A GitHub API Client for HttpUrlConnection
* <p>
* A GitHubClient can be used to send requests and retrieve their responses. GitHubClient is thread-safe and can be used
* to send multiple requests. GitHubClient also track some GitHub API information such as {@link #rateLimit()}.
* </p>
* <p>
* GitHubHttpUrlConnectionClient gets a new {@link HttpURLConnection} for each call to send.
* </p>
*/
class GitHubHttpUrlConnectionClient extends GitHubClient {
GitHubHttpUrlConnectionClient(String apiUrl,
String login,
String oauthAccessToken,
String jwtToken,
String password,
HttpConnector connector,
RateLimitHandler rateLimitHandler,
AbuseLimitHandler abuseLimitHandler,
GitHubRateLimitChecker rateLimitChecker,
Consumer<GHMyself> myselfConsumer) throws IOException {
super(apiUrl,
login,
oauthAccessToken,
jwtToken,
password,
connector,
rateLimitHandler,
abuseLimitHandler,
rateLimitChecker,
myselfConsumer);
}
@Nonnull
protected GitHubResponse.ResponseInfo getResponseInfo(GitHubRequest request) throws IOException {
HttpURLConnection connection;
try {
connection = HttpURLConnectionResponseInfo.setupConnection(this, request);
} catch (IOException e) {
// An error in here should be wrapped to bypass http exception wrapping.
throw new GHIOException(e.getMessage(), e);
}
// HttpUrlConnection is nuts. This call opens the connection and gets a response.
// Putting this on it's own line for ease of debugging if needed.
int statusCode = connection.getResponseCode();
Map<String, List<String>> headers = connection.getHeaderFields();
return new HttpURLConnectionResponseInfo(request, statusCode, headers, connection);
}
protected void handleLimitingErrors(@Nonnull GitHubResponse.ResponseInfo responseInfo) throws IOException {
if (isRateLimitResponse(responseInfo)) {
GHIOException e = new HttpException("Rate limit violation",
responseInfo.statusCode(),
responseInfo.headerField("Status"),
responseInfo.url().toString()).withResponseHeaderFields(responseInfo.headers());
rateLimitHandler.onError(e, ((HttpURLConnectionResponseInfo) responseInfo).connection);
} else if (isAbuseLimitResponse(responseInfo)) {
GHIOException e = new HttpException("Abuse limit violation",
responseInfo.statusCode(),
responseInfo.headerField("Status"),
responseInfo.url().toString()).withResponseHeaderFields(responseInfo.headers());
abuseLimitHandler.onError(e, ((HttpURLConnectionResponseInfo) responseInfo).connection);
}
}
/**
* Initial response information supplied to a {@link GitHubResponse.BodyHandler} when a response is initially
* received and before the body is processed.
*
* Implementation specific to {@link HttpURLConnection}.
*/
static class HttpURLConnectionResponseInfo extends GitHubResponse.ResponseInfo {
@Nonnull
private final HttpURLConnection connection;
HttpURLConnectionResponseInfo(@Nonnull GitHubRequest request,
int statusCode,
@Nonnull Map<String, List<String>> headers,
@Nonnull HttpURLConnection connection) {
super(request, statusCode, headers);
this.connection = connection;
}
@Nonnull
static HttpURLConnection setupConnection(@Nonnull GitHubClient client, @Nonnull GitHubRequest request)
throws IOException {
HttpURLConnection connection = client.getConnector().connect(request.url());
// if the authentication is needed but no credential is given, try it anyway (so that some calls
// that do work with anonymous access in the reduced form should still work.)
if (client.encodedAuthorization != null)
connection.setRequestProperty("Authorization", client.encodedAuthorization);
setRequestMethod(request.method(), connection);
buildRequest(request, connection);
return connection;
}
/**
* Set up the request parameters or POST payload.
*/
private static void buildRequest(GitHubRequest request, HttpURLConnection connection) throws IOException {
for (Map.Entry<String, String> e : request.headers().entrySet()) {
String v = e.getValue();
if (v != null)
connection.setRequestProperty(e.getKey(), v);
}
connection.setRequestProperty("Accept-Encoding", "gzip");
if (request.inBody()) {
connection.setDoOutput(true);
try (InputStream body = request.body()) {
if (body != null) {
connection.setRequestProperty("Content-type",
defaultString(request.contentType(), "application/x-www-form-urlencoded"));
byte[] bytes = new byte[32768];
int read;
while ((read = body.read(bytes)) != -1) {
connection.getOutputStream().write(bytes, 0, read);
}
} else {
connection.setRequestProperty("Content-type",
defaultString(request.contentType(), "application/json"));
Map<String, Object> json = new HashMap<>();
for (GitHubRequest.Entry e : request.args()) {
json.put(e.key, e.value);
}
getMappingObjectWriter().writeValue(connection.getOutputStream(), json);
}
}
}
}
private static void setRequestMethod(String method, HttpURLConnection connection) throws IOException {
try {
connection.setRequestMethod(method);
} catch (ProtocolException e) {
// JDK only allows one of the fixed set of verbs. Try to override that
try {
Field $method = HttpURLConnection.class.getDeclaredField("method");
$method.setAccessible(true);
$method.set(connection, method);
} catch (Exception x) {
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
}
// sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
try {
Field $delegate = connection.getClass().getDeclaredField("delegate");
$delegate.setAccessible(true);
Object delegate = $delegate.get(connection);
if (delegate instanceof HttpURLConnection) {
HttpURLConnection nested = (HttpURLConnection) delegate;
setRequestMethod(method, nested);
}
} catch (NoSuchFieldException x) {
// no problem
} catch (IllegalAccessException x) {
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
}
}
if (!connection.getRequestMethod().equals(method))
throw new IllegalStateException("Failed to set the request method to " + method);
}
/**
* {@inheritDoc}
*/
InputStream bodyStream() throws IOException {
return wrapStream(connection.getInputStream());
}
/**
* {@inheritDoc}
*/
String errorMessage() {
String result = null;
InputStream stream = null;
try {
stream = connection.getErrorStream();
if (stream != null) {
result = IOUtils.toString(wrapStream(stream), StandardCharsets.UTF_8);
}
} catch (Exception e) {
LOGGER.log(FINER, "Ignored exception get error message", e);
} finally {
IOUtils.closeQuietly(stream);
}
return result;
}
/**
* Handles the "Content-Encoding" header.
*
* @param stream
* the stream to possibly wrap
*
*/
private InputStream wrapStream(InputStream stream) throws IOException {
String encoding = headerField("Content-Encoding");
if (encoding == null || stream == null)
return stream;
if (encoding.equals("gzip"))
return new GZIPInputStream(stream);
throw new UnsupportedOperationException("Unexpected Content-Encoding: " + encoding);
}
private static final Logger LOGGER = Logger.getLogger(GitHubClient.class.getName());
}
}

View File

@@ -0,0 +1,79 @@
package org.kohsuke.github;
import java.io.IOException;
import java.util.function.Consumer;
import javax.annotation.Nonnull;
/**
* {@link PagedIterable} implementation that take a {@link Consumer} that initializes all the items on each page as they
* are retrieved.
*
* {@link GitHubPageContentsIterable} is immutable and thread-safe, but the iterator returned from {@link #iterator()}
* is not. Any one instance of iterator should only be called from a single thread.
*
* @param <T>
* the type of items on each page
*/
class GitHubPageContentsIterable<T> extends PagedIterable<T> {
private final GitHubClient client;
private final GitHubRequest request;
private final Class<T[]> receiverType;
private final Consumer<T> itemInitializer;
GitHubPageContentsIterable(GitHubClient client,
GitHubRequest request,
Class<T[]> receiverType,
Consumer<T> itemInitializer) {
this.client = client;
this.request = request;
this.receiverType = receiverType;
this.itemInitializer = itemInitializer;
}
/**
* {@inheritDoc}
*/
@Override
@Nonnull
public PagedIterator<T> _iterator(int pageSize) {
final GitHubPageIterator<T[]> iterator = GitHubPageIterator.create(client, receiverType, request, pageSize);
return new GitHubPageContentsIterator(iterator, itemInitializer);
}
/**
* Eagerly walk {@link Iterable} and return the result in a {@link GitHubResponse} containing an array of {@link T}
* items.
*
* @return the last response with an array containing all the results from all pages.
* @throws IOException
* if an I/O exception occurs.
*/
@Nonnull
GitHubResponse<T[]> toResponse() throws IOException {
GitHubPageContentsIterator iterator = (GitHubPageContentsIterator) iterator();
T[] items = toArray(iterator);
GitHubResponse<T[]> lastResponse = iterator.lastResponse();
return new GitHubResponse<>(lastResponse, items);
}
/**
* This class is not thread-safe. Any one instance should only be called from a single thread.
*/
private class GitHubPageContentsIterator extends PagedIterator<T> {
public GitHubPageContentsIterator(GitHubPageIterator<T[]> iterator, Consumer<T> itemInitializer) {
super(iterator, itemInitializer);
}
/**
* Gets the {@link GitHubResponse} for the last page received.
*
* @return the {@link GitHubResponse} for the last page received.
*/
private GitHubResponse<T[]> lastResponse() {
return ((GitHubPageIterator<T[]>) base).finalResponse();
}
}
}

View File

@@ -0,0 +1,179 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.annotation.Nonnull;
/**
* May be used for any item that has pagination information. Iterates over paginated {@link T} objects (not the items
* inside the page). Also exposes {@link #finalResponse()} to allow getting a full {@link GitHubResponse<T>} after
* iterating completes.
*
* Works for array responses, also works for search results which are single instances with an array of items inside.
*
* This class is not thread-safe. Any one instance should only be called from a single thread.
*
* @param <T>
* type of each page (not the items in the page).
*/
class GitHubPageIterator<T> implements Iterator<T> {
private final GitHubClient client;
private final Class<T> type;
/**
* The page that will be returned when {@link #next()} is called.
*
* <p>
* Will be {@code null} after {@link #next()} is called.
* </p>
* <p>
* Will not be {@code null} after {@link #fetch()} is called if a new page was fetched.
* </p>
*/
private T next;
/**
* The request that will be sent when to get a new response page if {@link #next} is {@code null}. Will be
* {@code null} when there are no more pages to fetch.
*/
private GitHubRequest nextRequest;
/**
* When done iterating over pages, it is on rare occasions useful to be able to get information from the final
* response that was retrieved.
*/
private GitHubResponse<T> finalResponse = null;
private GitHubPageIterator(GitHubClient client, Class<T> type, GitHubRequest request) {
if (!"GET".equals(request.method())) {
throw new IllegalStateException("Request method \"GET\" is required for page iterator.");
}
this.client = client;
this.type = type;
this.nextRequest = request;
}
/**
* Loads paginated resources.
*
* @param client
* the {@link GitHubClient} from which to request responses
* @param type
* type of each page (not the items in the page).
* @param <T>
* type of each page (not the items in the page).
* @return iterator
*/
static <T> GitHubPageIterator<T> create(GitHubClient client, Class<T> type, GitHubRequest request, int pageSize) {
try {
if (pageSize > 0) {
GitHubRequest.Builder<?> builder = request.toBuilder().with("per_page", pageSize);
request = builder.build();
}
return new GitHubPageIterator<>(client, type, request);
} catch (MalformedURLException e) {
throw new GHException("Unable to build GitHub API URL", e);
}
}
/**
* {@inheritDoc}
*/
public boolean hasNext() {
fetch();
return next != null;
}
/**
* Gets the next page.
*
* @return the next page.
*/
@Nonnull
public T next() {
fetch();
T result = next;
if (result == null)
throw new NoSuchElementException();
// If this is the last page, keep the response
next = null;
return result;
}
/**
* On rare occasions the final response from iterating is needed.
*
* @return the final response of the iterator.
*/
public GitHubResponse<T> finalResponse() {
if (hasNext()) {
throw new GHException("Final response is not available until after iterator is done.");
}
return finalResponse;
}
/**
* Fetch is called at the start of {@link #hasNext()} or {@link #next()} to fetch another page of data if it is
* needed.
* <p>
* If {@link #next} is not {@code null}, no further action is needed. If {@link #next} is {@code null} and
* {@link #nextRequest} is {@code null}, there are no more pages to fetch.
* </p>
* <p>
* Otherwise, a new response page is fetched using {@link #nextRequest}. The response is then checked to see if
* there is a page after it and {@link #nextRequest} is updated to point to it. If there are no pages available
* after the current response, {@link #nextRequest} is set to {@code null}.
* </p>
*/
private void fetch() {
if (next != null)
return; // already fetched
if (nextRequest == null)
return; // no more data to fetch
URL url = nextRequest.url();
try {
GitHubResponse<T> nextResponse = client.sendRequest(nextRequest,
(responseInfo) -> GitHubResponse.parseBody(responseInfo, type));
assert nextResponse.body() != null;
next = nextResponse.body();
nextRequest = findNextURL(nextResponse);
if (nextRequest == null) {
finalResponse = nextResponse;
}
} catch (IOException e) {
// Iterators do not throw IOExceptions, so we wrap any IOException
// in a runtime GHException to bubble out if needed.
throw new GHException("Failed to retrieve " + url, e);
}
}
/**
* Locate the next page from the pagination "Link" tag.
*/
private GitHubRequest findNextURL(GitHubResponse<T> nextResponse) throws MalformedURLException {
GitHubRequest result = null;
String link = nextResponse.headerField("Link");
if (link != null) {
for (String token : link.split(", ")) {
if (token.endsWith("rel=\"next\"")) {
// found the next page. This should look something like
// <https://api.github.com/repos?page=3&per_page=100>; rel="next"
int idx = token.indexOf('>');
result = nextResponse.request().toBuilder().setRawUrlPath(token.substring(1, idx)).build();
break;
}
}
}
return result;
}
}

View File

@@ -0,0 +1,145 @@
package org.kohsuke.github;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Objects;
import javax.annotation.Nonnull;
/**
* A GitHub API Rate Limit Checker called before each request. This class provides the basic infrastructure for calling
* the appropriate {@link RateLimitChecker} for a request and retrying as many times as needed. This class supports more
* complex throttling strategies and polling, but leaves the specifics to the {@link RateLimitChecker} implementations.
* <p>
* GitHub allots a certain number of requests to each user or application per period of time (usually per hour). The
* number of requests remaining is returned in the response header and can also be requested using
* {@link GitHub#getRateLimit()}. This requests per interval is referred to as the "rate limit".
* </p>
* <p>
* GitHub prefers that clients stop before exceeding their rate limit rather than stopping after they exceed it. The
* {@link RateLimitChecker} is called before each request to check the rate limit and wait if the checker criteria are
* met.
* </p>
* <p>
* Checking your rate limit using {@link GitHub#getRateLimit()} does not effect your rate limit, but each {@link GitHub}
* instance will attempt to cache and reuse the last see rate limit rather than making a new request.
* </p>
*/
class GitHubRateLimitChecker {
@Nonnull
private final RateLimitChecker core;
@Nonnull
private final RateLimitChecker search;
@Nonnull
private final RateLimitChecker graphql;
@Nonnull
private final RateLimitChecker integrationManifest;
GitHubRateLimitChecker() {
this(RateLimitChecker.NONE, RateLimitChecker.NONE, RateLimitChecker.NONE, RateLimitChecker.NONE);
}
GitHubRateLimitChecker(@Nonnull RateLimitChecker core,
@Nonnull RateLimitChecker search,
@Nonnull RateLimitChecker graphql,
@Nonnull RateLimitChecker integrationManifest) {
this.core = Objects.requireNonNull(core);
// for now only support rate limiting on core
// remove these asserts when that changes
assert search == RateLimitChecker.NONE;
assert graphql == RateLimitChecker.NONE;
assert integrationManifest == RateLimitChecker.NONE;
this.search = Objects.requireNonNull(search);
this.graphql = Objects.requireNonNull(graphql);
this.integrationManifest = Objects.requireNonNull(integrationManifest);
}
/**
* Checks whether there is sufficient requests remaining within this client's rate limit quota to make the current
* request.
* <p>
* This method does not do the actual check. Instead it select the appropriate {@link RateLimitChecker} and
* {@link GHRateLimit.Record} for the current request's urlPath. If the {@link RateLimitChecker} for this the
* current request's urlPath is {@link RateLimitChecker#NONE} the rate limit is not checked. If not, it calls
* {@link RateLimitChecker#checkRateLimit(GHRateLimit.Record, long)}. which decides if the rate limit has been
* exceeded and then sleeps for as long is it choose.
* </p>
* <p>
* It is up to the {@link RateLimitChecker#checkRateLimit(GHRateLimit.Record, long)} which decide if the rate limit
* has been exceeded. If it has, that method will sleep for as long is it chooses and then return {@code true}. If
* not, that method will return {@code false}.
* </p>
* <p>
* As long as {@link RateLimitChecker#checkRateLimit(GHRateLimit.Record, long)} returns {@code true}, this method
* will request updated rate limit information and call
* {@link RateLimitChecker#checkRateLimit(GHRateLimit.Record, long)} again. This looping allows implementers of
* {@link RateLimitChecker#checkRateLimit(GHRateLimit.Record, long)} to apply any number of strategies to
* controlling the speed at which requests are made. When it returns {@code false} this method will return and the
* request will be sent.
* </p>
*
* @param client
* the {@link GitHubClient} to check
* @param request
* the {@link GitHubRequest} to check against
* @throws IOException
* if there is an I/O error
*/
void checkRateLimit(GitHubClient client, GitHubRequest request) throws IOException {
RateLimitChecker guard = selectChecker(request.urlPath());
if (guard == RateLimitChecker.NONE) {
return;
}
// For the first rate limit, accept the current limit if a valid one is already present.
GHRateLimit rateLimit = client.rateLimit();
GHRateLimit.Record rateLimitRecord = rateLimit.getRecordForUrlPath(request.urlPath());
long waitCount = 0;
try {
while (guard.checkRateLimit(rateLimitRecord, waitCount)) {
waitCount++;
// When rate limit is exceeded, sleep for one additional second beyond when the
// called {@link RateLimitChecker} sleeps.
// Reset time is only accurate to the second, so adding a one second buffer for safety is a good idea.
// This also keeps polling clients from querying too often.
Thread.sleep(1000);
// After the first wait, always request a new rate limit from the server.
rateLimit = client.getRateLimit();
rateLimitRecord = rateLimit.getRecordForUrlPath(request.urlPath());
}
} catch (InterruptedException e) {
throw (IOException) new InterruptedIOException(e.getMessage()).initCause(e);
}
}
/**
* Gets the appropriate {@link RateLimitChecker} for a particular url path. Similar to
* {@link GHRateLimit#getRecordForUrlPath(String)}.
*
* @param urlPath
* the url path of the request
* @return the {@link RateLimitChecker} for a url path.
*/
@Nonnull
private RateLimitChecker selectChecker(@Nonnull String urlPath) {
if (urlPath.equals("/rate_limit")) {
return RateLimitChecker.NONE;
} else if (urlPath.startsWith("/search")) {
return search;
} else if (urlPath.startsWith("/graphql")) {
return graphql;
} else if (urlPath.startsWith("/app-manifests")) {
return integrationManifest;
} else {
return core;
}
}
}

View File

@@ -0,0 +1,667 @@
package org.kohsuke.github;
import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.WillClose;
import static java.util.Arrays.asList;
/**
* Class {@link GitHubRequest} represents an immutable instance used by the client to determine what information to
* retrieve from a GitHub server. Use the {@link Builder} construct a {@link GitHubRequest}.
* <p>
* NOTE: {@link GitHubRequest} should include the data type to be returned. Any use cases where the same request should
* be used to return different types of data could be handled in some other way. However, the return type is currently
* not specified until late in the building process, so this is still untyped.
* </p>
*/
class GitHubRequest {
private static final List<String> METHODS_WITHOUT_BODY = asList("GET", "DELETE");
private final List<Entry> args;
private final Map<String, String> headers;
private final Map<String, Object> injectedMappingValues;
private final String apiUrl;
private final String urlPath;
private final String method;
private final InputStream body;
private final boolean forceBody;
private final URL url;
private GitHubRequest(@Nonnull List<Entry> args,
@Nonnull Map<String, String> headers,
@Nonnull Map<String, Object> injectedMappingValues,
@Nonnull String apiUrl,
@Nonnull String urlPath,
@Nonnull String method,
@CheckForNull InputStream body,
boolean forceBody) throws MalformedURLException {
this.args = Collections.unmodifiableList(new ArrayList<>(args));
this.headers = Collections.unmodifiableMap(new LinkedHashMap<>(headers));
this.injectedMappingValues = Collections.unmodifiableMap(new LinkedHashMap<>(injectedMappingValues));
this.apiUrl = apiUrl;
this.urlPath = urlPath;
this.method = method;
this.body = body;
this.forceBody = forceBody;
String tailApiUrl = buildTailApiUrl();
url = getApiURL(apiUrl, tailApiUrl);
}
/**
* Create a new {@link Builder}.
*
* @return a new {@link Builder}.
*/
public static Builder<?> newBuilder() {
return new Builder<>();
}
/**
* Gets the final GitHub API URL.
*/
@Nonnull
static URL getApiURL(String apiUrl, String tailApiUrl) throws MalformedURLException {
if (tailApiUrl.startsWith("/")) {
if ("github.com".equals(apiUrl)) {// backward compatibility
return new URL(GitHubClient.GITHUB_URL + tailApiUrl);
} else {
return new URL(apiUrl + tailApiUrl);
}
} else {
return new URL(tailApiUrl);
}
}
/**
* Transform Java Enum into Github constants given its conventions
*
* @param en
* Enum to be transformed
* @return a String containing the value of a Github constant
*/
static String transformEnum(Enum<?> en) {
// by convention Java constant names are upper cases, but github uses
// lower-case constants. GitHub also uses '-', which in Java we always
// replace with '_'
return en.toString().toLowerCase(Locale.ENGLISH).replace('_', '-');
}
/**
* The method for this request, such as "GET", "PATCH", or "DELETE".
*
* @return the request method.
*/
@Nonnull
public String method() {
return method;
}
/**
* The arguments for this request. Depending on the {@link #method()} and {@code #inBody()} these maybe added to the
* url or to the request body.
*
* @return the {@link List<Entry>} of arguments
*/
@Nonnull
public List<Entry> args() {
return args;
}
/**
* The headers for this request.
*
* @return the {@link Map} of headers
*/
@Nonnull
public Map<String, String> headers() {
return headers;
}
/**
* The headers for this request.
*
* @return the {@link Map} of headers
*/
@Nonnull
public Map<String, Object> injectedMappingValues() {
return injectedMappingValues;
}
/**
* The base GitHub API URL for this request represented as a {@link String}
*
* @return the url string
*/
@Nonnull
public String apiUrl() {
return apiUrl;
}
/**
* The url path to be added to the {@link #apiUrl()} for this request. If this does not start with a "/", it instead
* represents the full url string for this request.
*
* @return a url path or full url string
*/
@Nonnull
public String urlPath() {
return urlPath;
}
/**
* The content type to to be sent by this request.
*
* @return the content type.
*/
@Nonnull
public String contentType() {
return headers.get("Content-type");
}
/**
* The {@link InputStream} to be sent as the body of this request.
*
* @return the {@link InputStream}.
*/
@CheckForNull
public InputStream body() {
return body;
}
/**
* The {@link URL} for this request. This is the actual URL the {@link GitHubClient} will send this request to.
*
* @return the request {@link URL}
*/
@Nonnull
public URL url() {
return url;
}
/**
* Whether arguments for this request should be included in the URL or in the body of the request.
*
* @return true if the arguements should be sent in the body of the request.
*/
public boolean inBody() {
return forceBody || !METHODS_WITHOUT_BODY.contains(method);
}
/**
* Create a {@link Builder} from this request. Initial values of the builder will be the same as this
* {@link GitHubRequest}.
*
* @return a {@link Builder} based on this request.
*/
public Builder<?> toBuilder() {
return new Builder<>(args, headers, injectedMappingValues, apiUrl, urlPath, method, body, forceBody);
}
private String buildTailApiUrl() {
String tailApiUrl = urlPath;
if (!inBody() && !args.isEmpty() && tailApiUrl.startsWith("/")) {
try {
StringBuilder argString = new StringBuilder();
boolean questionMarkFound = tailApiUrl.indexOf('?') != -1;
argString.append(questionMarkFound ? '&' : '?');
for (Iterator<Entry> it = args.listIterator(); it.hasNext();) {
Entry arg = it.next();
argString.append(URLEncoder.encode(arg.key, StandardCharsets.UTF_8.name()));
argString.append('=');
argString.append(URLEncoder.encode(arg.value.toString(), StandardCharsets.UTF_8.name()));
if (it.hasNext()) {
argString.append('&');
}
}
tailApiUrl += argString;
} catch (UnsupportedEncodingException e) {
throw new GHException("UTF-8 encoding required", e);
}
}
return tailApiUrl;
}
/**
* Class {@link Builder} follows the builder pattern for {@link GitHubRequest}.
*
* @param <B>
* The type of {@link Builder} to return from the various "with*" methods.
*/
static class Builder<B extends Builder<B>> {
@Nonnull
private final List<Entry> args;
/**
* The header values for this request.
*/
@Nonnull
private final Map<String, String> headers;
/**
* Injected local data map
*/
@Nonnull
private final Map<String, Object> injectedMappingValues;
/**
* The base GitHub API for this request.
*/
@Nonnull
private String apiUrl;
@Nonnull
private String urlPath;
/**
* Request method.
*/
@Nonnull
private String method;
private InputStream body;
private boolean forceBody;
/**
* Create a new {@link GitHubRequest.Builder}
*/
protected Builder() {
this(new ArrayList<>(),
new LinkedHashMap<>(),
new LinkedHashMap<>(),
GitHubClient.GITHUB_URL,
"/",
"GET",
null,
false);
}
private Builder(@Nonnull List<Entry> args,
@Nonnull Map<String, String> headers,
@Nonnull Map<String, Object> injectedMappingValues,
@Nonnull String apiUrl,
@Nonnull String urlPath,
@Nonnull String method,
@CheckForNull @WillClose InputStream body,
boolean forceBody) {
this.args = new ArrayList<>(args);
this.headers = new LinkedHashMap<>(headers);
this.injectedMappingValues = new LinkedHashMap<>(injectedMappingValues);
this.apiUrl = apiUrl;
this.urlPath = urlPath;
this.method = method;
this.body = body;
this.forceBody = forceBody;
}
/**
* Builds a {@link GitHubRequest} from this builder.
*
* @return a {@link GitHubRequest}
* @throws MalformedURLException
* if the GitHub API URL cannot be constructed
*/
public GitHubRequest build() throws MalformedURLException {
return new GitHubRequest(args, headers, injectedMappingValues, apiUrl, urlPath, method, body, forceBody);
}
/**
* With header requester.
*
* @param url
* the url
* @return the request builder
*/
public B withApiUrl(String url) {
this.apiUrl = url;
return (B) this;
}
/**
* Sets the request HTTP header.
* <p>
* If a header of the same name is already set, this method overrides it.
*
* @param name
* the name
* @param value
* the value
*/
public void setHeader(String name, String value) {
headers.put(name, value);
}
/**
* With header requester.
*
* @param name
* the name
* @param value
* the value
* @return the request builder
*/
public B withHeader(String name, String value) {
setHeader(name, value);
return (B) this;
}
/**
* Object to inject into binding.
*
* @param value
* the value
* @return the request builder
*/
public B injectMappingValue(@NonNull Object value) {
return injectMappingValue(value.getClass().getName(), value);
}
/**
* Object to inject into binding.
*
* @param name
* the name
* @param value
* the value
* @return the request builder
*/
public B injectMappingValue(@NonNull String name, Object value) {
this.injectedMappingValues.put(name, value);
return (B) this;
}
public B withPreview(String name) {
return withHeader("Accept", name);
}
/**
* With requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B with(String key, int value) {
return with(key, (Object) value);
}
/**
* With requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B with(String key, long value) {
return with(key, (Object) value);
}
/**
* With requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B with(String key, boolean value) {
return with(key, (Object) value);
}
/**
* With requester.
*
* @param key
* the key
* @param e
* the e
* @return the request builder
*/
public B with(String key, Enum<?> e) {
if (e == null)
return with(key, (Object) null);
return with(key, transformEnum(e));
}
/**
* With requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B with(String key, String value) {
return with(key, (Object) value);
}
/**
* With requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B with(String key, Collection<?> value) {
return with(key, (Object) value);
}
/**
* With requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B with(String key, Map<?, ?> value) {
return with(key, (Object) value);
}
/**
* With requester.
*
* @param body
* the body
* @return the request builder
*/
public B with(@WillClose /* later */ InputStream body) {
this.body = body;
return (B) this;
}
/**
* With nullable requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B withNullable(String key, Object value) {
args.add(new Entry(key, value));
return (B) this;
}
/**
* With requester.
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B with(String key, Object value) {
if (value != null) {
args.add(new Entry(key, value));
}
return (B) this;
}
/**
* Unlike {@link #with(String, String)}, overrides the existing value
*
* @param key
* the key
* @param value
* the value
* @return the request builder
*/
public B set(String key, Object value) {
for (int index = 0; index < args.size(); index++) {
if (args.get(index).key.equals(key)) {
args.set(index, new Entry(key, value));
return (B) this;
}
}
return with(key, value);
}
/**
* Method requester.
*
* @param method
* the method
* @return the request builder
*/
public B method(@Nonnull String method) {
this.method = method;
return (B) this;
}
/**
* Content type requester.
*
* @param contentType
* the content type
* @return the request builder
*/
public B contentType(String contentType) {
this.headers.put("Content-type", contentType);
return (B) this;
}
/**
* NOT FOR PUBLIC USE. Do not make this method public.
* <p>
* Sets the path component of api URL without URI encoding.
* <p>
* Should only be used when passing a literal URL field from a GHObject, such as {@link GHContent#refresh()} or
* when needing to set query parameters on requests methods that don't usually have them, such as
* {@link GHRelease#uploadAsset(String, InputStream, String)}.
*
* @param urlOrPath
* the content type
* @return the request builder
*/
B setRawUrlPath(String urlOrPath) {
Objects.requireNonNull(urlOrPath);
this.urlPath = urlOrPath;
return (B) this;
}
/**
* Path component of api URL. Appended to api url.
* <p>
* If urlPath starts with a slash, it will be URI encoded as a path. If it starts with anything else, it will be
* used as is.
*
* @param urlPathItems
* the content type
* @return the request builder
*/
public B withUrlPath(String... urlPathItems) {
// full url may be set and reset as needed
if (urlPathItems.length == 1 && !urlPathItems[0].startsWith("/")) {
return setRawUrlPath(urlPathItems[0]);
}
// Once full url is set, do not allow path setting
if (!this.urlPath.startsWith("/")) {
throw new GHException("Cannot append to url path after setting a full url");
}
String tailUrlPath = String.join("/", urlPathItems);
if (this.urlPath.endsWith("/")) {
tailUrlPath = StringUtils.stripStart(tailUrlPath, "/");
} else {
tailUrlPath = StringUtils.prependIfMissing(tailUrlPath, "/");
}
this.urlPath += urlPathEncode(tailUrlPath);
return (B) this;
}
/**
* Small number of GitHub APIs use HTTP methods somewhat inconsistently, and use a body where it's not expected.
* Normally whether parameters go as query parameters or a body depends on the HTTP verb in use, but this method
* forces the parameters to be sent as a body.
*
* @return the request builder
*/
public B inBody() {
forceBody = true;
return (B) this;
}
}
protected static class Entry {
final String key;
final Object value;
protected Entry(String key, Object value) {
this.key = key;
this.value = value;
}
}
/**
* Encode the path to url safe string.
*
* @param value
* string to be path encoded.
* @return The encoded string.
*/
private static String urlPathEncode(String value) {
try {
return new URI(null, null, value, null, null).toString();
} catch (URISyntaxException ex) {
throw new AssertionError(ex);
}
}
}

View File

@@ -0,0 +1,321 @@
package org.kohsuke.github;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonMappingException;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* A GitHubResponse
* <p>
* A {@link GitHubResponse} generated by from sending a {@link GitHubRequest} to a {@link GitHubClient}.
* </p>
*
* @param <T>
* the type of the data parsed from the body of a {@link ResponseInfo}.
*/
class GitHubResponse<T> {
private final int statusCode;
@Nonnull
private final GitHubRequest request;
@Nonnull
private final Map<String, List<String>> headers;
@CheckForNull
private final T body;
GitHubResponse(GitHubResponse<T> response, @CheckForNull T body) {
this.statusCode = response.statusCode();
this.request = response.request();
this.headers = response.headers();
this.body = body;
}
GitHubResponse(ResponseInfo responseInfo, @CheckForNull T body) {
this.statusCode = responseInfo.statusCode();
this.request = responseInfo.request();
this.headers = responseInfo.headers();
this.body = body;
}
/**
* Parses a {@link ResponseInfo} body into a new instance of {@link T}.
*
* @param responseInfo
* response info to parse.
* @param type
* the type to be constructed.
* @param <T>
* the type
* @return a new instance of {@link T}.
* @throws IOException
* if there is an I/O Exception.
*/
@CheckForNull
static <T> T parseBody(ResponseInfo responseInfo, Class<T> type) throws IOException {
if (responseInfo.statusCode() == HttpURLConnection.HTTP_NO_CONTENT && type != null && type.isArray()) {
// no content
return type.cast(Array.newInstance(type.getComponentType(), 0));
}
String data = responseInfo.getBodyAsString();
try {
InjectableValues.Std inject = new InjectableValues.Std();
inject.addValue(ResponseInfo.class, responseInfo);
return GitHubClient.getMappingObjectReader(responseInfo).forType(type).readValue(data);
} catch (JsonMappingException e) {
String message = "Failed to deserialize " + data;
throw new IOException(message, e);
}
}
/**
* Parses a {@link ResponseInfo} body into a new instance of {@link T}.
*
* @param responseInfo
* response info to parse.
* @param instance
* the object to fill with data parsed from body
* @param <T>
* the type
* @return a new instance of {@link T}.
* @throws IOException
* if there is an I/O Exception.
*/
@CheckForNull
static <T> T parseBody(ResponseInfo responseInfo, T instance) throws IOException {
String data = responseInfo.getBodyAsString();
try {
return GitHubClient.getMappingObjectReader(responseInfo).withValueToUpdate(instance).readValue(data);
} catch (JsonMappingException e) {
String message = "Failed to deserialize " + data;
throw new IOException(message, e);
}
}
/**
* The {@link URL} for this response.
*
* @return the {@link URL} for this response.
*/
@Nonnull
public URL url() {
return request.url();
}
/**
* The {@link GitHubRequest} for this response.
*
* @return the {@link GitHubRequest} for this response.
*/
@Nonnull
public GitHubRequest request() {
return request;
}
/**
* The status code for this response.
*
* @return the status code for this response.
*/
public int statusCode() {
return statusCode;
}
/**
* The headers for this response.
*
* @return the headers for this response.
*/
@Nonnull
public Map<String, List<String>> headers() {
return headers;
}
/**
* Gets the value of a header field for this response.
*
* @param name
* the name of the header field.
* @return the value of the header field, or {@code null} if the header isn't set.
*/
@CheckForNull
public String headerField(String name) {
String result = null;
if (headers.containsKey(name)) {
result = headers.get(name).get(0);
}
return result;
}
/**
* The body of the response parsed as a {@link T}
*
* @return body of the response
*/
public T body() {
return body;
}
/**
* Represents a supplier of results that can throw.
*
* <p>
* This is a <a href="package-summary.html">functional interface</a> whose functional method is
* {@link #apply(ResponseInfo)}.
*
* @param <T>
* the type of results supplied by this supplier
*/
@FunctionalInterface
interface BodyHandler<T> {
/**
* Gets a result.
*
* @return a result
* @throws IOException
* if an I/O Exception occurs.
*/
T apply(ResponseInfo input) throws IOException;
}
/**
* Initial response information supplied to a {@link BodyHandler} when a response is initially received and before
* the body is processed.
*/
static abstract class ResponseInfo {
private static final Comparator<String> nullableCaseInsensitiveComparator = Comparator
.nullsFirst(String.CASE_INSENSITIVE_ORDER);
private final int statusCode;
@Nonnull
private final GitHubRequest request;
@Nonnull
private final Map<String, List<String>> headers;
protected ResponseInfo(@Nonnull GitHubRequest request,
int statusCode,
@Nonnull Map<String, List<String>> headers) {
this.request = request;
this.statusCode = statusCode;
// Response header field names must be case-insensitive.
TreeMap<String, List<String>> caseInsensitiveMap = new TreeMap<>(nullableCaseInsensitiveComparator);
caseInsensitiveMap.putAll(headers);
this.headers = Collections.unmodifiableMap(caseInsensitiveMap);
}
/**
* Gets the value of a header field for this response.
*
* @param name
* the name of the header field.
* @return the value of the header field, or {@code null} if the header isn't set.
*/
@CheckForNull
public String headerField(String name) {
String result = null;
if (headers.containsKey(name)) {
result = headers.get(name).get(0);
}
return result;
}
/**
* The response body as an {@link InputStream}.
*
* @return the response body
* @throws IOException
* if an I/O Exception occurs.
*/
abstract InputStream bodyStream() throws IOException;
/**
* The error message for this response.
*
* @return if there is an error with some error string, that is returned. If not, {@code null}.
*/
abstract String errorMessage();
/**
* The {@link URL} for this response.
*
* @return the {@link URL} for this response.
*/
@Nonnull
public URL url() {
return request.url();
}
/**
* Gets the {@link GitHubRequest} for this response.
*
* @return the {@link GitHubRequest} for this response.
*/
@Nonnull
public GitHubRequest request() {
return request;
}
/**
* The status code for this response.
*
* @return the status code for this response.
*/
public int statusCode() {
return statusCode;
}
/**
* The headers for this response.
*
* @return the headers for this response.
*/
@Nonnull
public Map<String, List<String>> headers() {
return headers;
}
/**
* Gets the body of the response as a {@link String}.
*
* @return the body of the response as a {@link String}.
* @throws IOException
* if an I/O Exception occurs.
*/
String getBodyAsString() throws IOException {
InputStreamReader r = null;
try {
r = new InputStreamReader(this.bodyStream(), StandardCharsets.UTF_8);
return IOUtils.toString(r);
} finally {
IOUtils.closeQuietly(r);
}
}
}
}

View File

@@ -41,6 +41,6 @@ public class GitUser {
* @return This field doesn't appear to be consistently available in all the situations where this class is used.
*/
public Date getDate() {
return GitHub.parseDate(date);
return GitHubClient.parseDate(date);
}
}

View File

@@ -14,6 +14,7 @@ import java.net.URL;
*
* @author Kohsuke Kawaguchi
*/
@FunctionalInterface
public interface HttpConnector {
/**
* Opens a connection to the given URL.
@@ -29,18 +30,12 @@ public interface HttpConnector {
/**
* Default implementation that uses {@link URL#openConnection()}.
*/
HttpConnector DEFAULT = new ImpatientHttpConnector(new HttpConnector() {
public HttpURLConnection connect(URL url) throws IOException {
return (HttpURLConnection) url.openConnection();
}
});
HttpConnector DEFAULT = new ImpatientHttpConnector(url -> (HttpURLConnection) url.openConnection());
/**
* Stub implementation that is always off-line.
*/
HttpConnector OFFLINE = new HttpConnector() {
public HttpURLConnection connect(URL url) throws IOException {
throw new IOException("Offline");
}
HttpConnector OFFLINE = url -> {
throw new IOException("Offline");
};
}

View File

@@ -12,7 +12,7 @@ import javax.annotation.CheckForNull;
*
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
*/
public class HttpException extends IOException {
public class HttpException extends GHIOException {
static final long serialVersionUID = 1L;
private final int responseCode;
@@ -58,8 +58,7 @@ public class HttpException extends IOException {
* @see HttpURLConnection#getResponseMessage() HttpURLConnection#getResponseMessage()
*/
public HttpException(String message, int responseCode, String responseMessage, String url, Throwable cause) {
super(message);
initCause(cause);
super(message, cause);
this.responseCode = responseCode;
this.responseMessage = responseMessage;
this.url = url;
@@ -82,8 +81,7 @@ public class HttpException extends IOException {
*/
public HttpException(int responseCode, String responseMessage, String url, Throwable cause) {
super("Server returned HTTP response code: " + responseCode + ", message: '" + responseMessage + "'"
+ " for URL: " + url);
initCause(cause);
+ " for URL: " + url, cause);
this.responseCode = responseCode;
this.responseMessage = responseMessage;
this.url = url;

View File

@@ -1,6 +1,11 @@
package org.kohsuke.github;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -8,17 +13,18 @@ import java.util.Set;
import javax.annotation.Nonnull;
/**
* {@link Iterable} that returns {@link PagedIterator}
* {@link Iterable} that returns {@link PagedIterator}. {@link PagedIterable} is thread-safe but {@link PagedIterator}
* is not. Any one instance of {@link PagedIterator} should only be called from a single thread.
*
* @param <T>
* the type parameter
* the type of items on each page
* @author Kohsuke Kawaguchi
*/
public abstract class PagedIterable<T> implements Iterable<T> {
/**
* Page size. 0 is default.
*/
private int size = 0;
private int pageSize = 0;
/**
* Sets the pagination size.
@@ -31,7 +37,7 @@ public abstract class PagedIterable<T> implements Iterable<T> {
* @return the paged iterable
*/
public PagedIterable<T> withPageSize(int size) {
this.size = size;
this.pageSize = size;
return this;
}
@@ -42,11 +48,11 @@ public abstract class PagedIterable<T> implements Iterable<T> {
*/
@Nonnull
public final PagedIterator<T> iterator() {
return _iterator(size);
return _iterator(pageSize);
}
/**
* Iterator paged iterator.
* Iterator over page items.
*
* @param pageSize
* the page size
@@ -55,29 +61,131 @@ public abstract class PagedIterable<T> implements Iterable<T> {
@Nonnull
public abstract PagedIterator<T> _iterator(int pageSize);
/**
* Eagerly walk {@link PagedIterator} and return the result in an array.
*
* @param iterator
* the {@link PagedIterator} to read
* @return an array of all elements from the {@link PagedIterator}
* @throws IOException
* if an I/O exception occurs.
*/
protected T[] toArray(final PagedIterator<T> iterator) throws IOException {
try {
ArrayList<T[]> pages = new ArrayList<>();
int totalSize = 0;
T[] item;
do {
item = iterator.nextPageArray();
totalSize += Array.getLength(item);
pages.add(item);
} while (iterator.hasNext());
Class<T[]> type = (Class<T[]>) item.getClass();
return concatenatePages(type, pages, totalSize);
} catch (GHException e) {
// if there was an exception inside the iterator it is wrapped as a GHException
// if the wrapped exception is an IOException, throw that
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw e;
}
}
}
/**
* Eagerly walk {@link Iterable} and return the result in an array.
*
* @return the list
* @throws IOException
* if an I/O exception occurs.
*/
@Nonnull
public T[] toArray() throws IOException {
return toArray(iterator());
}
/**
* Eagerly walk {@link Iterable} and return the result in a list.
*
* @return the list
* @throws IOException
* if an I/O Exception occurs
*/
public List<T> asList() {
ArrayList<T> r = new ArrayList<>();
for (PagedIterator<T> i = iterator(); i.hasNext();) {
r.addAll(i.nextPage());
}
return r;
@Nonnull
public List<T> toList() throws IOException {
return Collections.unmodifiableList(Arrays.asList(this.toArray()));
}
/**
* Eagerly walk {@link Iterable} and return the result in a set.
*
* @return the set
* @throws IOException
* if an I/O Exception occurs
*/
public Set<T> asSet() {
LinkedHashSet<T> r = new LinkedHashSet<>();
for (PagedIterator<T> i = iterator(); i.hasNext();) {
r.addAll(i.nextPage());
}
return r;
@Nonnull
public Set<T> toSet() throws IOException {
return Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(this.toArray())));
}
/**
* Eagerly walk {@link Iterable} and return the result in a list.
*
* @return the list
* @deprecated Use {@link #toList()} instead.
*/
@Nonnull
@Deprecated
public List<T> asList() {
try {
return this.toList();
} catch (IOException e) {
throw new GHException("Failed to retrieve list: " + e.getMessage(), e);
}
}
/**
* Eagerly walk {@link Iterable} and return the result in a set.
*
* @return the set
* @deprecated Use {@link #toSet()} instead.
*/
@Nonnull
@Deprecated
public Set<T> asSet() {
try {
return this.toSet();
} catch (IOException e) {
throw new GHException("Failed to retrieve list: " + e.getMessage(), e);
}
}
/**
* Concatenates a list of arrays into a single array.
*
* @param type
* the type of array to be returned.
* @param pages
* the list of arrays to be concatenated.
* @param totalLength
* the total length of the returned array.
* @return an array containing all elements from all pages.
*/
@Nonnull
private T[] concatenatePages(Class<T[]> type, List<T[]> pages, int totalLength) {
T[] result = type.cast(Array.newInstance(type.getComponentType(), totalLength));
int position = 0;
for (T[] page : pages) {
final int pageLength = Array.getLength(page);
System.arraycopy(page, 0, result, position, pageLength);
position += pageLength;
}
return result;
}
}

View File

@@ -4,67 +4,112 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* Iterator over a paginated data source.
* Iterator over a paginated data source. Iterates of the content items of each page, automatically requesting new pages
* as needed.
* <p>
* Aside from the normal iterator operation, this method exposes {@link #nextPage()} that allows the caller to retrieve
* items per page.
* Aside from the normal iterator operation, this method exposes {@link #nextPage()} and {@link #nextPageArray()} that
* allows the caller to retrieve entire pages.
*
* This class is not thread-safe. Any one instance should only be called from a single thread.
*
* @param <T>
* the type parameter
* @author Kohsuke Kawaguchi
*/
public abstract class PagedIterator<T> implements Iterator<T> {
private final Iterator<T[]> base;
public class PagedIterator<T> implements Iterator<T> {
@Nonnull
protected final Iterator<T[]> base;
@CheckForNull
private final Consumer<T> itemInitializer;
/**
* Current batch that we retrieved but haven't returned to the caller.
* Current batch of items. Each time {@link #next()} is called the next item in this array will be returned. After
* the last item of the array is returned, when {@link #next()} is called again, a new page of items will be fetched
* and iterating will continue from the first item in the new page.
*
* @see #fetch() {@link #fetch()} for details on how this field is used.
*/
private T[] current;
private int pos;
private T[] currentPage;
PagedIterator(Iterator<T[]> base) {
/**
* The index of the next item on the page, the item that will be returned when {@link #next()} is called.
*
* @see #fetch() {@link #fetch()} for details on how this field is used.
*/
private int nextItemIndex;
PagedIterator(@Nonnull Iterator<T[]> base, @CheckForNull Consumer<T> itemInitializer) {
this.base = base;
this.itemInitializer = itemInitializer;
}
/**
* Wrap up.
* This poorly named method, initializes items with local data after they are fetched. It is up to the implementer
* to decide what local data to apply.
*
* @param page
* the page
* the page of items to be initialized
*/
protected abstract void wrapUp(T[] page);
protected void wrapUp(T[] page) {
if (itemInitializer != null) {
for (T item : page) {
itemInitializer.accept(item);
}
}
}
/**
* {@inheritDoc}
*/
public boolean hasNext() {
fetch();
return current != null;
return currentPage.length > nextItemIndex;
}
/**
* {@inheritDoc}
*/
public T next() {
fetch();
if (current == null)
if (!hasNext())
throw new NoSuchElementException();
return current[pos++];
return currentPage[nextItemIndex++];
}
/**
* Fetch is called at the start of {@link #next()} or {@link #hasNext()} to fetch another page of data if it is
* needed and available.
* <p>
* If there is no current page yet (at the start of iterating), a page is fetched. If {@link #nextItemIndex} points
* to an item in the current page array, the state is valid - no more work is needed. If {@link #nextItemIndex} is
* greater than the last index in the current page array, the method checks if there is another page of data
* available.
* </p>
* <p>
* If there is another page, get that page of data and reset the check {@link #nextItemIndex} to the start of the
* new page.
* </p>
* <p>
* If no more pages are available, leave the page and index unchanged. In this case, {@link #hasNext()} will return
* {@code false} and {@link #next()} will throw an exception.
* </p>
*/
private void fetch() {
while (current == null || current.length <= pos) {
if (!base.hasNext()) {// no more to retrieve
current = null;
pos = 0;
return;
}
current = base.next();
wrapUp(current);
pos = 0;
if ((currentPage == null || currentPage.length <= nextItemIndex) && base.hasNext()) {
// On first call, always get next page (may be empty array)
T[] result = Objects.requireNonNull(base.next());
wrapUp(result);
currentPage = result;
nextItemIndex = 0;
}
// invariant at the end: there's some data to retrieve
}
public void remove() {
throw new UnsupportedOperationException();
}
/**
@@ -73,11 +118,31 @@ public abstract class PagedIterator<T> implements Iterator<T> {
* @return the list
*/
public List<T> nextPage() {
fetch();
List<T> r = Arrays.asList(current);
r = r.subList(pos, r.size());
current = null;
pos = 0;
return Arrays.asList(nextPageArray());
}
/**
* Gets the next page worth of data.
*
* @return the list
*/
@Nonnull
T[] nextPageArray() {
// if we have not fetched any pages yet, always fetch.
// If we have fetched at least one page, check hasNext()
if (currentPage == null) {
fetch();
} else if (!hasNext()) {
throw new NoSuchElementException();
}
// Current should never be null after fetch
Objects.requireNonNull(currentPage);
T[] r = currentPage;
if (nextItemIndex != 0) {
r = Arrays.copyOfRange(r, nextItemIndex, r.length);
}
nextItemIndex = currentPage.length;
return r;
}
}

View File

@@ -4,6 +4,8 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Iterator;
import javax.annotation.Nonnull;
/**
* {@link PagedIterable} enhanced to report search result specific information.
*
@@ -15,16 +17,22 @@ import java.util.Iterator;
value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD",
"UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" },
justification = "Constructed by JSON API")
public abstract class PagedSearchIterable<T> extends PagedIterable<T> {
public class PagedSearchIterable<T> extends PagedIterable<T> {
private final GitHub root;
private final GitHubRequest request;
private final Class<? extends SearchResult<T>> receiverType;
/**
* As soon as we have any result fetched, it's set here so that we can report the total count.
*/
private SearchResult<T> result;
PagedSearchIterable(GitHub root) {
PagedSearchIterable(GitHub root, GitHubRequest request, Class<? extends SearchResult<T>> receiverType) {
this.root = root;
this.request = request;
this.receiverType = receiverType;
}
@Override
@@ -57,6 +65,14 @@ public abstract class PagedSearchIterable<T> extends PagedIterable<T> {
iterator().hasNext();
}
@Nonnull
@Override
public PagedIterator<T> _iterator(int pageSize) {
final Iterator<T[]> adapter = adapt(
GitHubPageIterator.create(root.getClient(), receiverType, request, pageSize));
return new PagedIterator<T>(adapter, null);
}
/**
* Adapts {@link Iterator}.
*
@@ -76,10 +92,6 @@ public abstract class PagedSearchIterable<T> extends PagedIterable<T> {
result = v;
return v.getItems(root);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}

View File

@@ -8,6 +8,12 @@ package org.kohsuke.github;
* @author Kohsuke Kawaguchi
*/
class Previews {
/**
* Check-runs and check-suites
*
* @see <a href="https://developer.github.com/v3/previews/#check-runs-and-check-suites-api">GitHub API Previews</a>
*/
static final String ANTIOPE = "application/vnd.github.antiope-preview+json";
/**
* Commit Search

View File

@@ -0,0 +1,112 @@
package org.kohsuke.github;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A GitHub API Rate Limit Checker called before each request
* <p>
* GitHub allots a certain number of requests to each user or application per period of time (usually per hour). The
* number of requests remaining is returned in the response header and can also be requested using
* {@link GitHub#getRateLimit()}. This requests per interval is referred to as the "rate limit".
* </p>
* <p>
* GitHub prefers that clients stop before exceeding their rate limit rather than stopping after they exceed it. The
* {@link RateLimitChecker} is called before each request to check the rate limit and wait if the checker criteria are
* met.
* </p>
* <p>
* Checking your rate limit using {@link GitHub#getRateLimit()} does not effect your rate limit, but each {@link GitHub}
* instance will attempt to cache and reuse the last see rate limit rather than making a new request.
* </p>
*/
public abstract class RateLimitChecker {
private static final Logger LOGGER = Logger.getLogger(RateLimitChecker.class.getName());
public static final RateLimitChecker NONE = new RateLimitChecker() {
};
/**
* Decides whether the current request exceeds the allowed "rate limit" budget. If this determines the rate limit
* will be exceeded, this method should sleep for some amount of time and must return {@code true}. Implementers are
* free to choose whatever strategy they prefer for what is considered to exceed the budget and how long to sleep.
*
* <p>
* The caller of this method figures out which {@link GHRateLimit.Record} applies for the current request add
* provides it to this method.
* </p>
* <p>
* It is important to remember that rate limit reset times are only accurate to the second. Trying to sleep to
* exactly the reset time would be likely to produce worse behavior rather than better. For this reason
* {@link GitHubRateLimitChecker} may choose to add more sleep times when a checker indicates the rate limit was
* exceeded.
* </p>
* <p>
* As long as this method returns {@code true} it is guaranteed that {@link GitHubRateLimitChecker} will get updated
* rate limit information and call this method again with {@code count} incremented by one. After this method
* returns {@code true} at least once, the calling {@link GitHubRateLimitChecker} may choose to wait some additional
* period of time between calls to this checker.
* </p>
* <p>
* After this checker returns {@code false}, the calling {@link GitHubRateLimitChecker} will let the request
* continue. If this method returned {@code true} at least once for a particular request, the calling
* {@link GitHubRateLimitChecker} may choose to wait some additional period of time before letting the request be
* sent.
* </p>
*
* @param rateLimitRecord
* the current {@link GHRateLimit.Record} to check against.
* @param count
* the number of times in a row this method has been called for the current request
* @return {@code false} if the current request does not exceed the allowed budget, {@code true} if the current
* request exceeded the budget.
* @throws InterruptedException
* if the thread is interrupted while sleeping
*/
protected boolean checkRateLimit(GHRateLimit.Record rateLimitRecord, long count) throws InterruptedException {
return false;
}
protected final boolean sleepUntilReset(GHRateLimit.Record record) throws InterruptedException {
// Sleep until reset
long sleepMilliseconds = record.getResetDate().getTime() - System.currentTimeMillis();
if (sleepMilliseconds > 0) {
String message = String.format(
"GitHub API - Current quota has %d remaining of %d. Waiting for quota to reset at %tT.",
record.getRemaining(),
record.getLimit(),
record.getResetDate());
LOGGER.log(Level.INFO, message);
Thread.sleep(sleepMilliseconds);
return true;
}
return false;
}
/**
* A {@link RateLimitChecker} with a simple number as the limit.
*/
public static class LiteralValue extends RateLimitChecker {
private final int sleepAtOrBelow;
public LiteralValue(int sleepAtOrBelow) {
if (sleepAtOrBelow < 0) {
throw new IllegalArgumentException("sleepAtOrBelow must >= 0");
}
this.sleepAtOrBelow = sleepAtOrBelow;
}
@Override
protected boolean checkRateLimit(GHRateLimit.Record record, long count) throws InterruptedException {
if (record.getRemaining() <= sleepAtOrBelow) {
return sleepUntilReset(record);
}
return false;
}
}
}

View File

@@ -11,7 +11,14 @@ import com.fasterxml.jackson.annotation.JsonValue;
* @see GHReaction
*/
public enum ReactionContent {
PLUS_ONE("+1"), MINUS_ONE("-1"), LAUGH("laugh"), CONFUSED("confused"), HEART("heart"), HOORAY("hooray");
PLUS_ONE("+1"),
MINUS_ONE("-1"),
LAUGH("laugh"),
CONFUSED("confused"),
HEART("heart"),
HOORAY("hooray"),
ROCKET("rocket"),
EYES("eyes");
private final String content;

File diff suppressed because it is too large Load Diff

View File

@@ -1,12 +1,20 @@
package org.kohsuke.github;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.extension.responsetemplating.helpers.HandlebarsCurrentDateHelper;
import org.apache.commons.io.IOUtils;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.hamcrest.StringDescription;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.kohsuke.github.junit.GitHubWireMockRule;
import wiremock.com.github.jknack.handlebars.Helper;
import wiremock.com.github.jknack.handlebars.Options;
import java.io.File;
import java.io.FileInputStream;
@@ -37,11 +45,7 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
*/
protected GitHub gitHub;
/**
* {@link GitHub} instance for use before/after test. Traffic will not be part of snapshot when taken. Should only
* be used when isUseProxy() or isTakeSnapShot().
*/
protected GitHub gitHubBeforeAfter;
private GitHub gitHubBeforeAfter;
protected final String baseFilesClassPath = this.getClass().getName().replace('.', '/');
protected final String baseRecordPath = "src/test/resources/" + baseFilesClassPath + "/wiremock";
@@ -49,6 +53,8 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
@Rule
public final GitHubWireMockRule mockGitHub;
protected final TemplatingHelper templating = new TemplatingHelper();
public AbstractGitHubWireMockTest() {
mockGitHub = new GitHubWireMockRule(this.getWireMockOptions());
}
@@ -126,6 +132,13 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
mockGitHub.isUseProxy());
}
protected void verifyAuthenticated(GitHub instance) {
assertThat(
"GitHub connection believes it is anonymous. Make sure you set GITHUB_OAUTH or both GITHUB_LOGIN and GITHUB_PASSWORD environment variables",
instance.isAnonymous(),
Matchers.is(false));
}
protected GHUser getUser() {
return getUser(gitHub);
}
@@ -163,9 +176,10 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
protected GHRepository getTempRepository(String name) throws IOException {
String fullName = GITHUB_API_TEST_ORG + '/' + name;
if (mockGitHub.isUseProxy()) {
cleanupRepository(fullName);
GHRepository repository = gitHubBeforeAfter.getOrganization(GITHUB_API_TEST_ORG)
GHRepository repository = getGitHubBeforeAfter().getOrganization(GITHUB_API_TEST_ORG)
.createRepository(name)
.description("A test repository for testing the github-api project: " + name)
.homepage("http://github-api.kohsuke.org/")
@@ -199,7 +213,7 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
if (mockGitHub.isUseProxy()) {
tempGitHubRepositories.add(fullName);
try {
GHRepository repository = gitHubBeforeAfter.getRepository(fullName);
GHRepository repository = getGitHubBeforeAfter().getRepository(fullName);
if (repository != null) {
repository.delete();
}
@@ -210,6 +224,17 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
}
}
/**
* {@link GitHub} instance for use before/after test. Traffic will not be part of snapshot when taken. Should only
* be used when isUseProxy() or isTakeSnapShot().
*
* @return a github instance after checking Authentication
*/
public GitHub getGitHubBeforeAfter() {
verifyAuthenticated(gitHubBeforeAfter);
return gitHubBeforeAfter;
}
protected void kohsuke() {
// No-op for now
// Generally this means the test is doing something that requires additional access rights
@@ -219,4 +244,71 @@ public abstract class AbstractGitHubWireMockTest extends Assert {
// assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));
}
public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
assertThat("", actual, matcher);
}
public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
if (!matcher.matches(actual)) {
Description description = new StringDescription();
description.appendText(reason)
.appendText(System.lineSeparator())
.appendText("Expected: ")
.appendDescriptionOf(matcher)
.appendText(System.lineSeparator())
.appendText(" but: ");
matcher.describeMismatch(actual, description);
throw new AssertionError(description.toString());
}
}
public static void assertThat(String reason, boolean assertion) {
if (!assertion) {
throw new AssertionError(reason);
}
}
public static void assertEquals(Object expected, Object actual) {
assertThat(actual, Matchers.equalTo(expected));
}
public static void assertNotEquals(Object expected, Object actual) {
assertThat(actual, Matchers.not(expected));
}
public static void assertNotNull(Object actual) {
assertThat(actual, Matchers.notNullValue());
}
public static void assertNull(Object actual) {
assertThat(actual, Matchers.nullValue());
}
public static void assertTrue(Boolean condition) {
assertThat(condition, Matchers.is(true));
}
public static void assertFalse(Boolean condition) {
assertThat(condition, Matchers.is(false));
}
protected static class TemplatingHelper {
public Date testStartDate = new Date();
public ResponseTemplateTransformer newResponseTransformer() {
testStartDate = new Date();
return ResponseTemplateTransformer.builder()
.global(true)
.maxCacheEntries(0L)
.helper("testStartDate", new Helper<Object>() {
private HandlebarsCurrentDateHelper helper = new HandlebarsCurrentDateHelper();
@Override
public Object apply(final Object context, final Options options) throws IOException {
return this.helper.apply(TemplatingHelper.this.testStartDate, options);
}
})
.build();
}
}
}

Some files were not shown because too many files have changed in this diff Show More