-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepositoriesAutomation.yaml
145 lines (130 loc) · 4.74 KB
/
RepositoriesAutomation.yaml
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
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: "3"
run: always
vars:
BASE_TMP_DIR: { sh: echo $(echo $TMPDIR)gh-automations }
REPOSITORIES_DIR: "{{.BASE_TMP_DIR}}/repositories"
DEFAULT_PR_MESSAGE: "reconcile repository"
PR_LIST_FILE: "{{.USER_WORKING_DIR}}/pr-list.txt"
PR_LIST_AUTO_APPROVE: "{{.USER_WORKING_DIR}}/pr-approve.txt"
PR_LIST_MERGE: "{{.USER_WORKING_DIR}}/pr-merge.txt"
BRANCH_NAME_PREFIX: "repo-reconciliation"
tasks:
reconcile-repository:
label: "reconciler @ {{.REPO_NAME}}"
prefix: "reconciler @ {{.REPO_NAME}}"
desc: executes the given script in the context of the given repository
deps: [requirements]
requires: { vars: [REPO_NAME, SCRIPTS_LIST, REPOSITORIES_DIR] }
vars:
TIMESTAMP: { sh: date +%s }
REPO_DIR: "{{.REPOSITORIES_DIR}}/{{.REPO_NAME}}"
BRANCH_NAME: "{{.BRANCH_NAME_PREFIX}}-{{.TIMESTAMP}}"
cmds:
- mkdir -p {{.REPO_DIR}}
- defer: rm -rf {{.REPO_DIR}}
- task: checkout
vars:
REPOSITORY_DIR: "{{.REPO_DIR}}"
REPO_NAME: "{{.REPO_NAME}}"
- task: delete-old-branches
vars:
REPOSITORY_DIR: "{{.REPO_DIR}}"
BRANCH_PREFIX: "{{.BRANCH_NAME_PREFIX}}"
- task: create-branch
vars:
REPOSITORY_DIR: "{{.REPO_DIR}}"
BRANCH_NAME: "{{.BRANCH_NAME}}"
- for: { var: SCRIPTS_LIST, split: "," }
task: run-script-for-repository
vars:
REPOSITORY_DIR: "{{.REPO_DIR}}"
SCRIPT_PATH: "{{.ITEM}}"
- task: open-pr
vars:
REPOSITORY_DIR: "{{.REPO_DIR}}"
BRANCH_NAME: "{{.BRANCH_NAME}}"
##############################
# start of internal tasks
run-script-for-repository:
internal: true
label: "exec @ {{.SCRIPT_NAME}}"
prefix: "exec @ {{.SCRIPT_NAME}}"
vars:
SCRIPT_NAME:
sh: basename {{.SCRIPT_PATH}}
requires: { vars: [REPOSITORY_DIR, SCRIPT_PATH] }
desc: runs the given script in the context of the given repository
dir: "/{{.REPOSITORY_DIR}}"
cmd: bash {{.SCRIPT_PATH}}
preconditions:
- sh: "[ -f {{.SCRIPT_PATH}} ]"
msg: ✗ directory '{{.SCRIPT_PATH}}' does not exist."
- sh: "[[ '{{.SCRIPT_PATH}}' = /* ]]"
msg: ✗ directory '{{.SCRIPT_PATH}}' must be an absolute path."
create-branch:
label: "checkout branch"
prefix: "checkout branch"
internal: true
requires: { vars: [REPOSITORY_DIR, BRANCH_NAME] }
desc: creates a branch on the repository on the given directory
dir: "/{{.REPOSITORY_DIR}}"
cmd: git checkout -b {{.BRANCH_NAME}}
status:
- git show-ref --quiet refs/heads/{{.BRANCH_NAME}}
open-pr:
label: "create PR"
prefix: "create PR"
silent: true
internal: true
requires: { vars: [REPOSITORY_DIR, BRANCH_NAME, GH_TOKEN] }
dir: "/{{.REPOSITORY_DIR}}"
vars:
COMMIT_MESSAGE: "chore: changes autogenerated by reconciliation script"
PR_TITLE: "chore: {{.DEFAULT_PR_MESSAGE}}"
env:
GH_TOKEN: "{{.GH_TOKEN}}"
cmd: |
git add --all
git commit -m "{{.COMMIT_MESSAGE}}"
git push origin {{.BRANCH_NAME}}
pullRequestLink=$(gh pr create --base main --head "{{.BRANCH_NAME}}" --title "{{.PR_TITLE}}" --body "PR autogenerated.")
echo "$pullRequestLink" >> {{.PR_LIST_FILE}}
echo "gh pr review $pullRequestLink --approve" >> {{.PR_LIST_AUTO_APPROVE}}
echo "gh pr merge $pullRequestLink --admin --squash" >> {{.PR_LIST_MERGE}}
status:
- test -z "$(git status --porcelain)" # if there are no changes to commit
checkout:
label: "checkout @ {{.REPO_NAME}}"
prefix: "checkout @ {{.REPO_NAME}}"
internal: true
requires: { vars: [REPO_NAME, REPOSITORY_DIR] }
dir: "/{{.REPOSITORY_DIR}}"
cmds:
- rm -rf {{.REPOSITORY_DIR}}/*
- git clone [email protected]:{{.REPO_NAME}}.git {{.REPOSITORY_DIR}}
- git fetch --all
delete-old-branches:
label: "cleanup branches"
prefix: "cleanup branches"
silent: true
internal: true
requires: { vars: [REPOSITORY_DIR, BRANCH_PREFIX] }
dir: "/{{.REPOSITORY_DIR}}"
cmds:
- |
BRANCHES=$(git branch -r | grep "origin/{{.BRANCH_PREFIX}}" | sed 's/origin\///')
if [ -z "$BRANCHES" ]; then
echo "no branches found with prefix '{{.BRANCH_PREFIX}}'"
else
for BRANCH in $BRANCHES; do
echo "deleting branch '$BRANCH'"
git push origin --delete "$BRANCH"
done
fi
requirements:
preconditions:
- sh: gh --version
msg: "GH cli is not installed. Please run 'brew install gh' or follow the README instructions"
- sh: test -n "{{.GH_TOKEN}}"
msg: "GITHUB_PAT environment variables is not set. Please set it before running this task"