Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create new branch for user to push to #6

Closed
ckingbailey opened this issue Feb 4, 2020 · 2 comments · Fixed by #13
Closed

create new branch for user to push to #6

ckingbailey opened this issue Feb 4, 2020 · 2 comments · Fixed by #13

Comments

@ckingbailey
Copy link
Owner

ckingbailey commented Feb 4, 2020

After user is authenticated, try to create a new branch on the target repo. The app will push to this branch on user's behalf.

Get the SHA of master branch*

GET https://api.github.com/repos/:USER/:REPO/git/ref/heads/master

Start new branch off master

POST https://api.github.com/repos/:USER/:REPO/git/refs, body:

{
  "ref": "refs/heads/Publiccode-Pusher/add-publiccode-yml",
  "sha": "{ SHA of master branch from previous step }"
}

Gist here

Note that this endpoint only accepts JSON, and some kind of special GitHub JSON application/vnd.github+json. Docs here https://developer.github.com/v3/git/refs/

You'll get back a response like

{
  "ref": "refs/heads/add-publiccode-yaml",
  "node_id": "MDM6UmVmMjEzMTEzODcwOmFkZC1wdWJsaWNjb2RlLXlhbWw=",
  "url": "https://api.github.com/repos/ckingbailey/publiccode-pusher/git/refs/heads/add-publiccode-yaml",
  "object": {
    "sha": "6839f3114250a180a948ff3dc52689241de45a2f",
    "type": "commit",
    "url": "https://api.github.com/repos/ckingbailey/publiccode-pusher/git/commits/6839f3114250a180a948ff3dc52689241de45a2f"
  }
}

Hold onto that SHA. You'll need it to push the publiccode.yml to our new branch.

If you get a "403 Forbidden" or the like then the user doesn't have sufficient permissions on the target repo. I haven't tried this on a repo I don't have access to yet so I don't know what exactly that response will look like. If the user lacks permissions then we resort to strategies outlined in issue #3

* If repo has no master branch, or is not called master branch, that's too complicated for this app to handle at this point

@ckingbailey
Copy link
Owner Author

Check it out! You can also use this endpoint https://api.github.com/repos/:owner/:repo/collaborators/:user/permission to check the user's permission level. It gives a response like

{
  "permission": "write",
  "user": {
    "login": "ckingbailey",
    "id": 20404311,
    "node_id": "MDQ6VXNlcjIwNDA0MzEx",
    "avatar_url": "https://avatars3.githubusercontent.com/u/20404311?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/ckingbailey",
    "html_url": "https://github.com/ckingbailey",
    "followers_url": "https://api.github.com/users/ckingbailey/followers",
    "following_url": "https://api.github.com/users/ckingbailey/following{/other_user}",
    "gists_url": "https://api.github.com/users/ckingbailey/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/ckingbailey/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/ckingbailey/subscriptions",
    "organizations_url": "https://api.github.com/users/ckingbailey/orgs",
    "repos_url": "https://api.github.com/users/ckingbailey/repos",
    "events_url": "https://api.github.com/users/ckingbailey/events{/privacy}",
    "received_events_url": "https://api.github.com/users/ckingbailey/received_events",
    "type": "User",
    "site_admin": false
  }
}

@ckingbailey
Copy link
Owner Author

/user response, which gets authenticated user, looks like:

{
  "login": "ckingbailey",
  "id": 20404311,
  "node_id": "MDQ6VXNlcjIwNDA0MzEx",
  "avatar_url": "https://avatars3.githubusercontent.com/u/20404311?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/ckingbailey",
  "html_url": "https://github.com/ckingbailey",
  "followers_url": "https://api.github.com/users/ckingbailey/followers",
  "following_url": "https://api.github.com/users/ckingbailey/following{/other_user}",
  "gists_url": "https://api.github.com/users/ckingbailey/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/ckingbailey/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/ckingbailey/subscriptions",
  "organizations_url": "https://api.github.com/users/ckingbailey/orgs",
  "repos_url": "https://api.github.com/users/ckingbailey/repos",
  "events_url": "https://api.github.com/users/ckingbailey/events{/privacy}",
  "received_events_url": "https://api.github.com/users/ckingbailey/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Colin",
  "company": null,
  "blog": "ckingbailey.com",
  "location": "SF Bay Area",
  "email": "[email protected]",
  "hireable": true,
  "bio": "JS & Python 😃 / PHP 😐\r\nHack Night Lead, @openoakland \r\nToo many side projects sometimes",
  "public_repos": 29,
  "public_gists": 1,
  "followers": 15,
  "following": 12,
  "created_at": "2016-07-11T17:17:25Z",
  "updated_at": "2020-02-14T06:57:26Z"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant