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