Skip to content

Commit

Permalink
Merge pull request #19 from stakater/revamp-app-release
Browse files Browse the repository at this point in the history
Revamp app release
  • Loading branch information
kahootali authored Jul 24, 2019
2 parents 39b971d + 522eaf7 commit b6d9a23
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 20 deletions.
15 changes: 8 additions & 7 deletions src/io/stakater/StakaterCommands.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def formatBitbucketUrl(url) {
return url.trim()
}

def postPRComment(comment, pr, project, provider, token) {
def postPRComment(comment, pr, project, provider, token, user) {
switch(provider){
case "github":
postPRCommentToGithub(comment, pr, project, token)
Expand All @@ -246,7 +246,7 @@ def postPRComment(comment, pr, project, provider, token) {
break

case "bitbucket":
postPRCommentToBitbucket(comment, pr, project, token)
postPRCommentToBitbucket(comment, pr, project, token, user)
default:
error "${provider} is not supported"
break
Expand Down Expand Up @@ -299,21 +299,22 @@ def getGitLabMergeRequestsByBranchName(project, branchName, token){
}
}

def postPRCommentToBitbucket(comment, pr, project, token) {
def postPRCommentToBitbucket(comment, pr, project, token, user) {
def changeAuthor = env.CHANGE_AUTHOR
if (!changeAuthor){
echo "no commit author found so cannot comment on PR"
return
echo "no commit author found so commenting without tagging author"
} else {
comment = "@${changeAuthor} " + comment
}

if (!pr){
echo "no pull request number found so cannot comment on PR"
return
}

comment = "@${changeAuthor} " + comment
echo "adding ${comment} to https://api.bitbucket.org/2.0/repositories/${project}/pullrequests/${pr}/comments"

sh "curl -u stakater-user:${token} -X POST -H \"Content-Type: application/json\" https://api.bitbucket.org/2.0/repositories/${project}/pullrequests/${pr}/comments -d '{\"content\":{\"raw\":\"${comment}\"}}'"
sh "curl -u ${user}:${token} -X POST -H \"Content-Type: application/json\" https://api.bitbucket.org/2.0/repositories/${project}/pullrequests/${pr}/comments -d '{\"content\":{\"raw\":\"${comment}\"}}'"
}

def postPRCommentToGithub(comment, pr, project, githubToken) {
Expand Down
25 changes: 25 additions & 0 deletions src/io/stakater/app/App.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,29 @@ def build(String appType, String version, String goal) {
}
}

def getImageVersion(String repoUrl, String imagePrefix, String prNumber, String buildNumber) {
def utils = new io.fabric8.Utils()
def branchName = utils.getBranch()
def git = new io.stakater.vc.Git()
def imageVersion = ''

// For CD
if (branchName.equalsIgnoreCase("master")) {
sh "stk generate version > commandResult"
def version = readFile('commandResult').trim()
sh "rm commandResult .VERSION"
version = 'v' + version
imageVersion = imagePrefix + version
}
// For CI
else {
if(imagePrefix == "") {
imagePrefix = "0.0.0"
}
imageVersion = imagePrefix + "-" + prNumber + '-' + buildNumber + '-SNAPSHOT'
}

return imageVersion
}

return this
8 changes: 4 additions & 4 deletions src/io/stakater/notifications/NotificationManager.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def sendError(Map notificationConfig, Map gitConfig, String buildNumber, String
def git = new io.stakater.vc.Git()

if(gitConfig.cloneUsingToken) {
git.addCommentToPullRequest(commentMessage, gitConfig.tokenSecret)
git.addCommentToPullRequest(commentMessage, gitConfig.tokenSecret, gitConfig.user)
} else {
git.addCommentToPullRequest(commentMessage)
git.addCommentToPullRequest(commentMessage, gitConfig.user)
}
}

Expand All @@ -31,9 +31,9 @@ def sendSuccess(Map notificationConfig, Map gitConfig, String dockerImage, Strin
def git = new io.stakater.vc.Git()

if(gitConfig.cloneUsingToken){
git.addCommentToPullRequest(commentMessage, gitConfig.tokenSecret)
git.addCommentToPullRequest(commentMessage, gitConfig.tokenSecret, gitConfig.user)
} else {
git.addCommentToPullRequest(commentMessage)
git.addCommentToPullRequest(commentMessage, gitConfig.user)
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/io/stakater/vc/Git.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def checkoutRepoUsingToken(String username, String tokenSecretName, String repoU
"""
}

def addCommentToPullRequest(String message) {
def addCommentToPullRequest(String message, String user) {
def flow = new StakaterCommands()
def url = flow.getScmPushUrl()
def provider = flow.getProvider(url)
Expand All @@ -92,18 +92,18 @@ def addCommentToPullRequest(String message) {
def providerToken = flow.getProviderToken(provider)
switch(provider) {
case "github":
flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken)
flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken, user)
break
case "gitlab":
def result = flow.getGitLabMergeRequestsByBranchName(project, env.BRANCH_NAME == null ? env.REPO_CLONE_BRANCH : env.BRANCH_NAME, providerToken)
result.each{value ->
def prMessage = "@${value.author.username} " + message
echo "Commenting on MR with id: ${value.iid}, and message: ${prMessage}"
flow.postPRComment(prMessage, value.iid, project, provider, providerToken)
flow.postPRComment(prMessage, value.iid, project, provider, providerToken, user)
}
break
case "bitbucket":
def result = flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken)
def result = flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken, user)
break

default:
Expand All @@ -112,7 +112,7 @@ def addCommentToPullRequest(String message) {
}
}
//Overloaded function to send the token if already got that
def addCommentToPullRequest(String message, String token) {
def addCommentToPullRequest(String message, String token, String user) {
def flow = new StakaterCommands()
def url = flow.getScmPushUrl()
def provider = flow.getProvider(url)
Expand All @@ -122,18 +122,18 @@ def addCommentToPullRequest(String message, String token) {
def providerToken = token
switch(provider) {
case "github":
flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken)
flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken, user)
break
case "gitlab":
def result = flow.getGitLabMergeRequestsByBranchName(project, env.BRANCH_NAME == null ? env.REPO_CLONE_BRANCH : env.BRANCH_NAME, providerToken)
result.each{value ->
def prMessage = "@${value.author.username} " + message
echo "Commenting on MR with id: ${value.iid}, and message: ${prMessage}"
flow.postPRComment(prMessage, value.iid, project, provider, providerToken)
flow.postPRComment(prMessage, value.iid, project, provider, providerToken, user)
}
break
case "bitbucket":
def result = flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken)
def result = flow.postPRComment(message, env.CHANGE_ID, "${env.REPO_OWNER}/${env.REPO_NAME}", provider, providerToken, user)
break

default:
Expand Down
2 changes: 1 addition & 1 deletion vars/releaseApplication.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def call(body) {

stage('Create Version') {
dockerImage = "${packageConfig.dockerRepositoryURL}/${baseConfig.repoOwner.toLowerCase()}/${baseConfig.imageName}"
version = stakaterCommands.getImageVersionForNodeCiAndCd(repoUrl, baseConfig.imagePrefix, repoBranch, "${env.BUILD_NUMBER}")
version = app.getImageVersion(repoUrl, baseConfig.imagePrefix, repoBranch, "${env.BUILD_NUMBER}")
echo "Version: ${version}"
}

Expand Down

0 comments on commit b6d9a23

Please sign in to comment.