-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BXMSPROD-1182] Add support for gerrit (#443)
* added support for gerrit * added test cases * bump version * updated readme and changelog
- Loading branch information
1 parent
d997fa8
commit 76bf5e7
Showing
7 changed files
with
349 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.