Skip to content

Commit

Permalink
fix(965): [3]$GIT_BRANCH invalid value in PR builds (#150)
Browse files Browse the repository at this point in the history
* fix GIT_BRANCH

* use github api

* fix indent

* add new variable

* add origin

* fix shell script

* fix: modification

* fix: _getPrInfo

* support fork pr

* fix: customPrCommands.json

* delete: lint rule

* add: test case of fork pr

* fix: test title

* fix: typo

* fix: test _getPrInfo
  • Loading branch information
ibu1224 authored Mar 2, 2020
1 parent 2e0d886 commit 2d97773
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,14 @@ class GithubScm extends Scm {
// For pull requests
if (config.prRef) {
const prRef = config.prRef.replace('merge', 'head:pr');
const baseRepo = config.prSource === 'fork' ? 'upstream' : 'origin';

// Fetch a pull request
command.push(`echo Fetching PR and merging with ${branch}`);
command.push(`$SD_GIT_WRAPPER "git fetch origin ${prRef}"`);

command.push(`export PR_BRANCH_NAME=${baseRepo}/${config.prBranchName}`);

// Merge a pull request with pipeline branch
command.push(`$SD_GIT_WRAPPER "git merge ${config.sha}"`);
command.push(`export GIT_BRANCH=origin/refs/${prRef}`);
Expand Down Expand Up @@ -1323,6 +1327,7 @@ class GithubScm extends Scm {
name: `PR-${pullRequestInfo.data.number}`,
ref: `pull/${pullRequestInfo.data.number}/merge`,
sha: pullRequestInfo.data.head.sha,
prBranchName: pullRequestInfo.data.head.ref,
url: pullRequestInfo.data.html_url,
username: pullRequestInfo.data.user.login,
title: pullRequestInfo.data.title,
Expand Down
2 changes: 1 addition & 1 deletion test/data/customPrCommands.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "sd-checkout-code",
"command": "export SD_GIT_WRAPPER=\"$(if [ `uname` = 'Darwin' ]; then echo 'eval'; else echo 'sd-step exec core/git'; fi)\" && echo Exporting environment variables && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@github.com:screwdriver-cd/guide; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/guide; else export SCM_URL=https://github.com/screwdriver-cd/guide; fi && export GIT_URL=$SCM_URL.git && export GIT_MERGE_AUTOEDIT=no && echo Setting user name and user email && $SD_GIT_WRAPPER \"git config --global user.name pqrs\" && $SD_GIT_WRAPPER \"git config --global user.email dev-null@my.email.com\" && export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR && if [ ! -z $SD_CHECKOUT_DIR ]; then export SD_CHECKOUT_DIR_FINAL=$SD_CHECKOUT_DIR; fi && echo Cloning github.com/screwdriver-cd/guide, on branch branchName && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $SD_GIT_WRAPPER \"git clone --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; else if [ ! -z \"$GIT_SHALLOW_CLONE_SINCE\" ]; then export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--shallow-since='$GIT_SHALLOW_CLONE_SINCE'\"; else if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then export GIT_SHALLOW_CLONE_DEPTH=50; fi; export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--depth=$GIT_SHALLOW_CLONE_DEPTH\"; fi; export GIT_SHALLOW_CLONE_BRANCH=\"--no-single-branch\"; if [ \"$GIT_SHALLOW_CLONE_SINGLE_BRANCH\" = true ]; then export GIT_SHALLOW_CLONE_BRANCH=\"\"; fi; $SD_GIT_WRAPPER \"git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; fi && $SD_GIT_WRAPPER \"git reset --hard branchName --\" && echo Reset to branchName && echo Fetching PR and merging with branchName && $SD_GIT_WRAPPER \"git fetch origin pull/3/head:pr\" && $SD_GIT_WRAPPER \"git merge 12345\" && export GIT_BRANCH=origin/refs/pull/3/head:pr && $SD_GIT_WRAPPER \"git submodule init\" && $SD_GIT_WRAPPER \"git submodule update --recursive\""
"command": "export SD_GIT_WRAPPER=\"$(if [ `uname` = 'Darwin' ]; then echo 'eval'; else echo 'sd-step exec core/git'; fi)\" && echo Exporting environment variables && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@github.com:screwdriver-cd/guide; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/guide; else export SCM_URL=https://github.com/screwdriver-cd/guide; fi && export GIT_URL=$SCM_URL.git && export GIT_MERGE_AUTOEDIT=no && echo Setting user name and user email && $SD_GIT_WRAPPER \"git config --global user.name pqrs\" && $SD_GIT_WRAPPER \"git config --global user.email dev-null@my.email.com\" && export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR && if [ ! -z $SD_CHECKOUT_DIR ]; then export SD_CHECKOUT_DIR_FINAL=$SD_CHECKOUT_DIR; fi && echo Cloning github.com/screwdriver-cd/guide, on branch branchName && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $SD_GIT_WRAPPER \"git clone --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; else if [ ! -z \"$GIT_SHALLOW_CLONE_SINCE\" ]; then export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--shallow-since='$GIT_SHALLOW_CLONE_SINCE'\"; else if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then export GIT_SHALLOW_CLONE_DEPTH=50; fi; export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--depth=$GIT_SHALLOW_CLONE_DEPTH\"; fi; export GIT_SHALLOW_CLONE_BRANCH=\"--no-single-branch\"; if [ \"$GIT_SHALLOW_CLONE_SINGLE_BRANCH\" = true ]; then export GIT_SHALLOW_CLONE_BRANCH=\"\"; fi; $SD_GIT_WRAPPER \"git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; fi && $SD_GIT_WRAPPER \"git reset --hard branchName --\" && echo Reset to branchName && echo Fetching PR and merging with branchName && $SD_GIT_WRAPPER \"git fetch origin pull/3/head:pr\" && export PR_BRANCH_NAME=origin/prBranchName && $SD_GIT_WRAPPER \"git merge 12345\" && export GIT_BRANCH=origin/refs/pull/3/head:pr && $SD_GIT_WRAPPER \"git submodule init\" && $SD_GIT_WRAPPER \"git submodule update --recursive\""
}
4 changes: 4 additions & 0 deletions test/data/forkPrCommands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "sd-checkout-code",
"command": "export SD_GIT_WRAPPER=\"$(if [ `uname` = 'Darwin' ]; then echo 'eval'; else echo 'sd-step exec core/git'; fi)\" && echo Exporting environment variables && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@github.com:screwdriver-cd/guide; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/guide; else export SCM_URL=https://github.com/screwdriver-cd/guide; fi && export GIT_URL=$SCM_URL.git && export GIT_MERGE_AUTOEDIT=no && echo Setting user name and user email && $SD_GIT_WRAPPER \"git config --global user.name sd-buildbot\" && $SD_GIT_WRAPPER \"git config --global user.email dev-null@screwdriver.cd\" && export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR && if [ ! -z $SD_CHECKOUT_DIR ]; then export SD_CHECKOUT_DIR_FINAL=$SD_CHECKOUT_DIR; fi && echo Cloning github.com/screwdriver-cd/guide, on branch branchName && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $SD_GIT_WRAPPER \"git clone --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; else if [ ! -z \"$GIT_SHALLOW_CLONE_SINCE\" ]; then export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--shallow-since='$GIT_SHALLOW_CLONE_SINCE'\"; else if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then export GIT_SHALLOW_CLONE_DEPTH=50; fi; export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--depth=$GIT_SHALLOW_CLONE_DEPTH\"; fi; export GIT_SHALLOW_CLONE_BRANCH=\"--no-single-branch\"; if [ \"$GIT_SHALLOW_CLONE_SINGLE_BRANCH\" = true ]; then export GIT_SHALLOW_CLONE_BRANCH=\"\"; fi; $SD_GIT_WRAPPER \"git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; fi && $SD_GIT_WRAPPER \"git reset --hard branchName --\" && echo Reset to branchName && echo Fetching PR and merging with branchName && $SD_GIT_WRAPPER \"git fetch origin pull/3/head:pr\" && export PR_BRANCH_NAME=upstream/prBranchName && $SD_GIT_WRAPPER \"git merge 12345\" && export GIT_BRANCH=origin/refs/pull/3/head:pr && $SD_GIT_WRAPPER \"git submodule init\" && $SD_GIT_WRAPPER \"git submodule update --recursive\""
}
2 changes: 1 addition & 1 deletion test/data/prCommands.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "sd-checkout-code",
"command": "export SD_GIT_WRAPPER=\"$(if [ `uname` = 'Darwin' ]; then echo 'eval'; else echo 'sd-step exec core/git'; fi)\" && echo Exporting environment variables && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@github.com:screwdriver-cd/guide; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/guide; else export SCM_URL=https://github.com/screwdriver-cd/guide; fi && export GIT_URL=$SCM_URL.git && export GIT_MERGE_AUTOEDIT=no && echo Setting user name and user email && $SD_GIT_WRAPPER \"git config --global user.name sd-buildbot\" && $SD_GIT_WRAPPER \"git config --global user.email dev-null@screwdriver.cd\" && export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR && if [ ! -z $SD_CHECKOUT_DIR ]; then export SD_CHECKOUT_DIR_FINAL=$SD_CHECKOUT_DIR; fi && echo Cloning github.com/screwdriver-cd/guide, on branch branchName && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $SD_GIT_WRAPPER \"git clone --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; else if [ ! -z \"$GIT_SHALLOW_CLONE_SINCE\" ]; then export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--shallow-since='$GIT_SHALLOW_CLONE_SINCE'\"; else if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then export GIT_SHALLOW_CLONE_DEPTH=50; fi; export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--depth=$GIT_SHALLOW_CLONE_DEPTH\"; fi; export GIT_SHALLOW_CLONE_BRANCH=\"--no-single-branch\"; if [ \"$GIT_SHALLOW_CLONE_SINGLE_BRANCH\" = true ]; then export GIT_SHALLOW_CLONE_BRANCH=\"\"; fi; $SD_GIT_WRAPPER \"git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; fi && $SD_GIT_WRAPPER \"git reset --hard branchName --\" && echo Reset to branchName && echo Fetching PR and merging with branchName && $SD_GIT_WRAPPER \"git fetch origin pull/3/head:pr\" && $SD_GIT_WRAPPER \"git merge 12345\" && export GIT_BRANCH=origin/refs/pull/3/head:pr && $SD_GIT_WRAPPER \"git submodule init\" && $SD_GIT_WRAPPER \"git submodule update --recursive\""
"command": "export SD_GIT_WRAPPER=\"$(if [ `uname` = 'Darwin' ]; then echo 'eval'; else echo 'sd-step exec core/git'; fi)\" && echo Exporting environment variables && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@github.com:screwdriver-cd/guide; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/guide; else export SCM_URL=https://github.com/screwdriver-cd/guide; fi && export GIT_URL=$SCM_URL.git && export GIT_MERGE_AUTOEDIT=no && echo Setting user name and user email && $SD_GIT_WRAPPER \"git config --global user.name sd-buildbot\" && $SD_GIT_WRAPPER \"git config --global user.email dev-null@screwdriver.cd\" && export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR && if [ ! -z $SD_CHECKOUT_DIR ]; then export SD_CHECKOUT_DIR_FINAL=$SD_CHECKOUT_DIR; fi && echo Cloning github.com/screwdriver-cd/guide, on branch branchName && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $SD_GIT_WRAPPER \"git clone --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; else if [ ! -z \"$GIT_SHALLOW_CLONE_SINCE\" ]; then export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--shallow-since='$GIT_SHALLOW_CLONE_SINCE'\"; else if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then export GIT_SHALLOW_CLONE_DEPTH=50; fi; export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--depth=$GIT_SHALLOW_CLONE_DEPTH\"; fi; export GIT_SHALLOW_CLONE_BRANCH=\"--no-single-branch\"; if [ \"$GIT_SHALLOW_CLONE_SINGLE_BRANCH\" = true ]; then export GIT_SHALLOW_CLONE_BRANCH=\"\"; fi; $SD_GIT_WRAPPER \"git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH --recursive --quiet --progress --branch branchName $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; fi && $SD_GIT_WRAPPER \"git reset --hard branchName --\" && echo Reset to branchName && echo Fetching PR and merging with branchName && $SD_GIT_WRAPPER \"git fetch origin pull/3/head:pr\" && export PR_BRANCH_NAME=origin/prBranchName && $SD_GIT_WRAPPER \"git merge 12345\" && export GIT_BRANCH=origin/refs/pull/3/head:pr && $SD_GIT_WRAPPER \"git submodule init\" && $SD_GIT_WRAPPER \"git submodule update --recursive\""
}
17 changes: 16 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const testPayloadBadAction = require('./data/github.pull_request.badAction.json'
const testPayloadPing = require('./data/github.ping.json');
const testCommands = require('./data/commands.json');
const testPrCommands = require('./data/prCommands.json');
const testForkPrCommands = require('./data/forkPrCommands.json');
const testCustomPrCommands = require('./data/customPrCommands.json');
const testRepoCommands = require('./data/repoCommands.json');
const testRootDirCommands = require('./data/rootDirCommands.json');
Expand Down Expand Up @@ -185,7 +186,9 @@ describe('index', function () {
host: 'github.com',
org: 'screwdriver-cd',
repo: 'guide',
sha: '12345'
sha: '12345',
prSource: 'branch',
prBranchName: 'prBranchName'
};
});

Expand All @@ -205,6 +208,16 @@ describe('index', function () {
});
});

it('promises to get the checkout command for a pull request from forked repo', () => {
config.prRef = 'pull/3/merge';
config.prSource = 'fork';

return scm.getCheckoutCommand(config)
.then((command) => {
assert.deepEqual(command, testForkPrCommands);
});
});

it('promises to get the checkout command with custom username and email', () => {
config.prRef = 'pull/3/merge';

Expand Down Expand Up @@ -2149,6 +2162,7 @@ jobs:
title: 'new-feature',
createTime: '2011-01-26T19:01:12Z',
userProfile: 'https://github.com/octocat',
prBranchName: 'new-topic',
baseBranch: 'master',
mergeable: true
}
Expand Down Expand Up @@ -2185,6 +2199,7 @@ jobs:
title: 'new-feature',
createTime: '2011-01-26T19:01:12Z',
userProfile: 'https://github.com/octocat',
prBranchName: 'new-topic',
baseBranch: 'master',
mergeable: true
}
Expand Down

0 comments on commit 2d97773

Please sign in to comment.