OwnerIDAndType returns the ID and OwnerType. The special login "@me" or an empty string queries the current user.
(login string)
| 1244 | |
| 1245 | // OwnerIDAndType returns the ID and OwnerType. The special login "@me" or an empty string queries the current user. |
| 1246 | func (c *Client) OwnerIDAndType(login string) (string, OwnerType, error) { |
| 1247 | if login == "@me" || login == "" { |
| 1248 | var query viewerLogin |
| 1249 | err := c.doQueryWithProgressIndicator("ViewerOwner", &query, nil) |
| 1250 | if err != nil { |
| 1251 | return "", "", err |
| 1252 | } |
| 1253 | return query.Viewer.Id, ViewerOwner, nil |
| 1254 | } |
| 1255 | |
| 1256 | variables := map[string]interface{}{ |
| 1257 | "login": githubv4.String(login), |
| 1258 | } |
| 1259 | var query struct { |
| 1260 | User struct { |
| 1261 | Login string |
| 1262 | Id string |
| 1263 | } `graphql:"user(login: $login)"` |
| 1264 | Organization struct { |
| 1265 | Login string |
| 1266 | Id string |
| 1267 | } `graphql:"organization(login: $login)"` |
| 1268 | } |
| 1269 | |
| 1270 | err := c.doQueryWithProgressIndicator("UserOrgOwner", &query, variables) |
| 1271 | if err != nil { |
| 1272 | // Due to the way the queries are structured, we don't know if a login belongs to a user |
| 1273 | // or to an org, even though they are unique. To deal with this, we try both - if neither |
| 1274 | // is found, we return the error. |
| 1275 | var graphErr api.GraphQLError |
| 1276 | if errors.As(err, &graphErr) { |
| 1277 | if graphErr.Match("NOT_FOUND", "user") && graphErr.Match("NOT_FOUND", "organization") { |
| 1278 | return "", "", err |
| 1279 | } else if graphErr.Match("NOT_FOUND", "organization") { // org isn't found must be a user |
| 1280 | return query.User.Id, UserOwner, nil |
| 1281 | } else if graphErr.Match("NOT_FOUND", "user") { // user isn't found must be an org |
| 1282 | return query.Organization.Id, OrgOwner, nil |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | return "", "", errors.New("unknown owner type") |
| 1288 | } |
| 1289 | |
| 1290 | // issueOrPullRequest is used to query the global id of an issue or pull request by its URL. |
| 1291 | type issueOrPullRequest struct { |
no test coverage detected