-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from pipedrive/update-status-with-remote
Update status with remote
- Loading branch information
Showing
8 changed files
with
250 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Launch", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "debug", | ||
"program": "${workspaceRoot}", | ||
"buildFlags": "-ldflags=-X=main.version=0.0.28", | ||
"env": { | ||
"GITHUB_API_TOKEN":"x" | ||
}, | ||
"cwd": "x", | ||
"args": [ | ||
"sync" | ||
], | ||
"port": 8080, | ||
"host": "127.0.0.1" | ||
} | ||
] | ||
} |
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 +1 @@ | ||
0.0.28 | ||
0.0.29 |
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
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,114 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/Clever/microplane/initialize" | ||
"github.com/Clever/microplane/lib" | ||
"github.com/Clever/microplane/merge" | ||
"github.com/Clever/microplane/push" | ||
"github.com/Clever/microplane/sync" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var syncCmd = &cobra.Command{ | ||
Use: "sync", | ||
Short: "Sync workflow status with remote repo", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// find files and folders to explain the status of each repo | ||
initPath := outputPath("", "init") | ||
|
||
if _, err := os.Stat(initPath); os.IsNotExist(err) { | ||
log.Fatalf("must run init first: %s\n", err.Error()) | ||
} | ||
|
||
var initOutput initialize.Output | ||
if err := loadJSON(initPath, &initOutput); err != nil { | ||
log.Fatalf("error loading init.json: %s\n", err.Error()) | ||
} | ||
|
||
repos, err := whichRepos(cmd) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
err = parallelize(repos, syncOneRepo) | ||
if err != nil { | ||
// TODO: dig into errors and display them with more detail | ||
log.Fatal(err) | ||
} | ||
}, | ||
} | ||
|
||
func syncOneRepo(r lib.Repo, ctx context.Context) error { | ||
log.Printf("syncing: %s/%s", r.Owner, r.Name) | ||
repoName := r.Name | ||
|
||
var pushOutput struct { | ||
push.Output | ||
Error string | ||
} | ||
|
||
if !(loadJSON(outputPath(repoName, "push"), &pushOutput) == nil && pushOutput.Success) { | ||
return nil | ||
} | ||
output, err := syncPush(r, ctx, pushOutput.Output) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var mergeOutput struct { | ||
merge.Output | ||
Error string | ||
} | ||
|
||
if loadJSON(outputPath(repoName, "merge"), &mergeOutput) == nil && mergeOutput.Success { | ||
return nil | ||
} | ||
|
||
if err := syncMerge(r, ctx, output); err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("synced: %s/%s", r.Owner, r.Name) | ||
return nil | ||
} | ||
|
||
func syncPush(r lib.Repo, ctx context.Context, pushOutput push.Output) (sync.Output, error) { | ||
|
||
var output sync.Output | ||
var err error | ||
if r.IsGitlab() { | ||
output, err = sync.GitlabSyncPush(ctx, r, pushOutput, repoLimiter) | ||
} else if r.IsGithub() { | ||
output, err = sync.GithubSyncPush(ctx, r, pushOutput, repoLimiter) | ||
} | ||
if err != nil { | ||
return sync.Output{}, err | ||
} | ||
pushOutput.CommitSHA = output.CommitSHA | ||
pushOutput.PullRequestCombinedStatus = output.PullRequestCombinedStatus | ||
|
||
writeJSON(pushOutput, outputPath(r.Name, "push")) | ||
return output, nil | ||
} | ||
func syncMerge(r lib.Repo, ctx context.Context, output sync.Output) error { | ||
if !output.Merged { | ||
return nil | ||
} | ||
|
||
mergeOutputPath := outputPath(r.Name, "merge") | ||
mergeWorkDir := filepath.Dir(mergeOutputPath) | ||
if err := os.MkdirAll(mergeWorkDir, 0755); err != nil { | ||
return err | ||
} | ||
|
||
writeJSON(merge.Output{ | ||
Success: true, | ||
MergeCommitSHA: output.MergeCommitSHA, | ||
}, mergeOutputPath) | ||
return 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,43 @@ | ||
package sync | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/Clever/microplane/lib" | ||
"github.com/Clever/microplane/push" | ||
) | ||
|
||
type Output struct { | ||
CommitSHA string | ||
PullRequestCombinedStatus string | ||
MergeCommitSHA string | ||
Merged bool | ||
} | ||
|
||
func GithubSyncPush(ctx context.Context, r lib.Repo, po push.Output, repoLimiter *time.Ticker) (Output, error) { | ||
// Create Github Client | ||
p := lib.NewProviderFromConfig(r.ProviderConfig) | ||
client, err := p.GithubClient(ctx) | ||
if err != nil { | ||
return Output{}, err | ||
} | ||
|
||
pr, _, err := client.PullRequests.Get(ctx, r.Owner, r.Name, po.PullRequestNumber) | ||
if err != nil { | ||
return Output{}, err | ||
} | ||
|
||
<-repoLimiter.C | ||
cs, _, err := client.Repositories.GetCombinedStatus(ctx, r.Owner, r.Name, *pr.Head.SHA, nil) | ||
if err != nil { | ||
return Output{}, err | ||
} | ||
|
||
return Output{ | ||
CommitSHA: *pr.Head.SHA, | ||
PullRequestCombinedStatus: *cs.State, | ||
MergeCommitSHA: *pr.MergeCommitSHA, | ||
Merged: *pr.Merged, | ||
}, 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,36 @@ | ||
package sync | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Clever/microplane/lib" | ||
"github.com/Clever/microplane/push" | ||
"github.com/xanzy/go-gitlab" | ||
) | ||
|
||
func GitlabSyncPush(ctx context.Context, r lib.Repo, po push.Output, repoLimiter *time.Ticker) (Output, error) { | ||
// Create client | ||
p := lib.NewProviderFromConfig(r.ProviderConfig) | ||
client, err := p.GitlabClient() | ||
if err != nil { | ||
return Output{}, err | ||
} | ||
pid := fmt.Sprintf("%s/%s", r.Owner, r.Name) | ||
mr, _, err := client.MergeRequests.GetMergeRequest(pid, po.PullRequestNumber, nil) | ||
if err != nil { | ||
return Output{}, err | ||
} | ||
pipelineStatus, err := push.GetPipelineStatus(client, r.Owner, r.Name, &gitlab.ListProjectPipelinesOptions{SHA: &mr.SHA}) | ||
if err != nil { | ||
return Output{}, err | ||
} | ||
|
||
return Output{ | ||
CommitSHA: mr.SHA, | ||
PullRequestCombinedStatus: pipelineStatus, | ||
MergeCommitSHA: mr.MergeCommitSHA, | ||
Merged: mr.MergeStatus == "merged", | ||
}, nil | ||
} |