Skip to content

Commit b065d8f

Browse files
committed
fix: update team creation to match new Forgejo SDK requirements
Updated the team creation logic to use the new UnitsMap structure required by the latest Forgejo SDK version. Added explicit error handling for the team creation process to ensure failures are properly returned.
1 parent 39b1aaa commit b065d8f

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

pkg/provider/gitea/acl.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func (v *Provider) CheckPolicyAllowing(_ context.Context, event *info.Event, all
2323
// we explicitly disallow the policy when there is no team on org
2424
return false, fmt.Sprintf("no teams on org %s", event.Organization)
2525
}
26+
if resp.StatusCode == http.StatusForbidden {
27+
v.Logger.Warnf("policy check: ListOrgTeams returned 403 for org %s, sender %s: %v", event.Organization, event.Sender, err)
28+
return false, fmt.Sprintf("unable to list teams on org %s: the token used doesn't have permission to list teams in this org, make sure the token owner is a member of the org", event.Organization)
29+
}
2630
if err != nil {
2731
// probably a 500 or another api error, no need to try again and again with other teams
2832
return false, fmt.Sprintf("error while getting org team, error: %s", err.Error())

pkg/provider/gitea/acl_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestCheckPolicyAllowing(t *testing.T) {
2525
name string
2626
allowedTeams []string
2727
listOrgReply string
28+
listOrgStatusCode int
2829
listTeamMemberships string
2930
wantAllowed bool
3031
wantReason string
@@ -58,6 +59,13 @@ func TestCheckPolicyAllowing(t *testing.T) {
5859
wantReason: `error while getting org team, error: invalid character 't' in literal true (expecting 'r')`,
5960
listOrgReply: `ttttttaaa`,
6061
},
62+
{
63+
name: "forbidden when listing org teams",
64+
allowedTeams: []string{"allowedTeam"},
65+
listOrgStatusCode: http.StatusForbidden,
66+
wantAllowed: false,
67+
wantReason: "unable to list teams on org myorg: the token used doesn't have permission to list teams in this org, make sure the token owner is a member of the org",
68+
},
6169
}
6270

6371
for _, tt := range tests {
@@ -69,8 +77,12 @@ func TestCheckPolicyAllowing(t *testing.T) {
6977
Organization: "myorg",
7078
Sender: "allowedUser",
7179
}
72-
if tt.listOrgReply != "" {
80+
if tt.listOrgReply != "" || tt.listOrgStatusCode != 0 {
7381
mux.HandleFunc(fmt.Sprintf("/orgs/%s/teams", event.Organization), func(rw http.ResponseWriter, _ *http.Request) {
82+
if tt.listOrgStatusCode != 0 {
83+
rw.WriteHeader(tt.listOrgStatusCode)
84+
return
85+
}
7486
fmt.Fprint(rw, tt.listOrgReply)
7587
})
7688
}

test/pkg/gitea/crd.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitea
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67

78
"codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v3"
@@ -15,8 +16,14 @@ import (
1516
const webhookSecretName = "webhook-secret"
1617

1718
// CreateToken creates gitea token with all scopes.
19+
// It creates the token for the authenticated admin user rather than the org,
20+
// because Forgejo 13+ doesn't allow org tokens to list org teams.
1821
func CreateToken(topts *TestOpts) (string, error) {
19-
token, _, err := topts.GiteaCNX.Client().CreateAccessToken(topts.Opts.Organization, forgejo.CreateAccessTokenOption{
22+
userInfo, _, err := topts.GiteaCNX.Client().GetMyUserInfo()
23+
if err != nil {
24+
return "", fmt.Errorf("failed to get current user info: %w", err)
25+
}
26+
token, _, err := topts.GiteaCNX.Client().CreateAccessToken(userInfo.UserName, forgejo.CreateAccessTokenOption{
2027
Name: topts.TargetNS,
2128
Scopes: []forgejo.AccessTokenScope{forgejo.AccessTokenScopeAll},
2229
})

test/pkg/gitea/scm.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,32 @@ func CreateGiteaRepo(giteaClient *forgejo.Client, user, name, defaultBranch, hoo
112112
// Create a new repo
113113
if onOrg {
114114
logger.Infof("Creating org %s", name)
115+
adminUser := user
115116
user = "org-" + name
116117
_, _, err := giteaClient.CreateOrg(forgejo.CreateOrgOption{
117118
Name: user,
118119
})
119120
if err != nil {
120121
return nil, fmt.Errorf("failed to create org: %w", err)
121122
}
123+
// Ensure admin user is in the Owners team so that ListOrgTeams works
124+
// with the admin token (Forgejo 13+ requires org membership).
125+
teams, _, listErr := giteaClient.ListOrgTeams(user, forgejo.ListTeamsOptions{})
126+
if listErr != nil {
127+
logger.Warnf("failed to list org teams for %s: %v", user, listErr)
128+
} else {
129+
for _, team := range teams {
130+
if team.Name == "Owners" {
131+
_, addErr := giteaClient.AddTeamMember(team.ID, adminUser)
132+
if addErr != nil {
133+
logger.Warnf("failed to add user %s to Owners team in org %s: %v", adminUser, user, addErr)
134+
} else {
135+
logger.Infof("added user %s to Owners team in org %s", adminUser, user)
136+
}
137+
break
138+
}
139+
}
140+
}
122141
logger.Infof("Creating gitea repository on org %s", name)
123142
repo, _, err = giteaClient.CreateOrgRepo(user, forgejo.CreateRepoOption{
124143
Name: name,
@@ -170,13 +189,16 @@ func GetGiteaRepo(giteaClient *forgejo.Client, user, name string, _ *zap.Sugared
170189
func CreateTeam(topts *TestOpts, orgName, teamName string) (*forgejo.Team, error) {
171190
team, _, err := topts.GiteaCNX.Client().CreateTeam(orgName, forgejo.CreateTeamOption{
172191
Permission: forgejo.AccessModeWrite,
173-
Units: []forgejo.RepoUnitType{
174-
forgejo.RepoUnitPulls,
192+
UnitsMap: map[string]string{
193+
string(forgejo.RepoUnitPulls): "write",
175194
},
176195
Name: teamName,
177196
})
197+
if err != nil {
198+
return nil, err
199+
}
178200
topts.ParamsRun.Clients.Log.Infof("Team %s has been created on Org %s", team.Name, orgName)
179-
return team, err
201+
return team, nil
180202
}
181203

182204
func RemoveCommentMatching(topts *TestOpts, commentString *regexp.Regexp) error {

0 commit comments

Comments
 (0)