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
|
||||
}
|
||||
@@ -195,3 +195,25 @@ func TestCreateReviewCommentsSubmitsOneReview(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanWrite(t *testing.T) {
|
||||
tests := []struct {
|
||||
name, permission, role string
|
||||
want bool
|
||||
}{
|
||||
{"admin", "admin", "admin", true},
|
||||
{"write", "write", "write", true},
|
||||
{"maintain by role", "write", "maintain", true},
|
||||
{"read", "read", "read", false},
|
||||
{"triage", "read", "triage", false},
|
||||
{"none", "none", "", false},
|
||||
{"empty", "", "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := canWrite(tt.permission, tt.role); got != tt.want {
|
||||
t.Errorf("canWrite(%q, %q) = %v, want %v", tt.permission, tt.role, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
apiVersion: dev.archie.package.v1
|
||||
displayName: GitHub
|
||||
description: GitHub issues, pull requests and reviews for archie-core, as a token or a GitHub App.
|
||||
version: 1.2.0
|
||||
version: 1.3.0
|
||||
contributes:
|
||||
extensions:
|
||||
- surface: forge
|
||||
|
||||
Reference in new issue
Block a user