Skip to content

customise BASE_URL for git-lfs download #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
62c9f13
Add fork explanation to README
Mar 20, 2018
e148fc8
Add create_comments option to source to disable creating comments
Mar 20, 2018
27bfda7
Use pull request updatedDate for version
mmb Mar 25, 2018
b44b8a6
When there are no versions return empty list instead of null
mmb Mar 28, 2018
c38e8a0
Fix Concourse docs links
mmb Mar 31, 2018
d75905a
Fix resource repository and example uri
mmb Mar 31, 2018
3d8dbd9
README yaml formatting
mmb Mar 31, 2018
fd1fe23
Add test script and travis build
mmb Mar 31, 2018
aa5dbfe
Merge pull request #3 from mmb/test
mmb Mar 31, 2018
64c2f51
Add Travis badge to README
mmb Mar 31, 2018
bb0435d
Update Dockerfile with hadolint recommendations
mmb Apr 5, 2018
6d93140
Add hashbangs and shellcheck directives
mmb Apr 5, 2018
f0d349e
Merge pull request #4 from mmb/lint-first-pass
mmb Apr 5, 2018
524462d
Check should error if git ls-remote uri fails
mmb Apr 6, 2018
8ccdfa3
Bump openssh-client version
mmb Apr 6, 2018
b69f1ad
Merge pull request #5 from mmb/bad-uri
mmb Apr 6, 2018
4e4ee82
Add the original branch name as a git config value to be used later i…
Apr 17, 2018
74a1190
Fix error on pull requests that are already merged
mmb May 12, 2018
4de5f78
Merge pull request #8 from mmb/already-merged
mmb May 12, 2018
7a58319
Fix error on pull requests that are declined
mmb May 14, 2018
9faf2b2
Merge pull request #9 from mmb/declined
mmb May 14, 2018
3b14d3f
Merge pull request #6 from emerald-squad/personnal-acces-token
mmb May 26, 2018
70ef703
Switch from gliderlabs/alpine to official alpine for docker base
mmb May 26, 2018
3d4332a
Merge pull request #10 from mmb/alpine-image
mmb May 26, 2018
299c612
Merge branch 'add-branch-name' of https://github.com/emerald-squad/co…
mmb May 26, 2018
39f97f5
Merge branch 'emerald-squad-add-branch-name'
mmb May 26, 2018
88d67ab
Update pullrequest.branch setting with more specific errors
mmb May 26, 2018
4232a80
Add a custom comment after the comment made by the resource
Apr 25, 2018
5378186
- Add optional sleep_between_fetches to avoid overloading bitBucket s…
ywei2017 Mar 16, 2020
7d13234
Merge pull request #18 from ywei2017/master
mmb Mar 20, 2020
ed38e9c
Add global cleanup function
mmb May 12, 2020
ad45c83
Check if ssh-agent is running before killall
mmb May 12, 2020
d283701
Only clean up temp files created by bitbucket_request
mmb May 12, 2020
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
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: required

services:
- docker

script: scripts/test
21 changes: 10 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
FROM gliderlabs/alpine:edge
FROM alpine:3.7

RUN apk --update add \
ca-certificates \
bash \
jq \
curl \
git \
openssh-client
RUN apk --no-cache add \
bash=4.4.19-r1 \
ca-certificates=20190108-r0 \
curl=7.61.1-r3 \
git=2.15.4-r0 \
jq=1.5-r5 \
openssh-client=7.5_p1-r10

# can't `git pull` unless we set these
RUN git config --global user.email "git@localhost" && \
git config --global user.name "git"

ADD scripts/install_git_lfs.sh install_git_lfs.sh
COPY scripts/install_git_lfs.sh install_git_lfs.sh
RUN ./install_git_lfs.sh

