Skip to content

Commit

Permalink
feat: add pull-request-operation output
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Feb 1, 2021
1 parent 2455e15 commit b5f41d9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ All inputs are **optional**. If not set, sensible defaults will be used.

### Action outputs

The pull request number and URL are available as step outputs.
The following outputs can be used by subsequent workflow steps.

- `pull-request-number` - The pull request number.
- `pull-request-url` - The URL of the pull request.
- `pull-request-operation` - The pull request operation performed by the action, `created`, `updated` or `closed`.

Step outputs can be accessed as in the following example.
Note that in order to read the step outputs the action step must have an id.

```yml
Expand Down
33 changes: 23 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,20 @@ function createPullRequest(inputs) {
inputs.base = result.base;
if (result.hasDiffWithBase) {
// Create or update the pull request
yield githubHelper.createOrUpdatePullRequest(inputs, baseRemote.repository, branchRepository);
const pull = yield githubHelper.createOrUpdatePullRequest(inputs, baseRemote.repository, branchRepository);
// Set outputs
core.startGroup('Setting outputs');
core.setOutput('pull-request-number', pull.number);
core.setOutput('pull-request-url', pull.html_url);
if (pull.created) {
core.setOutput('pull-request-operation', 'created');
}
else if (result.action == 'updated') {
core.setOutput('pull-request-operation', 'updated');
}
// Deprecated
core.exportVariable('PULL_REQUEST_NUMBER', pull.number);
core.endGroup();
}
else {
// There is no longer a diff with the base
Expand All @@ -406,6 +419,10 @@ function createPullRequest(inputs) {
branchRemoteName,
`refs/heads/${inputs.branch}`
]);
// Set outputs
core.startGroup('Setting outputs');
core.setOutput('pull-request-operation', 'closed');
core.endGroup();
}
}
}
Expand Down Expand Up @@ -919,7 +936,8 @@ class GitHubHelper {
core.info(`Created pull request #${pull.number} (${headBranch} => ${inputs.base})`);
return {
number: pull.number,
html_url: pull.html_url
html_url: pull.html_url,
created: true
};
}
catch (e) {
Expand All @@ -937,7 +955,8 @@ class GitHubHelper {
core.info(`Updated pull request #${pull.number} (${headBranch} => ${inputs.base})`);
return {
number: pull.number,
html_url: pull.html_url
html_url: pull.html_url,
created: false
};
});
}
Expand All @@ -956,13 +975,6 @@ class GitHubHelper {
const headBranch = `${headOwner}:${inputs.branch}`;
// Create or update the pull request
const pull = yield this.createOrUpdate(inputs, baseRepository, headBranch);
// Set outputs
core.startGroup('Setting outputs');
core.setOutput('pull-request-number', pull.number);
core.setOutput('pull-request-url', pull.html_url);
// Deprecated
core.exportVariable('PULL_REQUEST_NUMBER', pull.number);
core.endGroup();
// Set milestone, labels and assignees
const updateIssueParams = {};
if (inputs.milestone) {
Expand Down Expand Up @@ -1003,6 +1015,7 @@ class GitHubHelper {
}
}
}
return pull;
});
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/create-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,24 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {

if (result.hasDiffWithBase) {
// Create or update the pull request
await githubHelper.createOrUpdatePullRequest(
const pull = await githubHelper.createOrUpdatePullRequest(
inputs,
baseRemote.repository,
branchRepository
)

// Set outputs
core.startGroup('Setting outputs')
core.setOutput('pull-request-number', pull.number)
core.setOutput('pull-request-url', pull.html_url)
if (pull.created) {
core.setOutput('pull-request-operation', 'created')
} else if (result.action == 'updated') {
core.setOutput('pull-request-operation', 'updated')
}
// Deprecated
core.exportVariable('PULL_REQUEST_NUMBER', pull.number)
core.endGroup()
} else {
// There is no longer a diff with the base
// Check we are in a state where a branch exists
Expand All @@ -215,6 +228,10 @@ export async function createPullRequest(inputs: Inputs): Promise<void> {
branchRemoteName,
`refs/heads/${inputs.branch}`
])
// Set outputs
core.startGroup('Setting outputs')
core.setOutput('pull-request-operation', 'closed')
core.endGroup()
}
}
}
Expand Down
19 changes: 8 additions & 11 deletions src/github-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Repository {
interface Pull {
number: number
html_url: string
created: boolean
}

export class GitHubHelper {
Expand Down Expand Up @@ -55,7 +56,8 @@ export class GitHubHelper {
)
return {
number: pull.number,
html_url: pull.html_url
html_url: pull.html_url,
created: true
}
} catch (e) {
if (
Expand Down Expand Up @@ -87,7 +89,8 @@ export class GitHubHelper {
)
return {
number: pull.number,
html_url: pull.html_url
html_url: pull.html_url,
created: false
}
}

Expand All @@ -107,21 +110,13 @@ export class GitHubHelper {
inputs: Inputs,
baseRepository: string,
headRepository: string
): Promise<void> {
): Promise<Pull> {
const [headOwner] = headRepository.split('/')
const headBranch = `${headOwner}:${inputs.branch}`

// Create or update the pull request
const pull = await this.createOrUpdate(inputs, baseRepository, headBranch)

// Set outputs
core.startGroup('Setting outputs')
core.setOutput('pull-request-number', pull.number)
core.setOutput('pull-request-url', pull.html_url)
// Deprecated
core.exportVariable('PULL_REQUEST_NUMBER', pull.number)
core.endGroup()

// Set milestone, labels and assignees
const updateIssueParams = {}
if (inputs.milestone) {
Expand Down Expand Up @@ -169,5 +164,7 @@ export class GitHubHelper {
}
}
}

return pull
}
}

0 comments on commit b5f41d9

Please sign in to comment.