Skip to content

Commit

Permalink
creating branch with graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisStatham committed Jul 15, 2024
1 parent e6fe0ef commit 083eb9f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 18 deletions.
10 changes: 2 additions & 8 deletions internal/multigitter/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,6 @@ func (r *Runner) runSingleRepo(ctx context.Context, repo scm.Repository) (scm.Pu
}
}

if r.UseGHAPI {
// The API requires the branch to exist in order to push a commit to it.
// Force pushing to the branch guarantees it will exist in the state we expect.
_ = sourceController.Push(ctx, "origin", true)
}

cmd := prepareScriptCommand(ctx, repo, tmpDir, r.ScriptPath, r.Arguments)
if r.DryRun {
cmd.Env = append(cmd.Env, "DRY_RUN=true")
Expand Down Expand Up @@ -355,8 +349,8 @@ func (r *Runner) runSingleRepo(ctx context.Context, repo scm.Repository) (scm.Pu
input.Headline = r.CommitMessage
input.BranchName = r.FeatureBranch
array := strings.Split(repo.CloneURL(), "/")
repoName := strings.Trim(array[len(array)-1], ".git")
input.RepositoryNameWithOwner = strings.TrimSpace(fmt.Sprintf("%v/%v\n", array[len(array)-2], repoName))
input.RepositoryName = strings.Trim(array[len(array)-1], ".git")
input.Owner = array[len(array)-2]
input.Additions = sourceController.Additions()
input.Deletions = sourceController.Deletions()
input.ExpectedHeadOid = sourceController.OldHash()
Expand Down
103 changes: 93 additions & 10 deletions internal/scm/github/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,55 @@ func graphQLEndpoint(u string) (string, error) {
return baseEndpoint.String(), nil
}

func (g *Github) getRepositoryID(ctx context.Context, owner string, name string) (error, string) {

Check failure on line 105 in internal/scm/github/graphql.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/scm/github/graphql.go#L105

error-return: error should be the last type when returning multiple items (revive)
Raw output
internal/scm/github/graphql.go:105:1: error-return: error should be the last type when returning multiple items (revive)
func (g *Github) getRepositoryID(ctx context.Context, owner string, name string) (error, string) {
	query := `query($owner: String!, $name: String!) {
	repository(owner: $owner, name: $name) {
	  databaseId
	} 
	}`

	var v getRepositoryInput

	v.Name = name
	v.Owner = owner

	var result getRepositoryOutput

	err := g.makeGraphQLRequest(ctx, query, v, &result)

	if err != nil {
		return err, ""
	}

	return nil, result.Data.Repository.DatabaseId
}
query := `query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
databaseId
}
}`

var v getRepositoryInput

v.Name = name
v.Owner = owner

var result getRepositoryOutput

err := g.makeGraphQLRequest(ctx, query, v, &result)

if err != nil {
return err, ""
}

return nil, result.Data.Repository.DatabaseId
}

func (g *Github) createRef(ctx context.Context, branchName string, oid string, repositoryId string) error {

Check failure on line 128 in internal/scm/github/graphql.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/scm/github/graphql.go#L128

var-naming: method parameter repositoryId should be repositoryID (revive)
Raw output
internal/scm/github/graphql.go:128:80: var-naming: method parameter repositoryId should be repositoryID (revive)
func (g *Github) createRef(ctx context.Context, branchName string, oid string, repositoryId string) error {
                                                                               ^
query := `mutation($input: CreateRefInput!){
createRef(input: $input) {
ref {
name
}
}
}`

var v createRefInput

v.Input.Name = branchName
v.Input.Oid = oid
v.Input.RepositoryId = repositoryId

var result map[string]interface{}

err := g.makeGraphQLRequest(ctx, query, v, &result)

if err != nil {
return err
}

return nil
}

func (g *Github) CommitThroughAPI(ctx context.Context, input CreateCommitOnBranchInput) error {
query := `
mutation ($input: CreateCommitOnBranchInput!) {
Expand All @@ -110,12 +159,24 @@ func (g *Github) CommitThroughAPI(ctx context.Context, input CreateCommitOnBranc
url
}
}
}
`
}`

var v createCommitOnBranchInput

v.Input.Branch.RepositoryNameWithOwner = input.RepositoryNameWithOwner
err, repoID := g.getRepositoryID(ctx, input.Owner, input.BranchName)

if err != nil {
return err
}

err = g.createRef(ctx, input.BranchName, input.ExpectedHeadOid, repoID)

if err != nil {
return err
}

v.Input.Branch.RepositoryNameWithOwner = input.Owner + "/" + input.RepositoryName

v.Input.Branch.BranchName = input.BranchName
v.Input.ExpectedHeadOid = input.ExpectedHeadOid
v.Input.Message.Headline = input.Headline
Expand All @@ -135,7 +196,7 @@ func (g *Github) CommitThroughAPI(ctx context.Context, input CreateCommitOnBranc

var result map[string]interface{}

err := g.makeGraphQLRequest(ctx, query, v, &result)
err = g.makeGraphQLRequest(ctx, query, v, &result)

if err != nil {
return err
Expand Down Expand Up @@ -210,11 +271,33 @@ type createCommitOnBranchInput struct {
} `json:"input"`
}

type createRefInput struct {
Input struct {
Name string `json:"name"`
Oid string `json:"oid"`
RepositoryId string `json:"repositoryId"`

Check failure on line 278 in internal/scm/github/graphql.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/scm/github/graphql.go#L278

var-naming: struct field RepositoryId should be RepositoryID (revive)
Raw output
internal/scm/github/graphql.go:278:3: var-naming: struct field RepositoryId should be RepositoryID (revive)
		RepositoryId string `json:"repositoryId"`
		^
} `json:"input"`
}

type getRepositoryInput struct {
Name string `json:"name"`
Owner string `json:"owner"`
}

type getRepositoryOutput struct {
Data struct {
Repository struct {
DatabaseId string `json:"databaseId"`

Check failure on line 290 in internal/scm/github/graphql.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/scm/github/graphql.go#L290

var-naming: struct field DatabaseId should be DatabaseID (revive)
Raw output
internal/scm/github/graphql.go:290:4: var-naming: struct field DatabaseId should be DatabaseID (revive)
			DatabaseId string `json:"databaseId"`
			^
} `json:"repository"`
} `json:"data"`
}

type CreateCommitOnBranchInput struct {
RepositoryNameWithOwner string
BranchName string
Headline string
Additions map[string]string
Deletions []string
ExpectedHeadOid string
RepositoryName string
Owner string
BranchName string
Headline string
Additions map[string]string
Deletions []string
ExpectedHeadOid string
}

0 comments on commit 083eb9f

Please sign in to comment.