Skip to content

Commit a9f4c3a

Browse files
committed
populate-owners: add test for insertStringSlice
Also update readme, add comments, and remove some console logging
1 parent 0f47559 commit a9f4c3a

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

tools/populate-owners/README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# Populating `OWNERS` and `OWNERS_ALIASES`
22

3-
This utility pulls `OWNERS` and `OWNERS_ALIASES` from upstream OpenShift repositories.
3+
This utility updates the OWNERS files from remote Openshift repositories.
4+
45
Usage:
6+
populate-owners [repo-name-regex]
7+
8+
Args:
9+
[repo-name-regex] A go regex which which matches the repos to update, by default all repos are selected
510

611
```console
7-
$ go run main.go
12+
$ go run main.go [repo-name-regex]
813
```
914

1015
Or, equivalently, execute [`populate-owners.sh`](../../ci-operator/populate-owners.sh) from anywhere in this repository.
@@ -15,13 +20,9 @@ For example, the presence of [`ci-operator/jobs/openshift/origin`](../../ci-oper
1520
The `HEAD` branch for each upstream repository is pulled to extract its `OWNERS` and `OWNERS_ALIASES`.
1621
If `OWNERS` is missing, the utility will ignore `OWNERS_ALIASES`, even if it is present upstream.
1722

18-
Once all the upstream content has been fetched, the utility namespaces any colliding upstream aliases.
19-
Collisions only occur if multiple upstreams define the same alias with different member sets.
20-
When that happens, the utility replaces the upstream alias with a `{organization}-{repository}-{upstream-alias}`.
21-
For example, if [openshift/origin][] and [openshift/installer][] both defined an alias for `security` with different member sets, the utility would rename them to `openshift-origin-security` and `openshift-installer-security` respectively.
22-
23-
After namespacing aliases, the utility writes `OWNERS_ALIASES` to the root of this repository.
24-
If no upstreams define aliases, then the utility removes `OWNER_ALIASES` from the root of this repository.
23+
Any aliases present in the upstream `OWNERS` file will be resolved to the set of usernames they represent in the associated
24+
`OWNERS_ALIASES` file. The local `OWNERS` files will therefore not contain any alias names. This avoids any conflicts between
25+
upstream alias names coming from different repos.
2526

2627
The utility also iterates through the `ci-operator/{type}/{organization}/{repository}` for `{type}` in `config`, `jobs`, and `templates`, writing `OWNERS` to reflect the upstream configuration.
2728
If the upstream did not have an `OWNERS` file, the utility removes the associated `ci-operator/*/{organization}/{repository}/OWNERS`.

tools/populate-owners/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,16 @@ func writeYAML(path string, data interface{}, prefix []string) (err error) {
300300
return encoder.Encode(data)
301301
}
302302

303-
// insertStringSlice inserts a string slice into a given index
304-
// in another string slice. Returns a new slice with the insert
305-
// slice replacing the elements between begin and end index.
303+
// insertStringSlice inserts a string slice into another string slice
304+
// replacing the elements starting with the begin index up to the end
305+
// index. The element at end index in the original slice will remain
306+
// in the resulting slice. Returns a new slice with the elements
307+
// replaced. If the begin index is larger than the end, or either of the
308+
// indexes are out of range of the slice, the original slice is returned
309+
// unmodified.
306310
func insertStringSlice(insert []string, intoSlice []string,
307311
begin int, end int) []string {
308312
if begin > end || begin < 0 || end > len(intoSlice) {
309-
fmt.Printf("invalid begin: %v, or end: %v \n", begin, end)
310313
return intoSlice
311314
}
312315
firstPart := intoSlice[:begin]
@@ -419,10 +422,10 @@ const (
419422
usage = `Update the OWNERS files from remote repositories.
420423
421424
Usage:
422-
%s [repo-name-regex]
425+
%s [repo-name-regex]
423426
424427
Args:
425-
[repo-name-regex] A go regex which which matches the repos to update
428+
[repo-name-regex] A go regex which which matches the repos to update, by default all repos are selected
426429
427430
`
428431
)

tools/populate-owners/main_test.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"io/ioutil"
5-
"log"
65
"os"
76
"os/exec"
87
"path/filepath"
@@ -269,6 +268,27 @@ func TestExtractOwners(t *testing.T) {
269268
}
270269
}
271270

271+
func TestInsertSlice(t *testing.T) {
272+
// test replacing two elements of a slice
273+
given := []string{"alice", "bob", "carol", "david", "emily"}
274+
expected := []string{"alice", "bob", "charlie", "debbie", "emily"}
275+
actual := insertStringSlice([]string{"charlie", "debbie"}, given, 2, 4)
276+
assertEqual(t, actual, expected)
277+
278+
// test replacing all elements after the first
279+
expected = []string{"alice", "eddie"}
280+
actual = insertStringSlice([]string{"eddie"}, given, 1, len(given))
281+
assertEqual(t, actual, expected)
282+
283+
// test invalid begin and end indexes, should return the slice unmodified
284+
actual = insertStringSlice([]string{}, given, 5, 2)
285+
assertEqual(t, given, given)
286+
actual = insertStringSlice([]string{}, given, -1, 2)
287+
assertEqual(t, given, given)
288+
actual = insertStringSlice([]string{}, given, 1, len(given)+1)
289+
assertEqual(t, given, given)
290+
}
291+
272292
func TestResolveAliases(t *testing.T) {
273293
given := &orgRepo{
274294
Owners: &owners{Approvers: []string{"alice", "sig-alias", "david"},
@@ -280,8 +300,6 @@ func TestResolveAliases(t *testing.T) {
280300
Reviewers: []string{"adam", "bob", "carol"}},
281301
Aliases: &aliases{Aliases: map[string][]string{"sig-alias": {"bob", "carol"}}},
282302
}
283-
log.Println("given:", given)
284-
log.Println("expected:", expected)
285303
assertEqual(t, given.resolveOwnerAliases(), expected.Owners)
286304
}
287305

0 commit comments

Comments
 (0)