-
Notifications
You must be signed in to change notification settings - Fork 47
196 lines (179 loc) · 7.38 KB
/
auto-create-repo.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
name: auto create repo
on:
pull_request_target:
types: [opened, synchronize, closed]
paths:
- "repos.yml"
env:
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
jobs:
create_repo:
if: github.event.pull_request.merged
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- name: use node@18
uses: actions/setup-node@v3
with:
node-version: 18
- name: install depends for load scripts
run: |
npm install @octokit/rest@19.0.13
npm install @octokit/auth-app@6.1.1
- name: Get token using github-script
id: get-token
uses: actions/github-script@v6
with:
script: |
global["fetch"] = fetch
const { Octokit } = require("@octokit/rest");
const { createAppAuth } = require("@octokit/auth-app");
const appOctokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: process.env.APP_ID,
privateKey: process.env.APP_PRIVATE_KEY,
}
});
const app_installation = await appOctokit.rest.apps.getRepoInstallation({
owner: context.payload.organization.login,
repo: context.payload.repository.name
});
const { token } = await appOctokit.auth({
type: "installation",
installationId: app_installation.data.id
});
core.setOutput('app_token', token)
- name: init osc
run: |
sudo apt update
sudo apt install osc
mkdir ~/.config/osc
echo "${{ secrets.OSCRC }}" > ~/.config/osc/oscrc
- name: create_repo
id: create_repo
shell: python
env:
GITHUB_TOKEN: ${{ steps.get-token.outputs.app_token }}
CHANGE_ID: ${{ github.event.pull_request.number }}
ORG: ${{ github.repository_owner }}
run: |
import requests
import yaml
import os
import logging
header = {
"Accept": "application/vnd.github+json",
"Authorization":"Bearer " + os.environ.get("GITHUB_TOKEN")
}
header1 = {
"Accept": "application/vnd.github.v3.diff"
}
create_repo_url = "https://api.github.com/repos/" + os.environ.get("ORG") + "/template-repository/generate"
def set_output(name, value):
output_file = os.environ.get("GITHUB_OUTPUT")
with open(output_file, "w") as output:
output.write(name + "=" + value + "\n")
def get_pr_diff():
res = requests.get("https://api.github.com/repos/" + os.environ.get("ORG") + "/Repository-Manager/pulls/" + os.environ.get("CHANGE_ID") ,headers = header1)
data = res.text
f = open("diff","w")
f.write(data)
f.close()
reponame = os.popen("cat diff | (grep '+ - repo:'||true) |awk '{print $4 $5}' ").read()
data1 = str(reponame)
data1 = data1.split('\n')
print(data1)
delrepos = os.popen("cat diff | (grep '\- - repo:'||true) |awk '{print $4 $5}' ").read()
data2 = str(delrepos)
data2 = data2.split('\n')
return data1, data2
def check_repo(repo):
org = os.environ.get("ORG")
if '#' in repo:
repo, c = repo.split('#', 1)
res = requests.get("https://api.github.com/repos/" + org + "/" + repo)
print("check_repo ",res.text)
if res.status_code == 200:
return repo
def del_obs_package(repo):
# delete obs package
component = "community"
if '#' in repo:
repo, c = repo.split('#', 1)
if c and c != '':
component = c
os.popen("osc rdelete deepin:Develop:%s/%s -m 'Remove by Repository-Manager pr' " % (component, repo)).read()
os.popen("osc rdelete deepin:Unstable:%s/%s -m 'Remove by Repository-Manager pr' " % (component, repo)).read()
def create_obs_package(repo):
print("create_repo: " + repo)
org = os.environ.get("ORG")
component = "community"
if '#' in repo:
repo, c = repo.split('#', 1)
if c and c != '':
component = c
os.popen("if [ ! -d deepin:Develop:%s ];then osc co deepin:Develop:%s template-repository;fi" % (component, component)).read()
os.chdir("deepin:Develop:%s" % component)
os.popen("osc mkpac " + repo).read()
os.popen("cp template-repository/_service " + repo).read()
os.popen("sed -i 's|template-repository|" + repo + "|g' " + repo + "/_service").read()
os.popen("osc add " + repo + "/_service").read()
os.popen('''osc ci -m "init"''').read()
os.chdir("../")
os.popen("if [ ! -d deepin:Unstable:%s ];then osc co deepin:Unstable:%s template-repository;fi" % (component, component)).read()
os.chdir("deepin:Unstable:%s" % component)
os.popen("osc mkpac " + repo).read()
os.popen("cp template-repository/_service " + repo).read()
os.popen("sed -i 's|template-repository|" + repo + "|g' " + repo + "/_service").read()
os.popen("osc add " + repo + "/_service").read()
os.popen('''osc ci -m "init"''').read()
os.chdir("../")
def create_repo(repo):
print("create_repo: " + repo)
org = os.environ.get("ORG")
component = "community"
if '#' in repo:
repo, c = repo.split('#', 1)
if c and c != '':
component = c
data_repo = {
'owner': org,
'name': repo
}
url = create_repo_url
if component != "community":
url = "https://api.github.com/repos/" + org + "/template-repository-main/generate"
res = requests.post(url, json = data_repo, headers = header)
print("create_repo response: " + res.text)
print("create_repo url: " + url)
try:
data, dels = get_pr_diff()
#for repo in dels:
# if repo != '':
# print(repo)
# del_obs_package(repo)
for repo in data:
if repo != '':
print(repo)
if check_repo(repo) == None:
create_repo(repo)
create_obs_package(repo)
except BaseException as e:
logging.error(e)
exit(-10)
- name: notify
uses: actions/github-script@v5
with:
script: |
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Created Successfully'
})
return