Skip to content

Commit bc9d7b3

Browse files
authored
[PLATFORM-2803] Archive repositories (#25)
[PLATFORM-2803] Archive & Boneyard repositories
1 parent eb64313 commit bc9d7b3

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ else
7979
$(eval export INTEGRATIONS := $(shell $(OPERATIONS_PREFIX)list-updated-integrations))
8080
endif
8181

82+
# For now, we won't allow to run all tests in CI
83+
ifneq ($(CI)$(INTEGRATIONS),true)
8284
$(KARMA) start $(KARMA_FLAGS) $(KARMA_CONF) --single-run;
85+
else
86+
@echo Nothing to test
87+
endif
88+
8389

8490
test-all: install
8591
$(KARMA) start $(KARMA_FLAGS) $(KARMA_CONF) --single-run;

operations/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ Options:
4949
- `--monorepoPath=<string>`: Local path where the monorepo lives. Default to `.`.
5050
- `--tmpPath=<string>`: Temporal folder. Default to `/tmp/integrations`.
5151

52+
## archive-integration-repository
53+
54+
Archives the repository of the integration:
55+
1. Removes all webhooks.
56+
1. Archives the repository.
57+
58+
Options:
59+
- `--verbose`
60+
- `--integration=<name>`
61+
- `--tmpPath=<string>`: Temporal folder. Default to `/tmp/integrations`.
62+
63+
## boneyard-integration-repository
64+
65+
Moves the repository to the boneyard organization.
66+
67+
Options:
68+
- `--verbose`
69+
- `--integration=<name>`
70+
- `--tmpPath=<string>`: Temporal folder. Default to `/tmp/integrations`.
71+
5272
## libgit2
5373

5474
Install libgit2 v27 following [this script](ci/install-libgit2). This is also required
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"os"
6+
"path"
7+
8+
"github.com/segmentio/analytics.js-integrations/operations"
9+
)
10+
11+
const organization = "segment-integrations"
12+
13+
var integrationName string
14+
var tmpPath string
15+
16+
func init() {
17+
flag.BoolVar(&operations.Verbose, "verbose", false, "prints more stuff")
18+
flag.StringVar(&integrationName, "integration", "", "integration name")
19+
flag.StringVar(&tmpPath, "tmpPath", "/tmp/integrations", "path where the integration code is going to be stored")
20+
}
21+
22+
func main() {
23+
24+
operations.GetAuthToken()
25+
26+
flag.Parse()
27+
if integrationName == "" {
28+
operations.Log("No integration provided")
29+
flag.Usage()
30+
os.Exit(1)
31+
}
32+
33+
github := operations.NewGitHubClient()
34+
integration, err := operations.OpenIntegrationRepo(github, organization, integrationName, path.Join(tmpPath, integrationName))
35+
if err != nil {
36+
os.Exit(1)
37+
}
38+
39+
operations.Log("Archiving %s", integration.RepositoryName)
40+
41+
if err := integration.Archive(github); err != nil {
42+
os.Exit(1)
43+
}
44+
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"os"
6+
"path"
7+
8+
"github.com/segmentio/analytics.js-integrations/operations"
9+
)
10+
11+
const organization = "segment-integrations"
12+
const boneyard = "segment-boneyard"
13+
14+
var integrationName string
15+
var tmpPath string
16+
17+
func init() {
18+
flag.BoolVar(&operations.Verbose, "verbose", false, "prints more stuff")
19+
flag.StringVar(&integrationName, "integration", "", "integration name")
20+
flag.StringVar(&tmpPath, "tmpPath", "/tmp/integrations", "path where the integration code is going to be stored")
21+
}
22+
23+
func main() {
24+
25+
operations.GetAuthToken()
26+
27+
flag.Parse()
28+
if integrationName == "" {
29+
operations.Log("No integration provided")
30+
flag.Usage()
31+
os.Exit(1)
32+
}
33+
34+
github := operations.NewGitHubClient()
35+
integration, err := operations.OpenIntegrationRepo(github, organization, integrationName, path.Join(tmpPath, integrationName))
36+
if err != nil {
37+
os.Exit(1)
38+
}
39+
40+
operations.Log("Archiving %s", integration.RepositoryName)
41+
42+
if err := integration.MoveToBoneyard(github, boneyard); err != nil {
43+
os.Exit(1)
44+
}
45+
46+
}

