Skip to content

Commit 85d9c61

Browse files
authored
feat: add a shared action for adding a collaborator to all repositories (#179)
1 parent 2837b91 commit 85d9c61

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9+
- shared action for adding a collaborator to all repositories
910
- clean workflow which removes resources from state
1011
- information on how to handle private GitHub Management repository
1112
- warning about GitHub Management repository access
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {Config} from '../../yaml/config.js'
2+
import {Repository} from '../../resources/repository.js'
3+
import * as core from '@actions/core'
4+
import {
5+
Permission,
6+
RepositoryCollaborator
7+
} from '../../resources/repository-collaborator.js'
8+
9+
export async function runAddCollaboratorToAllRepos(
10+
username: string,
11+
permission: Permission,
12+
repositoryFilter: (repository: Repository) => boolean = (): boolean => true
13+
): Promise<void> {
14+
const config = Config.FromPath()
15+
16+
await addCollaboratorToAllRepos(
17+
config,
18+
username,
19+
permission,
20+
repositoryFilter
21+
)
22+
23+
config.save()
24+
}
25+
26+
export async function addCollaboratorToAllRepos(
27+
config: Config,
28+
username: string,
29+
permission: Permission,
30+
repositoryFilter: (repository: Repository) => boolean = () => true
31+
): Promise<void> {
32+
const collaborators = config
33+
.getResources(RepositoryCollaborator)
34+
.filter(c => c.username === username)
35+
36+
const repositories = config
37+
.getResources(Repository)
38+
.filter(r => !r.archived)
39+
.filter(repositoryFilter)
40+
.filter(r => !collaborators.some(c => c.repository === r.name))
41+
42+
for (const repository of repositories) {
43+
const collaborator = new RepositoryCollaborator(
44+
repository.name,
45+
username,
46+
permission
47+
)
48+
core.info(
49+
`Adding ${collaborator.username} as a collaborator with ${collaborator.permission} access to ${collaborator.repository} repository`
50+
)
51+
config.addResource(collaborator)
52+
}
53+
}

0 commit comments

Comments
 (0)