feat(forge): HasWriteAccess and issue author in the github and gitea extensions (archie #1830)
publish / publish (push) Successful in 2m1s
publish / publish (push) Successful in 2m1s
This commit is contained in:
10 files changed
+416
-135
No files matched your search
@@ -145,6 +145,7 @@ func (c *GitHubClient) AssignedIssues(ctx context.Context, owner, repo, assignee
|
||||
Title: is.GetTitle(),
|
||||
Body: is.GetBody(),
|
||||
Labels: labelNames(is),
|
||||
Author: is.GetUser().GetLogin(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -190,6 +191,7 @@ func (c *GitHubClient) IssuesWithLabel(ctx context.Context, owner, repo, label s
|
||||
Title: is.GetTitle(),
|
||||
Body: is.GetBody(),
|
||||
Labels: labelNames(is),
|
||||
Author: is.GetUser().GetLogin(),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -604,3 +606,34 @@ func (c *GitHubClient) VerifyPush(ctx context.Context, owner, repo string) error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasWriteAccess reports whether user holds write, maintain or admin access to
|
||||
// owner/repo. A user the forge does not know has none.
|
||||
func (c *GitHubClient) HasWriteAccess(ctx context.Context, owner, repo, user string) (bool, error) {
|
||||
if user == "" {
|
||||
return false, nil
|
||||
}
|
||||
level, resp, err := c.gh.Repositories.GetPermissionLevel(ctx, owner, repo, user)
|
||||
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, apiError(fmt.Sprintf("check write access for %s on %s/%s", user, owner, repo), err)
|
||||
}
|
||||
return canWrite(level.GetPermission(), level.GetRoleName()), nil
|
||||
}
|
||||
|
||||
// canWrite maps a permission and role name onto write access. The permission
|
||||
// field folds maintain into write, but the role name is checked too so a
|
||||
// custom role named for it is not dropped.
|
||||
func canWrite(permission, role string) bool {
|
||||
switch strings.ToLower(permission) {
|
||||
case "admin", "write", "maintain":
|
||||
return true
|
||||
}
|
||||
switch strings.ToLower(role) {
|
||||
case "admin", "write", "maintain":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in new issue
Block a user