Repository navigation
179 lines (163 loc) · 7.05 KB
/
Copy pathremove-script-command.yml
File metadata and controls
179 lines (163 loc) · 7.05 KB
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
name: Remove script on /remove
# A maintainer comments `/remove` on a script issue: every file belonging to
# that script is deleted from main and the issue is closed as not planned.
# The script is resolved from the issue form the same way move-to-main-repo.yaml
# and delete_new_script.yaml do it ("Name of the Script" + "Script Type").
on:
issue_comment:
types: [created]
permissions:
contents: read
issues: write
concurrency:
group: remove-script-${{ github.event.issue.number }}
cancel-in-progress: false
jobs:
remove:
runs-on: ubuntu-latest
if: >-
github.repository == 'community-scripts/DevScripts' &&
!github.event.issue.pull_request &&
github.event.issue.state == 'open' &&
startsWith(github.event.comment.body, '/remove')
steps:
- name: Check commenter permission
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
ACTOR: ${{ github.event.comment.user.login }}
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
first_line=$(printf '%s\n' "$COMMENT_BODY" | head -n1 | tr -d '\r' | sed 's/[[:space:]]*$//')
if [[ "$first_line" != "/remove" ]]; then
echo "Comment is not exactly /remove, ignoring."
exit 1
fi
perm=$(gh api "repos/$REPO/collaborators/$ACTOR/permission" --jq '.permission' 2>/dev/null || echo none)
case "$perm" in
admin|maintain|write) echo "$ACTOR has $perm permission." ;;
*) echo "::error::$ACTOR ($perm) may not use /remove."; exit 1 ;;
esac
- name: Acknowledge
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
COMMENT_ID: ${{ github.event.comment.id }}
run: gh api -X POST "repos/$REPO/issues/comments/$COMMENT_ID/reactions" -f content=eyes >/dev/null
# main is protected by a ruleset that GITHUB_TOKEN cannot bypass; the
# mergebot app can, so the checkout (and with it the push) uses its token.
- name: Generate a token
id: app-token
uses: actions/create-github-app-token@v3
with:
app-id: ${{ vars.GHAPP_MERGEBOT_ID }}
private-key: ${{ secrets.GHAPP_MERGEBOT_PRIVATE_KEY }}
owner: community-scripts
repositories: DevScripts
- uses: actions/checkout@v7
with:
ref: main
token: ${{ steps.app-token.outputs.token }}
- name: Resolve script files
id: resolve
env:
# Through the environment, never interpolated into the script body:
# issue text is attacker-controlled.
ISSUE_BODY: ${{ github.event.issue.body }}
ISSUE_TITLE: ${{ github.event.issue.title }}
run: |
field() {
printf '%s\n' "$ISSUE_BODY" | tr -d '\r' \
| sed -n "/^###[[:space:]]*$1/,/^###/p" \
| sed '1d;/^###/d;/^[[:space:]]*$/d;/^_No response_$/d' | head -n1
}
name_raw=$(field "Name of the Script")
[[ -z "$name_raw" ]] && name_raw="$ISSUE_TITLE"
slug=$(echo "$name_raw" | tr '[:upper:]' '[:lower:]' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//;s/[[:space:]]\+/-/g')
if [[ ! "$slug" =~ ^[a-z0-9][a-z0-9._-]*$ ]]; then
echo "::error::'Name of the Script' does not give a valid slug: '$name_raw'"
exit 1
fi
type_raw=$(field "Script Type")
case "$type_raw" in
"CT (LXC Container)"*) type=ct ;;
"VM (Virtual Machine)"*) type=vm ;;
"Addon (tools/addon)"*) type=addon ;;
"PVE Tool (tools/pve)"*) type=pve ;;
"Other"*) type=other ;;
*) [[ "$slug" == *-vm ]] && type=vm || type=ct ;;
esac
files=()
case "$type" in
ct)
files+=("ct/${slug}.sh" "install/${slug}-install.sh" "ct/headers/${slug}"
"ct/deferred/${slug}.sh" "install/deferred/${slug}-install.sh")
# Shape A Alpine scripts may share the json of the plain app name;
# only drop it when no Debian script of that name is left behind.
if [[ "$slug" == alpine-* && ! -f "ct/${slug#alpine-}.sh" ]]; then
files+=("json/${slug#alpine-}.json")
fi
;;
vm) files+=("vm/${slug}.sh") ;;
addon) files+=("tools/addon/${slug}.sh") ;;
pve) files+=("tools/pve/${slug}.sh") ;;
other)
path=$(field "Script path" | sed 's/^[[:space:]`]*//;s/[[:space:]`]*$//')
if [[ -z "$path" || "$path" == /* || "$path" == *..* || ! "$path" =~ ^(ct|install|vm|tools|turnkey)/[A-Za-z0-9._/-]+\.sh$ ]]; then
echo "::error::Script type is Other but 'Script path' is missing or invalid: '$path'"
exit 1
fi
files+=("$path")
;;
esac
files+=("json/${slug}.json")
existing=()
for f in "${files[@]}"; do
[[ -e "$f" ]] && existing+=("$f")
done
echo "Slug: $slug Type: $type"
printf 'Found: %s\n' "${existing[@]}"
{
echo "slug=$slug"
echo "type=$type"
echo "files=${existing[*]}"
} >>"$GITHUB_OUTPUT"
- name: Delete files and push
id: push
if: steps.resolve.outputs.files != ''
env:
SLUG: ${{ steps.resolve.outputs.slug }}
FILES: ${{ steps.resolve.outputs.files }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
ACTOR: ${{ github.event.comment.user.login }}
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
read -ra files <<<"$FILES"
git rm -r --quiet -- "${files[@]}"
git commit -m "Remove ${SLUG} (#${ISSUE_NUMBER})" -m "Requested by @${ACTOR} via /remove."
for i in 1 2 3 4; do
git push origin HEAD:main && break
[[ $i == 4 ]] && exit 1
sleep $((i * 5))
git pull --rebase origin main
done
echo "sha=$(git rev-parse HEAD)" >>"$GITHUB_OUTPUT"
- name: Comment and close issue
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
SLUG: ${{ steps.resolve.outputs.slug }}
FILES: ${{ steps.resolve.outputs.files }}
SHA: ${{ steps.push.outputs.sha }}
run: |
if [[ -z "$FILES" ]]; then
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" \
--body "No files found for \`${SLUG}\` on main, so nothing was removed and the issue stays open. Check the script name in the issue body."
exit 0
fi
list=$(printf -- '- `%s`\n' $FILES)
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" \
--body "$(printf 'Removed `%s` in %s:\n\n%s' "$SLUG" "$SHA" "$list")"
gh issue close "$ISSUE_NUMBER" --repo "$REPO" --reason "not planned"