2018-10-23 22:24:13 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import jwt, time, urllib.request, json, datetime, argparse, sys, textwrap
|
|
|
|
from github import Github
|
|
|
|
from colorama import Fore, Style
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Create PR to update nixpkgs fork')
|
|
|
|
parser.add_argument('--private-key')
|
|
|
|
parser.add_argument('--app-id')
|
|
|
|
parser.add_argument('--installation-id')
|
|
|
|
parser.add_argument('--repo')
|
|
|
|
parser.add_argument('--cache-dir')
|
|
|
|
args = vars(parser.parse_args())
|
|
|
|
|
|
|
|
channel_req = urllib.request.Request(url='https://nixos.org/channels/nixos-18.09-small/git-revision')
|
|
|
|
latest_commit = urllib.request.urlopen(channel_req).read().decode('utf-8')
|
|
|
|
try:
|
|
|
|
previous_commit = open(args['cache_dir'] + '/git-revision', 'r').read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
open(args['cache_dir'] + '/git-revision', 'w').write(latest_commit)
|
|
|
|
print("Premier lancement, le hash du dernier commit à été sauvegardé")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
print("Dernier commit : " + latest_commit)
|
|
|
|
print("Commit précédent : " + previous_commit)
|
|
|
|
|
|
|
|
if latest_commit != previous_commit:
|
|
|
|
bearer_token = jwt.encode({
|
|
|
|
'iat': int(time.time()),
|
|
|
|
'exp': int(time.time()) + (10 * 60),
|
|
|
|
'iss': args['app_id']
|
|
|
|
},
|
|
|
|
open(args['private_key'],"r").read(),
|
|
|
|
algorithm='RS256')
|
|
|
|
|
|
|
|
req = urllib.request.Request(url='https://api.github.com/app/installations/' +
|
|
|
|
args['installation_id'] +
|
|
|
|
'/access_tokens',
|
|
|
|
method='POST')
|
|
|
|
|
|
|
|
req.add_header('Authorization', 'Bearer ' + bearer_token.decode('utf-8'))
|
|
|
|
req.add_header('Accept', 'application/vnd.github.machine-man-preview+json')
|
|
|
|
|
|
|
|
token = json.loads(urllib.request.urlopen(req).read().decode('utf-8'))['token']
|
|
|
|
|
|
|
|
g = Github(token)
|
|
|
|
repo = g.get_repo(args['repo'])
|
|
|
|
|
|
|
|
branch = "upgrade-" + datetime.datetime.now().strftime('%Y-%m-%d') + '-' + latest_commit[:11];
|
|
|
|
|
|
|
|
repo.create_git_ref('refs/heads/' + branch, latest_commit)
|
|
|
|
|
|
|
|
pr_message = textwrap.dedent("""\
|
|
|
|
### Pull request automatique
|
|
|
|
### Avancement mise à jour
|
|
|
|
- [ ] Fusionner la branche
|
|
|
|
- [ ] Mettre à jour le repo local
|
|
|
|
- [ ] Exécuter `nixos-rebuild -I nixpkgs=https://github.com/nyanloutre/nixpkgs/archive/""" + latest_commit + """.tar.gz switch`
|
|
|
|
""")
|
|
|
|
|
|
|
|
pr = repo.create_pull(title=branch, body=pr_message, base='nixos-18.09', head=branch)
|
|
|
|
|
|
|
|
print("Pull request numéro " + pr.id + " créée")
|
|
|
|
print("URL : " + pr.html_url)
|
|
|
|
print("État : " + ((Fore.GREEN + "Fusionnable") if pr.mergeable else (Fore.RED + "Conflit")) + Style.RESET_ALL)
|
2018-10-23 22:27:38 +02:00
|
|
|
|
|
|
|
open(args['cache_dir'] + '/git-revision', 'w').write(latest_commit)
|
2018-10-23 22:24:13 +02:00
|
|
|
else:
|
|
|
|
print(Fore.GREEN + "Aucun changement détecté" + Style.RESET_ALL)
|