Skip to content

Commit b6a3b24

Browse files
committed
feat: adding migration scripts
1 parent ec7508a commit b6a3b24

8 files changed

+145
-0
lines changed

gh-cli/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ This DELETES *ALL* workflow runs for a particular workflow in a repo. Can pass i
442442

443443
This disables all workflows in a repository; helpful if forking or copying someone else's code and you don't want all of the actions to continuously trigger.
444444

445+
### download-migration-archive-for-repository.sh
446+
447+
Downloads the most recent migration archive/export for a given organization repository.
448+
445449
### download-private-release-artifact.sh
446450

447451
Downloads a release artifact from a private/internal repository. Can either download latest version or specific version, and supports file pattern matching to download one or multiple files. See [docs](https://cli.github.com/manual/gh_release_download) for more info.
@@ -657,6 +661,14 @@ Gets the status of a [GitHub Enterprise Importer (GEI) migration](https://docs.g
657661

658662
Gets the usage of a label in a repository. Returns data in table format.
659663

664+
### get-most-recent-migration-id-for-organization.sh
665+
666+
Returns the most recent migration ID for a given organization.
667+
668+
### get-most-recent-migration-id-for-repository.sh
669+
670+
Returns the most recent migration ID for a given organization repository.
671+
660672
### get-organization-active-repositories.sh
661673

662674
Gets a list of repositories in an organization that have had code pushed to it in the last X days.
@@ -741,6 +753,10 @@ It contains the following data:
741753

742754
By default, it returns all migrations, but there is an optional `max-migrations` parameter to limit the number of migrations returned (must lower or equal to 100)).
743755

756+
### get-organization-migrations.sh
757+
758+
Returns the migrations (exports) against an organization.
759+
744760
### get-organization-repositories-by-property.sh
745761

746762
Gets a list of repositories in an organization that have one or more given [custom properties](https://docs.github.com/en/enterprise-cloud@latest/organizations/managing-organization-settings/managing-custom-properties-for-repositories-in-your-organization) values.
@@ -1006,6 +1022,10 @@ Example output:
10061022
],
10071023
```
10081024

1025+
### lock-repository-with-migration.sh
1026+
1027+
Creates a (mostly) empty migration for a given organization repository so that it can create a lock.
1028+
10091029
### parent-organization-teams.sh
10101030

10111031
Sets the parents of teams in an target organization based on existing child/parent relationship on a source organization teams.
@@ -1128,6 +1148,14 @@ Notable caps on the API:
11281148
- 500 requests per 24 hours for organizations on paid plans
11291149
- these caps do not apply to Enterprise Managed Users (EMU)
11301150

1151+
### unlock-repository-migration-by-id.sh
1152+
1153+
Unlocks / deletes the lock for a migrated repository - requires the migration ID to be passed in.
1154+
1155+
### unlock-repository-migration.sh
1156+
1157+
Unlocks / deletes the lock for a migrated repository by getting the most recent migration ID and unlocking it.
1158+
11311159
### update-branch-protection-rule.sh
11321160

11331161
Updates a branch protection rule for a given branch.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# downloads the most recent migration archive/export for a given organization repository
4+
# gets the most recent migration id for a given organization repository and then tries to download the archive
5+
6+
if [ -z "$2" ]; then
7+
echo "Usage: $0 <org> <repo>"
8+
echo "Example: ./download-migration-archive-for-repository.sh joshjohanning-org test-repo-export"
9+
exit 1
10+
fi
11+
12+
org="$1"
13+
repo="$2"
14+
15+
id=$(./get-most-recent-migration-id-for-repository.sh $org $repo true)
16+
17+
gh api /orgs/$org/migrations/$id/archive > $repo-archive.tar.gz
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# returns the most recent migration ID for a given organization
4+
5+
if [ -z "$1" ]; then
6+
echo "Usage: $0 <org>"
7+
echo "Example: ./get-most-recent-migration-id-for-organization.sh joshjohanning-org"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
13+
gh api /orgs/joshjohanning-org/migrations --jq 'sort_by(.created_at) | last | .id'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# returns the most recent migration ID for a given organization repository
4+
# pass in true as the third argument to return only the migration id
5+
6+
if [ -z "$2" ]; then
7+
echo "Usage: $0 <org> <repo> <return-id-only: true|false>"
8+
echo "Example: ./get-most-recent-migration-id-for-repository.sh joshjohanning-org test-repo-export"
9+
exit 1
10+
fi
11+
12+
org="$1"
13+
repo="$2"
14+
return_only_id=${3:-false}
15+
16+
migrations=$(gh api -X GET /orgs/$org/migrations -F per_page=100 --jq '.[] | {id: .id, repositories: .repositories.[].full_name, state: .state, created_at: .created_at}')
17+
18+
if [ "$return_only_id" = "false" ]; then
19+
most_recent_migration=$(echo "$migrations" | jq -s -r --arg repo "$org/$repo" 'map(select(.repositories == $repo)) | sort_by(.created_at) | last')
20+
else
21+
most_recent_migration=$(echo "$migrations" | jq -s -r --arg repo "$org/$repo" 'map(select(.repositories == $repo)) | sort_by(.created_at) | last | .id')
22+
fi
23+
24+
echo "$most_recent_migration"

gh-cli/get-organization-migrations.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# this returns the migrations (exports) against an organization
4+
5+
if [ -z "$1" ]; then
6+
echo "Usage: $0 <org>"
7+
echo "Example: ./get-organization-migrations.sh joshjohanning-org"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
13+
gh api -X GET /orgs/$org/migrations -F per_page=100 --jq '.[] | {id: .id, repositories: .repositories.[].full_name, state: .state, created_at: .created_at}'
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# creates a (mostly) empty migration for a given organization repository so that it can create a lock
4+
5+
if [ -z "$2" ]; then
6+
echo "Usage: $0 <org> <repo>"
7+
echo "Example: ./lock-repository-with-migration.sh joshjohanning-org test-repo-export"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
repo="$2"
13+
14+
gh api -X POST /orgs/$org/migrations -f "repositories[]=$org/$repo" -F lock_repositories=true -f "exclude[]=repositories" --jq '{id: .id, state: .state, updated_at: .updated_at}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# unlocks / deletes the lock for a migrated repository
4+
# you need to get the migration id first; ./get-most-recent-migration-id-for-repository.sh joshjohanning-org test-repo-export
5+
6+
# Note: Use this other script to automatically look up the migration id for the repository:
7+
# ./unlock-migrated-repository.sh joshjohanning-org test-repo-export
8+
9+
if [ -z "$2" ]; then
10+
echo "Usage: $0 <org> <migration-id>"
11+
echo "Example: ./unlock-repository-migration-by-id.sh joshjohanning-org test-repo-export 4451412"
12+
exit 1
13+
fi
14+
15+
org="$1"
16+
repo="$2"
17+
id="$3"
18+
19+
gh api -X DELETE /orgs/$org/migrations/$id/repos/$repo/lock

gh-cli/unlock-repository-migration.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# unlocks / deletes the lock for a migrated repository
4+
# gets the most recent migration id for a given organization repository and then tries to delete the lock
5+
6+
if [ -z "$2" ]; then
7+
echo "Usage: $0 <org> <repo>"
8+
echo "Example: ./unlock-repository-migration.sh joshjohanning-org test-repo-export"
9+
exit 1
10+
fi
11+
12+
org="$1"
13+
repo="$2"
14+
15+
id=$(./get-most-recent-migration-id-for-repository.sh $org $repo true)
16+
17+
gh api -X DELETE /orgs/$org/migrations/$id/repos/test-repo-export/lock

0 commit comments

Comments
 (0)