Skip to content

Commit

Permalink
Implement the 'issue' subcommand (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucktay authored Jul 21, 2019
1 parent 5eb64ee commit dded605
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 20 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ A support tool for use with Terraform stacks, Azure DevOps build pipelines, and

It currently has the following functions:

- queueing Terraform builds in Azure DevOps
- initialising Terraform against remote state storage, for local execution
- queueing Terraform builds in an Azure DevOps CI/CD pipeline
- cancelling unneeded releases of aforementioned builds
- creating GitHub issues in corresponding projects

Expand All @@ -18,16 +19,25 @@ All of these functions are executed contextually against a specific Terraform st

There are numerous installation options for `stack`:

- [Homebrew](https://brew.sh/)
- [Homebrew](https://brew.sh)
- building from the source code hosted here
- directly downloading a pre-built binary for your desired platform

### Homebrew

#### First time install

``` shell
brew tap jlucktay/tap
brew install jlucktay/tap/stack
```

#### Ongoing upgrades

``` shell
brew upgrade jlucktay/tap/stack
```

### Building from source

#### Prerequisites
Expand Down Expand Up @@ -62,10 +72,15 @@ from GitHub. Links to the appropriate pages on each site are in the example file

`stack` has several subcommands:

- `init`
- `build`
- `cancel`
- `issue`

### `stack init`

Coming soon!

### `stack build`

``` console
Expand All @@ -79,7 +94,10 @@ Coming soon!

### `stack issue`

Coming soon!
``` console
$ stack issue There's a problem with this stack!
New issue: https://github.com/MyGitHubOrg/MyGitHubRepo/issues/1234
```

## Further implementation ideas

Expand Down
69 changes: 69 additions & 0 deletions cmd/stack/issue.go
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
package main

import (
"context"
"fmt"
"log"
"net/url"
"strings"

"github.com/google/go-github/v26/github"
"github.com/jlucktay/stack/pkg/common"
"github.com/pkg/errors"
"github.com/spf13/viper"
"golang.org/x/oauth2"
)

// issue flow:
// 0. get current stack directory
// 1. send issue to GitHub with appropriate directory tag
// 2. print the URL of the newly-created issue

func createIssue(text ...string) {
// 0
stackPath, errStackPath := common.GetStackPath(
viper.GetString("stackPrefix"),
fmt.Sprintf(
"github.com/%s/%s",
viper.GetString("github.org"),
viper.GetString("github.repo"),
),
)
if errStackPath != nil {
log.Fatal(errStackPath)
}

// 1
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: viper.GetString("github.pat"),
},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)

issueRequest := &github.IssueRequest{
Title: github.String(strings.Join(text, " ")),
Labels: &[]string{
stackPath,
},
}

issue, _, errCreate := client.Issues.Create(
ctx,
viper.GetString("github.org"),
viper.GetString("github.repo"),
issueRequest,
)
if errCreate != nil {
log.Fatal(errors.Wrap(errCreate, "errCreate!\n"))
}

// 2
newIssue, errParse := url.Parse(issue.GetHTMLURL())
if errParse != nil {
log.Fatal(errors.Wrap(errParse, "errParse!\n"))
}

fmt.Printf("New issue: %s\n", newIssue)
}
24 changes: 8 additions & 16 deletions cmd/stack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func main() {

// Set up 'issue' subcommand
issueCommand := flag.NewFlagSet("issue", flag.ExitOnError)
issueCommand.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "issueCommand.Usage: just give me some words!\n")
}

// Set up 'version' subcommand
versionCommand := flag.NewFlagSet("version", flag.ExitOnError)
Expand Down Expand Up @@ -85,9 +88,12 @@ func main() {
log.Fatalf("error parsing cancel flags: %v", errParse)
}
case issueCommand.Name():
if errParse := issueCommand.Parse(os.Args[2:]); errParse != nil {
log.Fatalf("error parsing issue flags: %v", errParse)
// Execute on 'issue' subcommand
if len(os.Args[2:]) == 0 {
issueCommand.Usage()
log.Fatalf("No issue text was given!")
}
createIssue(os.Args[2:]...)
case versionCommand.Name():
versionCommand.Usage()
os.Exit(0)
Expand All @@ -110,18 +116,4 @@ func main() {

cancelCommand.Visit(func(f *flag.Flag) {})
}

// Execute on 'issue' subcommand
if issueCommand.Parsed() {
fmt.Println("'issue' is not yet implemented.") // TODO
os.Exit(0)

if issueCommand.NFlag() == 0 {
fmt.Println("Please specify an issue to create/update.")
issueCommand.PrintDefaults()
os.Exit(1)
}

issueCommand.Visit(func(f *flag.Flag) {})
}
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/jlucktay/stack

go 1.12

require github.com/spf13/viper v1.4.0
require (
github.com/google/go-github/v26 v26.1.3
github.com/pkg/errors v0.8.0
github.com/spf13/viper v1.4.0
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-github/v26 v26.1.3 h1:n03e8IGgLdD78L+ETWxvqpBIBWEZLlTBCQVU2yImw1o=
github.com/google/go-github/v26 v26.1.3/go.mod h1:v6/FmX9au22j4CtYxnMhJJkP+JfOQDXALk7hI+MPDNM=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand All @@ -58,6 +62,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -96,18 +101,22 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco=
golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down

0 comments on commit dded605

Please sign in to comment.