r-lib / gh

Minimalistic GitHub API client in R
https://gh.r-lib.org
Other
223 stars 52 forks source link

How to write the description, tags and topics of a repo in r using the gh package #180

Closed derek-corcoran-barrios closed 12 months ago

derek-corcoran-barrios commented 1 year ago

I have a csv file with the desired description, tags and topics for several github repos, and I am trying to write them into my repos using the gh package in r, some are public and some private, however I have not been successful in doing so, here are some of the functions I wrote:

write_description <- function(repo, org, pat, description) {
  gh::gh(
    "/repos/:owner/:repo",
    method = "PUT",
    owner = org,
    repo = repo,
    .token = pat,
    description = description
  )
}

When I use it I like this:


write_description(repo = "TestTargets",
                  org = "derek-corcoran-barrios",
                  pat = "MY-PAT",
                  description = "Testing the targets package")

I get a long JSON without the description being updated

{
  "id": 613884650,
  "node_id": "R_kgDOJJci6g",
  "name": "TestTargets",
  "full_name": "derek-corcoran-barrios/TestTargets",
  "private": false,
  "owner": {
    "login": "derek-corcoran-barrios",
    "id": 10416736,
    "node_id": "MDQ6VXNlcjEwNDE2NzM2",
    "avatar_url": "https://avatars.githubusercontent.com/u/10416736?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/derek-corcoran-barrios",
    "html_url": "https://github.com/derek-corcoran-barrios",
    "followers_url": "https://api.github.com/users/derek-corcoran-barrios/followers",
    "following_url": "https://api.github.com/users/derek-corcoran-barrios/following{/other_user}",
    "gists_url": "https://api.github.com/users/derek-corcoran-barrios/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/derek-corcoran-barrios/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/derek-corcoran-barrios/subscriptions",
    "organizations_url": "https://api.github.com/users/derek-corcoran-barrios/orgs",
    "repos_url": "https://api.github.com/users/derek-corcoran-barrios/repos",
    "events_url": "https://api.github.com/users/derek-corcoran-barrios/events{/privacy}",
    "received_events_url": "https://api.github.com/users/derek-corcoran-barrios/received_events",
    "type": "User",
    "site_admin": false
  },
  "html_url": "https://github.com/derek-corcoran-barrios/TestTargets",
  "description": {},
  "fork": false,
  "url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets",
  "forks_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/forks",
  "keys_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/keys{/key_id}",
  "collaborators_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/collaborators{/collaborator}",
  "teams_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/teams",
  "hooks_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/hooks",
  "issue_events_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/issues/events{/number}",
  "events_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/events",
  "assignees_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/assignees{/user}",
  "branches_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/branches{/branch}",
  "tags_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/tags",
  "blobs_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/git/blobs{/sha}",
  "git_tags_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/git/tags{/sha}",
  "git_refs_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/git/refs{/sha}",
  "trees_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/git/trees{/sha}",
  "statuses_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/statuses/{sha}",
  "languages_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/languages",
  "stargazers_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/stargazers",
  "contributors_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/contributors",
  "subscribers_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/subscribers",
  "subscription_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/subscription",
  "commits_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/commits{/sha}",
  "git_commits_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/git/commits{/sha}",
  "comments_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/comments{/number}",
  "issue_comment_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/issues/comments{/number}",
  "contents_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/contents/{+path}",
  "compare_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/compare/{base}...{head}",
  "merges_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/merges",
  "archive_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/{archive_format}{/ref}",
  "downloads_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/downloads",
  "issues_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/issues{/number}",
  "pulls_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/pulls{/number}",
  "milestones_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/milestones{/number}",
  "notifications_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/notifications{?since,all,participating}",
  "labels_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/labels{/name}",
  "releases_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/releases{/id}",
  "deployments_url": "https://api.github.com/repos/derek-corcoran-barrios/TestTargets/deployments",
  "created_at": "2023-03-14T13:17:03Z",
  "updated_at": "2023-03-14T13:17:12Z",
  "pushed_at": "2023-03-14T13:17:05Z",
  "git_url": "git://github.com/derek-corcoran-barrios/TestTargets.git",
  "ssh_url": "git@github.com:derek-corcoran-barrios/TestTargets.git",
  "clone_url": "https://github.com/derek-corcoran-barrios/TestTargets.git",
  "svn_url": "https://github.com/derek-corcoran-barrios/TestTargets",
  "homepage": {},
  "size": 10,
  "stargazers_count": 0,
  "watchers_count": 0,
  "language": "R",
  "has_issues": true,
  "has_projects": true,
  "has_downloads": true,
  "has_wiki": true,
  "has_pages": false,
  "has_discussions": false,
  "forks_count": 0,
  "mirror_url": {},
  "archived": false,
  "disabled": false,
  "open_issues_count": 0,
  "license": {},
  "allow_forking": true,
  "is_template": false,
  "web_commit_signoff_required": false,
  "topics": [],
  "visibility": "public",
  "forks": 0,
  "open_issues": 0,
  "watchers": 0,
  "default_branch": "master",
  "permissions": {
    "admin": true,
    "maintain": true,
    "push": true,
    "triage": true,
    "pull": true
  },
  "temp_clone_token": "",
  "allow_squash_merge": true,
  "allow_merge_commit": true,
  "allow_rebase_merge": true,
  "allow_auto_merge": false,
  "delete_branch_on_merge": false,
  "allow_update_branch": false,
  "use_squash_pr_title_as_default": false,
  "squash_merge_commit_message": "COMMIT_MESSAGES",
  "squash_merge_commit_title": "COMMIT_OR_PR_TITLE",
  "merge_commit_message": "PR_TITLE",
  "merge_commit_title": "MERGE_MESSAGE",
  "security_and_analysis": {
    "secret_scanning": {
      "status": "disabled"
    },
    "secret_scanning_push_protection": {
      "status": "disabled"
    }
  },
  "network_count": 0,
  "subscribers_count": 1
} 

The same happens with my other functions:

write topics

write_topics <- function(repo, org, pat, topics) {
  gh::gh(
    "/repos/:owner/:repo/topics",
    owner = org,
    repo = repo,
    .token = pat,
    names = topics
  )
}

When I do:

write_topics(repo = "TestTargets",
             org = "derek-corcoran-barrios",
             pat = "MY-PAT",
             topics = "biodiversity")

I get

{
  "names": []
} 

Any idea of what I am doing wrong?

I have tried all the abovementioned functions

gaborcsardi commented 1 year ago

You need this endpoint to update a repo: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#update-a-repository

It goes like this in gh:

gh::gh(
  "PATCH /repos/:owner/:repo", 
  owner = "gaborcsardi", 
  repo = "playground", 
  description = "My GitHub playground"
)