Skip to content

Commit 5e9d044

Browse files
committed
Archive repositories
1 parent eb64313 commit 5e9d044

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

operations/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ 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+
5263
## libgit2
5364

5465
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+
}

operations/github.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,40 @@ 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+
}

operations/integrationrepo.go

Lines changed: 20 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,22 @@ 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+
}

0 commit comments

Comments
 (0)