Skip to content

Commit

Permalink
Implement queueing
Browse files Browse the repository at this point in the history
Pipeline runs belonging to one repository now run sequentially. If a
pipeline run cannot start immediately, it is created as "pending", and a
process is started to periodically check if it can start.

Since the pipeline manager service may be restarted, it check on boot if
there are any pending runs for the repositories under its control and
starts the periodic check for those.

We can improve on the design by adding a signal in the finish task of a
pipeline run that the run will soon finish, reducing the up to 30s wait
time for the next run.

Closes #394.
  • Loading branch information
michaelsauter committed Mar 21, 2022
1 parent bf17cc0 commit aeed38c
Show file tree
Hide file tree
Showing 10 changed files with 996 additions and 541 deletions.
102 changes: 102 additions & 0 deletions internal/manager/bitbucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package manager

import (
"fmt"

"github.com/opendevstack/pipeline/pkg/bitbucket"
)

type bitbucketInterface interface {
bitbucket.CommitClientInterface
bitbucket.RawClientInterface
bitbucket.RepoClientInterface
}

type repository struct {
Project struct {
Key string `json:"key"`
} `json:"project"`
Slug string `json:"slug"`
}
type requestBitbucket struct {
EventKey string `json:"eventKey"`
Repository repository `json:"repository"`
Changes []struct {
Type string `json:"type"`
Ref struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
} `json:"ref"`
FromHash string `json:"fromHash"`
ToHash string `json:"toHash"`
} `json:"changes"`
PullRequest *struct {
FromRef struct {
Repository repository `json:"repository"`
ID string `json:"id"`
DisplayID string `json:"displayId"`
LatestCommit string `json:"latestCommit"`
} `json:"fromRef"`
} `json:"pullRequest"`
Comment *struct {
Text string `json:"text"`
} `json:"comment"`
}

func getCommitSHA(bitbucketClient bitbucket.CommitClientInterface, project, repository, gitFullRef string) (string, error) {
commitList, err := bitbucketClient.CommitList(project, repository, bitbucket.CommitListParams{
Until: gitFullRef,
})
if err != nil {
return "", fmt.Errorf("could not get commit list: %w", err)
}
return commitList.Values[0].ID, nil
}

type prInfo struct {
ID int
Base string
}

func extractPullRequestInfo(bitbucketClient bitbucket.CommitClientInterface, projectKey, repositorySlug, gitCommit string) (prInfo, error) {
var i prInfo

prPage, err := bitbucketClient.CommitPullRequestList(projectKey, repositorySlug, gitCommit)
if err != nil {
return i, err
}

for _, v := range prPage.Values {
if !v.Open {
continue
}
i.ID = v.ID
i.Base = v.ToRef.ID
break
}

return i, nil
}

func shouldSkip(bitbucketClient bitbucket.CommitClientInterface, projectKey, repositorySlug, gitCommit string) bool {
c, err := bitbucketClient.CommitGet(projectKey, repositorySlug, gitCommit)
if err != nil {
return false
}
return isCiSkipInCommitMessage(c.Message)
}

// getRepoNames retrieves the name of all repositories within the project
// identified by projectKey.
func getRepoNames(bitbucketClient bitbucket.RepoClientInterface, projectKey string) ([]string, error) {
repos := []string{}
rl, err := bitbucketClient.RepoList(projectKey)
if err != nil {
return repos, err
}
for _, n := range rl.Values {
repos = append(repos, n.Name)
}
return repos, nil
}
Loading

0 comments on commit aeed38c

Please sign in to comment.