Skip to content

Instantly share code, notes, and snippets.

@erral
Created May 6, 2021 06:17
Show Gist options
  • Save erral/c24fcc7063566ae7d5ea3204aefc06f6 to your computer and use it in GitHub Desktop.
Save erral/c24fcc7063566ae7d5ea3204aefc06f6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3.7
from git import Actor
from git import Repo
import datetime
import os
import time
import json
import requests
EITBAPI_PATH = "/home/erral/dev/eitbapi"
def create_pull_request(
project_name, repo_name, title, description, head_branch, base_branch, git_token
):
"""Creates the pull request for the head_branch against the base_branch"""
git_pulls_api = "https://api.github.com/repos/{0}/{1}/pulls".format(
project_name, repo_name
)
headers = {
"Accept": "application/vnd.github.v3+json",
"Authorization": "token {0}".format(git_token),
"Content-Type": "application/json",
}
payload = {
"title": title,
"body": description,
"head": head_branch,
"base": base_branch,
}
r = requests.post(git_pulls_api, headers=headers, data=json.dumps(payload))
if not r.ok:
print("Request Failed: {0}".format(r.text))
def main():
date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
timestamp = int(time.time())
user_name = "Mikel Larreategi"
user_mail = "[email protected]"
message = f"Update program list {date}"
branch_name = f"create-pull-request-{timestamp}"
author = Actor(user_name, user_mail)
# Update current version of the repo
repo = Repo(EITBAPI_PATH)
repo.git.pull("origin", "master")
# Rebuild the cache
os.system(f"{EITBAPI_PATH}/bin/python {EITBAPI_PATH}/rebuild_cache.py")
# Commit the changes to a branch and push it
repo.git.checkout("HEAD", b=branch_name)
repo.index.add("cache.json")
repo.index.add("radio-cache.json")
repo.index.commit(message, author=author)
repo.index.write()
repo.git.push("origin", branch_name)
repo.git.checkout("master")
create_pull_request(
"erral", # project_name
"eitbapi", # repo_name
"This is an automated PR to update the cache", # title
"This PR was created automatically", # description
branch_name, # head_branch
"master", # base_branch
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", # git_token
)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment