added script
This commit is contained in:
commit
58f5f3a909
|
@ -0,0 +1,94 @@
|
|||
import urllib3
|
||||
import json
|
||||
import os
|
||||
import argparse
|
||||
|
||||
required_properties = [
|
||||
"tag_name", "tag_description",
|
||||
"release_name", "release_description",
|
||||
"build_path", "target",
|
||||
"attachment_name", "gitea_repo",
|
||||
"repo_name", "repo_user"]
|
||||
|
||||
def parse_props(path:str) ->dict:
|
||||
if not os.path.exists(path):
|
||||
raise Exception(f"property path does not exists: {path}")
|
||||
props = {}
|
||||
with open(path) as f:
|
||||
for line in f:
|
||||
if line.strip().startswith("#"):
|
||||
continue
|
||||
p = line.strip().split("=")
|
||||
props[p[0].strip()] = p[1].strip()
|
||||
for item in required_properties:
|
||||
if item not in props:
|
||||
raise Exception("Missing required property on your property file: {item}")
|
||||
return props
|
||||
|
||||
|
||||
class GiteaRepoBridge:
|
||||
|
||||
def __init__(self, token, giteaURL:str, repository_name:str, repository_user:str) -> None:
|
||||
self.token = token
|
||||
self.giteaURL = giteaURL
|
||||
self.repository_user= repository_user
|
||||
self.repository_name = repository_name
|
||||
self.headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}
|
||||
|
||||
|
||||
def create_tag(self, tag_name:str, tag_description:str, target:str):
|
||||
body = { "message": tag_description, "tag_name": tag_name, "target": target }
|
||||
url = f'{self.giteaURL}/repos/{self.repository_user}/{self.repository_name}/tags'
|
||||
http = urllib3.PoolManager()
|
||||
response = http.request("POST", url=url, headers=self.headers, body=json.dumps(body))
|
||||
st = response.status
|
||||
if st >=200 and st<300:
|
||||
r = json.loads(response.data)
|
||||
return (r['commit']['sha'], r['name'])
|
||||
raise Exception(f"Error to create Tags status_code: {st} error message {response.data}")
|
||||
|
||||
|
||||
def create_release(self, release_name:str, release_descritpion:str, tag_name:str, tag_sha:str):
|
||||
url = f'{self.giteaURL}/repos/{self.repository_user}/{self.repository_name}/releases'
|
||||
data = { "body": release_descritpion, "draft": False, "name": release_name, "prerelease": False, "tag_name": tag_name, "target_commitish": tag_sha }
|
||||
http = urllib3.PoolManager()
|
||||
response = http.request("POST", url=url, headers=self.headers, body=json.dumps(data))
|
||||
st = response.status
|
||||
if st >=200 and st<300:
|
||||
r = json.loads(response.data)
|
||||
return (r['id'], r['name'])
|
||||
raise Exception(f"Error to create Tags status_code: {st} error message {response.data}")
|
||||
|
||||
def create_attachment(self, attachment_path:str, attachment_name:str, release_id:str, release_name:str):
|
||||
http = urllib3.PoolManager()
|
||||
os.system(f"tar -czf /tmp/attachment.tar.gz -P {attachment_path}")
|
||||
with open("/tmp/attachment.tar.gz", 'rb') as f:
|
||||
file_data = f.read()
|
||||
header = { 'Authorization': f'token {self.token}' }
|
||||
url = f'{self.giteaURL}/repos/{self.repository_user}/{self.repository_name}/releases/{release_id}/assets?name={attachment_name}.tar.gz'
|
||||
response = http.request("POST", url, headers=header, fields= {"attachment": (f"{attachment_name}.tar.gz", file_data)})
|
||||
st = response.status
|
||||
if st >=200 and st<300:
|
||||
r = json.loads(response.data)
|
||||
return (r['id'], r['name'])
|
||||
raise Exception(f"Error to upload attachement status_code: {st} error message {response.data}")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(
|
||||
description="This scripts help you to create a release on gitea")
|
||||
parser.add_argument("-p","--property", help='a path of a property file including all the properties needed for this script',required=True)
|
||||
parser.add_argument("-t","--token", help='a token api generated by your gitea repos',required=True)
|
||||
pargs = parser.parse_args()
|
||||
props = parse_props(pargs.property)
|
||||
token =pargs.token
|
||||
|
||||
b = GiteaRepoBridge(token=token, giteaURL=props['gitea_repo'], repository_name=props["repo_name"],repository_user=props["repo_user"])
|
||||
target_sha, target_name = b.create_tag(props["tag_name"], props["tag_description"], props["target"] )
|
||||
print(f"Created tag : Name:{target_name}, SHA:{target_sha}")
|
||||
release_id, release_name = b.create_release(release_name=props['release_name'], release_descritpion=props['release_description']
|
||||
,tag_name=target_name, tag_sha=target_sha)
|
||||
print(f"Created Release : Name:{release_name}, ID:{release_id}")
|
||||
b.create_attachment(attachment_path=props['build_path'], attachment_name=props['attachment_name'],release_id=release_id, release_name=release_name)
|
||||
print(f"Uploaded attachment on release {release_id}")
|
|
@ -0,0 +1,52 @@
|
|||
# Gitea Auto release
|
||||
_author: Sandro La Bruzzo_
|
||||
|
||||
|
||||
##Description
|
||||
This python script automatically creates a release on a Gitea repository using the [swagger api](https://docs.gitea.io/en-us/api-usage/) provided by Gitea.
|
||||
It requires the `token API` you can generate through the gitea user interface.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
` pip install -r requirements.txt`
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
`python gitea_release.py --property gitea.properties_PATH -token ATOKEN`
|
||||
|
||||
|
||||
It requires a property file containing all the info needed to generate the Release and attach a `tar.gz` of your build folder to the Release.
|
||||
|
||||
below an example of the property file (that you can include inside your git repository)
|
||||
|
||||
## Release Property File
|
||||
|
||||
```
|
||||
#The name of the tag
|
||||
tag_name= TAG_3.0
|
||||
# A description of the tag
|
||||
tag_description= Tag 3.0
|
||||
#The name of your branch you want to release
|
||||
target=main
|
||||
#The release name
|
||||
release_name= Release_3.0
|
||||
#The release description
|
||||
release_description= This is a release 3.0
|
||||
#The path of your build folder (this folder will be compressed in a tar.gz file)
|
||||
build_path=/Users/A/PATH/OF/BUILD
|
||||
#The name of the attachment
|
||||
attachment_name= release
|
||||
# The name of your gitea swagger api repo
|
||||
gitea_repo=https://code-repo.d4science.org/api/v1
|
||||
# The repository name you want to release
|
||||
repo_name = openaire-graph-docs
|
||||
#The user from whom the token was generated
|
||||
repo_user=sandro.labruzzo
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Enjoy
|
|
@ -0,0 +1 @@
|
|||
urllib3==1.26.13
|
Loading…
Reference in New Issue