diff --git a/.changeset/honest-planets-sin.md b/.changeset/honest-planets-sin.md index c3c9d16dd7..5035d8cee4 100644 --- a/.changeset/honest-planets-sin.md +++ b/.changeset/honest-planets-sin.md @@ -2,10 +2,12 @@ '@backstage/plugin-scaffolder-backend': minor --- -**BREAKING** Fixed bug in `publish:github` action that didn't permit to add users as collaborators. +Fixed bug in `publish:github` action that didn't permit to add users as collaborators. This fix required changing the way parameters are passed to the action. -In order to add a team as collaborator, now you must use the `team` field instead of `username`. -In order to add a user as collaborator, you must use the `username` field. +In order to add a team as collaborator, now you must use the `team` field instead of `user`. +In order to add a user as collaborator, you must use the `user` field. + +It's still possible to use the field `username` but is deprecated in favor of `team`. ```yaml - id: publish @@ -18,5 +20,5 @@ In order to add a user as collaborator, you must use the `username` field. - access: ... team: my_team - access: ... - username: my_username + user: my_username ``` diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts index 0fd6373c7e..034c3007c4 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts @@ -399,7 +399,7 @@ describe('publish:github', () => { collaborators: [ { access: 'pull', - username: 'robot-1', + user: 'robot-1', }, { access: 'push', diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts index 89b6a78e72..6e6beb6e53 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts @@ -59,13 +59,17 @@ export function createPublishGithubAction(options: { repoVisibility?: 'private' | 'internal' | 'public'; collaborators?: Array< | { - username: string; + user: string; access: 'pull' | 'push' | 'admin' | 'maintain' | 'triage'; } | { team: string; access: 'pull' | 'push' | 'admin' | 'maintain' | 'triage'; } + | { + username: string; + access: 'pull' | 'push' | 'admin' | 'maintain' | 'triage'; + } >; token?: string; topics?: string[]; @@ -173,13 +177,26 @@ export function createPublishGithubAction(options: { description: 'The type of access for the user', enum: ['push', 'pull', 'admin', 'maintain', 'triage'], }, - username: { + user: { type: 'string', description: 'The name of the user that will be added as a collaborator', }, }, }, + { + properties: { + access: { + type: 'string', + description: 'The type of access for the user', + enum: ['push', 'pull', 'admin', 'maintain', 'triage'], + }, + username: { + type: 'string', + description: 'Not a valid field anymore', + }, + }, + }, { additionalProperties: false, properties: { @@ -327,11 +344,22 @@ export function createPublishGithubAction(options: { if (collaborators) { for (const collaborator of collaborators) { try { - if ('username' in collaborator) { + if ('user' in collaborator) { await client.rest.repos.addCollaborator({ owner, repo, - username: collaborator.username, + username: collaborator.user, + permission: collaborator.access, + }); + } else if ('username' in collaborator) { + ctx.logger.warn( + 'The field `username` is deprecated in favor of `team`', + ); + await client.rest.teams.addOrUpdateRepoPermissionsInOrg({ + org: owner, + team_slug: collaborator.username, + owner, + repo, permission: collaborator.access, }); } else if ('team' in collaborator) { @@ -345,10 +373,7 @@ export function createPublishGithubAction(options: { } } catch (e) { assertError(e); - const name = - 'username' in collaborator - ? collaborator.username - : collaborator.team; + const name = extractCollaboratorName(collaborator); ctx.logger.warn( `Skipping ${collaborator.access} access for ${name}, ${e.message}`, ); @@ -418,3 +443,23 @@ export function createPublishGithubAction(options: { }, }); } + +function extractCollaboratorName( + collaborator: + | { + user: string; + access: 'pull' | 'push' | 'admin' | 'maintain' | 'triage'; + } + | { + team: string; + access: 'pull' | 'push' | 'admin' | 'maintain' | 'triage'; + } + | { + username: string; + access: 'pull' | 'push' | 'admin' | 'maintain' | 'triage'; + }, +) { + if ('username' in collaborator) return collaborator.username; + if ('user' in collaborator) return collaborator.user; + return collaborator.team; +}