Update repos.yml #2341
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |