Skip to content

Commit 2f93cf4

Browse files
authored
fix security (#46)
1 parent 7928b93 commit 2f93cf4

File tree

5 files changed

+118
-95
lines changed

5 files changed

+118
-95
lines changed

.github/workflows/create_repos.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
name: Run create repos
3+
on:
4+
workflow_dispatch: null
5+
6+
jobs:
7+
dry-run:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v4
12+
13+
- uses: actions/create-github-app-token@v1
14+
id: app-token-all
15+
with:
16+
app-id: ${{ secrets.ALLIANZ_APP_ID }}
17+
private-key: ${{ secrets.ALLIANZ_APP_PRIVATE_KEY }}
18+
owner: allianz
19+
20+
- name: Plan github organization changes in allianz
21+
env:
22+
GH_TOKEN: ${{ steps.app-token-all.outputs.token }}
23+
run: scripts/create_repos.sh --org allianz --dry-run
24+
25+
- uses: actions/create-github-app-token@v1
26+
id: app-token-inc
27+
with:
28+
app-id: ${{ secrets.ALLIANZ_INCUBATOR_APP_ID }}
29+
private-key: ${{ secrets.ALLIANZ_INCUBATOR_APP_PRIVATE_KEY }}
30+
owner: allianz-incubator
31+
32+
- name: Plan github organization changes in allianz-incubator
33+
env:
34+
GH_TOKEN: ${{ steps.app-token-inc.outputs.token }}
35+
run: scripts/create_repos.sh --org allianz-incubator --dry-run
36+
37+
38+
apply:
39+
needs: dry-run
40+
environment: github.com
41+
runs-on: ubuntu-latest
42+
steps:
43+
- name: Checkout
44+
uses: actions/checkout@v4
45+
46+
- uses: actions/create-github-app-token@v1
47+
id: app-token-all
48+
with:
49+
app-id: ${{ secrets.ALLIANZ_APP_ID }}
50+
private-key: ${{ secrets.ALLIANZ_APP_PRIVATE_KEY }}
51+
owner: allianz
52+
53+
- name: Create repositories and teams in allianz
54+
env:
55+
GH_TOKEN: ${{ steps.app-token-all.outputs.token }}
56+
run: scripts/create_repos.sh --org allianz
57+
58+
- uses: actions/create-github-app-token@v1
59+
id: app-token-inc
60+
with:
61+
app-id: ${{ secrets.ALLIANZ_INCUBATOR_APP_ID }}
62+
private-key: ${{ secrets.ALLIANZ_INCUBATOR_APP_PRIVATE_KEY }}
63+
owner: allianz-incubator
64+
65+
- name: Create repositories and teams in allianz-incubator
66+
env:
67+
GH_TOKEN: ${{ steps.app-token-inc.outputs.token }}
68+
run: scripts/create_repos.sh --org allianz-incubator
File renamed without changes.

scripts/archive_repos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,5 @@ done
164164

165165
# Print dry run results
166166
if [ "$DRY_RUN" = true ]; then
167-
echo -e "\nPlanned changes:\n$DRY_RUN_MESSAGES"
167+
echo -e "\nFindings:\n$DRY_RUN_MESSAGES"
168168
fi

scripts/create_repos.sh

Lines changed: 37 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
#
33
# GitHub Management Script
44
#
5-
# Usage: ./create_repos.sh [--apply] [--debug]
5+
# Usage: ./create_repos.sh --org <organization_name> [--dry-run] [--debug]
66
#
77
# Parameters:
8-
# --apply: Apply changes to GitHub (default is dry-run mode).
9-
# --debug: Enable debug mode for additional information.
8+
# --org: The name of the organization on GitHub.
9+
# --dry-run: Optional flag to simulate script execution without making changes.
1010
#
1111
# Description:
1212
# This Bash script automates GitHub repository and team management based on a YAML configuration file.
1313
# It uses GitHub CLI (gh) and yq for interaction and configuration parsing, respectively.
14-
# The script can create, transfer, and synchronize repositories and teams, and it supports dry-run mode.
14+
# The script can create and synchronize repositories and teams, and it supports dry-run mode.
1515

1616
cd "$(dirname "$0")"
1717
IFS=$'\n' # keep whitespace when iterating with for loops
@@ -26,12 +26,18 @@ if ! command -v yq &> /dev/null || ! command -v gh &> /dev/null; then
2626
fi
2727

2828
# Parse command line parameters
29-
DRY_RUN=true
29+
ORG_NAME=""
30+
DRY_RUN=false
3031
DEBUG=false
32+
3133
while [ $# -gt 0 ]; do
3234
case "$1" in
33-
--apply)
34-
DRY_RUN=false
35+
--org)
36+
shift
37+
ORG_NAME=$1
38+
;;
39+
--dry-run)
40+
DRY_RUN=true
3541
;;
3642
--debug)
3743
DEBUG=true
@@ -88,29 +94,6 @@ create_repo() {
8894
}
8995

9096

91-
# Function to transfer a GitHub repository from one organization to another
92-
transfer_repo() {
93-
local name=$1
94-
95-
if [ "$DRY_RUN" = true ]; then
96-
DRY_RUN_MESSAGES+="~ Would transfer repository $name from allianz-incubator to allianz.\n"
97-
else
98-
local response=$(gh api \
99-
--method POST \
100-
-H "Accept: application/vnd.github+json" \
101-
-H "X-GitHub-Api-Version: 2022-11-28" \
102-
repos/allianz-incubator/$name/transfer \
103-
-f new_owner=allianz)
104-
105-
if [ $? -eq 0 ] && [ "$(echo $response | jq -r '.id')" != "null" ]; then
106-
echo -e "\e[32m✓\e[0m Repository '$name' successfully transfered to organization allianz."
107-
else
108-
echo "Error transfering repo '$name' at line $LINENO. $response.">&2; exit 1;
109-
fi
110-
fi
111-
}
112-
113-
11497
# Function to create a new GitHub team and set up team synchronization
11598
create_team() {
11699
local name=$1
@@ -159,7 +142,7 @@ create_team() {
159142
if [ "$DRY_RUN" = true ]; then
160143
DRY_RUN_MESSAGES+="+ Would setup team sync: team '$name' with AD Group '$giam_name'.\n"
161144
else
162-
load_teams # Update cache to include new team slug
145+
load_teams $org # Update cache to include new team slug
163146
local slug_name=$(get_team_slug $name) || exit 1
164147
local response=$(echo $ad_group | gh api \
165148
--method PATCH \
@@ -271,13 +254,11 @@ load_repositories() {
271254
}
272255

273256

274-
# Function to load existing teams from GitHub
257+
# Function to load existing teams from GitHub and cache the result
275258
load_teams() {
276-
CACHED_ALLIANZ_TEAMS=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /orgs/allianz/teams) || {
277-
echo "Error fetching teams for allianz at line $LINENO. $CACHED_ALLIANZ_TEAMS."; exit 1; }
278-
279-
CACHED_ALLIANZ_INCUBATOR_TEAMS=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /orgs/allianz-incubator/teams) || {
280-
echo "Error fetching teams for allianz-incubator at line $LINENO. $CACHED_ALLIANZ_INCUBATOR_TEAMS."; exit 1; }
259+
local org="$1"
260+
CACHED_TEAMS=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /orgs/$org/teams) || {
261+
echo "Error fetching teams for allianz at line $LINENO. $CACHED_TEAMS."; exit 1; }
281262
}
282263

283264

@@ -297,12 +278,7 @@ load_team_permissions(){
297278
# Function to get the list of teams for a given organization
298279
get_teams(){
299280
local org="$1"
300-
301-
if [ "$org" == "allianz" ]; then
302-
echo "$CACHED_ALLIANZ_TEAMS"
303-
else
304-
echo "$CACHED_ALLIANZ_INCUBATOR_TEAMS"
305-
fi
281+
echo "$CACHED_TEAMS"
306282
}
307283

308284

@@ -311,14 +287,11 @@ get_team_slug(){
311287
local name="$1"
312288

313289
# Search for the team in both organizations
314-
local slug_allianz=$(jq -r '.[] | select(.name == "'"$name"'") | .slug' <<< "$CACHED_ALLIANZ_TEAMS") || exit 1
315-
local slug_incubator=$(jq -r '.[] | select(.name == "'"$name"'") | .slug' <<< "$CACHED_ALLIANZ_INCUBATOR_TEAMS") || exit 1
316-
290+
local slug=$(jq -r '.[] | select(.name == "'"$name"'") | .slug' <<< "$CACHED_TEAMS") || exit 1
291+
317292
# Return the first non-empty slug found
318-
if [ -n "$slug_allianz" ]; then
319-
echo "$slug_allianz"
320-
elif [ -n "$slug_incubator" ]; then
321-
echo "$slug_incubator"
293+
if [ -n "$slug" ]; then
294+
echo "$slug"
322295
else
323296
echo "Error: team slug not found for $name" >&2; exit 1
324297
fi
@@ -334,53 +307,31 @@ get_team_slug(){
334307
# warnings for inconsistent repository configurations.
335308
#
336309
process_repos() {
310+
local org="$1"
337311
echo "READING REPOSITORIES..."
338312

339313
# Status
340-
local existing_main_repos=$(load_repositories allianz) || exit 1
341-
local existing_incubator_repos=$(load_repositories allianz-incubator) || exit 1
342-
local desired_main_repos=$(yq eval '.repositories[] | select(.stage == "allianz") | .name' "$YAML_FILE" | sort -u) || exit 1
343-
local desired_incubator_repos=$(yq eval '.repositories[] | select(.stage == "allianz-incubator") | .name' "$YAML_FILE" | sort -u) || exit 1
344-
314+
local existing_repos=$(load_repositories $org) || exit 1
315+
local desired_repos=$(yq eval '.repositories[] | select(.stage == "'"$org"'") | .name' "$YAML_FILE" | sort -u) || exit 1
316+
345317
## calculate changes
346-
local repos_to_add_in_incubator=$(comm -23 <(echo "$desired_incubator_repos") <(echo "$existing_incubator_repos")) || exit 1
347-
local repos_to_add_in_main=$(comm -23 <(comm -23 <(echo "$desired_main_repos") <(echo "$existing_main_repos")) <(echo "$existing_incubator_repos")) || exit 1
348-
local repos_to_transfer_to_main=$(comm -12 <(comm -23 <(echo "$desired_main_repos") <(echo "$existing_main_repos")) <(echo "$existing_incubator_repos")) || exit 1
349-
318+
local repos_to_add=$(comm -23 <(echo "$desired_repos") <(echo "$existing_repos")) || exit 1
319+
350320
# Debug
351321
print_debug
352322
print_debug "Existing Repositories in allianz:"
353-
print_debug "$existing_main_repos" | sed 's/^/ /'
323+
print_debug "$existing_repos" | sed 's/^/ /'
354324
print_debug
355325
print_debug "Desired Repositories in allianz:"
356-
print_debug "$desired_main_repos" | sed 's/^/ /'
326+
print_debug "$desired_repos" | sed 's/^/ /'
357327
print_debug
358328
print_debug "Repositories to Add in allianz:"
359-
print_debug "$repos_to_add_in_main" | sed 's/^/ /'
360-
print_debug
361-
print_debug "Repositories to Transfer to allianz:"
362-
print_debug "$repos_to_transfer_to_main" | sed 's/^/ /'
363-
print_debug
364-
print_debug "Existing Repositories in allianz-incubator:"
365-
print_debug "$existing_incubator_repos" | sed 's/^/ /'
366-
print_debug
367-
print_debug "Desired Repositories in allianz-incubator:"
368-
print_debug "$desired_incubator_repos" | sed 's/^/ /'
329+
print_debug "$repos_to_add" | sed 's/^/ /'
369330
print_debug
370-
print_debug "Repositories to Add in allianz-incubator:"
371-
print_debug "$repos_to_add_in_incubator" | sed 's/^/ /'
372-
print_debug
373-
374331

375332
# Iterate over changes
376-
for repo in $repos_to_add_in_incubator; do
377-
create_repo $repo "allianz-incubator"
378-
done
379-
for repo in $repos_to_add_in_main; do
380-
create_repo $repo "allianz"
381-
done
382-
for repo in $repos_to_transfer_to_main; do
383-
transfer_repo $repo
333+
for repo in $repos_to_add; do
334+
create_repo $repo $org
384335
done
385336
}
386337

@@ -478,10 +429,9 @@ process_teams() {
478429

479430
# Run
480431
validate_yaml
481-
process_repos
482-
load_teams
483-
process_teams allianz
484-
process_teams allianz-incubator
432+
process_repos $ORG_NAME
433+
load_teams $ORG_NAME
434+
process_teams $ORG_NAME
485435

486436
# Print warnings
487437
if [ -n "$warning_messages" ]; then

scripts/lint_repos.sh

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fi
2828
OUTPUT_DIR="../results"
2929
CHECKOUT_DIR="../lint_cache"
3030
DOCUMENTATION="https://github.com/allianz/ospo/blob/main/guides/standards_and_compliance.md"
31+
ISSUE_TITLE="Standards Compliance Notice"
3132

3233
# Parse command line parameters
3334
ORG_NAME=""
@@ -105,13 +106,17 @@ close_issue() {
105106
local issue_number=$(issue_number "$repo" "$issue_title")
106107

107108
if [ -n "$issue_number" ]; then
108-
echo "Closing the existing issue in the repository '$repo'."
109-
gh issue close -R "$repo" "$issue_number"
110-
else
111-
echo "No open issue found. Nothing to do."
109+
if [ "$DRY_RUN" = true ]; then
110+
DRY_RUN_MESSAGES+="Dry run: Would close the existing issue in the repository '$repo'."
111+
else
112+
gh issue close -R "$repo" "$issue_number"
113+
echo "Closed the existing issue in the repository '$repo'."
114+
fi
112115
fi
113116
}
114117

118+
119+
115120
# Clones the specified repository and saves description and topics information to local files
116121
#
117122
# This function clones the specified GitHub repository locally using the GitHub CLI (`gh`).
@@ -200,11 +205,11 @@ lint_repos() {
200205
echo "The repository is not compliant."
201206
failure="Hello there! 👋 Repository '$repo' doesn't meet our standards. Take a look at the [documentation]($DOCUMENTATION) for assistance."
202207
report=$(cat "$OUTPUT_DIR/$repo.md")
203-
create_issue_if_not_exists "$repo" "Standards Compliance Notice" "$failure\n\n$report"
208+
create_issue_if_not_exists "$repo" "$ISSUE_TITLE" "$failure\n\n$report"
204209
else
205210
echo
206-
echo "The repository compliant."
207-
close_issue "$repo" "Repo lint error"
211+
echo "The repository is compliant."
212+
close_issue "$repo" "$ISSUE_TITLE"
208213
fi
209214
done
210215
}

0 commit comments

Comments
 (0)