Skip to content

Commit

Permalink
Support teardown on PR closed (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Dec 29, 2023
1 parent 73b151f commit 6e62845
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ pipeline:
forge_repo_token:
from_secret: FORGE_TOKEN # access token for your forge
when:
event: pull_request
event:
- pull_request
- pull_request_closed
```
## Running from the CLI
Expand All @@ -42,7 +44,7 @@ docker run --rm -it \
-e PLUGIN_FORGE_TYPE="gitea" \
-e PLUGIN_FORGE_URL="https://codeberg.org" \
-e PLUGIN_FORGE_REPO_TOKEN="FORGE_TOKEN" \
-e CI_BUILD_EVENT=pull_request \
-e CI_PIPELINE_EVENT=pull_request \
-e CI_REPO_OWNER=REPO_OWNER \
-e CI_REPO_NAME=REPO_NAME \
-e CI_COMMIT_PULL_REQUEST=99 \
Expand All @@ -57,7 +59,7 @@ plugin-surge-preview
surge --version
```

To [tear down a project on surge.sh](https://surge.sh/help/tearing-down-a-project), run the CLI with the environment variable `CI_BUILD_EVENT=pull_close`.
To [tear down a project on surge.sh](https://surge.sh/help/tearing-down-a-project), run the CLI with the environment variable `CI_PIPELINE_EVENT=pull_request_closed`.

## Credits

Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func run(c *cli.Context) error {
RepoOwner: c.String("repo-owner"),
RepoName: c.String("repo-name"),
PipelineEvent: c.String("pipeline-event"),
PullRequestId: c.Int("pull-request-id"),
PullRequestID: c.Int("pull-request-id"),
ForgeType: c.String("forge-type"),
ForgeUrl: c.String("forge-url"),
ForgeURL: c.String("forge-url"),
ForgeRepoToken: c.String("forge-repo-token"),
}

Expand Down
36 changes: 18 additions & 18 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type Plugin struct {
RepoOwner string
RepoName string
PipelineEvent string
PullRequestId int
PullRequestID int
ForgeType string
ForgeUrl string
ForgeURL string
ForgeRepoToken string
comment *comment
}
Expand All @@ -42,33 +42,33 @@ func (p *Plugin) Exec(ctx context.Context) error {
}

p.comment = &comment{}
err := p.comment.Load(p.ForgeType, p.ForgeUrl, p.ForgeRepoToken)
err := p.comment.Load(p.ForgeType, p.ForgeURL, p.ForgeRepoToken)
if err != nil {
return err
}

switch p.PipelineEvent {
case "pull_request":
return p.deploy(ctx)
case "pull_close":
case "pull_request_closed":
return p.teardown(ctx)
default:
return errors.New("unsupported pipeline event, please only run on pull_request or pull_close")
}
}

func (p *Plugin) deploy(ctx context.Context) error {
url := p.getPreviewUrl()
url := p.getPreviewURL()
repo := p.RepoOwner + "/" + p.RepoName

comment, err := p.comment.Find(ctx, repo, p.PullRequestId)
comment, err := p.comment.Find(ctx, repo, p.PullRequestID)
if err != nil && err.Error() != "Comment not found" {
return err
}

commentText := fmt.Sprintf("Deploying preview to https://%s", url)
fmt.Println(commentText)
comment, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestId, comment, commentText)
comment, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}
Expand All @@ -79,7 +79,7 @@ func (p *Plugin) deploy(ctx context.Context) error {

commentText = fmt.Sprintf("Deployment of preview was successful: https://%s", url)
fmt.Println(commentText)
_, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestId, comment, commentText)
_, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}
Expand All @@ -88,17 +88,17 @@ func (p *Plugin) deploy(ctx context.Context) error {
}

func (p *Plugin) teardown(ctx context.Context) error {
url := p.getPreviewUrl()
url := p.getPreviewURL()
repo := p.RepoOwner + "/" + p.RepoName

comment, err := p.comment.Find(ctx, repo, p.PullRequestId)
comment, err := p.comment.Find(ctx, repo, p.PullRequestID)
if err != nil && err.Error() != "Comment not found" {
return err
}

commentText := fmt.Sprintf("Teading down https://%s\n", url)
commentText := fmt.Sprintf("Tearing down https://%s\n", url)
fmt.Println(commentText)
comment, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestId, comment, commentText)
comment, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}
Expand All @@ -109,19 +109,19 @@ func (p *Plugin) teardown(ctx context.Context) error {

commentText = "Deployment of preview was torn down"
fmt.Println(commentText)
_, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestId, comment, commentText)
_, err = p.comment.UpdateOrCreateComment(ctx, repo, p.PullRequestID, comment, commentText)
if err != nil {
return err
}

return nil
}

func (p *Plugin) getPreviewUrl() string {
func (p *Plugin) getPreviewURL() string {
pattern := regexp.MustCompile(`[^a-zA-Z0-9\-]`)
owner := pattern.ReplaceAllString(p.RepoOwner, "-")
repo := pattern.ReplaceAllString(p.RepoName, "-")
return fmt.Sprintf("%s-%s-pr-%d.surge.sh", owner, repo, p.PullRequestId)
return fmt.Sprintf("%s-%s-pr-%d.surge.sh", owner, repo, p.PullRequestID)
}

func (p *Plugin) runSurgeCommand(teardown bool) error {
Expand All @@ -134,8 +134,8 @@ func (p *Plugin) runSurgeCommand(teardown bool) error {
cmdArg = "teardown"
}

cmd := exec.Command("surge", cmdArg, p.getPreviewUrl(), `--token`, p.SurgeToken)
fmt.Println("#", strings.Join(append(cmd.Args, p.getPreviewUrl(), "--token ****"), " "))
cmd := exec.Command("surge", cmdArg, p.getPreviewURL(), `--token`, p.SurgeToken)
fmt.Println("#", strings.Join(append(cmd.Args, p.getPreviewURL(), "--token ****"), " "))
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
Expand All @@ -145,7 +145,7 @@ func (p *Plugin) runSurgeCommand(teardown bool) error {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
io.Copy(writer, stdout)
_, _ = io.Copy(writer, stdout)
}()

if err := cmd.Run(); err != nil {
Expand Down

0 comments on commit 6e62845

Please sign in to comment.