operations/github.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,58 @@ func (g *GitHub) UpdateTopics(topics []string, project Project) error {
567567

568568
return nil
569569
}
570+
571+
// DeleteAllWebHooks retrieves and deletes all webhooks for the project
572+
func (g *GitHub) DeleteAllWebHooks(project Project) error {
573+
ops := github.ListOptions{PerPage: 50}
574+
hooks, _, err := g.V3.Repositories.ListHooks(context.Background(), project.Organization, project.RepositoryName, &ops)
575+
if err != nil {
576+
LogError(err, "Error listing hooks")
577+
return err
578+
}
579+
580+
for _, hook := range hooks {
581+
if _, err := g.V3.Repositories.DeleteHook(context.Background(), project.Organization, project.RepositoryName, *hook.ID); err != nil {
582+
LogError(err, "Error removing hook %d", *hook.ID)
583+
return err
584+
}
585+
}
586+
587+
return nil
588+
}
589+
590+
// ArchiveRepository patches the repo and archives it
591+
func (g *GitHub) ArchiveRepository(project Project) error {
592+
if project.IsArchived {
593+
return nil
594+
}
595+
596+
repo := github.Repository{
597+
Archived: github.Bool(true),
598+
}
599+
600+
if _, _, err := g.V3.Repositories.Edit(context.Background(), project.Organization, project.RepositoryName, &repo); err != nil {
601+
LogError(err, "Error archiving repository %s", project.RepositoryName)
602+
return err
603+
}
604+
605+
return nil
606+
}
607+
608+
// Transfer moves the repository to another organization
609+
func (g *GitHub) Transfer(project Project, organization string) error {
610+
if project.Organization == organization {
611+
return nil
612+
}
613+
614+
transfer := github.TransferRequest{
615+
NewOwner: organization,
616+
}
617+
618+
if _, _, err := g.V3.Repositories.Transfer(context.Background(), project.Organization, project.RepositoryName, transfer); err != nil {
619+
LogError(err, "Error transfering repository %s", project.RepositoryName)
620+
return err
621+
}
622+
623+
return nil
624+
}

operations/integrationrepo.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package operations
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"path"
@@ -417,3 +418,33 @@ func (i *IntegrationRepo) updateIssueTemplate(monorepo Monorepo) error {
417418

418419
return writeFileWithTemplate(file, i.issueTmpl, info)
419420
}
421+
422+
// Archive archives the repo and removes webhooks.
423+
func (i *IntegrationRepo) Archive(github *GitHub) error {
424+
if i.IsArchived {
425+
return nil
426+
}
427+
428+
if !i.IsMigrated() {
429+
err := errors.New("The integration has not been migrated. Migrate the integration and then archive the repository")
430+
LogError(err, "Unable to archive %s", i.Name)
431+
return err
432+
}
433+
434+
if err := github.DeleteAllWebHooks(i.Project); err != nil {
435+
return err
436+
}
437+
438+
return github.ArchiveRepository(i.Project)
439+
}
440+
441+
// MoveToBoneyard moves the repo to the boneyard organization only if it was archived and migrated.
442+
func (i *IntegrationRepo) MoveToBoneyard(github *GitHub, boneyard string) error {
443+
if !i.IsMigrated() || !i.IsArchived {
444+
err := errors.New("The integration has not been migrated or archived. Migrate and archive the integration and then try again")
445+
LogError(err, "Unable to move %s", i.Name)
446+
return err
447+
}
448+
449+
return github.Transfer(i.Project, boneyard)
450+
}

0 commit comments

Comments
 (0)