Skip to content

Commit 0f9d5ae

Browse files
authored
Merge pull request #1 from chrisgavin/initial-code
Add the initial code.
2 parents 190ee42 + 8f9d643 commit 0f9d5ae

File tree

15 files changed

+667
-0
lines changed

15 files changed

+667
-0
lines changed

.github/workflows/ci.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
name: Build
14+
runs-on: ubuntu-20.04
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
- name: Setup Go
19+
uses: actions/setup-go@v2
20+
with:
21+
go-version: 1.17.1
22+
- name: Build
23+
uses: goreleaser/goreleaser-action@v2
24+
with:
25+
version: latest
26+
args: --rm-dist --snapshot
27+
28+
test:
29+
name: Test
30+
runs-on: ubuntu-20.04
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v2
34+
- name: Setup Go
35+
uses: actions/setup-go@v2
36+
with:
37+
go-version: 1.17.1
38+
- name: Test
39+
run: go test ./...
40+
41+
lint:
42+
name: Lint
43+
runs-on: ubuntu-20.04
44+
steps:
45+
- name: Checkout
46+
uses: actions/checkout@v2
47+
- name: Setup Go
48+
uses: actions/setup-go@v2
49+
with:
50+
go-version: 1.17.1
51+
- name: Check Modules
52+
run: |
53+
go mod tidy
54+
if [ ! -z "$(git status --porcelain=v1)" ]; then
55+
>&2 echo "Please run \`go mod tidy\` and commit the result."
56+
exit 1
57+
fi
58+
- name: Lint
59+
uses: golangci/golangci-lint-action@v2
60+
with:
61+
version: v1.29

.github/workflows/codeql.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: CodeQL
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
schedule:
11+
- cron: 0 0 * * 0
12+
13+
jobs:
14+
analyse:
15+
name: Analyse
16+
runs-on: ubuntu-20.04
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
- name: Initialize CodeQL
22+
uses: github/codeql-action/init@v1
23+
with:
24+
languages: go
25+
- name: Perform CodeQL Analysis
26+
uses: github/codeql-action/analyze@v1

.github/workflows/release.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "**"
7+
8+
jobs:
9+
release:
10+
name: Release
11+
runs-on: ubuntu-20.04
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
with:
16+
fetch-depth: 0
17+
- name: Setup Go
18+
uses: actions/setup-go@v2
19+
with:
20+
go-version: 1.17.1
21+
- name: Release
22+
uses: goreleaser/goreleaser-action@v2
23+
with:
24+
version: latest
25+
args: release --rm-dist
26+
env:
27+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist/
2+
/gh-dispatch

.goreleaser.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
project_name: gh-dispatch
2+
3+
builds:
4+
- goos: [linux, darwin, windows]
5+
goarch: [amd64, arm64]
6+
ldflags:
7+
- -X github.com/chrisgavin/gh-dispatch/internal/version.version={{.Version}}
8+
- -X github.com/chrisgavin/gh-dispatch/internal/version.commit={{.Commit}}
9+
10+
archives:
11+
- format: binary
12+
name_template: "gh-dispatch-{{.Os}}-{{.Arch}}"

