Skip to content

Commit

Permalink
export github actions provider
Browse files Browse the repository at this point in the history
  • Loading branch information
walteh committed Nov 19, 2023
1 parent bc63361 commit b02ce3c
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 168 deletions.
123 changes: 2 additions & 121 deletions cmd/simver_github_actions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ package main

import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/rs/zerolog"
"github.com/walteh/simver"
Expand All @@ -15,122 +12,6 @@ import (
"github.com/walteh/terrors"
)

var (
Err = terrors.New("simver.cmd.simver_github_actions.Err")
)

type PullRequestResolver struct {
gh simver.PRProvider
git simver.GitProvider
}

func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails, error) {

head_ref := os.Getenv("GITHUB_REF")

if head_ref != "" && strings.HasPrefix(head_ref, "refs/pull/") {
// this is easy, we know that this is a pr event

num := strings.TrimPrefix(head_ref, "refs/pull/")
num = strings.TrimSuffix(num, "/merge")

n, err := strconv.Atoi(num)
if err != nil {
return nil, Err.Trace(err, "error converting PR number to int")
}

pr, exists, err := p.gh.PRDetailsByPRNumber(ctx, n)
if err != nil {
return nil, Err.Trace(err, "error getting PR details by PR number")
}

if !exists {
return nil, errors.New("PR does not exist, but we are in a PR event")
}

return pr, nil
}

if !strings.HasPrefix(head_ref, "refs/heads/") {
return nil, errors.New("not a PR event and not a push event")
}

branch := strings.TrimPrefix(head_ref, "refs/heads/")

sha := os.Getenv("GITHUB_SHA")

pr, exists, err := p.gh.PRDetailsByCommit(ctx, sha)
if err != nil {
return nil, Err.Trace(err, "error getting PR details by commit")
}

if exists {
return pr, nil
}

isPush := os.Getenv("GITHUB_EVENT_NAME") == "push"

if !isPush {
return nil, errors.New("not a PR event and not a push event")
}

// get the parent commit
parent, err := p.git.CommitFromRef(ctx, "HEAD^")
if err != nil {
return nil, Err.Trace(err, "error getting parent commit")
}

return simver.NewPushSimulatedPRDetails(parent, sha, branch), nil

}

func NewGitHubActionsProvider() (simver.GitProvider, simver.TagProvider, simver.TagWriter, simver.PRProvider, *PullRequestResolver, error) {

token := os.Getenv("GITHUB_TOKEN")
repoPath := os.Getenv("GITHUB_WORKSPACE")
readOnly := os.Getenv("SIMVER_READ_ONLY")

org := os.Getenv("GITHUB_REPOSITORY_OWNER")
repo := os.Getenv("GITHUB_REPOSITORY")

repo = strings.TrimPrefix(repo, org+"/")

c := &gitexec.GitProviderOpts{
RepoPath: repoPath,
Token: token,
User: "github-actions[bot]",
Email: "41898282+github-actions[bot]@users.noreply.github.com",
TokenEnvName: "GITHUB_TOKEN",
GitExecutable: "git",
ReadOnly: readOnly == "true" || readOnly == "1",
}

pr := &gitexec.GHProvierOpts{
GitHubToken: token,
RepoPath: repoPath,
GHExecutable: "gh",
Org: org,
Repo: repo,
}

git, err := gitexec.NewGitProvider(c)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating git provider")
}

gh, err := gitexec.NewGHProvider(pr)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating gh provider")
}

gha, err := NewGitProviderGithubActions(git)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating gh provider")
}

return gha, git, git, gh, &PullRequestResolver{gh, git}, nil
}

