OwnerIDAndType returns the ID and OwnerType. The special login "@me" or an empty string queries the current user.
(login string)
| 1197 | |
| 1198 | // OwnerIDAndType returns the ID and OwnerType. The special login "@me" or an empty string queries the current user. |
| 1199 | func (c *Client) OwnerIDAndType(login string) (string, OwnerType, error) { |
| 1200 | if login == "@me" || login == "" { |
| 1201 | var query viewerLogin |
| 1202 | err := c.doQueryWithProgressIndicator("ViewerOwner", &query, nil) |
| 1203 | if err != nil { |
| 1204 | return "", "", err |
| 1205 | } |
| 1206 | return query.Viewer.Id, ViewerOwner, nil |
| 1207 | } |
| 1208 | |
| 1209 | variables := map[string]interface{}{ |
| 1210 | "login": githubv4.String(login), |
| 1211 | } |
| 1212 | var query struct { |
| 1213 | User struct { |
| 1214 | Login string |
| 1215 | Id string |
| 1216 | } `graphql:"user(login: $login)"` |
| 1217 | Organization struct { |
| 1218 | Login string |
| 1219 | Id string |
| 1220 | } `graphql:"organization(login: $login)"` |
| 1221 | } |
| 1222 | |
| 1223 | err := c.doQueryWithProgressIndicator("UserOrgOwner", &query, variables) |
| 1224 | if err != nil { |
| 1225 | // Due to the way the queries are structured, we don't know if a login belongs to a user |
| 1226 | // or to an org, even though they are unique. To deal with this, we try both - if neither |
| 1227 | // is found, we return the error. |
| 1228 | var graphErr api.GraphQLError |
| 1229 | if errors.As(err, &graphErr) { |
| 1230 | if graphErr.Match("NOT_FOUND", "user") && graphErr.Match("NOT_FOUND", "organization") { |
| 1231 | return "", "", err |
| 1232 | } else if graphErr.Match("NOT_FOUND", "organization") { // org isn't found must be a user |
| 1233 | return query.User.Id, UserOwner, nil |
| 1234 | } else if graphErr.Match("NOT_FOUND", "user") { // user isn't found must be an org |
| 1235 | return query.Organization.Id, OrgOwner, nil |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | return "", "", errors.New("unknown owner type") |
| 1241 | } |
| 1242 | |
| 1243 | // issueOrPullRequest is used to query the global id of an issue or pull request by its URL. |
| 1244 | type issueOrPullRequest struct { |
no test coverage detected