ADD assets/ /opt/resource/
RUN chmod +x /opt/resource/*
COPY assets /opt/resource
111 changes: 65 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
> This resource is no longer actively maintained.
[![Build Status](https://travis-ci.org/mmb/concourse-bitbucket-pullrequest-resource.svg?branch=master)](https://travis-ci.org/mmb/concourse-bitbucket-pullrequest-resource)

This resource is a fork of
https://github.com/laurentverbruggen/concourse-bitbucket-pullrequest-resource.

That resource is no longer maintained but this one will continue to be
developed.

# Concourse Bitbucket Pull Request Resource

Expand All @@ -17,13 +23,13 @@ Use this resource by adding the following to the `resource_types` section of a p
```yaml
---
resource_types:
- name: concourse-bitbucket-pullrequest
type: docker-image
source:
repository: laurentverbruggen/concourse-bitbucket-pullrequest-resource
- name: concourse-bitbucket-pullrequest
type: docker-image
source:
repository: mm62/concourse-bitbucket-pullrequest-resource
```

See [concourse docs](http://concourse.ci/configuring-resource-types.html) for more details on adding `resource_types` to a pipeline config.
See [concourse docs](https://concourse-ci.org/resource-types.html) for more details on adding `resource_types` to a pipeline config.

## Source Configuration

Expand Down Expand Up @@ -60,6 +66,8 @@ See [concourse docs](http://concourse.ci/configuring-resource-types.html) for mo
* `only_for_branch`: *Optional.* If specified only pull requests which target those branches will be considered.
It will accept a regular expression as determined by [egrep](http://linuxcommand.org/man_pages/egrep1.html).

* `sleep_between_fetches`: *Optional (default: 0).* Number of seconds to sleep between checking pull requests, avoiding too frequent fetches for check method.

* `only_without_conflicts`: *Optional (default: true).* If enabled only pull requests which are not in a conflicted state will be built.

* `only_when_mergeable`: *Optional (default: false).* If enabled only pull requests which are mergeable (all tasks done, required number of approvers reached, ...) will be built.
Expand All @@ -71,6 +79,8 @@ It will accept a regular expression as determined by [egrep](http://linuxcommand
* `rebuild_phrase`: *Optional (default: test this please).* Regular expression as determined by [egrep](http://linuxcommand.org/man_pages/egrep1.html) will match all comments in pull request overview.
If a match is found the pull request will be rebuilt.

* `create_comments`: *Optional (default: false).* If true write comments with build status to pull requests.

## Behavior

### `check`: Search for pull requests to build.
Expand All @@ -82,11 +92,13 @@ Check will return a version for every pull request that matches the criteria def
Clones the repository to the destination, and locks it down to a given ref.

** IMPORTANT **
It is essential that you set the [version](https://concourse.ci/get-step.html#get-version) to `every` on the get step of your job configuration.
It is essential that you set the [version](https://concourse-ci.org/get-step.html#get-step-version) to `every` on the get step of your job configuration.
It will allow you to build all versions instead of only the latest.

Submodules are initialized and updated recursively.

Note: the name of the branch from which the pull request has been created is stored in the special git config `pullrequest.branch` so that you can use it as reference in your pipeline.

#### Parameters

* `depth`: *Optional.* If a positive integer is given, *shallow* clone the repository using the `--depth` option. Using this flag voids your warranty.
Expand All @@ -110,52 +122,59 @@ Set the status message on specified pull request.

* `status`: *Required.* The status of success, failure or pending.

* [`on_success`](https://concourse.ci/on-success-step.html) and [`on_failure`](https://concourse.ci/on-failure-step.html) triggers may be useful for you when you wanted to reflect build result to the pull request (see the example below).
* [`on_success`](https://concourse-ci.org/on-success-step-hook.html) and [`on_failure`](https://concourse-ci.org/on-failure-step-hook.html) triggers may be useful for you when you wanted to reflect build result to the pull request (see the example below).

* `comment`: *Optional.* A custom comment that you want added to the status message.
Any occurence of `[[BRANCH]]` will be replace by the actual branch name form the
pull request.

* `commentFile`: *Optional.* The path to a file that contains a custom comment to
add to the message. This allow the comment to be built by a previous task in the job.

## Example pipeline

```yaml
resource_types:
- name: concourse-bitbucket-pullrequest
type: docker-image
source:
repository: laurentverbruggen/concourse-bitbucket-pullrequest-resource
- name: concourse-bitbucket-pullrequest
type: docker-image
source:
repository: mm62/concourse-bitbucket-pullrequest-resource

resources:
- name: pullrequest
type: concourse-bitbucket-pullrequest
source:
username: {{bitbucket-username}}
password: {{bitbucket-password}}
uri: laurentverbruggen/concourse-bitbucket-pullrequest-resource
- name: pullrequest
type: concourse-bitbucket-pullrequest
source:
username: {{bitbucket-username}}
password: {{bitbucket-password}}
uri: https://your-bitbucket.com/project/repo

jobs:
- name: test pull request
plan:
- get: pullrequest
trigger: true
version: every
- put: pullrequest
params:
path: pullrequest
status: pending
- task: test
config:
platform: linux

inputs:
- name: pullrequest

...

on_success:
put: pullrequest
params:
path: pullrequest
status: success
on_failure:
put: pullrequest
params:
path: pullrequest
status: failure
- name: test pull request
plan:
- get: pullrequest
trigger: true
version: every
- put: pullrequest
params:
path: pullrequest
status: pending
- task: test
config:
platform: linux

inputs:
- name: pullrequest

...

on_success:
put: pullrequest
params:
path: pullrequest
status: success
on_failure:
put: pullrequest
params:
path: pullrequest
status: failure
```
42 changes: 32 additions & 10 deletions assets/check
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ only_for_branch=$(jq -r '.source.only_for_branch // "."' < "$payload")
only_without_conflicts=$(jq -r '.source.only_without_conflicts // "true"' < "$payload")
only_when_mergeable=$(jq -r '.source.only_when_mergeable // "false"' < "$payload")
only_when_asked=$(jq -r '.source.only_when_asked // "false"' < "$payload")
sleep_between_fetches=$(jq -r '.source.sleep_between_fetches // "0"' < "$payload")
sleep_between_fetches=$(echo "$sleep_between_fetches" | sed 's/[^0-9\.]//g')
rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "false"' < "$payload")
rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payload")
CURRENT_VERSION_DATE=$(jq -r '.version.date // "0"' < "$payload")

configure_git_ssl_verification "$skip_ssl_verification"
configure_git_global "${git_config_payload}"
Expand All @@ -55,10 +58,14 @@ if [ "$rebuild_when_target_changed" == "true" ]; then
fi

# collect all pull requests from uri
pull_requests=$(git ls-remote "$uri" | grep -E "/pull\-requests/[0-9]+/${prq_branch}" | cat)
REMOTES=$(git ls-remote "$uri")
set +e
PULL_REQUESTS=$(echo "$REMOTES" | grep -E "/pull\\-requests/[0-9]+/${prq_branch}")
set -e


versions="[]"
if [ -n "$pull_requests" ]; then
if [ -n "$PULL_REQUESTS" ]; then
log "Calculating repository specifics"
# determine repository name for calling REST api
repo_name=$(basename "$uri" | sed "s/.git$//")
Expand All @@ -72,6 +79,7 @@ if [ -n "$pull_requests" ]; then

versions="[]"
while read pull_request ; do
sleep $sleep_between_fetches # throttle requests to avoid excessive load to server
log "Verifying pull request"
# determine hash and prq number from grep
prq_number=$(echo "$pull_request" | sed -E "s/^.*\/pull-requests\/([0-9]+)\/.*$/\\1/")
Expand All @@ -80,10 +88,12 @@ if [ -n "$pull_requests" ]; then
# verify target branch of prq
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")

if [ "$prq" = "ERROR" ]; then
if [ "$prq" = "NO_SUCH_PULL_REQUEST" ]; then
continue
fi

PULL_REQUEST_DATE=$(echo "$prq" | jq -r '.updatedDate')

log "Pull request #${prq_number}"

prq_to_branch=$(echo "$prq" | jq -r '.toRef.displayId')
Expand All @@ -92,6 +102,10 @@ if [ -n "$pull_requests" ]; then
if [ "$only_when_mergeable" == "true" -o "$only_without_conflicts" == "true" ]; then
prq_merge=$(bitbucket_pullrequest_merge "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")

if [ "$prq_merge" = "ALREADY_MERGED" ] || [ "$prq_merge" = "DECLINED" ]; then
continue
fi

# verify if prq has merge conflicts
conflicted=$(echo "$prq_merge" | jq -r '.conflicted')
if [ "$conflicted" == "true" -a "$only_without_conflicts" == "true" ]; then continue; fi
Expand All @@ -102,7 +116,6 @@ if [ -n "$pull_requests" ]; then
fi

# edit timestamp to version to force new build when rebuild_phrase is included in comments
prq_verify_date=$(echo "$prq" | jq -r '.createdDate')
skip_build=false
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]')
if [ -n "$comments" ]; then
Expand All @@ -118,20 +131,29 @@ if [ -n "$pull_requests" ]; then

# edit timestamp to force new build when rebuild_phrase is included in comments
if echo "$text" | grep -Ec "$rebuild_phrase" > /dev/null; then
prq_verify_date=$(echo "$comment" | jq -r '.createdDate')
PULL_REQUEST_DATE=$(echo "$comment" | jq -r '.createdDate')
break
fi
done <<< "$comments"
fi

if [ "$PULL_REQUEST_DATE" -lt "$CURRENT_VERSION_DATE" ]; then
continue
fi

# add prq to versions
if [ "$skip_build" == "false" ]; then
pretty_date=$(date_from_epoch_seconds "$(( ($prq_verify_date + 500) / 1000))")
versions+=" + [{ id: \"$prq_number\", hash: \"$prq_hash\", date: \"$pretty_date\", change: $prq_verify_date }]"
versions+=" + [{ id: \"$prq_number\", hash: \"$prq_hash\", date: \"$PULL_REQUEST_DATE\" }]"
fi

fi
done <<< "$pull_requests"
done <<< "$PULL_REQUESTS"
fi

# On the first request return only the current version.
if [ "$CURRENT_VERSION_DATE" -eq "0" ]; then
jq -n "$versions | sort_by((.date | tonumber), (.id | tonumber), .hash) | .[-1:]" > /tmp/check_result
else
jq -n "$versions | sort_by((.date | tonumber), (.id | tonumber), .hash)" > /tmp/check_result
fi

jq -n "$versions | sort_by(.change) | map(del(.change))" >&3
cat /tmp/check_result >&3
26 changes: 18 additions & 8 deletions assets/helpers/bitbucket.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ bitbucket_request() {
rm -f "$request_data"
}

# register the cleanup function to be called on the EXIT signal
trap request_result_cleanup EXIT

local extra_options=""
if [ -n "$data" ]; then
method=${method:-POST}
Expand Down Expand Up @@ -72,8 +69,14 @@ bitbucket_request() {
jq -c '.values' < "$request_result"
elif [ "$(jq -c '.errors' < "$request_result")" == "null" ]; then
jq '.' < "$request_result"
elif [ "${request_result/NoSuchPullRequestException}" = "${request_result}" ]; then
printf "ERROR"
elif grep -q NoSuchPullRequestException "$request_result"; then
printf "NO_SUCH_PULL_REQUEST"
return
elif grep -q 'This pull request has already been merged' "$request_result"; then
printf "ALREADY_MERGED"
return
elif grep -q 'This pull request has been declined and must be reopened before it can be merged' "$request_result"; then
printf "DECLINED"
return
else
log "Bitbucket request ($request_url) failed: $(cat $request_result)"
Expand Down Expand Up @@ -158,26 +161,33 @@ bitbucket_pullrequest_progress_comment() {
# $2: hash of merge commit
# $3: hash of source commit
# $4: hash of target commit
# $5: custom comment
local hash="$2"

local progress_msg_end=""
local custom_comment=""

if [ "$hash" == "$3" ]; then
progress_msg_end+=" into $4]"
else
progress_msg_end="] $3 into $4"
fi

if [ -n "$5" ]; then
custom_comment="\n\n$5"
fi

local build_url="$ATC_EXTERNAL_URL/teams/$(rawurlencode "$BUILD_TEAM_NAME")/pipelines/$(rawurlencode "$BUILD_PIPELINE_NAME")/jobs/$(rawurlencode "$BUILD_JOB_NAME")/builds/$(rawurlencode "$BUILD_NAME")"
local build_result_pre=" \n\n **["
local build_result_post="]($build_url)** - Build #$BUILD_NAME"

case "$1" in
success)
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✓ BUILD SUCCESS${build_result_post}" ;;
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✓ BUILD SUCCESS${build_result_post}${custom_comment}" ;;
failure)
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✕ BUILD FAILED${build_result_post}" ;;
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✕ BUILD FAILED${build_result_post}${custom_comment}" ;;
pending)
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Started")${progress_msg_end}${build_result_pre}&#8987; BUILD IN PROGRESS${build_result_post}" ;;
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Started")${progress_msg_end}${build_result_pre}&#8987; BUILD IN PROGRESS${build_result_post}${custom_comment}" ;;
esac
}

Expand Down
3 changes: 2 additions & 1 deletion assets/helpers/git.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

load_pubkey() {
local private_key_path=$TMPDIR/git-resource-private-key

Expand All @@ -7,7 +9,6 @@ load_pubkey() {
chmod 0600 $private_key_path

eval $(ssh-agent) >/dev/null 2>&1
trap "kill $SSH_AGENT_PID" 0

SSH_ASKPASS=$ASSETS/helpers/askpass.sh DISPLAY= ssh-add $private_key_path >/dev/null

Expand Down
Loading