Post

3 followers Follow
0
Avatar

How to get the last git tag name to use into triggers?

Hi,

I would like to know if there is a way to get last git tag name to use into a trigger?
I am looking for GitClient class, but I am not able to find it on the web.

I would like to use the right method name (if exists) to get last git tag name.

As in getting the commitId :

triggerState = "%s" % client.getLatestRevision()
commitId = triggerState

Thank you.

Edgar

Edgar Fernandes

Please sign in to leave a comment.

11 comments

0
Avatar

Hi,

Unfortunately there is no way to retrieve back the tag name using the GitClient class.

It should be possible, though, using the API of your git provider. In case you are using Github this can be achieved as:

import json
from xlrelease.HttpRequest import HttpRequest

request = HttpRequest({"url": "https://api.github.com"}, yourUsername, yourPassword)
response = request.get('/repos/your-repository-name/tags', contentType='application/json')
tags = json.loads(response.response)

tagName = tags[0]['name']
# do something with the tag name

Angel Gavalda

 

 

Angel Gavalda 0 votes
0
Avatar

Hi Angel,

Thank you for your answer.
Unfortunately I am not using GitHub, but an internal git system without external API :(

Do you know if it's possible to use git commands?

ex: git tag

Thank you.

Edgar

Edgar Fernandes 0 votes
1
Avatar

 

Hi Edgar

There is no way to execute git commands unless XL-Release is in the same machine as git repository and you could execute commands using jython, something like:

import subprocess
subprocess.call(["git", "describe"])

Other option, internally the GitClient class is using JGit (https://eclipse.org/jgit/), so from your trigger script you could have access to the tags with this:

from org.eclipse.jgit.api import Git
from org.eclipse.jgit.transport import UsernamePasswordCredentialsProvider

lsRemoteCommand = Git.lsRemoteRepository().setRemote("...git-repository-root...").setTags(True).setHeads(False)
lsRemoteCommand.setCredentialsProvider(UsernamePasswordCredentialsProvider("...username...","...password..."))

print lsRemoteCommand.call()

I don't think the list is ordered though

 

 

Angel Gavalda 1 vote
0
Avatar

Hi Angel,

Thank you for your answer.I tested your proposal.

In my GIT repository I have 2 tags :
- v1
- v2

Here is the result of the command :

[Ref[refs/tags/v2=8c13717f9956e2306c08c90a3ca5717248d420ec], Ref[refs/tags/v1=3278961fced4b88e940bf8f7becf2318259e3e8f]]

I am new in python...so, do you have a magic command to extract my 2 tags : v1 and v2?

Thanks a lot.

Edgar

Edgar Fernandes 0 votes
0
Avatar

Hi Hes,

Thank you for your answer.

I wrote this script that displays the expected result :

import re    

tags = "[Ref[refs/tags/v2=8c13717f9956e2306c08c90a3ca5717248d420ec], Ref[refs/tags/v1=3278961fced4b88e940bf8f7becf2318259e3e8f]]"
print re.search('\[Ref\[refs\/tags\/(.*?)\=[a-f0-9]*\]', tags).group(1)


Result :

v2


Edgar

Edgar Fernandes 0 votes
1
Avatar

Hi,

FYI I updated my script because sort was not well ordered.

This is my new script :

from org.eclipse.jgit.api import Git
from org.eclipse.jgit.transport import UsernamePasswordCredentialsProvider
import re

lsRemoteCommand = Git.lsRemoteRepository().setRemote(gitRepository['url']).setTags(True)
lsRemoteCommand.setCredentialsProvider(UsernamePasswordCredentialsProvider(credentials['username'],credentials['password']))
tags = lsRemoteCommand.call()

data = []
for tag in tags:
   tag = str(tag)
   tag = re.sub('\=[a-f0-9]*\]','', re.sub('Ref\[refs\/tags\/',' ',tag))
   data.extend([tag])

data.sort(key=lambda s: map(int, s.replace('v','',1).replace('-','',1).split('.')))
lastGitTag = data[-1]

The goal of this script is to get all tags on a GIT repository, to sort them and to store the last one on a variable...to be used on a GIT trigger for example.

I added it on my ext/git/Poll.py script.

There are my tags retrieved :

[Ref[refs/tags/v1.0.4=919a3788c840e9bee665c1890e3fb384f9423e38], Ref[refs/tags/v1.0.5=fabf0f17c8f7b4d0df0df60982cdea0a6a2f8953], Ref[refs/tags/v1.0.2=94e692e141d3bc3ff209a3bc1ba2ba344ec19d2b], Ref[refs/tags/v1.0.3=f3462a9b22ee6576498ce00e7212a1daac33c3d5], Ref[refs/tags/v1.0.8=05c21123b7a637ca1de5604cf5bbf9454bbf0a44], Ref[refs/tags/v1.0.9=0c7c6a1099d3a0225b8dc4dc081523c71bbd7e5f], Ref[refs/tags/v1.0.6=dd0ed8e37b1d64c641429b2430ad1c6aec2b4254], Ref[refs/tags/v1.0.7=58b900d7a84aad22cfbbedb2b66760c9ce799afc], Ref[refs/tags/v1.0.4-3=d022fca449bc701ccf03faa7fe325b49173061ec], Ref[refs/tags/v1.0.0=e04bb106105ce21c5dd5fbb7478b545dc76a66cd], Ref[refs/tags/v1.0.1=87b87a09398b1688e89c66a2fc00cd426b0f5d61], Ref[refs/tags/v1.1.0=a54e8467399fe1eced5cee9a6496160ba08c3c25], Ref[refs/tags/v2.0.1=c2fc922fd425078480565971dee8184584f2f10d], Ref[refs/tags/v2.0.2=6a6d2c3b8dcd3f4e1047cb45866fe52e548f9a4f], Ref[refs/tags/v2.0.3=f500525610aeccfd7b0dff94a584fa39cfc69a23], Ref[refs/tags/v1.0.4-1=6332dbea1e02653b4a6c0949fbac9951e29a872d], Ref[refs/tags/v1.0.4-2=23bb84b02662cf1630534c787c4c14c9b2885db6], Ref[refs/tags/v1.0.10=cd3a3e33352f847f3f05b88a4694801fd3b0c370], Ref[refs/tags/v2.0.0=347a33b01c40e777d6f8ad94b55c7c31919895c6]]

There are my tags parsed and sorted (the last one is the latest) :

v1.0.0
v1.0.1
v1.0.2
v1.0.3
v1.0.4
v1.0.5
v1.0.6
v1.0.7
v1.0.8
v1.0.9
v1.0.10
v1.0.4-1
v1.0.4-2
v1.0.4-3
v1.1.0
v2.0.0
v2.0.1
v2.0.2
v2.0.3

Edgar

Edgar Fernandes 1 vote
0
Avatar

You're welcome!

In fact, I updated it again, to be compatible with Our Orange Company version naming.

GoRoCo
=> v1.2.10 = G01R02C10

So, this is my new code :

from org.eclipse.jgit.api import Git
from org.eclipse.jgit.transport import UsernamePasswordCredentialsProvider
import re


# Get GIT tags
lsRemoteCommand = Git.lsRemoteRepository().setRemote(gitRepository['url']).setTags(True)
lsRemoteCommand.setCredentialsProvider(UsernamePasswordCredentialsProvider(credentials['username'],credentials['password']))
tags = lsRemoteCommand.call()

# Parse tags and store them into a list
data = []
for tag in tags:
    tag = str(tag)
    tag = re.sub('\=[a-f0-9]*\]','', re.sub('Ref\[refs\/tags\/',' ',tag))
    data.extend([tag])

# Sort function
_nsre = re.compile('([0-9]+)')
def natural_sort_key(s):
return [int(text) if text.isdigit() else text.lower() for text in re.split(_nsre, s)]

# Sort tags
data.sort(key=natural_sort_key)

# Variable to use in trigger
lastGitTag = data[-1]

So, now we can sort any tags.

Edgar Fernandes 0 votes