Skip to content

Commit e546749

Browse files
authored
Merge pull request #48 from codacy/new-endpoint-to-add-key-on-repo
Add new endpoint to add key on repository
2 parents cd902f5 + 7bad3ce commit e546749

File tree

7 files changed

+139
-21
lines changed

7 files changed

+139
-21
lines changed

.circleci/config.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# CircleCI 2.0 configuration file
2+
version: 2
3+
4+
# Re-usable blocks to reduce boilerplate in job definitions.
5+
references:
6+
7+
sbt_jvm_defaults: &sbt_jvm_defaults
8+
JAVA_OPTS: -Xmx3200m
9+
10+
default_sbt_job: &default_sbt_job
11+
machine: true
12+
working_directory: ~/workdir
13+
environment:
14+
<<: *sbt_jvm_defaults
15+
16+
default_aws_job: &default_aws_job
17+
docker:
18+
- image: codacy/ci-aws:3.0.2
19+
working_directory: ~/workdir/.aws
20+
21+
setup_aws_credentials: &setup_aws_credentials
22+
run:
23+
name: Setup AWS Credentials
24+
command: |
25+
mkdir -p ~/.aws && touch ~/.aws/credentials
26+
cat >> ~/.aws/credentials << EOF
27+
[default]
28+
aws_access_key_id=$ACCESS_KEY_ID
29+
aws_secret_access_key=$SECRET_ACCESS_KEY
30+
[integration]
31+
source_profile = default
32+
role_arn = arn:aws:iam::$AWS_ACCOUNT_ID_INTEGRATION:role/$CONTINUOUS_DELIVERY_ROLE
33+
region=eu-west-1
34+
35+
restore_sbt_cache: &restore_sbt_cache
36+
restore_cache:
37+
keys:
38+
- sbt-cache-{{ .Branch }}-{{ checksum "build.sbt" }}-{{ .Environment.CIRCLE_SHA1 }}
39+
- sbt-cache-{{ .Branch }}-{{ checksum "build.sbt" }}
40+
- sbt-cache-{{ .Branch }}
41+
- sbt-cache
42+
43+
clean_sbt_cache: &clean_sbt_cache
44+
run:
45+
name: CleanCache
46+
command: |
47+
find $HOME/.sbt -name "*.lock" | xargs rm | true
48+
find $HOME/.ivy2 -name "ivydata-*.properties" | xargs rm | true
49+
50+
save_sbt_cache: &save_sbt_cache
51+
save_cache:
52+
key: sbt-cache-{{ .Branch }}-{{ checksum "build.sbt" }}-{{ .Environment.CIRCLE_SHA1 }}
53+
paths:
54+
- "~/.ivy2/cache"
55+
- "~/.sbt"
56+
- "~/.m2"
57+
58+
jobs:
59+
test:
60+
<<: *default_sbt_job
61+
steps:
62+
- attach_workspace:
63+
at: ~/
64+
- *restore_sbt_cache
65+
- run:
66+
name: Test
67+
command: |
68+
sbt +test
69+
70+
workflows:
71+
version: 2
72+
compile_deploy:
73+
jobs:
74+
- test:
75+
context: CodacyAWS
76+
requires:

circle.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.1.6
1+
sbt.version=1.2.8

project/plugins.sbt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import sbt._
22

3-
resolvers ++= Seq(
4-
DefaultMavenRepository,
5-
"Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
6-
"Bintray Repository" at "https://dl.bintray.com/sbt/ivy-releases/",
7-
"Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/",
8-
Classpaths.typesafeReleases,
9-
Classpaths.sbtPluginReleases
10-
)
11-
123
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.3")
134

145
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.1")
156

16-
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.2.0")
7+
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.2.0")
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codacy.client.bitbucket.v2
2+
3+
import play.api.libs.functional.syntax._
4+
import play.api.libs.json._
5+
6+
case class DeployKey(id: Long, key: String, label: String)
7+
8+
object DeployKey {
9+
implicit val reader: Reads[DeployKey] = (
10+
(__ \ "id").read[Long] and
11+
(__ \ "key").read[String] and
12+
(__ \ "label").read[String]
13+
)(DeployKey.apply _)
14+
}

src/main/scala/com/codacy/client/bitbucket/v2/service/RepositoryServices.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.codacy.client.bitbucket.v2.service
22

3-
import com.codacy.client.bitbucket.v2.Repository
3+
import com.codacy.client.bitbucket.v2.{DeployKey, Repository}
44
import com.codacy.client.bitbucket.client.{BitbucketClient, Request, RequestResponse}
5+
import play.api.libs.json.Json
56

67
class RepositoryServices(client: BitbucketClient) {
78

@@ -13,4 +14,14 @@ class RepositoryServices(client: BitbucketClient) {
1314
client.executePaginated(Request(s"https://bitbucket.org/api/2.0/repositories/$username", classOf[Seq[Repository]]))
1415
}
1516

17+
def createKey(username: String, repo: String, key: String, label: String = "Codacy Key"): RequestResponse[DeployKey] = {
18+
val url = s"https://bitbucket.org/api/2.0/repositories/$username/$repo/deploy-keys"
19+
20+
val values = Json.obj(
21+
"key" -> key,
22+
"label" -> label
23+
)
24+
25+
client.postJson(Request(url, classOf[DeployKey]), values)
26+
}
1627
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.codacy.client.bitbucket.v2
2+
import org.scalatest.{Matchers, _}
3+
import play.api.libs.json.Json
4+
5+
class RepositorySpecs extends FlatSpec with Matchers {
6+
7+
"RepositorySpecs" should "successfully parse DeployKey" in {
8+
val input = """
9+
|{
10+
| "id": 123,
11+
| "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAK/b1cHHDr/TEV1JGQl+WjCwStKG6Bhrv0rFpEsYlyTBm1fzN0VOJJYn4ZOPCPJwqse6fGbXntEs+BbXiptR+++HycVgl65TMR0b5ul5AgwrVdZdT7qjCOCgaSV74/9xlHDK8oqgGnfA7ZoBBU+qpVyaloSjBdJfLtPY/xqj4yHnXKYzrtn/uFc4Kp9Tb7PUg9Io3qohSTGJGVHnsVblq/rToJG7L5xIo0OxK0SJSQ5vuId93ZuFZrCNMXj8JDHZeSEtjJzpRCBEXHxpOPhAcbm4MzULgkFHhAVgp4JbkrT99/wpvZ7r9AdkTg7HGqL3rlaDrEcWfL7Lu6TnhBdq5",
12+
| "label": "mydeploykey",
13+
| "type": "deploy_key",
14+
| "created_on": "2018-08-15T23:50:59.993890+00:00",
15+
| "repository": {
16+
| "full_name": "mleu/test",
17+
| "name": "test",
18+
| "type": "repository",
19+
| "uuid": "{85d08b4e-571d-44e9-a507-fa476535aa98}"
20+
| },
21+
| "links":{
22+
| "self":{
23+
| "href": "https://api.bitbucket.org/2.0/repositories/mleu/test/deploy-keys/123"
24+
| }
25+
| },
26+
| "last_used": null,
27+
| "comment": "mleu@C02W454JHTD8"
28+
|}
29+
""".stripMargin
30+
val json = Json.parse(input)
31+
val value = json.validate[DeployKey]
32+
33+
value.fold(e => fail(s"$e"), r => r.id shouldBe 123)
34+
}
35+
}

0 commit comments

Comments
 (0)