Skip to content

Commit

Permalink
#44 do pin with build ids to make it work in all branches
Browse files Browse the repository at this point in the history
  • Loading branch information
jk1 committed Jan 24, 2022
1 parent cc426f6 commit 3d3bba3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 40 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
}

jar {
dependsOn configurations.subproject
from { configurations.subproject.collect { zipTree(it) }}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ class RequestBuilder {
"/guestAuth/app/rest/builds/$locator/number"
}
}

static def GET_BUILD_ID = { BuildLocator locator, Boolean authenticate ->
if (authenticate) {
"/httpAuth/app/rest/builds/$locator/id"
} else {
"/guestAuth/app/rest/builds/$locator/id"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class BuildLocator {
String branch
String tag
String number
String id
Boolean noFilter

@Override
String toString() {
Expand All @@ -36,6 +38,12 @@ class BuildLocator {
if (number) {
builder.append(",number:${encode(number)}")
}
if (id) {
builder.append(",id:${encode(id)}")
}
if (noFilter){
builder.append(",defaultFilter:false")
}
return builder.toString()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jk1.tcdeps.processing

import com.github.jk1.tcdeps.client.RestClient
import com.github.jk1.tcdeps.model.BuildLocator
import com.github.jk1.tcdeps.model.DependencyDescriptor
import org.gradle.api.GradleException
Expand All @@ -19,9 +20,12 @@ class DependencyPinner implements DependencyProcessor {
config.setDefaultMessage("Pinned when building dependent build $project.name $project.version")
if (config.pinEnabled) {
dependencies.findAll { shouldPin(it) }.unique().each {
pinBuild(it)
if (config.tag){
tagBuild(it)
def buildId = resolveBuildId(it)
if (buildId) {
pinBuild(buildId, it)
if (config.tag){
tagBuild(buildId, it)
}
}
}
} else {
Expand All @@ -37,66 +41,82 @@ class DependencyPinner implements DependencyProcessor {
return !dep.version.changing && !config.excludes.contains(dep.buildTypeId)
}

private def pinBuild(DependencyDescriptor dependency) {
private def pinBuild(String buildId, DependencyDescriptor dependency) {
BuildLocator buildLocator = dependency.version.buildLocator
buildLocator.buildTypeId = dependency.buildTypeId
buildLocator.branch = dependency.branch
buildLocator.id = buildId
logger.debug("Pinning the build: $buildLocator")
// todo: rewrite this to avoid boilerplate
def response
try {
response = restClient.put {
def response = restClient.put {
baseUrl config.url
locator buildLocator
action PIN
body config.message
login credentials?.username
password credentials?.password
}
assertResponse(response, buildLocator)
} catch (Exception e) {
String message = "Unable to pin build: $buildLocator"
if (config.stopBuildOnFail) {
throw new GradleException(message, e)
} else {
logger.warn(message, e)
}
}
if (response && !response.isOk()) {
String message = "Unable to pin build: $buildLocator. Server response: HTTP $response.code \n $response.body"
if (config.stopBuildOnFail) {
throw new GradleException(message)
} else {
logger.warn(message)
}
handleException(e, buildLocator)
}
}

private def tagBuild(DependencyDescriptor dependency) {
private def tagBuild(String buildId, DependencyDescriptor dependency) {
BuildLocator buildLocator = dependency.version.buildLocator
buildLocator.buildTypeId = dependency.buildTypeId
buildLocator.branch = dependency.branch
buildLocator.id = buildId
logger.debug("Tagging the build: $buildLocator")
// todo: rewrite this to avoid boilerplate
def response
try {
response = restClient.post {
def response = restClient.post {
baseUrl config.url
locator buildLocator
action TAG
body config.tag
login credentials?.username
password credentials?.password
}
assertResponse(response, buildLocator)
} catch (Exception e) {
String message = "Unable to tag build: $buildLocator"
if (config.stopBuildOnFail) {
throw new GradleException(message, e)
} else {
logger.warn(message, e)
handleException(e, buildLocator)
}
}

private def resolveBuildId(DependencyDescriptor dependency) {
BuildLocator buildLocator = dependency.version.buildLocator
buildLocator.buildTypeId = dependency.buildTypeId
buildLocator.noFilter = true
try {
def response = restClient.get {
baseUrl config.url
locator buildLocator
action GET_BUILD_ID
body config.tag
login credentials?.username
password credentials?.password
}
assertResponse(response, buildLocator)
return response.body
} catch (Exception e) {
handleException(e, buildLocator)
return null
}
}

private def handleException(Exception e, BuildLocator buildLocator) {
if (e instanceof GradleException) {
throw e
}
String message = "Unable to pin/tag build: $buildLocator"
if (config.stopBuildOnFail) {
throw new GradleException(message, e)
} else {
logger.warn(message, e)
}
}

private def assertResponse(RestClient.Response response, BuildLocator buildLocator) {
if (response && !response.isOk()) {
String message = "Unable to tag build: $buildLocator. Server response: HTTP $response.code \n $response.body"
String message = "Unable to pin/tag build: $buildLocator. Server response: HTTP $response.code \n $response.body"
if (config.stopBuildOnFail) {
throw new GradleException(message)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,22 @@ class TeamCityRepoSpec extends Specification {
when:
project.repositories.teamcityServer {
url = "http://teamcity"
pin {
// pinning usually requires authentication
credentials {
username = "name"
password = "secret"
stopBuildOnFail = true // not mandatory, default to 'false'
message = "Pinned for MyCoolProject" // not mandatory
}
pin {
stopBuildOnFail = true
message = "Pinned for MyCoolProject"
}
}
def repo = project.repositories.findByName("TeamCity")

then:
repo.getUrl().toString() == "http://teamcity/guestAuth/repository/download"
repo.getUrl().toString() == "http://teamcity/httpAuth/repository/download"
repo.credentials.username == "name"
repo.credentials.password == "secret"
repo.pin.pinEnabled
repo.pin.username == "name"
repo.pin.password == "secret"
repo.pin.message == "Pinned for MyCoolProject"
repo.pin.stopBuildOnFail
}
Expand Down

0 comments on commit 3d3bba3

Please sign in to comment.