func main() {

ctx := context.Background()
Expand All @@ -139,13 +20,13 @@ func main() {

zerolog.SetGlobalLevel(zerolog.DebugLevel)

_, tagprov, tagwriter, _, prr, err := NewGitHubActionsProvider()
_, tagreader, tagwriter, _, prr, err := gitexec.BuildGitHubActionsProviders()
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msg("error creating provider")
os.Exit(1)
}

ee, _, err := simver.LoadExecution(ctx, tagprov, prr)
ee, _, err := simver.LoadExecution(ctx, tagreader, prr)
if err != nil {
zerolog.Ctx(ctx).Error().Err(err).Msgf("error loading execution")
fmt.Println(terrors.FormatErrorCaller(err))
Expand Down
47 changes: 0 additions & 47 deletions cmd/simver_github_actions/provider.go
Original file line number Diff line number Diff line change
@@ -1,48 +1 @@
package main

import (
"context"
"errors"
"os"

"github.com/walteh/simver"
)

type gitProviderGithubActions struct {
internal simver.GitProvider
}

var _ simver.GitProvider = (*gitProviderGithubActions)(nil)

func NewGitProviderGithubActions(ref simver.GitProvider) (simver.GitProvider, error) {
if os.Getenv("GITHUB_ACTIONS") == "true" {
return &gitProviderGithubActions{
internal: ref,
}, nil
} else {
return nil, errors.New("not running in GitHub Actions")
}
}

// Branch implements simver.GitProvider.
func (me *gitProviderGithubActions) Branch(ctx context.Context) (string, error) {
head_ref := os.Getenv("GITHUB_HEAD_REF")
if head_ref != "" {
return head_ref, nil
}
return os.Getenv("GITHUB_REF"), nil
}

// CommitFromRef implements simver.GitProvider.
func (me *gitProviderGithubActions) CommitFromRef(ctx context.Context, ref string) (string, error) {
return me.internal.CommitFromRef(ctx, ref)
}

// GetHeadRef implements simver.GitProvider.
func (me *gitProviderGithubActions) GetHeadRef(ctx context.Context) (string, error) {
head_ref := os.Getenv("GITHUB_HEAD_REF")
if head_ref != "" {
return head_ref, nil
}
return os.Getenv("GITHUB_REF"), nil
}
167 changes: 167 additions & 0 deletions gitexec/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package gitexec

import (
"context"
"errors"
"os"
"strconv"
"strings"

"github.com/walteh/simver"
"github.com/walteh/terrors"
)

var (
Err = terrors.New("ErrExecGitHubActions")
)

func BuildGitHubActionsProviders() (simver.GitProvider, simver.TagProvider, simver.TagWriter, simver.PRProvider, simver.PRResolver, error) {

token := os.Getenv("GITHUB_TOKEN")
repoPath := os.Getenv("GITHUB_WORKSPACE")
readOnly := os.Getenv("SIMVER_READ_ONLY")

org := os.Getenv("GITHUB_REPOSITORY_OWNER")
repo := os.Getenv("GITHUB_REPOSITORY")

repo = strings.TrimPrefix(repo, org+"/")

c := &GitProviderOpts{
RepoPath: repoPath,
Token: token,
User: "github-actions[bot]",
Email: "41898282+github-actions[bot]@users.noreply.github.com",
TokenEnvName: "GITHUB_TOKEN",
GitExecutable: "git",
ReadOnly: readOnly == "true" || readOnly == "1",
}

pr := &GHProvierOpts{
GitHubToken: token,
RepoPath: repoPath,
GHExecutable: "gh",
Org: org,
Repo: repo,
}

git, err := NewGitProvider(c)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating git provider")
}

gh, err := NewGHProvider(pr)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating gh provider")
}

gha, err := NewGitProviderGithubActions(git)
if err != nil {
return nil, nil, nil, nil, nil, Err.Trace(err, "error creating gh provider")
}

return gha, git, git, gh, &PullRequestResolver{gh, git}, nil
}

type PullRequestResolver struct {
gh simver.PRProvider
git simver.GitProvider
}

func (p *PullRequestResolver) CurrentPR(ctx context.Context) (*simver.PRDetails, error) {

head_ref := os.Getenv("GITHUB_REF")

if head_ref != "" && strings.HasPrefix(head_ref, "refs/pull/") {
// this is easy, we know that this is a pr event

num := strings.TrimPrefix(head_ref, "refs/pull/")
num = strings.TrimSuffix(num, "/merge")

n, err := strconv.Atoi(num)
if err != nil {
return nil, Err.Trace(err, "error converting PR number to int")
}

pr, exists, err := p.gh.PRDetailsByPRNumber(ctx, n)
if err != nil {
return nil, Err.Trace(err, "error getting PR details by PR number")
}

if !exists {
return nil, errors.New("PR does not exist, but we are in a PR event")
}

return pr, nil
}

if !strings.HasPrefix(head_ref, "refs/heads/") {
return nil, errors.New("not a PR event and not a push event")
}

branch := strings.TrimPrefix(head_ref, "refs/heads/")

sha := os.Getenv("GITHUB_SHA")

pr, exists, err := p.gh.PRDetailsByCommit(ctx, sha)
if err != nil {
return nil, Err.Trace(err, "error getting PR details by commit")
}

if exists {
return pr, nil
}

isPush := os.Getenv("GITHUB_EVENT_NAME") == "push"

if !isPush {
return nil, errors.New("not a PR event and not a push event")
}

// get the parent commit
parent, err := p.git.CommitFromRef(ctx, "HEAD^")
if err != nil {
return nil, Err.Trace(err, "error getting parent commit")
}

return simver.NewPushSimulatedPRDetails(parent, sha, branch), nil

}

type gitProviderGithubActions struct {
internal simver.GitProvider
}

var _ simver.GitProvider = (*gitProviderGithubActions)(nil)

func NewGitProviderGithubActions(ref simver.GitProvider) (simver.GitProvider, error) {
if os.Getenv("GITHUB_ACTIONS") == "true" {
return &gitProviderGithubActions{
internal: ref,
}, nil
} else {
return nil, errors.New("not running in GitHub Actions")
}
}

// Branch implements simver.GitProvider.
func (me *gitProviderGithubActions) Branch(ctx context.Context) (string, error) {
head_ref := os.Getenv("GITHUB_HEAD_REF")
if head_ref != "" {
return head_ref, nil
}
return os.Getenv("GITHUB_REF"), nil
}

// CommitFromRef implements simver.GitProvider.
func (me *gitProviderGithubActions) CommitFromRef(ctx context.Context, ref string) (string, error) {
return me.internal.CommitFromRef(ctx, ref)
}

// GetHeadRef implements simver.GitProvider.
func (me *gitProviderGithubActions) GetHeadRef(ctx context.Context) (string, error) {
head_ref := os.Getenv("GITHUB_HEAD_REF")
if head_ref != "" {
return head_ref, nil
}
return os.Getenv("GITHUB_REF"), nil
}

0 comments on commit b02ce3c

Please sign in to comment.