Skip to content

Commit

Permalink
Merge pull request #2 from DanielMabbett/relase/v0.2.0
Browse files Browse the repository at this point in the history
Release/v0.2.0
  • Loading branch information
DanielMabbett authored Nov 15, 2021
2 parents d4bdf23 + a566c04 commit 1ae0857
Show file tree
Hide file tree
Showing 632 changed files with 271,881 additions and 113 deletions.
Empty file added GNUmakefile
Empty file.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# terrabot

[![Go Report Card](https://goreportcard.com/badge/github.com/danielmabbett/terrabot)](https://goreportcard.com/report/github.com/danielmabbett/terrabot)

[![Go](https://github.com/DanielMabbett/terrabot/actions/workflows/go.yml/badge.svg)](https://github.com/DanielMabbett/terrabot/actions/workflows/go.yml)

Push Terraform Plans back to your PRs and make your process more gitops!

The idea originally came from the https://github.com/runatlantis/atlantis tool so check this out first and see if it fulfills your requirements.
Expand All @@ -19,7 +24,7 @@ terraform plan -no-color > plan.txt
# https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
# set your PAC by export PAC=yourpac

terrabot push \
terrabot push azure-devops \
--organisation "test" \
--project "test" \
--repo "test" \
Expand All @@ -29,6 +34,10 @@ terrabot push \

```

Then it provides others in your pull requests with viewing the changes without having to leave the page:

![Screenshot](./images/screenshot1.png)

## Contributors

Contributions are welcome!
Expand Down
124 changes: 124 additions & 0 deletions cmd/azuredevops.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package cmd

import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/microsoft/azure-devops-go-api/azuredevops"
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
"github.com/spf13/cobra"
)

func init() {
// rootCmd.AddCommand(versionCmd)

pushAzureDevOpsCmd.Flags().StringVarP(&organisation, "organisation", "o", "", "the organisation name to target in Azure DevOps Services.")
pushAzureDevOpsCmd.Flags().StringVarP(&project, "project", "p", "", "the project name to target in Azure DevOps Services.")
pushAzureDevOpsCmd.Flags().StringVarP(&repo, "repo", "r", "", "the git repository name to target in Azure DevOps Services.")
pushAzureDevOpsCmd.Flags().IntVarP(&pullRequestID, "pull-request-id", "i", 0, "The pull request ID in for the Git Repo in Azure DevOps Services.")
pushAzureDevOpsCmd.Flags().StringVarP(&personalAccessToken, "token", "t", "", "Your PAC to Azure DevOps Services.")
pushAzureDevOpsCmd.Flags().StringVarP(&planFile, "plan", "", "plan.txt", "The terraform plan file. Currently only supports .txt file outputs.")
pushAzureDevOpsCmd.MarkFlagRequired("organisation")
pushAzureDevOpsCmd.MarkFlagRequired("project")
pushAzureDevOpsCmd.MarkFlagRequired("repo")
pushAzureDevOpsCmd.MarkFlagRequired("pull-request-id")
pushAzureDevOpsCmd.MarkFlagRequired("personal-access-token")
pushAzureDevOpsCmd.MarkFlagRequired("plan")
}

var organisation string
var project string
var repo string
var pullRequestID int
var personalAccessToken string
var planFile string

var pushAzureDevOpsCmd = &cobra.Command{
Use: "azure-devops",
Short: "Push a comment to a Pull Request to Azure DevOps services.",
Run: func(cmd *cobra.Command, args []string) {

// azdo alternative
organizationUrl := "https://dev.azure.com/" + organisation
connection := azuredevops.NewPatConnection(organizationUrl, personalAccessToken)

ctx := context.Background()

gitClient, err := git.NewClient(ctx, connection)
if err != nil {
fmt.Println("error")
fmt.Println(err)
return
}

b, err := ioutil.ReadFile(planFile)
if err != nil {
panic(err)
}
s := string(b)

f, err := os.Open(planFile)
if err != nil {
panic(err)
}
defer f.Close()

scanner := bufio.NewScanner(f)

line := 1

plan := ""
for scanner.Scan() {
if strings.Contains(scanner.Text(), "Plan:") {
plan = scanner.Text()
}

line++
}
if err := scanner.Err(); err != nil {
panic(err)
}

fmt.Println(plan)
overview := plan
overviewWrap := "`" + overview + "`"

fullDetails := s
fullDetailsWrap := "```\n" + fullDetails + "\n```"

content := `🤖 Terrabot Response ⚡
Overview
` + overviewWrap + `
<details><summary>Full Details</summary>
` + fullDetailsWrap

thread := git.CreateThreadArgs{
CommentThread: &git.GitPullRequestCommentThread{
Comments: &[]git.Comment{
{
Content: PtrString(content),
},
},
},
PullRequestId: &pullRequestID,
Project: &project,
RepositoryId: &repo,
}

_, err = gitClient.CreateThread(ctx, thread)
if err != nil {
fmt.Println("Error on Creating Thread")
fmt.Println(err)
return
}

},
}

func PtrString(v string) *string { return &v }
97 changes: 1 addition & 96 deletions cmd/push.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,10 @@
package cmd

import (
"context"
"fmt"
"io/ioutil"
"strings"

"github.com/microsoft/azure-devops-go-api/azuredevops"
"github.com/microsoft/azure-devops-go-api/azuredevops/git"
"github.com/spf13/cobra"
)

func init() {
// rootCmd.AddCommand(versionCmd)

pushCmd.Flags().StringVarP(&organisation, "organisation", "o", "", "the organisation name to target in Azure DevOps Services.")
pushCmd.Flags().StringVarP(&project, "project", "p", "", "the project name to target in Azure DevOps Services.")
pushCmd.Flags().StringVarP(&repo, "repo", "r", "", "the git repository name to target in Azure DevOps Services.")
pushCmd.Flags().IntVarP(&pullRequestID, "pull-request-id", "i", 0, "The pull request ID in for the Git Repo in Azure DevOps Services.")
pushCmd.Flags().StringVarP(&personalAccessToken, "token", "t", "", "Your PAC to Azure DevOps Services.")
pushCmd.Flags().StringVarP(&planFile, "plan", "", "plan.txt", "The terraform plan file. Currently only supports .txt file outputs.")

pushCmd.MarkFlagRequired("organisation")
pushCmd.MarkFlagRequired("project")
pushCmd.MarkFlagRequired("repo")
pushCmd.MarkFlagRequired("pull-request-id")
pushCmd.MarkFlagRequired("personal-access-token")
pushCmd.MarkFlagRequired("plan")
}

var organisation string
var project string
var repo string
var pullRequestID int
var personalAccessToken string
var planFile string

var pushCmd = &cobra.Command{
Use: "push",
Short: "Push a comment to a Pull Request",
Long: `All software has versions. This is Hugo's`,
Run: func(cmd *cobra.Command, args []string) {
// azdo alternative
organizationUrl := "https://dev.azure.com/" + organisation
connection := azuredevops.NewPatConnection(organizationUrl, personalAccessToken)

ctx := context.Background()

gitClient, err := git.NewClient(ctx, connection)
if err != nil {
fmt.Println("error")
fmt.Println(err)
return
}

var text string = "Plan:"

// read the whole file at once
b, err := ioutil.ReadFile(planFile)
if err != nil {
panic(err)
}
s := string(b)
//check whether s contains substring text
fmt.Println(strings.Contains(s, text))
fmt.Println(strings.Trim(s, text))

overviewWrap := "`overview. Comming soon.`"

fullDetails := s
fullDetailsWrap := "```\n" + fullDetails + "\n```"

content := `🤖 Terrabot Response ⚡
Overview
` + overviewWrap + `
<details><summary>Full Details</summary>
` + fullDetailsWrap

thread := git.CreateThreadArgs{
CommentThread: &git.GitPullRequestCommentThread{
Comments: &[]git.Comment{
{
Content: PtrString(content),
},
},
},
PullRequestId: &pullRequestID,
Project: &project,
RepositoryId: &repo,
}

_, err = gitClient.CreateThread(ctx, thread)
if err != nil {
fmt.Println("Error on Creating Thread")
fmt.Println(err)
return
}

},
Short: "Push a terraform output to a PR somewhere.",
}

func PtrString(v string) *string { return &v }
6 changes: 2 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ var (
rootCmd = &cobra.Command{
Use: "terrabot",
Short: "A generator for Cobra based Applications",
Long: `Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
}
)

Expand All @@ -39,8 +36,9 @@ func init() {
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache")

// rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(pushCmd)
pushCmd.AddCommand(pushAzureDevOpsCmd)
}

func initConfig() {
Expand Down
6 changes: 3 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of Hugo",
Long: `All software has versions. This is Hugo's`,
Short: "Print the version number of Terrabot",
Long: `All software has versions. This is Terrabot's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")
fmt.Println("Terrabot Version -- v0.2")
},
}
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module terrabot
go 1.16

require (
github.com/goccy/go-json v0.7.10 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5 // indirect
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/goccy/go-json v0.7.10 h1:ulhbuNe1JqE68nMRXXTJRrUu0uhouf0VevLINxQq4Ec=
github.com/goccy/go-json v0.7.10/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
Expand Down Expand Up @@ -169,8 +167,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
Expand All @@ -186,8 +182,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5 h1:YH424zrwLTlyHSH/GzLMJeu5zhYVZSx5RQxGKm1h96s=
Expand Down
Binary file added images/screenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions vendor/github.com/fsnotify/fsnotify/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1ae0857

Please sign in to comment.