-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
169 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |