Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
prudhvigodithi committed Sep 26, 2024
2 parents 90e9954 + 6b545a3 commit e8288db
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 50 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ module.exports = {
'import/prefer-default-export': 'off',
'max-classes-per-file': 'off',
'no-new': 'off',
"no-console": "off",
"no-underscore-dangle": 'off',
'no-console': 'off',
'no-underscore-dangle': 'off',
'max-len': ['error', { code: 160, ignoreComments: true }],
},
};
8 changes: 5 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies and build the code
run: |
npm install
npm run build
run: npm install
- name: Verify styling
run: npm run format-dryrun
- name: Run build
run: npm run build
6 changes: 3 additions & 3 deletions ADMINS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## Admins

| Admin | GitHub ID | Affiliation |
| --------------- | --------------------------------------- | ----------- |
| Henri Yandell | [hyandell](https://github.com/hyandell) | Amazon |
| Admin | GitHub ID | Affiliation |
| ------------- | --------------------------------------- | ----------- |
| Henri Yandell | [hyandell](https://github.com/hyandell) | Amazon |

[This document](https://github.com/opensearch-project/.github/blob/main/ADMINS.md) explains what admins do in this repo. and how they should be doing it. If you're interested in becoming a maintainer, see [MAINTAINERS](MAINTAINERS.md). If you're interested in contributing, see [CONTRIBUTING](CONTRIBUTING.md).
24 changes: 12 additions & 12 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ This code of conduct applies to all spaces provided by the OpenSource project in

**Our open source communities endeavor to:**

* Be Inclusive: We are committed to being a community where everyone can join and contribute. This means using inclusive and welcoming language.
* Be Welcoming: We are committed to maintaining a safe space for everyone to be able to contribute.
* Be Respectful: We are committed to encouraging differing viewpoints, accepting constructive criticism and work collaboratively towards decisions that help the project grow. Disrespectful and unacceptable behavior will not be tolerated.
* Be Collaborative: We are committed to supporting what is best for our community and users. When we build anything for the benefit of the project, we should document the work we do and communicate to others on how this affects their work.
- Be Inclusive: We are committed to being a community where everyone can join and contribute. This means using inclusive and welcoming language.
- Be Welcoming: We are committed to maintaining a safe space for everyone to be able to contribute.
- Be Respectful: We are committed to encouraging differing viewpoints, accepting constructive criticism and work collaboratively towards decisions that help the project grow. Disrespectful and unacceptable behavior will not be tolerated.
- Be Collaborative: We are committed to supporting what is best for our community and users. When we build anything for the benefit of the project, we should document the work we do and communicate to others on how this affects their work.

**Our Responsibility. As contributors, members, or bystanders we each individually have the responsibility to behave professionally and respectfully at all times. Disrespectful and unacceptable behaviors include, but are not limited to:**

* The use of violent threats, abusive, discriminatory, or derogatory language;
* Offensive comments related to gender, gender identity and expression, sexual orientation, disability, mental illness, race, political or religious affiliation;
* Posting of sexually explicit or violent content;
* The use of sexualized language and unwelcome sexual attention or advances;
* Public or private harassment of any kind;
* Publishing private information, such as physical or electronic address, without permission;
* Other conduct which could reasonably be considered inappropriate in a professional setting;
* Advocating for or encouraging any of the above behaviors.
- The use of violent threats, abusive, discriminatory, or derogatory language;
- Offensive comments related to gender, gender identity and expression, sexual orientation, disability, mental illness, race, political or religious affiliation;
- Posting of sexually explicit or violent content;
- The use of sexualized language and unwelcome sexual attention or advances;
- Public or private harassment of any kind;
- Publishing private information, such as physical or electronic address, without permission;
- Other conduct which could reasonably be considered inappropriate in a professional setting;
- Advocating for or encouraging any of the above behaviors.

**Enforcement and Reporting Code of Conduct Issues:**

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Contributing to this Project

OpenSearch is a community project that is built and maintained by people just like **you**.
OpenSearch is a community project that is built and maintained by people just like **you**.
[This document](https://github.com/opensearch-project/.github/blob/main/CONTRIBUTING.md) explains how you can contribute to this and related projects.
12 changes: 6 additions & 6 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ This document contains a list of maintainers in this repo. See [opensearch-proje

## Current Maintainers

| Maintainer | GitHub ID | Affiliation |
| ------------------ | --------------------------------------------------------- | ----------- |
| Peter Zhu | [peterzhuamazon](https://github.com/peterzhuamazon) | Amazon |
| Saurabh Sing | [getsaurabh02](https://github.com/getsaurabh02) | Amazon |
| Maintainer | GitHub ID | Affiliation |
| ------------ | --------------------------------------------------- | ----------- |
| Peter Zhu | [peterzhuamazon](https://github.com/peterzhuamazon) | Amazon |
| Saurabh Sing | [getsaurabh02](https://github.com/getsaurabh02) | Amazon |

## Emeritus

| Maintainer | GitHub ID | Affiliation |
| ------------------ | --------------------------------------------------------- | ----------- |
| Maintainer | GitHub ID | Affiliation |
| ---------- | --------- | ----------- |
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ The automation app utilizes the [Probot](https://probot.github.io/) framework an
### Service

A **Service** is an instance of the app that manages and manipulates specific `Resource` while performing defined `Operation`.
* **Resource**: Objects or entities the service will manage or modify, such as GitHub organizations, project, repositories, issues, etc.
* **Operation**: A list of `Tasks` triggered by events with the resources.
* **Task**: Executed sequentially within an `Operation` to perform action, such as create comments, update labels, add issue to project, etc.
* **Call**: The callstack that contains the implementation of the aformentioned task action.

- **Resource**: Objects or entities the service will manage or modify, such as GitHub organizations, project, repositories, issues, etc.
- **Operation**: A list of `Tasks` triggered by events with the resources.
- **Task**: Executed sequentially within an `Operation` to perform action, such as create comments, update labels, add issue to project, etc.
- **Call**: The callstack that contains the implementation of the aformentioned task action.

### Create a Service

To create a service, you need two configuration files:
* **Resource configuration file**: Defines the resources that the service will manage or modify (`configs/resources/sample-resource.yml`).
* **Operation configuration file**: Defines the operations (tasks) that the service will execute with the resources (`configs/resources/sample-operation.yml`).

- **Resource configuration file**: Defines the resources that the service will manage or modify (`configs/resources/sample-resource.yml`).
- **Operation configuration file**: Defines the operations (tasks) that the service will execute with the resources (`configs/resources/sample-operation.yml`).

### Start the Service

Expand All @@ -44,6 +46,7 @@ RESOURCE_CONFIG=configs/resources/sample-resource.yml OPERATION_CONFIG=configs/o
```

When you run the above command, the following takes place:

1. The app starts a `Service` instance based on the specified configurations.
1. Retrieves the [GitHub Context](https://probot.github.io/api/latest/classes/context.Context.html) (or any other defined context) for all the resources listed in the resource config file.
1. Registers and listens for events, executes the `Tasks` defined in the operation config. These tasks will be executed sequentially when the corresponding events occur.
Expand All @@ -63,4 +66,3 @@ This project is licensed under the [Apache v2.0 License](LICENSE).
## Copyright

Copyright OpenSearch Contributors. See [NOTICE](NOTICE) for details.

9 changes: 5 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
roots: ['<rootDir>/test'], // Points to 'test' directory where your test files live
testMatch: ['**/*.test.ts'], // Match test files ending with `.test.ts`
transform: {
'^.+\\.tsx?$': 'ts-jest'
}
'^.+\\.tsx?$': 'ts-jest',
},
};

11 changes: 7 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"start": "npm run build && probot run ./bin/app.js",
"clean": "rm -rf ./bin/*",
"format": "prettier --write \"src/**/*.{js,ts}\" \"test/**/*.ts\" \"configs/**/*.{yaml,yml}\"",
"format-dryrun": "prettier --check \"src/**/*.{js,ts}\" \"test/**/*.ts\" \"configs/**/*.{yaml,yml}\"",
"lint": "eslint --fix \"src/**/*.ts\" --ignore-pattern \"**/*.d.ts\"",
"test": "jest"
},
Expand All @@ -39,7 +40,7 @@
"@eslint/js": "^9.10.0",
"@octokit/types": "^13.5.0",
"@octokit/webhooks-types": "^7.5.1",
"@types/jest": "^29.5.12",
"@types/jest": "^29.5.13",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.0.0",
"@types/randomstring": "^1.3.0",
Expand Down
4 changes: 2 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default async (app: Probot) => {
app.log.info('OpenSearch Automation App is starting now......');

const srvObj = new Service('Hello World Service');
const resourceConfig: string = process.env.RESOURCE_CONFIG || '';
const processConfig: string = process.env.OPERATION_CONFIG || '';
const resourceConfig: string = process.env.RESOURCE_CONFIG || 'configs/resources/sample-resource.yml';
const processConfig: string = process.env.OPERATION_CONFIG || 'configs/operations/sample-operation.yml';

if (resourceConfig === '' || processConfig === '') {
throw new Error(`Invalid config path: RESOURCE_CONFIG=${resourceConfig} or OPERATION_CONFIG=${processConfig}`);
Expand Down
11 changes: 6 additions & 5 deletions src/utility/opensearchclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ export class OpensearchClient {
const client = new OpenSearchClient({
...AwsSigv4Signer({
region: this.region,
getCredentials: () => Promise.resolve({
accessKeyId: credentials.AccessKeyId!,
secretAccessKey: credentials.SecretAccessKey!,
sessionToken: credentials.SessionToken!,
}),
getCredentials: () =>
Promise.resolve({
accessKeyId: credentials.AccessKeyId!,
secretAccessKey: credentials.SecretAccessKey!,
sessionToken: credentials.SessionToken!,
}),
}),
node: this.openSearchUrl,
});
Expand Down
107 changes: 107 additions & 0 deletions test/utility/opensearchclient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { STSClient, AssumeRoleCommand } from '@aws-sdk/client-sts';
import { Client as OpenSearchClient } from '@opensearch-project/opensearch';
import { OpensearchClient } from '../../src/utility/opensearchclient'; // Adjust import path as needed

jest.mock('@aws-sdk/client-sts');
jest.mock('@opensearch-project/opensearch');
jest.mock('@opensearch-project/opensearch/lib/aws/index', () => ({
AwsSigv4Signer: jest.fn().mockReturnValue({}),
}));

describe('OpensearchClient', () => {
const mockRoleArn = 'arn:aws:iam::123456789012:role/MyRole';
const mockRegion = 'us-east-1';
const mockOpenSearchUrl = 'https://my-opensearch-cluster.example.com';
const mockCredentials = {
AccessKeyId: 'mockAccessKeyId',
SecretAccessKey: 'mockSecretAccessKey',
SessionToken: 'mockSessionToken',
};

beforeAll(() => {
process.env.ROLE_ARN = mockRoleArn;
process.env.REGION = mockRegion;
process.env.OPENSEARCH_URL = mockOpenSearchUrl;
});

afterAll(() => {
delete process.env.ROLE_ARN;
delete process.env.REGION;
delete process.env.OPENSEARCH_URL;
});

// Ensure the STSClient mock is shared across all tests
beforeEach(() => {
(STSClient as jest.Mock).mockImplementation(() => ({
send: jest.fn().mockResolvedValue({
Credentials: mockCredentials,
}),
}));
});

describe('getClient', () => {
it('should return an OpenSearch client with valid credentials', async () => {
const opensearchClient = new OpensearchClient();
const client = await opensearchClient.getClient();

expect(STSClient).toHaveBeenCalledWith({ region: mockRegion });
expect(AssumeRoleCommand).toHaveBeenCalledWith({
RoleArn: mockRoleArn,
RoleSessionName: 'githubWorkflowRunsMonitorSession',
});
expect(client).toBeInstanceOf(OpenSearchClient);
});

it('should throw an error if credentials are undefined', async () => {
// Override the mock for this test case
(STSClient as jest.Mock).mockImplementation(() => ({
send: jest.fn().mockResolvedValue({
Credentials: undefined,
}),
}));

const opensearchClient = new OpensearchClient();

await expect(opensearchClient.getClient()).rejects.toThrow(
'Failed to assume role: credentials are undefined.'
);
});
});

describe('bulkIndex', () => {
let consoleErrorSpy: jest.SpyInstance;

beforeEach(() => {
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
});

afterEach(() => {
consoleErrorSpy.mockRestore();
});

it('should throw an error if bulk indexing fails', async () => {
const mockClient = {
bulk: jest.fn().mockResolvedValue({
body: {
errors: true,
items: [
{
index: { error: { type: 'some_error', reason: 'Some error occurred' } },
},
],
},
}),
};

(OpenSearchClient as jest.Mock).mockImplementation(() => mockClient);

const opensearchClient = new OpensearchClient();
const documents = [{ id: 1, name: 'Document 1' }];
const index = 'test-index';

await expect(opensearchClient.bulkIndex(index, documents)).rejects.toThrow(
'Bulk indexing errors: [{"type":"some_error","reason":"Some error occurred"}]'
);
});
});
});

0 comments on commit e8288db

Please sign in to comment.