Skip to content

Instantly share code, notes, and snippets.

@embano1
Last active April 20, 2021 13:40
Show Gist options
  • Select an option

  • Save embano1/496e14aa10006f40ff360e463028a210 to your computer and use it in GitHub Desktop.

Select an option

Save embano1/496e14aa10006f40ff360e463028a210 to your computer and use it in GitHub Desktop.
List and disable Github Actions in accessible Github Repositories

⚠️ !!!WARNING!!! ⚠️

Use at your own risk and verify your input/output before performing any (potentially destructive) call against the Github API.

Retrieve Github Repositories

⚠️ These commands assume a working gh CLI environment, i.e. authenticated with at least repo-level permissions (see docs).

Retrieve accessible Github Repositories:

gh api graphql --paginate -f query='
    query($endCursor: String) {
      viewer {
        repositories(first: 100, after: $endCursor) {
          nodes { nameWithOwner }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  ' > repos-raw.json

Inspect the output, e.g. remove repositories where you want to keep Github Actions enabled. Especially for repositories that you are contributing to, but not the (sole) owner of.

The trimmed down input (file) for the subsequent commands should look like:

cat repos-cleaned.txt
ghusername/myrepo1
ghusername/myrepo2
ghusername/myrepo3

Check Github Actions Status

cat repos-cleaned.txt | xargs -I{} sh -c 'echo {}; gh api /repos/{}/actions/permissions'

Example response:

ghusername/myrepo1
{
  "enabled": true,
  "allowed_actions": "all"
}
ghusername/myrepo2
{
  "enabled": false
}
ghusername/myrepo3
{
  "enabled": false
}

Disable Github Actions

gh api will handle pagination, throttling, etc. for you. The HTTP response headers will be printed (-i parameter) for further inspection (204 is what we want).

To disable Github Actions in all repositories specified in repos-cleaned.txt run the following command:

⚠️ You might want to start with a handful of repositories to verify the behavior before firing this off on a huge list of repositories.

cat repos-cleaned.txt| xargs -I{} gh api -i -X PUT /repos/{}/actions/permissions -F enabled=false

References

Repo API: https://docs.github.com/en/rest/reference/repos#list-repositories-for-the-authenticated-user Actions API: https://docs.github.com/en/rest/reference/actions#set-github-actions-permissions-for-a-repository

Note that there's separate APIs of org-level scopes/permissions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment