Skip to content

Commit cda5afc

Browse files
authored
Merge pull request #87 from multiformats/master-upgrade
upgrade@7919415966
2 parents 0d23097 + 4afaf71 commit cda5afc

File tree

17 files changed

+311
-54
lines changed

17 files changed

+311
-54
lines changed

.github/actions/git-config-user/action.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ description: Configure git user
44
runs:
55
using: composite
66
steps:
7-
- run: |
7+
- if: github.event_name == 'workflow_dispatch'
8+
run: |
89
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com>"
910
git config --global user.name "${GITHUB_ACTOR}"
1011
shell: bash
12+
- if: github.event_name != 'workflow_dispatch'
13+
run: |
14+
git config --global user.name "github-actions[bot]"
15+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
16+
shell: bash

.github/workflows/apply.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,6 @@ jobs:
7878
SHA: ${{ needs.prepare.outputs.sha }}
7979
run: gh run download -n "${TF_WORKSPACE}_${SHA}.tfplan" --repo "${GITHUB_REPOSITORY}"
8080
- name: Terraform Apply
81-
run: terraform apply -lock-timeout=0s -no-color "${TF_WORKSPACE}.tfplan"
81+
run: |
82+
terraform show -json > $TF_WORKSPACE.tfstate.json
83+
terraform apply -lock-timeout=0s -no-color "${TF_WORKSPACE}.tfplan"

.github/workflows/fix.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ jobs:
104104
run: npm ci && npm run build
105105
working-directory: scripts
106106
- name: Fix
107+
id: fix
107108
run: node lib/actions/fix-yaml-config.js
108109
working-directory: scripts
109110
- name: Upload YAML config
@@ -113,6 +114,15 @@ jobs:
113114
path: github/${{ env.TF_WORKSPACE }}.yml
114115
if-no-files-found: error
115116
retention-days: 1
117+
# NOTE(galargh, 2024-02-15): This will only work if GitHub as Code is used for a single organization
118+
- name: Comment on pull request
119+
if: github.event_name == 'pull_request_target' && steps.fix.outputs.comment
120+
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
121+
with:
122+
header: fix
123+
number: ${{ github.event.pull_request.number }}
124+
message: ${{ steps.fix.outputs.comment }}
125+
116126
push:
117127
needs: [prepare, fix]
118128
permissions:

.github/workflows/plan.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ jobs:
8787
run: terraform init
8888
working-directory: terraform
8989
- name: Plan terraform
90-
run: terraform plan -refresh=false -lock=false -out="${TF_WORKSPACE}.tfplan" -no-color
90+
run: |
91+
terraform show -json > $TF_WORKSPACE.tfstate.json
92+
terraform plan -refresh=false -lock=false -out="${TF_WORKSPACE}.tfplan" -no-color
9193
working-directory: terraform
9294
- name: Upload terraform plan
9395
uses: actions/upload-artifact@v3
@@ -156,6 +158,7 @@ jobs:
156158
- name: Comment on pull request
157159
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
158160
with:
161+
header: plan
159162
number: ${{ github.event.pull_request.number }}
160163
message: |
161164
Before merge, verify that all the following plans are correct. They will be applied as-is after the merge.

.github/workflows/upgrade.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010

1111
jobs:
1212
upgrade:
13-
uses: protocol/github-mgmt-template/.github/workflows/upgrade_reusable.yml@master
13+
uses: pl-strflt/github-mgmt-template/.github/workflows/upgrade_reusable.yml@master
1414
with:
1515
ref: inputs.ref
1616
secrets:

docs/EXAMPLE.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ repositories: # This group defines repositories (https://registry.terraform.io/p
7474
default_branch: master
7575
delete_branch_on_merge: false
7676
description: GitHub Management
77-
homepage_url: https://github.com/protocol/github-mgmt-template
77+
homepage_url: https://github.com/pl-strflt/github-mgmt-template
7878
is_template: false
7979
vulnerability_alerts: false
8080
archive_on_destroy: true
@@ -87,7 +87,7 @@ repositories: # This group defines repositories (https://registry.terraform.io/p
8787
branch: master
8888
path: /docs
8989
template:
90-
owner: protocol
90+
owner: pl-strflt
9191
repository: github-mgmt-template
9292
topics:
9393
- github

docs/HOWTOS.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
*Example*
1313

14-
I want to invite `galargh` as an admin to `protocol` organization through GitHub Management.
14+
I want to invite `galargh` as an admin to `pl-strflt` organization through GitHub Management.
1515

1616
I ensure the YAML configuration file has the following entry:
1717
```yaml
@@ -29,7 +29,7 @@ I push my changes to a new branch and create a PR. An admin reviews the PR and m
2929
3030
*Example*
3131
32-
I want to demote `galargh` from being an `admin` of `protocol` organization to a regular `member` through GitHub Management.
32+
I want to demote `galargh` from being an `admin` of `pl-strflt` organization to a regular `member` through GitHub Management.
3333

3434
I change the entry for `galargh` in the YAML configuration file from:
3535
```yaml
@@ -54,7 +54,7 @@ I push my changes to a new branch and create a PR. An admin reviews the PR and m
5454

5555
*Example*
5656

57-
I want to be able to configure who the member of the `protocol` organization is through GitHub Management.
57+
I want to be able to configure who the member of the `pl-strflt` organization is through GitHub Management.
5858

5959
I add `github_membership` to `resource_types` array in [terraform/locals_override.tf](../terraform/locals_override.tf). I push my changes to a new branch and create a PR. An admin reviews the PR and merges the PR if everything looks OK. Then, they synchronize GitHub Management with GitHub configuration.
6060