cmd/root.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/AlecAivazis/survey/v2"
8+
"github.com/chrisgavin/gh-dispatch/internal/dispatcher"
9+
"github.com/chrisgavin/gh-dispatch/internal/locator"
10+
"github.com/chrisgavin/gh-dispatch/internal/version"
11+
"github.com/cli/go-gh"
12+
"github.com/go-git/go-git/v5"
13+
"github.com/pkg/errors"
14+
log "github.com/sirupsen/logrus"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
type rootFlagFields struct {
19+
}
20+
21+
var rootFlags = rootFlagFields{}
22+
23+
var SilentErr = errors.New("SilentErr")
24+
25+
var rootCmd = &cobra.Command{
26+
Short: "A GitHub CLI extension that makes it easy to dispatch GitHub Actions workflows.",
27+
Version: fmt.Sprintf("%s (%s)", version.Version(), version.Commit()),
28+
SilenceErrors: true,
29+
SilenceUsage: true,
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
workflows, err := locator.ListWorkflowsInRepository()
32+
if err != nil {
33+
return errors.Wrap(err, "Failed to list workflows in repository.")
34+
}
35+
if len(workflows) == 0 {
36+
log.Error("No dispatchable workflows found in repository.")
37+
return SilentErr
38+
}
39+
workflowNames := []string{}
40+
for workflowName := range workflows {
41+
workflowNames = append(workflowNames, workflowName)
42+
}
43+
workflowQuestion := &survey.Select{
44+
Message: "What workflow do you want to dispatch?",
45+
Options: workflowNames,
46+
}
47+
48+
var workflowName string
49+
if err := survey.AskOne(workflowQuestion, &workflowName); err != nil {
50+
return errors.Wrap(err, "Unable to ask for workflow.")
51+
}
52+
53+
workflow := workflows[workflowName]
54+
55+
inputQuestions := []*survey.Question{}
56+
for _, input := range workflow.Inputs {
57+
inputQuestions = append(inputQuestions, &survey.Question{
58+
Name: input.Name,
59+
Prompt: &survey.Input{
60+
Message: fmt.Sprintf("Input for %s:", input.Name),
61+
Help: input.Description,
62+
},
63+
})
64+
}
65+
inputAnswers := map[string]interface{}{}
66+
if err := survey.Ask(inputQuestions, &inputAnswers); err != nil {
67+
return errors.Wrap(err, "Unable to ask for inputs.")
68+
}
69+
70+
currentRepository, err := gh.CurrentRepository()
71+
if err != nil {
72+
return errors.Wrap(err, "Unable to determine current repository. Has it got a remote on GitHub?")
73+
}
74+
75+
gitRepository, err := git.PlainOpen(".")
76+
if err != nil {
77+
return errors.Wrap(err, "Unable to open git repository.")
78+
}
79+
head, err := gitRepository.Head()
80+
if err != nil {
81+
return errors.Wrap(err, "Unable to get repository HEAD.")
82+
}
83+
reference := head.Name().String()
84+
85+
err = dispatcher.DispatchWorkflow(currentRepository, reference, workflowName, inputAnswers)
86+
if err != nil {
87+
return err
88+
}
89+
return nil
90+
},
91+
}
92+
93+
func (f *rootFlagFields) Init(cmd *cobra.Command) error {
94+
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
95+
cmd.PrintErrln(err)
96+
cmd.PrintErrln()
97+
cmd.PrintErr(cmd.UsageString())
98+
return SilentErr
99+
})
100+
101+
return nil
102+
}
103+
104+
func Execute(ctx context.Context) error {
105+
err := rootFlags.Init(rootCmd)
106+
if err != nil {
107+
return err
108+
}
109+
110+
return rootCmd.ExecuteContext(ctx)
111+
}

go.mod

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module github.com/chrisgavin/gh-dispatch
2+
3+
go 1.17
4+
5+
require (
6+
github.com/AlecAivazis/survey/v2 v2.3.2
7+
github.com/cli/go-gh v0.0.3
8+
github.com/go-git/go-git/v5 v5.4.2
9+
github.com/pkg/errors v0.9.1
10+
github.com/sirupsen/logrus v1.8.1
11+
github.com/spf13/cobra v1.4.0
12+
gopkg.in/yaml.v2 v2.4.0
13+
)
14+
15+
require (
16+
github.com/Microsoft/go-winio v0.4.16 // indirect
17+
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
18+
github.com/acomagu/bufpipe v1.0.3 // indirect
19+
github.com/cli/safeexec v1.0.0 // indirect
20+
github.com/cli/shurcooL-graphql v0.0.1 // indirect
21+
github.com/emirpasic/gods v1.12.0 // indirect
22+
github.com/go-git/gcfg v1.5.0 // indirect
23+
github.com/go-git/go-billy/v5 v5.3.1 // indirect
24+
github.com/henvic/httpretty v0.0.6 // indirect
25+
github.com/imdario/mergo v0.3.12 // indirect
26+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
27+
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
28+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
29+
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
30+
github.com/mattn/go-colorable v0.1.2 // indirect
31+
github.com/mattn/go-isatty v0.0.8 // indirect
32+
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
33+
github.com/mitchellh/go-homedir v1.1.0 // indirect
34+
github.com/sergi/go-diff v1.1.0 // indirect
35+
github.com/spf13/pflag v1.0.5 // indirect
36+
github.com/xanzy/ssh-agent v0.3.0 // indirect
37+
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
38+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
39+
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
40+
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
41+
golang.org/x/text v0.3.6 // indirect
42+
gopkg.in/warnings.v0 v0.1.2 // indirect
43+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
44+
)

0 commit comments

Comments
 (0)