Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use current directory by default #5

Merged
merged 10 commits into from
Aug 17, 2024
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
37 changes: 33 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ env:
jobs:
build:
name: build
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up Go
Expand All @@ -27,7 +30,33 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- name: go build
run: go build -v ./...
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
- name: go test
run: go test -v ./...

test-run:
name: test-run
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: go build
run: go build .
- name: Generate GH token
id: generate-token
uses: actions/create-github-app-token@v1
with:
version: v1.58
app-id: ${{ vars.GH_TOKEN_APP_ID }}
private-key: ${{ secrets.GH_TOKEN_APP_PRIVATE_KEY }}
- name: Generate HTML output
run: |
./act3 \
-f html \
-t ./examples/html/template.html
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
31 changes: 31 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: lint

on:
push:
branches: [ "main" ]
pull_request:
paths:
- "go.*"
- "**/*.go"
- ".github/workflows/*.yml"

permissions:
contents: read

env:
GO_VERSION: '1.22.5'

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.58
11 changes: 5 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
tags:
- 'v*'

permissions:
id-token: write

env:
GO_VERSION: '1.22.5'

Expand All @@ -21,20 +24,16 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...
- name: Install Cosign
uses: sigstore/cosign-installer@v3
with:
cosign-release: 'v2.2.3'
- name: Store Cosign private key in a file
run: 'echo "$COSIGN_KEY" > cosign.key'
shell: bash
env:
COSIGN_KEY: ${{secrets.COSIGN_KEY}}
- name: Release Binaries
uses: goreleaser/goreleaser-action@v6
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{secrets.GH_PAT}}
COSIGN_PASSWORD: ${{secrets.COSIGN_PASSWORD}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ cosign.key
cosign.pub
justfile
examples/html/docs/sample.html
.cmds
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
linters:
enable:
- errcheck
- errname
- errorlint
- goconst
- gofumpt
- gosimple
- govet
- ineffassign
- nilerr
- prealloc
- predeclared
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- unconvert
- unused
- usestdlibvars
- wastedassign
14 changes: 9 additions & 5 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: 2

release:
draft: true

before:
hooks:
- go mod tidy
Expand All @@ -14,15 +17,16 @@ builds:

signs:
- cmd: cosign
stdin: "{{.Env.COSIGN_PASSWORD}}"
signature: "${artifact}.sig"
certificate: "${artifact}.pem"
args:
- "sign-blob"
- "--key=cosign.key"
- "--oidc-issuer=https://token.actions.githubusercontent.com"
- "--output-certificate=${certificate}"
- "--output-signature=${signature}"
- "${artifact}"
- "--yes" # needed on cosign 2.0.0+
artifacts: all

- "--yes"
artifacts: checksum

brews:
- name: act3
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ workflows:

`{{runNumber}}` gets replaced with the actual run number of the workflow.

You can find the ID for your workflow as follows:
You can find the ID for a workflow as follows:

```bash
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR_GH_TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/<OWNER>/<REPO>/actions/workflows/<WORKFLOW_FILE>
https://api.github.com/repos/<OWNER>/<REPO>/actions/workflows

# or

gh api repos/<OWNER>/<REPO>/actions/workflows/<WORKFLOW_FILE>
gh api repos/<OWNER>/<REPO>/actions/workflows

# use node_id from the response
```
Expand Down
7 changes: 3 additions & 4 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"os/user"
"strings"

"github.com/dhth/act3/ui"
"github.com/dhth/act3/internal/gh"
"gopkg.in/yaml.v3"
)

type Config struct {
Workflows []ui.Workflow `yaml:"workflows"`
Workflows []gh.Workflow `yaml:"workflows"`
}

func expandTilde(path string) string {
Expand All @@ -24,7 +24,7 @@ func expandTilde(path string) string {
return path
}

func ReadConfig(configFilePath string) ([]ui.Workflow, error) {
func ReadConfig(configFilePath string) ([]gh.Workflow, error) {
localFile, err := os.ReadFile(expandTilde(configFilePath))
if err != nil {
return nil, err
Expand All @@ -36,5 +36,4 @@ func ReadConfig(configFilePath string) ([]ui.Workflow, error) {
}

return config.Workflows, nil

}
100 changes: 100 additions & 0 deletions cmd/current.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

//
import (
"errors"
"fmt"
"net/url"
"strings"

ghapi "github.com/cli/go-gh/v2/pkg/api"
"github.com/dhth/act3/internal/gh"
"github.com/go-git/go-git/v5"
)

var (
errCouldntGetRepo = errors.New("couldn't get repository")
errNoRemotesFound = errors.New("no remotes found")
errRemoteURLEmpty = errors.New("remote URL is empty")
errInvalidURLFormat = errors.New("remote URL has invalid format")
errCouldntParseRemoteURL = errors.New("couldn't parse remote URL")
)

func getWorkflowsForCurrentRepo(ghClient *ghapi.RESTClient, repo string) ([]gh.Workflow, error) {
wd, err := gh.GetWorkflowDetails(ghClient, repo)
if err != nil {
return nil, err
}

workflows := make([]gh.Workflow, len(wd.Workflows))
for i, w := range wd.Workflows {
workflows[i] = gh.Workflow{
ID: w.NodeID,
Repo: repo,
Name: w.Name,
}
}

return workflows, nil
}

func getCurrentRepo() (string, error) {
repo, err := git.PlainOpen(".")
if err != nil {
return "", fmt.Errorf("%w: %s", errCouldntGetRepo, err.Error())
}

remotes, err := repo.Remotes()
if err != nil {
return "", fmt.Errorf("%w: %s", errNoRemotesFound, err.Error())
}

if len(remotes) == 0 {
return "", fmt.Errorf("%w", errNoRemotesFound)
}

remote := remotes[0]
if remote == nil {
return "", fmt.Errorf("%w", errNoRemotesFound)
}

remoteURL := remote.Config().URLs[0]

userRepo, err := extractRepoName(remoteURL)
if err != nil {
return "", fmt.Errorf("%w: %s", errCouldntParseRemoteURL, err.Error())
}

return userRepo, nil
}

func extractRepoName(remoteURL string) (string, error) {
if remoteURL == "" {
return "", fmt.Errorf("%w", errRemoteURLEmpty)
}
if strings.HasPrefix(remoteURL, "git@") {
parts := strings.Split(remoteURL, ":")
if len(parts) < 2 {
return "", fmt.Errorf("%w", errInvalidURLFormat)
}
return strings.TrimSuffix(parts[1], ".git"), nil
}

parsedURL, err := url.Parse(remoteURL)
if err != nil {
return "", fmt.Errorf("%w: %s", errInvalidURLFormat, err.Error())
}

if parsedURL.Scheme == "" {
return "", fmt.Errorf("%w: URL scheme is empty", errInvalidURLFormat)
}

if strings.Count(parsedURL.Path, "/") > 2 {
return "", fmt.Errorf("%w", errInvalidURLFormat)
}

userRepo := strings.TrimSuffix(parsedURL.Path, ".git")
userRepo = strings.TrimPrefix(userRepo, "/")

return userRepo, nil
}
Loading