Skip to content
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

feat(get-merge-base): add get-merge-base helper #582

Closed
wants to merge 2 commits into from
Closed
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
21 changes: 21 additions & 0 deletions .github/workflows/get-merge-base.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Get Merge Base

on:
pull_request:
branches: [ main ]
paths:
- 'src/helpers/get-merge-base.ts'

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./
id: merge_base
with:
helper: get-merge-base

- run: echo "Merge Base is ${{ steps.merge_base.outputs.output }}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Thumbs.db

node_modules
.idea
github-helpers.iml
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ Additionally, the following parameters can be used for additional control over t
- Returns a comma-separated list of changed files for a PR
- Include the regular expression parameter 'pattern' to add a filter, it will return any filepath that matches.

### [get-merge-base](.github/workflows/get-merge-base.yml)

- Get merge base commit SHA for a PR

### [initiate-deployment](.github/workflows/deployments.yml#L12)

- Creates a new in-progress Github "deployment" for a commit. More information on Github deployment events can be found [here](https://docs.github.com/en/rest/reference/repos#deployments)
Expand Down
83 changes: 83 additions & 0 deletions dist/354.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/354.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42144,6 +42144,16 @@ var map = {
832,
913
],
"./get-merge-base": [
6354,
832,
354
],
"./get-merge-base.ts": [
6354,
832,
354
],
"./initiate-deployment": [
420,
832,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/helpers/get-merge-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2023 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import * as core from '@actions/core';
import { context } from '@actions/github';
import { octokit } from '../octokit';

export const getMergeBase = async () => {
try {
const { data: pullRequest } = await octokit.pulls.get({ pull_number: context.issue.number, ...context.repo });
return pullRequest.base.sha;
} catch (error) {
core.error(`Error while fetching merge base commit SHA: ${error}`);
}
return null;
};
46 changes: 46 additions & 0 deletions test/helpers/get-merge-base.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2023 Expedia, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Mocktokit } from '../types';
import { octokit } from '../../src/octokit';
import { getMergeBase } from '../../src/helpers/get-merge-base';

jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
getOctokit: jest.fn(() => ({ rest: { pulls: { get: jest.fn() } } }))
}));

describe('getMergeBase', () => {
it('should return merge base commit SHA', async () => {
(octokit.pulls.get as unknown as Mocktokit).mockImplementation(async () => ({
data: {
base: {
sha: 'merge_base_sha'
}
}
}));

const result = await getMergeBase();

expect(result).toBe('merge_base_sha');
});

it('should handle errors', async () => {
(octokit.pulls.get as unknown as jest.Mock).mockRejectedValue(new Error('API error'));

const result = await getMergeBase();

expect(result).toBeNull();
});
});
Loading