@@ -67,7 +67,7 @@ I add `github_membership` to `resource_types` array in [terraform/locals_overrid
6767

6868
*Example*
6969

70-
I do not want to configure the roles of `protocol` organization members through GitHub Management anymore.
70+
I do not want to configure the roles of `pl-strflt` organization members through GitHub Management anymore.
7171

7272
I ensure that `terraform/resources_override.tf` contains the following entry:
7373
```tf

scripts/__tests__/__resources__/files/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GitHub Management via Terraform: pl-strflt
22

3-
This repository is responsible for managing GitHub configuration of `pl-strflt` organisation as code with Terraform. It was created from [github-mgmt-template](https://github.com/protocol/github-mgmt-template) and it will receive updates from that repository.
3+
This repository is responsible for managing GitHub configuration of `pl-strflt` organisation as code with Terraform. It was created from [github-mgmt-template](https://github.com/pl-strflt/github-mgmt-template) and it will receive updates from that repository.
44

55
**IMPORTANT**: Having write access to GitHub Management repository can be as powerful as having admin access to the organizations managed by that repository.
66

scripts/__tests__/__resources__/github/default.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ repositories:
7878
triage:
7979
- ipdx
8080
template:
81-
owner: protocol
81+
owner: pl-strflt
8282
repository: github-mgmt-template
8383
visibility: public
8484
vulnerability_alerts: false

scripts/__tests__/__resources__/terraform/terraform.tfstate

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,7 @@
14091409
"svn_url": "https://github.com/pl-strflt/github-mgmt",
14101410
"template": [
14111411
{
1412-
"owner": "protocol",
1412+
"owner": "pl-strflt",
14131413
"repository": "github-mgmt-template"
14141414
}
14151415
],
@@ -1794,7 +1794,7 @@
17941794
"commit_email": "piotr.galar@gmail.com",
17951795
"commit_message": "Update README",
17961796
"commit_sha": "ff32de62d3dd9bc01a2dabd8439d413e8a250dfe",
1797-
"content": "# GitHub Management via Terraform: pl-strflt\n\nThis repository is responsible for managing GitHub configuration of `pl-strflt` organisation as code with Terraform. It was created from [github-mgmt-template](https://github.com/protocol/github-mgmt-template) and it will receive updates from that repository.\n\n**IMPORTANT**: Having write access to GitHub Management repository can be as powerful as having admin access to the organizations managed by that repository.\n\n*NOTE*: Because we don't have merge queue functionality enabled for the repository yet, after a merge, wait for the `Apply` and `Update` workflows to complete before merging any other PRs.\n\nTo learn more, check out:\n- [What is GitHub Management and how does it work?](docs/ABOUT.md)\n- [How to set up GitHub Management?](docs/SETUP.md)\n- [How to work with GitHub Management?](docs/HOWTOS.md)\n",
1797+
"content": "# GitHub Management via Terraform: pl-strflt\n\nThis repository is responsible for managing GitHub configuration of `pl-strflt` organisation as code with Terraform. It was created from [github-mgmt-template](https://github.com/pl-strflt/github-mgmt-template) and it will receive updates from that repository.\n\n**IMPORTANT**: Having write access to GitHub Management repository can be as powerful as having admin access to the organizations managed by that repository.\n\n*NOTE*: Because we don't have merge queue functionality enabled for the repository yet, after a merge, wait for the `Apply` and `Update` workflows to complete before merging any other PRs.\n\nTo learn more, check out:\n- [What is GitHub Management and how does it work?](docs/ABOUT.md)\n- [How to set up GitHub Management?](docs/SETUP.md)\n- [How to work with GitHub Management?](docs/HOWTOS.md)\n",
17981798
"file": "README.md",
17991799
"id": "github-mgmt/README.md",
18001800
"overwrite_on_create": false,
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import {Config} from '../../yaml/config'
2+
import { State } from '../../terraform/state'
3+
import { RepositoryCollaborator } from '../../resources/repository-collaborator'
4+
import { Member } from '../../resources/member'
5+
import { TeamMember } from '../../resources/team-member'
6+
import { RepositoryTeam } from '../../resources/repository-team'
7+
8+
function getAccessSummaryFrom(source: State | Config): Record<string, any> {
9+
const members = source.getResources(Member)
10+
const teamMembers = source.getResources(TeamMember)
11+
const teamRepositories = source.getResources(RepositoryTeam)
12+
const repositoryCollaborators = source.getResources(RepositoryCollaborator)
13+
14+
const usernames = new Set<string>([
15+
...members.map(member => member.username),
16+
...repositoryCollaborators.map(collaborator => collaborator.username),
17+
])
18+
19+
const accessSummary: Record<string, any> = {}
20+
21+
for (const username of usernames) {
22+
const role = members.find(member => member.username === username)?.role
23+
const teams = teamMembers.filter(teamMember => teamMember.username === username).map(teamMember => teamMember.team)
24+
const repositoryCollaborator = repositoryCollaborators.filter(repositoryCollaborator => repositoryCollaborator.username === username)
25+
const teamRepository = teamRepositories.filter(teamRepository => teams.includes(teamRepository.team))
26+
27+
const repositories: Record<string, any> = {}
28+
29+
for (const rc of repositoryCollaborator) {
30+
repositories[rc.repository] = repositories[rc.repository] ?? []
31+
repositories[rc.repository].push({permission: rc.permission, type: 'collaborator'})
32+
}
33+
34+
for (const tr of teamRepository) {
35+
repositories[tr.repository] = repositories[tr.repository] ?? []
36+
repositories[tr.repository].push({permission: tr.permission, type: 'team', team: tr.team})
37+
}
38+
39+
accessSummary[username] = {
40+
role,
41+
teams,
42+
repositories
43+
}
44+
}
45+
46+
return accessSummary
47+
}
48+
49+
function describeAccessSummary(accessSummary: Record<string, any>): string {
50+
const lines: string[] = []
51+
const permissions = ['admin', 'maintain', 'push', 'triage', 'pull']
52+
53+
for (const [username, summary] of Object.entries(accessSummary)) {
54+
lines.push(`User @${username}:`)
55+
if (summary.role !== undefined) {
56+
lines.push(` - is a ${summary.role} of the organization`)
57+
} else {
58+
lines.push(` - is not a member of the organization`)
59+
}
60+
if (Object.keys(summary.repositories).length > 0) {
61+
for (const permission of permissions) {
62+
const buffer = []
63+
const index = permission.indexOf(permission)
64+
for (const [repository, accessList] of Object.entries(summary.repositories) as [string, any][]) {
65+
const access = accessList.find((a: any) => a.permission === permission)
66+
if (access !== undefined) {
67+
const higher = accessList.filter((a: any) => permissions.indexOf(a.permission) < index)
68+
if (higher.length === 0) {
69+
if (access.type === 'collaborator') {
70+
buffer.push(` - ${repository} as a direct collaborator`)
71+
} else {
72+
buffer.push(` - ${repository} through team @${access.team}`)
73+
}
74+
}
75+
}
76+
}
77+
if (buffer.length > 0) {
78+
lines.push(` - has ${permission} access to:`)
79+
lines.push(...buffer)
80+
} else {
81+
lines.push(` - has no ${permission} access to any repository`)
82+
}
83+
}
84+
} else {
85+
lines.push(` - has no access to any repository`)
86+
}
87+
}
88+
89+
return lines.join('\n')
90+
}
91+
92+
export async function getAccessSummaryDescription(): Promise<string> {
93+
const config = Config.FromPath()
94+
95+
const accessSummary = getAccessSummaryFrom(config)
96+
97+
const description = describeAccessSummary(accessSummary)
98+
99+
return description
100+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {Config} from '../../yaml/config'
2+
import {Repository} from '../../resources/repository'
3+
import { State } from '../../terraform/state'
4+
5+
export async function toggleArchivedRepos(): Promise<void> {
6+
const state = await State.New()
7+
const config = Config.FromPath()
8+
9+
const resources = state.getAllResources()
10+
const stateRepositories = state.getResources(Repository)
11+
const configRepositories = config.getResources(Repository)
12+
13+
for (const configRepository of configRepositories) {
14+
if (configRepository.archived) {
15+
config.removeResource(configRepository)
16+
const repository = new Repository(configRepository.name)
17+
repository.archived = true
18+
config.addResource(repository)
19+
} else {
20+
const stateRepository = stateRepositories.find(r => r.name === configRepository.name)
21+
if (stateRepository !== undefined && stateRepository.archived) {
22+
config.addResource(stateRepository)
23+
for (const resource of resources) {
24+
if ('repository' in resource && resource.repository === stateRepository.name) {
25+
config.addResource(resource)
26+
}
27+
}
28+
}
29+
}
30+
}
31+
32+
config.save()
33+
}

scripts/src/main.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import 'reflect-metadata'
22
import {sync} from './sync'
33
import {State} from './terraform/state'
44
import {Config} from './yaml/config'
5+
import { toggleArchivedRepos } from './actions/shared/toggle-archived-repos'
56

6-
async function run(): Promise<void> {
7+
async function runSync(): Promise<void> {
78
const state = await State.New()
89
const config = Config.FromPath()
910

@@ -12,4 +13,13 @@ async function run(): Promise<void> {
1213
config.save()
1314
}
1415

16+
async function runToggleArchivedRepos(): Promise<void> {
17+
await toggleArchivedRepos()
18+
}
19+
20+
async function run(): Promise<void> {
21+
await runSync()
22+
await runToggleArchivedRepos()
23+
}
24+
1525
run()

scripts/src/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,26 @@ export function yamlify(value: any): YAML.ParsedNode {
1515
}
1616
return node
1717
}
18+
19+
export function globToRegex(globPattern: string): RegExp {
20+
const regexPattern = globPattern
21+
.split('')
22+
.map(char => {
23+
if (char === '*') {
24+
return '.*'
25+
} else if (char === '?') {
26+
return '.'
27+
} else if (
28+
['.', '\\', '+', '(', ')', '[', ']', '{', '}', '|', '^', '$'].includes(
29+
char
30+
)
31+
) {
32+
return `\\${char}`
33+
} else {
34+
return char
35+
}
36+
})
37+
.join('')
38+
39+
return new RegExp(`^${regexPattern}$`)
40+
}

0 commit comments

Comments
 (0)