Tools: [skip ci] implement method to get commit number, date and branch name from sha

This commit is contained in:
wmayer
2020-04-13 13:32:32 +02:00
parent 19d090fe77
commit 752272a8cb

View File

@@ -127,6 +127,75 @@ class BazaarControl(VersionControl):
def printInfo(self):
print("bazaar")
class DebianGitHub(VersionControl):
#https://gist.github.com/0penBrain/7be59a48aba778c955d992aa69e524c5
#https://gist.github.com/yershalom/a7c08f9441d1aadb13777bce4c7cdc3b
#https://github.community/t5/GitHub-API-Development-and/How-to-get-all-branches-which-contain-a-commit-from-SHA-using/td-p/25006
def extractInfo(self, srcdir, bindir):
try:
f = open(srcdir+"/debian/git-build-recipe.manifest")
except:
return False
# Read the first two lines
recipe = f.readline()
commit = f.readline()
f.close()
import requests
base_url = "https://api.github.com"
owner = "FreeCAD"
repo = "FreeCAD"
sha = commit[commit.rfind(':') + 1 : -1]
request_url = "{}/repos/{}/{}/commits?per_page=1&sha={}".format(base_url, owner, repo, sha)
commit_req = requests.get(request_url)
if not commit_req.ok:
return False
commit_date = commit_req.headers.get('last-modified')
self.hash = sha
try:
# Try to convert into the same format as GitControl
t = time.strptime(commit_date, "%a, %d %b %Y %H:%M:%S GMT")
self.date = ("%d/%02d/%02d %02d:%02d:%02d") % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
except:
self.date = commit_date
self.branch = "master"
# Try to determine the branch of the sha
# There is no function of the rest API of GH but with the url below we get HTML code
branch_url = "https://github.com/{}/{}/branch_commits/{}".format(owner, repo, sha)
branch_req = requests.get(branch_url)
if branch_req.ok:
html = branch_req.text
pattern = "<li class=\"branch\"><a href="
start = html.find(pattern) + len(pattern)
end = html.find("\n", start)
link = html[start:end]
start = link.find(">") + 1
end = link.find("<", start)
self.branch = link[start:end]
self.url = "git://github.com/{}/{}.git {}".format(owner, repo, self.branch)
link = commit_req.headers.get("link")
beg = link.rfind("&page=") + 6
end = link.rfind(">")
self.rev = link[beg:end] + " (GitHub)"
return True
def writeVersion(self, lines):
content = VersionControl.writeVersion(self, lines)
content.append('// Git relevant stuff\n')
content.append('#define FCRepositoryHash "%s"\n' % (self.hash))
content.append('#define FCRepositoryBranch "%s"\n' % (self.branch))
return content
def printInfo(self):
print("Debian/GitHub")
class GitControl(VersionControl):
#http://www.hermanradtke.com/blog/canonical-version-numbers-with-git/
#http://blog.marcingil.com/2011/11/creating-build-numbers-using-git-commits/
@@ -374,7 +443,7 @@ def main():
if o in ("-b", "--bindir"):
bindir = a
vcs=[GitControl(), BazaarControl(), Subversion(), MercurialControl(), DebianChangelog(), UnknownControl()]
vcs=[GitControl(), DebianGitHub(), BazaarControl(), Subversion(), MercurialControl(), DebianChangelog(), UnknownControl()]
for i in vcs:
if i.extractInfo(srcdir, bindir):
# Open the template file and the version file