|
| 1 | +name: 'Custom Properties Update Action' |
| 2 | +description: 'Updates the custom properties for all repos in an org to values in org/governance/repo-properties.yaml' |
| 3 | +author: 'Andrew Brandt <andrew.brandt@hashgraph.com>' |
| 4 | +organization: 'PandasWhoCode' |
| 5 | +branding: |
| 6 | + icon: 'check-circle' |
| 7 | + color: 'purple' |
| 8 | + |
| 9 | +inputs: |
| 10 | + token: |
| 11 | + description: 'Personal Access Token' |
| 12 | + required: true |
| 13 | + |
| 14 | +runs: |
| 15 | + using: "composite" |
| 16 | + steps: |
| 17 | + - name: Install yq (mikefarah's version) |
| 18 | + shell: bash |
| 19 | + run: | |
| 20 | + sudo wget --quiet https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq |
| 21 | + sudo chmod +x /usr/bin/yq |
| 22 | + yq --version # confirm installed |
| 23 | +
|
| 24 | + - name: Convert from JSON to YAML |
| 25 | + shell: bash |
| 26 | + run: | |
| 27 | + REPO_NAME=${{ github.event.repository.name }} |
| 28 | + YAML_FILE="repo-properties.yaml" |
| 29 | + |
| 30 | + # Convert the YAML file to JSON for parsing |
| 31 | + yq eval -o=json repo-properties.yaml > repo-properties.json |
| 32 | +
|
| 33 | + - name: Create list of repo names |
| 34 | + shell: bash |
| 35 | + run: jq -r '.repositories[].name' repo-properties.json > repo-names.txt |
| 36 | + |
| 37 | + - name: Set custom properties in repos |
| 38 | + shell: bash |
| 39 | + env: |
| 40 | + GH_TOKEN: ${{ inputs.token }} |
| 41 | + run: | |
| 42 | + # Input files |
| 43 | + REPO_NAMES_FILE="repo-names.txt" |
| 44 | + JSON_FILE="repo-properties.json" |
| 45 | + |
| 46 | + # Extract the org name once |
| 47 | + ORG_NAME=$(jq -r '.org' "$JSON_FILE") |
| 48 | + echo "Org Name: ${ORG_NAME}" |
| 49 | + |
| 50 | + # Loop over each repo name |
| 51 | + while IFS= read -r REPO_NAME; do |
| 52 | + echo "Processing repo: ${REPO_NAME}" |
| 53 | + |
| 54 | + # Check if the repo exists |
| 55 | + if ! gh api "/repos/${ORG_NAME}/${REPO_NAME}" --silent > /dev/null 2>&1; then |
| 56 | + echo " Repository '${ORG_NAME}/${REPO_NAME}' does not exist. Skipping..." |
| 57 | + continue |
| 58 | + fi |
| 59 | + |
| 60 | + # Extract matching repository object without the 'name' field |
| 61 | + repo_props=$(jq -r --arg name "${REPO_NAME}" ' |
| 62 | + .repositories[] |
| 63 | + | select(.name == $name) |
| 64 | + | del(.name) |
| 65 | + ' "$JSON_FILE") |
| 66 | + |
| 67 | + # Check if a match was found |
| 68 | + if [[ "$repo_props" == "null" || -z "$repo_props" ]]; then |
| 69 | + echo "No matching data found for ${REPO_NAME}, skipping..." |
| 70 | + continue |
| 71 | + fi |
| 72 | + |
| 73 | + # Iterate over key-value pairs and set custom properties |
| 74 | + echo "$repo_props" | jq -r 'to_entries[] | "\(.key)=\(.value)"' | while IFS="=" read -r key value; do |
| 75 | + echo " Setting property '${key}' = '${value}' on ${ORG_NAME}/${REPO_NAME}" |
| 76 | + # check to see if date here |
| 77 | + if [[ "${key}" == *"-date"* || "${key}" == "date-"* ]]; then |
| 78 | + if [[ ! "${value}" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ && -n "${value}" ]]; then |
| 79 | + echo "Repo name is: ${REPO_NAME}" |
| 80 | + echo "Property name is: ${key}" |
| 81 | + echo "Date Value is: ${value}" |
| 82 | + echo "Invalid date format: Date needs to be formatted [YYYY-MM-DD]" |
| 83 | + exit 1 |
| 84 | + fi |
| 85 | + fi |
| 86 | + # API Call here |
| 87 | + gh api --method PATCH -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/${ORG_NAME}/${REPO_NAME}/properties/values -f "properties[][property_name]=${key}" -f "properties[][value]=${value}" |
| 88 | + done |
| 89 | + done < "${REPO_NAMES_FILE}" |
| 90 | + echo "Successfully set custom properties!" |
0 commit comments