-
Notifications
You must be signed in to change notification settings - Fork 18
140 lines (111 loc) · 4.98 KB
/
dependencies.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
name: Dependency Upgrader
on:
schedule:
- cron: '1 0 * * *'
jobs:
upgrade:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
persist-credentials: false
fetch-depth: 0
- name: Install workflow dependencies
run: pip install GitPython
- name: Upgrade project dependencies
id: upgrade
shell: python
run: |
import os
import re
import json
import http.client
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import Dict, Any
from urllib.parse import urlparse
from git import Actor, Repo, IndexFile
config_file = ".github/workflows/.auto-upgrade.json"
update_file = "build.gradle"
author = Actor("github-actions[bot]", "github-actions[bot]@users.noreply.github.com")
def request_data(url: str, method: str='GET') -> str:
response = ""
try:
o = urlparse(url)
if o.scheme == 'https':
h = http.client.HTTPSConnection(o.hostname)
else:
h = http.client.HTTPConnection(o.hostname)
h.request(method, o.path)
response = h.getresponse()
if not (response.getcode() >= 200 & response.getcode() < 300):
raise http.client.HTTPException("Unexpected status code: {}".format(response.getcode()))
response = response.read().decode()
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
finally:
h.close()
return response
def upgrade(file: str, artifact: Dict[str, Any], index: IndexFile) -> str:
key = artifact['name']
data = request_data(artifact['index'])
latest = ET.fromstring(data).find('versioning/latest').text
print("::set-output name=ARTIFACT_ID::" + key + "--" + latest)
old_version_pattern = "def\s{}_version\s=\s\'([\d.]+)\'".format(key)
new_version = "def {}_version = '{}'".format(key, latest)
f = Path(file)
f.write_text(re.sub(old_version_pattern, new_version, f.read_text()))
changedFiles = [ item.a_path for item in index.diff(None) ]
if update_file in changedFiles:
index.add([update_file])
index.commit("build(deps): upgrade `{}` to {}".format(key, latest), author=author, committer=author)
if 'changelog' in artifact.keys():
changelog = " (<a href=\"{}\">changelog</a>)".format(artifact['changelog'].replace("{{VERSION}}", latest))
return "<li><code>{}</code> {}{}</li>".format(key, latest, changelog)
if __name__ == '__main__':
with open(config_file, 'r') as fd:
artifacts = json.load(fd)
print('Starting auto-update of versions:')
repo = Repo(os.getcwd())
artifactNotice = ""
for artifact in artifacts:
artifactNotice += upgrade(update_file, artifact, repo.index)
if artifactNotice == "":
print("Nothing updated.")
else:
body = "Bumps dependencies.<br /><details><summary>Upgrades</summary><ul>" + artifactNotice + "</ul></details><br />"
with open(os.path.join(os.getenv('RUNNER_TEMP', '/tmp'), "body.md"), "w") as fd:
fd.write(body)
print(body)
- name: Create Update Summary
id: summary
run: |
BODY="${RUNNER_TEMP}"/body.md
if [[ ! -f "$BODY" ]] ;then
echo "nothing to upgrade" ; exit 0
fi
echo ::set-output name=pr_body::"$(cat "${BODY}")"
- name: Push changes
uses: ad-m/[email protected]
with:
github_token: ${{ secrets.GH_PUSH_TOKEN }}
branch: ci/auto-upgrade
force: true
- name: Create Pull Request
id: cpr
if: steps.summary.outputs.pr_body != ''
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.GH_PUSH_TOKEN }}
author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
title: Automated Dependency Upgrade
body: ${{ steps.summary.outputs.pr_body }}
labels: dependencies
branch: ci/auto-upgrade
draft: true
- name: Check outputs
if: steps.summary.outputs.pr_body != ''
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"