-
Notifications
You must be signed in to change notification settings - Fork 501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support GitHub Apps #1766
Merged
Merged
support GitHub Apps #1766
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
modules/core/src/main/scala/org/scalasteward/core/vcs/github/GitHubApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2018-2020 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.vcs.github | ||
|
||
import better.files.File | ||
|
||
case class GitHubApp(id: Long, keyFile: File) |
74 changes: 74 additions & 0 deletions
74
modules/core/src/main/scala/org/scalasteward/core/vcs/github/GitHubAppApiAlg.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2018-2020 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.vcs.github | ||
|
||
import cats.Applicative | ||
import org.http4s.{Header, Uri} | ||
import org.scalasteward.core.util.HttpJsonClient | ||
|
||
class GitHubAppApiAlg[F[_]: Applicative]( | ||
gitHubApiHost: Uri | ||
)(implicit | ||
client: HttpJsonClient[F] | ||
) { | ||
|
||
private[this] val acceptHeader = | ||
Header("Accept", "application/vnd.github.v3+json") | ||
|
||
private[this] def addHeaders(jwt: String): client.ModReq = | ||
req => | ||
Applicative[F].point( | ||
req.putHeaders( | ||
Header("Authorization", s"Bearer $jwt"), | ||
acceptHeader | ||
) | ||
) | ||
|
||
/** [[https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#list-installations-for-the-authenticated-app]] | ||
* | ||
* TODO pagination use `page` query param | ||
*/ | ||
def installations(jwt: String): F[List[InstallationOut]] = | ||
client.get( | ||
(gitHubApiHost / "app" / "installations").withQueryParam("per_page", 100), | ||
addHeaders(jwt) | ||
) | ||
|
||
/** [[https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#create-an-installation-access-token-for-an-app]] */ | ||
def accessToken(jwt: String, installationId: Long): F[TokenOut] = | ||
client.post( | ||
gitHubApiHost / "app" / "installations" / installationId.toString / "access_tokens", | ||
addHeaders(jwt) | ||
) | ||
|
||
/** [[https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#list-repositories-accessible-to-the-app-installation]] | ||
* | ||
* TODO pagination use `page` query param | ||
*/ | ||
def repositories(token: String): F[RepositoriesOut] = | ||
client | ||
.get( | ||
(gitHubApiHost / "installation" / "repositories").withQueryParam("per_page", 100), | ||
req => | ||
Applicative[F].point( | ||
req.putHeaders( | ||
Header("Authorization", s"token ${token}"), | ||
acceptHeader | ||
) | ||
) | ||
) | ||
} |
77 changes: 77 additions & 0 deletions
77
modules/core/src/main/scala/org/scalasteward/core/vcs/github/GitHubAuthAlg.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2018-2020 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.vcs.github | ||
|
||
import java.io.FileReader | ||
import java.security.{KeyFactory, PrivateKey, Security} | ||
import java.security.spec.PKCS8EncodedKeySpec | ||
import java.util.Date | ||
|
||
import better.files.File | ||
import cats.effect.Sync | ||
import io.jsonwebtoken.{Jwts, SignatureAlgorithm} | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider | ||
import org.bouncycastle.util.io.pem.PemReader | ||
import scala.concurrent.duration.FiniteDuration | ||
import scala.util.Using | ||
|
||
trait GitHubAuthAlg[F[_]] { | ||
|
||
/** [[https://docs.github.com/en/free-pro-team@latest/developers/apps/authenticating-with-github-apps#authenticating-as-a-github-app]] */ | ||
def createJWT(app: GitHubApp, ttl: FiniteDuration): F[String] | ||
} | ||
|
||
object GitHubAuthAlg { | ||
def create[F[_]](implicit F: Sync[F]): GitHubAuthAlg[F] = | ||
new GitHubAuthAlg[F] { | ||
private[this] def parsePEMFile(pemFile: File): Array[Byte] = | ||
Using.resource(new PemReader(new FileReader(pemFile.toJava))) { reader => | ||
reader.readPemObject().getContent | ||
} | ||
|
||
private[this] def getPrivateKey(keyBytes: Array[Byte]): PrivateKey = { | ||
val kf = KeyFactory.getInstance("RSA") | ||
val keySpec = new PKCS8EncodedKeySpec(keyBytes) | ||
kf.generatePrivate(keySpec) | ||
} | ||
|
||
private[this] def readPrivateKey(file: File): PrivateKey = { | ||
val bytes = parsePEMFile(file) | ||
getPrivateKey(bytes) | ||
} | ||
|
||
/** [[https://docs.github.com/en/free-pro-team@latest/developers/apps/authenticating-with-github-apps#authenticating-as-a-github-app]] */ | ||
def createJWT(app: GitHubApp, ttl: FiniteDuration): F[String] = F.delay { | ||
Security.addProvider(new BouncyCastleProvider()) | ||
val ttlMillis = ttl.toMillis | ||
val nowMillis = System.currentTimeMillis() | ||
val now = new Date(nowMillis) | ||
val signingKey = readPrivateKey(app.keyFile) | ||
val builder = Jwts | ||
.builder() | ||
.setIssuedAt(now) | ||
.setIssuer(app.id.toString) | ||
.signWith(signingKey, SignatureAlgorithm.RS256) | ||
if (ttlMillis > 0) { | ||
val expMillis = nowMillis + ttlMillis | ||
val exp = new Date(expMillis) | ||
builder.setExpiration(exp) | ||
} | ||
builder.compact() | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
modules/core/src/main/scala/org/scalasteward/core/vcs/github/InstallationOut.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2018-2020 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.vcs.github | ||
|
||
import io.circe.Decoder | ||
import io.circe.generic.semiauto.deriveDecoder | ||
|
||
case class InstallationOut(id: Long) | ||
object InstallationOut { | ||
implicit val installationDecoder: Decoder[InstallationOut] = deriveDecoder | ||
} |
30 changes: 30 additions & 0 deletions
30
modules/core/src/main/scala/org/scalasteward/core/vcs/github/RepositoriesOut.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2018-2020 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.vcs.github | ||
|
||
import io.circe.Decoder | ||
import io.circe.generic.semiauto.deriveDecoder | ||
|
||
case class RepositoriesOut(repositories: List[Repository]) | ||
object RepositoriesOut { | ||
implicit val repositoriesDecoder: Decoder[RepositoriesOut] = deriveDecoder | ||
} | ||
|
||
case class Repository(full_name: String) | ||
object Repository { | ||
implicit val repositoryDecoder: Decoder[Repository] = deriveDecoder | ||
} |
25 changes: 25 additions & 0 deletions
25
modules/core/src/main/scala/org/scalasteward/core/vcs/github/TokenOut.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2018-2020 Scala Steward contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.scalasteward.core.vcs.github | ||
|
||
import io.circe.Decoder | ||
import io.circe.generic.semiauto.deriveDecoder | ||
|
||
case class TokenOut(token: String) | ||
object TokenOut { | ||
implicit val tokenDecoder: Decoder[TokenOut] = deriveDecoder | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1777