Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lotharschulz/9d80ecac73806981a6adc7e5ab2eb9a3 to your computer and use it in GitHub Desktop.
Save lotharschulz/9d80ecac73806981a6adc7e5ab2eb9a3 to your computer and use it in GitHub Desktop.

Repository Archiving & Unarchiving with GitHub GraphQL API

Repository Archiving & Unarchiving with GitHub GRAPHQL API sample code.

GitHub graphql explorer sample code - find a repository id

query FindRepoID {
    repository(owner:"[github user or org]", name:"[repository]"){
        id,
        isArchived,
    }
}

(lotharschulz/FindRepoID.graphql)

GitHub graphql explorer sample code - archive repository graphql mutation

mutation ArchiveRepository {
    archiveRepository(input:{clientMutationId:"true",repositoryId:"[repositoryID]"}) {
        repository {
            isArchived,
            description,
        }
    }
}

(lotharschulz/ArchiveRepository.graphql)

GitHub graphql explorer sample code - unarchive repository graphql mutation

mutation UnArchiveRepository {
    unarchiveRepository(input:{clientMutationId:"true",repositoryId:"[insert ID]"}) {
        repository {
            isArchived,
            description
        }
    }
}

(lotharschulz/UnArchiveRepository.graphql)

GraphQL API with curl

GitHub graphql API sample code - find a repository id

{
    "query": "query ($org: String!, $repo: String!) {repository(owner: $org, name: $repo) { id isArchived }  }",
    "variables": {
        "org": "[github user or org]",
        "repo": "[repository]"
    }
}

(lotharschulz/findRepoID.graphql)

call the graphql API with curl

curl -H "Authorization: bearer [github personal access token]" -X POST -H "Content-Type: application/json" -d @findRepoID.graphql https://api.github.com/graphql

graphql API answer:

{"data":{"repository":{"id":"[SampleRepoID]","isArchived":false}}}

GitHub graphql API sample code - archive repository graphql mutation

{
    "query": "mutation ArchiveRepository ($mutationId: String!, $repoID: String!) {archiveRepository(input:{clientMutationId:$mutationId, repositoryId:$repoID}) {repository { isArchived, description } } }",
    "variables": {
        "mutationId": "true",
        "repoID": "[repo id]"
    }
}

(lotharschulz/archiveRepository.graphql)

call the graphql API with curl

curl -H "Authorization: bearer [github personal access token]" -X POST -H "Content-Type: application/json" -d @archiveRepository.graphql https://api.github.com/graphql

graphql API answer:

{"data":{"archiveRepository":{"repository":{"isArchived":true,"description":"test repository"}}}}

GitHub graphql API sample code - unarchive repository graphql mutation

{
    "query": "mutation UnArchiveRepository ($mutationId: String!, $repoID: String!) {unarchiveRepository(input:{clientMutationId:$mutationId, repositoryId:$repoID}) {repository { isArchived, description } } }",
    "variables": {
        "mutationId": "true",
        "repoID": "[repo id]"
    }
}

(lotharschulz/unarchiveRepository.graphql)

call the graphql API with curl

curl -H "Authorization: bearer [github personal access token]" -X POST -H "Content-Type: application/json" -d @unarchiveRepository.graphql https://api.github.com/graphql

graphql API answer:

{"data":{"unarchiveRepository":{"repository":{"isArchived":false,"description":"test repository"}}}}

GitHub CLI

GitHub CLI sample code - find a repository id

query FindRepoID($name: String!, $owner: String!) {
    repository(owner: $owner, name: $name) {
        id,
        isArchived
    }
}

(lotharschulz/findRepoID_gh_cli.graphql)

use this file with the GitHub CLI in your terminal:

gh api graphql -F owner='[github user or organization]' -F name='[repository]' -f query="$(cat ./findRepoID_gh_cli.graphql)" | jq '.data.repository.id'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment