Skip to content

Commit

Permalink
[BXMSPROD-1182] Add support for gerrit (#443)
Browse files Browse the repository at this point in the history
* added support for gerrit

* added test cases

* bump version

* updated readme and changelog
  • Loading branch information
shubhbapna authored Jun 21, 2023
1 parent d997fa8 commit 76bf5e7
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

# v3.3.0
- Enabled support for Gerrit

# v3.2.0
- Enabled support for multiple platforms

# V3.1.8

## Enhancements:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -801,13 +801,13 @@ List of breaking changes from v2 to v3:
## Using multiple git platforms
You can now define multiple git platforms to clone your projects from. Currently only GitHub and GitLab are supported. Refer to [build-chain-configuration-reader](#https://github.com/kiegroup/build-chain-configuration-reader#platforms-only-in-version-23) on how to define multiple platforms. The advanatge of having this is that you can build projects that are hosted on one platform along with projects that are hosted on another platform.
You can now define multiple git platforms to clone your projects from. Currently only GitHub, GitLab and Gerrit are supported. Refer to [build-chain-configuration-reader](#https://github.com/kiegroup/build-chain-configuration-reader#platforms-only-in-version-23) on how to define multiple platforms. The advanatge of having this is that you can build projects that are hosted on one platform along with projects that are hosted on another platform.
build-chain runs with a default platform configured. This default platform is used for projects which don't have a platform defined and for reading and loading configuration from the definition file.

By default, when you run build-chain as a github action the default platform configuration used is GitHub.

When using build-chain as a CLI tool, build-chain will try to detect the default platform based on the definition file url. If it is not able to detect it from the url, it will use GitHub as the default configuration. If you want to override all of this, you will have the option to define default configuration as CLI options:
When using build-chain as a CLI tool, build-chain will try to detect the default platform based on the definition file url. If it is not able to detect it from the url, it will use GitHub as the default configuration. If you want to override all of this, you will have the option to define default configuration as CLI options (currently only GitHub and GitLab have default options):

```shell
-ghi, --defaultGithubId <id> default github id
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kie/build-chain-action",
"version": "3.2.0",
"version": "3.3.0",
"description": "Library to execute commands based on github projects dependencies.",
"main": "dist/index.js",
"author": "",
Expand Down Expand Up @@ -62,7 +62,7 @@
"@actions/core": "^1.8.2",
"@actions/exec": "^1.1.1",
"@actions/glob": "^0.3.0",
"@kie/build-chain-configuration-reader": "^3.1.3",
"@kie/build-chain-configuration-reader": "^3.1.4",
"@octokit/plugin-throttling": "^5.0.1",
"@octokit/request-error": "^2.1.0",
"@octokit/rest": "^18.12.0",
Expand Down
152 changes: 152 additions & 0 deletions src/service/git/gerrit-api-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { Pulls, Repo } from "@bc/domain/base-git-api-client";
import { BaseGitAPIClient } from "@bc/service/git/base-git-api-client";
import axios, { Axios } from "axios";

export class GerritAPIClient extends BaseGitAPIClient {
private client: Axios;

constructor(baseUrl: string, id: string) {
super(baseUrl, id);
this.client = axios.create({
baseURL: this.baseUrl,
headers: {
Authorization: `Basic ${this.tokenService.getToken(id)}`,
"User-Agent": "kiegroup/github-build-chain-action",
},
});
}

get repos() {
return {
getBranch: this.getBranch.bind(this),
get: this.getRepo.bind(this),
listForkName: this.listForkName.bind(this),
getForkNameForTargetRepoGivenSourceOwner:
this.getForkNameForTargetRepoGivenSourceOwner.bind(this),
};
}

get pulls() {
return {
list: this.listPulls.bind(this),
get: this.getPullRequest.bind(this),
};
}

private async getBranch(args: Repo["getBranch"]["parameters"]) {
const projectId = this.getProjectId(args.owner, args.repo);
const { data, status } = await this.client.get(
`/projects/${projectId}/branches/${args.branch}`
);
return { data, status };
}

private async getRepo(args: Repo["get"]["parameters"]) {
const projectId = this.getProjectId(args.owner, args.repo);
const { data, status } = await this.client.get(`/projects/${projectId}`);
return { data, status };
}

private async listForkName(_args: Repo["listForkName"]["parameters"]) {
this.logger.debug(
"Gerrit does not have the concept of forking. Returning empty array"
);
return { status: 200, data: [] };
}

private async getForkNameForTargetRepoGivenSourceOwner(
_args: Repo["getForkNameForTargetRepoGivenSourceOwner"]["parameters"]
) {
this.logger.debug(
"Gerrit does not have the concept of forking. Returning undefined"
);
return { status: 200, data: undefined };
}

private async listPulls(args: Pulls["list"]["parameters"]) {
const projectId = this.getProjectId(args.owner, args.repo);
let state;
switch (args.state) {
case "opened":
state = "open";
break;
case "closed":
case "merged":
state = "merged";
}

// looks like gerrit does not have any query for head branch
let query = `project:${projectId}`;
if (state) {
query += `+status:${state}`;
}
if (args.base) {
query += `+branch:${args.base}`;
}
const { data, status } = await this.client.get("/changes", {
params: {
q: query,
},
});
return { data, status };
}

private async getPullRequest(args: Pulls["get"]["parameters"]) {
const projectId = this.getProjectId(args.owner, args.repo);
const { data, status } = await this.client.get(
`/changes/${projectId}~${args.pull_number}`
);
const eventData = data as {
branch: string;
current_revision: string;
} & {
[key: string]: {
fetch: {
"anonymous http": {
url: string;
ref: string;
};
};
web_links: [
{
name: "gitweb";
url: string;
},
{
name: "browse";
url: string;
}
];
};
};

return {
data: {
html_url: `${
eventData[eventData.current_revision].fetch["anonymous http"].url
}${eventData[eventData.current_revision].web_links[1].url}`,
head: {
user: {
login: args.owner, // there are no forks so owner is same as base
},
ref: eventData.current_revision,
},
base: {
ref: eventData.branch,
repo: {
full_name: `${args.owner}/${args.repo}`,
name: args.repo,
owner: {
login: args.owner,
},
},
},
},
status,
};
}

private getProjectId(owner: string, repo: string) {
return encodeURIComponent(`${owner}/${repo}`);
}
}
7 changes: 7 additions & 0 deletions src/service/git/git-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ConfigurationService } from "@bc/service/config/configuration-service";
import { BaseGitAPIClient } from "@bc/service/git/base-git-api-client";
import { GerritAPIClient } from "@bc/service/git/gerrit-api-client";
import { GitTokenService } from "@bc/service/git/git-token-service";
import { GitHubAPIClient } from "@bc/service/git/github-api-client";
import { GitlabAPIClient } from "@bc/service/git/gitlab-api-client";
Expand Down Expand Up @@ -35,6 +36,12 @@ export class GitAPIClient {
platform.id
);
break;
case PlatformType.GERRIT:
this.clients[platform.id] = new GerritAPIClient(
platform.apiUrl,
platform.id
);
break;
default:
throw new Error(`${platform} is not supported`);
}
Expand Down
Loading

0 comments on commit 76bf5e7

Please sign in to comment.