Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ ARG base_image=alpine
ARG builder_image=golang

FROM ${builder_image} as builder
ADD . /go/src/github.com/aoldershaw/github-pr-resource
WORKDIR /go/src/github.com/aoldershaw/github-pr-resource
RUN curl -sL https://taskfile.dev/install.sh | sh
ADD . /go/src/github.com/aoldershaw/github-pr-resource
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reordered so we don't have to keep installing the install script every time we change the Go code.

RUN ./bin/task build

FROM ${base_image} as resource

RUN apk add --update --no-cache \
git \
git-lfs \
openssh
openssh \
git-crypt
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't get your install_git_crypt.sh script to work so I switched to this.


COPY scripts/askpass.sh /usr/local/bin/askpass.sh
ADD scripts/install_git_crypt.sh install_git_crypt.sh
RUN ./install_git_crypt.sh && rm ./install_git_crypt.sh

COPY --from=builder /go/src/github.com/aoldershaw/github-pr-resource/build /opt/resource

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ for a single PR.
| `v4_endpoint` | No | `https://api.github.com/graphql` | Endpoint to use for the V4 Github API (Graphql). |
| `paths` | No | `["terraform/*/*.tf"]` | Only produce new versions for commits that include changes to files that match one or more glob patterns or prefixes. Note: this differs from `source.paths` when listing PRs in that it applies on a commit-by-commit basis, whereas the former applies for the full PR. |
| `ignore_paths` | No | `[".ci/"]` | Inverse of the above. Pattern syntax is documented in [filepath.Match](https://golang.org/pkg/path/filepath/#Match), or a path prefix can be specified (e.g. `.ci/` will match everything in the `.ci` directory). |
| `labels` | No | `["bug", "enhancement"]` | The labels on the PR. The pipeline will only trigger on pull requests having at least one of the specified labels. |
| `disable_ci_skip` | No | `true` | Disable ability to skip builds with `[ci skip]` and `[skip ci]` in the commit message. |
| `git_crypt_key` | No | `AEdJVENSWVBUS0VZAAAAA...` | Base64 encoded git-crypt key. Setting this will unlock / decrypt the repository with git-crypt. To get the key simply execute `git-crypt export-key -- - | base64` in an encrypted repository. |
| `disable_git_lfs` | No | `true` | Disable Git LFS, skipping an attempt to convert pointers of files tracked into their corresponding objects when checked out into a working copy. |
Expand Down
8 changes: 7 additions & 1 deletion cmd/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,18 @@ func checkPR(stdin []byte) {
log.Fatalf("failed to create temp dir: %v", err)
}

// github client for checking labels
github, err := resource.NewGithubClient(request.Source.CommonConfig, request.Source.GithubConfig)
if err != nil {
log.Fatalf("failed to create github manager: %v", err)
}

// We never need git-lfs when we check for new commits, so always disable it.
git, err := resource.NewGitClient(request.Source.CommonConfig, true, repoDir, os.Stderr)
if err != nil {
log.Fatalf("failed to create git manager: %v", err)
}
response, err := pr.Check(request, git)
response, err := pr.Check(request, git, github)
if err != nil {
log.Fatalf("check failed: %v", err)
}
Expand Down
13 changes: 13 additions & 0 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ func (m *GithubClient) GetPullRequest(prNumber int, commitRef string) (*PullRequ
}
}
} `graphql:"commits(last:$commitsLast)"`
Labels struct {
Edges []struct {
Node struct {
LabelObject
}
}
} `graphql:"labels(first:$labelsFirst)"`
} `graphql:"pullRequest(number:$prNumber)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
Expand All @@ -213,19 +220,25 @@ func (m *GithubClient) GetPullRequest(prNumber int, commitRef string) (*PullRequ
"repositoryName": githubv4.String(m.Repository),
"prNumber": githubv4.Int(prNumber),
"commitsLast": githubv4.Int(100),
"labelsFirst": githubv4.Int(100),
}

// TODO: Pagination - in case someone pushes > 100 commits before the build has time to start :p
if err := m.V4.Query(context.TODO(), &query, vars); err != nil {
return nil, err
}

labels := make([]LabelObject, len(query.Repository.PullRequest.Labels.Edges))
for _, l := range query.Repository.PullRequest.Labels.Edges {
labels = append(labels, l.Node.LabelObject)
}
for _, c := range query.Repository.PullRequest.Commits.Edges {
if c.Node.Commit.OID == commitRef {
// Return as soon as we find the correct ref.
return &PullRequest{
PullRequestObject: query.Repository.PullRequest.PullRequestObject,
Tip: c.Node.Commit,
Labels: labels,
}, nil
}
}
Expand Down
26 changes: 25 additions & 1 deletion pr/check.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package pr

import (
"fmt"

resource "github.com/aoldershaw/github-pr-resource"
)

func Check(request CheckRequest, git resource.Git) (CheckResponse, error) {
func Check(request CheckRequest, git resource.Git, manager resource.Github) (CheckResponse, error) {
// First check commits
if err := git.Init(nil); err != nil {
return CheckResponse{}, err
}
Expand All @@ -28,6 +31,27 @@ func Check(request CheckRequest, git resource.Git) (CheckResponse, error) {
response[i] = Version{Ref: commit}
}

if len(commits) != 0 && len(request.Source.Labels) != 0 {
// Filter by PR labels
pull, err := manager.GetPullRequest(request.Source.Number, commits[0])
if err != nil {
return nil, fmt.Errorf("failed to get pull request: %s", err)
}
labelFound := false
LabelLoop:
for _, wantedLabel := range request.Source.Labels {
for _, targetLabel := range pull.Labels {
if targetLabel.Name == wantedLabel {
labelFound = true
break LabelLoop
}
}
}
if !labelFound {
return make(CheckResponse, 0), nil
}
}

return response, nil
}

Expand Down
1 change: 1 addition & 0 deletions pr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Source struct {
Paths []string `json:"paths"`
IgnorePaths []string `json:"ignore_paths"`
DisableCISkip bool `json:"disable_ci_skip"`
Labels []string `json:"labels"`
}

// Validate the source configuration.
Expand Down
24 changes: 0 additions & 24 deletions scripts/install_git_crypt.sh

This file was deleted.