Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ else
$(eval export INTEGRATIONS := $(shell $(OPERATIONS_PREFIX)list-updated-integrations))
endif

# For now, we won't allow to run all tests in CI
ifneq ($(CI)$(INTEGRATIONS),true)
$(KARMA) start $(KARMA_FLAGS) $(KARMA_CONF) --single-run;
else
@echo Nothing to test
endif


test-all: install
$(KARMA) start $(KARMA_FLAGS) $(KARMA_CONF) --single-run;
Expand Down
20 changes: 20 additions & 0 deletions operations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ Options:
- `--monorepoPath=<string>`: Local path where the monorepo lives. Default to `.`.
- `--tmpPath=<string>`: Temporal folder. Default to `/tmp/integrations`.

## archive-integration-repository

Archives the repository of the integration:
1. Removes all webhooks.
1. Archives the repository.

Options:
- `--verbose`
- `--integration=<name>`
- `--tmpPath=<string>`: Temporal folder. Default to `/tmp/integrations`.

## boneyard-integration-repository

Moves the repository to the boneyard organization.

Options:
- `--verbose`
- `--integration=<name>`
- `--tmpPath=<string>`: Temporal folder. Default to `/tmp/integrations`.

## libgit2

Install libgit2 v27 following [this script](ci/install-libgit2). This is also required
Expand Down
45 changes: 45 additions & 0 deletions operations/cmd/archive-integration-repository/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"flag"
"os"
"path"

"github.com/segmentio/analytics.js-integrations/operations"
)

const organization = "segment-integrations"

var integrationName string
var tmpPath string

func init() {
flag.BoolVar(&operations.Verbose, "verbose", false, "prints more stuff")
flag.StringVar(&integrationName, "integration", "", "integration name")
flag.StringVar(&tmpPath, "tmpPath", "/tmp/integrations", "path where the integration code is going to be stored")
}

func main() {

operations.GetAuthToken()

flag.Parse()
if integrationName == "" {
operations.Log("No integration provided")
flag.Usage()
os.Exit(1)
}

github := operations.NewGitHubClient()
integration, err := operations.OpenIntegrationRepo(github, organization, integrationName, path.Join(tmpPath, integrationName))
if err != nil {
os.Exit(1)
}

operations.Log("Archiving %s", integration.RepositoryName)

if err := integration.Archive(github); err != nil {
os.Exit(1)
}

}
46 changes: 46 additions & 0 deletions operations/cmd/boneyard-integration-repository/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"flag"
"os"
"path"

"github.com/segmentio/analytics.js-integrations/operations"
)

const organization = "segment-integrations"
const boneyard = "segment-boneyard"

var integrationName string
var tmpPath string

func init() {
flag.BoolVar(&operations.Verbose, "verbose", false, "prints more stuff")
flag.StringVar(&integrationName, "integration", "", "integration name")
flag.StringVar(&tmpPath, "tmpPath", "/tmp/integrations", "path where the integration code is going to be stored")
}

func main() {

operations.GetAuthToken()

flag.Parse()
if integrationName == "" {
operations.Log("No integration provided")
flag.Usage()
os.Exit(1)
}

github := operations.NewGitHubClient()
integration, err := operations.OpenIntegrationRepo(github, organization, integrationName, path.Join(tmpPath, integrationName))
if err != nil {
os.Exit(1)
}

operations.Log("Archiving %s", integration.RepositoryName)

if err := integration.MoveToBoneyard(github, boneyard); err != nil {
os.Exit(1)
}

}
55 changes: 55 additions & 0 deletions operations/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,58 @@ func (g *GitHub) UpdateTopics(topics []string, project Project) error {

return nil
}

// DeleteAllWebHooks retrieves and deletes all webhooks for the project
func (g *GitHub) DeleteAllWebHooks(project Project) error {
ops := github.ListOptions{PerPage: 50}
hooks, _, err := g.V3.Repositories.ListHooks(context.Background(), project.Organization, project.RepositoryName, &ops)
if err != nil {
LogError(err, "Error listing hooks")
return err
}

for _, hook := range hooks {
if _, err := g.V3.Repositories.DeleteHook(context.Background(), project.Organization, project.RepositoryName, *hook.ID); err != nil {
LogError(err, "Error removing hook %d", *hook.ID)
return err
}
}

return nil
}

// ArchiveRepository patches the repo and archives it
func (g *GitHub) ArchiveRepository(project Project) error {
if project.IsArchived {
return nil
}

repo := github.Repository{
Archived: github.Bool(true),
}

if _, _, err := g.V3.Repositories.Edit(context.Background(), project.Organization, project.RepositoryName, &repo); err != nil {
LogError(err, "Error archiving repository %s", project.RepositoryName)
return err
}

return nil
}

// Transfer moves the repository to another organization
func (g *GitHub) Transfer(project Project, organization string) error {
if project.Organization == organization {
return nil
}

transfer := github.TransferRequest{
NewOwner: organization,
}

if _, _, err := g.V3.Repositories.Transfer(context.Background(), project.Organization, project.RepositoryName, transfer); err != nil {
LogError(err, "Error transfering repository %s", project.RepositoryName)
return err
}

return nil
}
31 changes: 31 additions & 0 deletions operations/integrationrepo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package operations

import (
"errors"
"fmt"
"io/ioutil"
"path"
Expand Down Expand Up @@ -417,3 +418,33 @@ func (i *IntegrationRepo) updateIssueTemplate(monorepo Monorepo) error {

return writeFileWithTemplate(file, i.issueTmpl, info)
}

// Archive archives the repo and removes webhooks.
func (i *IntegrationRepo) Archive(github *GitHub) error {
if i.IsArchived {
return nil
}

if !i.IsMigrated() {
err := errors.New("The integration has not been migrated. Migrate the integration and then archive the repository")
LogError(err, "Unable to archive %s", i.Name)
return err
}

if err := github.DeleteAllWebHooks(i.Project); err != nil {
return err
}

return github.ArchiveRepository(i.Project)
}

// MoveToBoneyard moves the repo to the boneyard organization only if it was archived and migrated.
func (i *IntegrationRepo) MoveToBoneyard(github *GitHub, boneyard string) error {
if !i.IsMigrated() || !i.IsArchived {
err := errors.New("The integration has not been migrated or archived. Migrate and archive the integration and then try again")
LogError(err, "Unable to move %s", i.Name)
return err
}

return github.Transfer(i.Project, boneyard)
}