-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowing drone to sync with gitea. Co-authored-by: Marc Went <marc.went+git@redkubes.com>
- Loading branch information
Maurice Faber
authored
May 22, 2021
1 parent
cd6d90d
commit 2b2d2ff
Showing
12 changed files
with
144 additions
and
47 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,5 @@ | ||
export const orgName = 'otomi' | ||
export const teamNameOwners = 'Owners' | ||
export const teamNameViewer = 'otomi-viewer' | ||
export const repoName = 'values' | ||
export const username = 'otomi-admin' |
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import { CustomError } from 'ts-custom-error' | ||
|
||
export class GiteaDroneError extends CustomError {} | ||
export const orgName = 'otomi' | ||
export const repoName = 'values' | ||
export const username = 'otomi-admin' |
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,58 @@ | ||
import { OrganizationApi, AdminApi, User, Team } from '@redkubes/gitea-client-node' | ||
import { doApiCall } from '../../utils' | ||
import { cleanEnv, GITEA_PASSWORD, GITEA_URL } from '../../validators' | ||
import { orgName, teamNameOwners, username } from '../common' | ||
|
||
const env = cleanEnv({ | ||
GITEA_PASSWORD, | ||
GITEA_URL, | ||
}) | ||
const errors: string[] = [] | ||
export default async function main(): Promise<void> { | ||
let giteaUrl = env.GITEA_URL | ||
if (giteaUrl.endsWith('/')) { | ||
giteaUrl = giteaUrl.slice(0, -1) | ||
} | ||
// create the org | ||
const adminApi = new AdminApi(username, env.GITEA_PASSWORD, `${giteaUrl}/api/v1`) | ||
|
||
const users: User[] = await doApiCall(errors, `Get all users`, () => adminApi.adminGetAllUsers()) | ||
|
||
const orgApi = new OrganizationApi(username, env.GITEA_PASSWORD, `${giteaUrl}/api/v1`) | ||
await Promise.allSettled( | ||
[teamNameOwners].map( | ||
async (teamName): Promise<any[]> => { | ||
const orgTeams = (await doApiCall(errors, 'Find team ID if exists', () => | ||
orgApi.orgListTeams(orgName), | ||
)) as Team[] | ||
const teamID = orgTeams.find((team) => team.name === teamName)!.id as number | ||
|
||
const membersInTeam = (await doApiCall(errors, 'Find all members in this team', () => | ||
orgApi.orgListTeamMembers(teamID), | ||
)) as User[] | ||
const userIDsInTeam = membersInTeam.map((member) => member.id) | ||
const membersNotInTeam: User[] = users.filter((user) => !userIDsInTeam.includes(user.id)) | ||
|
||
// eslint-disable-next-line no-restricted-syntax | ||
return Promise.allSettled( | ||
membersNotInTeam.map((member) => | ||
doApiCall(errors, `Add ${member.login} to ${teamName}`, () => | ||
orgApi.orgAddTeamMember(teamID, member.login as string), | ||
), | ||
), | ||
) | ||
}, | ||
), | ||
) | ||
|
||
if (errors.length) { | ||
console.error(`Errors found: ${JSON.stringify(errors, null, 2)}`) | ||
process.exit(1) | ||
} else { | ||
console.info('Success!') | ||
} | ||
} | ||
// Run main only on execution, not on import (like tests) | ||
if (typeof require !== 'undefined' && require.main === module) { | ||
main() | ||
} |
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 |
---|---|---|
@@ -1,36 +1,55 @@ | ||
import { CreateRepoOption, UserApi } from '@redkubes/gitea-client-node' | ||
import { doApiCall, handleErrors } from '../../utils' | ||
|
||
import { OrganizationApi, CreateRepoOption, CreateOrgOption, CreateTeamOption } from '@redkubes/gitea-client-node' | ||
import { doApiCall } from '../../utils' | ||
import { cleanEnv, GITEA_PASSWORD, GITEA_URL } from '../../validators' | ||
import { repoName, username } from './common' | ||
import { orgName, repoName, username, teamNameViewer } from '../common' | ||
|
||
const env = cleanEnv({ | ||
GITEA_PASSWORD, | ||
GITEA_URL, | ||
}) | ||
|
||
const errors: string[] = [] | ||
|
||
export async function createTeam(orgApi: OrganizationApi): Promise<void> { | ||
const readOnlyTeam: CreateTeamOption = { | ||
...new CreateTeamOption(), | ||
canCreateOrgRepo: false, | ||
name: teamNameViewer, | ||
includesAllRepositories: true, | ||
permission: CreateTeamOption.PermissionEnum.Read, | ||
units: ['repo.code'], | ||
} | ||
return doApiCall( | ||
errors, | ||
`Creating team "${teamNameViewer}" in org "${orgName}"`, | ||
() => orgApi.orgCreateTeam(orgName, readOnlyTeam), | ||
422, | ||
) | ||
} | ||
|
||
export default async function main(): Promise<void> { | ||
let giteaUrl = env.GITEA_URL | ||
if (giteaUrl.endsWith('/')) { | ||
giteaUrl = giteaUrl.slice(0, -1) | ||
} | ||
|
||
// create the org | ||
const userApi = new UserApi(username, env.GITEA_PASSWORD, `${giteaUrl}/api/v1`) | ||
// const orgApi = new OrganizationApi(username, env.GITEA_PASSWORD, `${giteaUrl}/api/v1`) | ||
// const orgOption = { ...new CreateOrgOption(), username: orgName, repoAdminChangeTeamAccess: true } | ||
// await doApiCall(errors, `Creating org "${orgName}"`, () => orgApi.orgCreate(orgOption), 422) | ||
const orgApi = new OrganizationApi(username, env.GITEA_PASSWORD, `${giteaUrl}/api/v1`) | ||
const orgOption = { ...new CreateOrgOption(), username: orgName, repoAdminChangeTeamAccess: true } | ||
await doApiCall(errors, `Creating org "${orgName}"`, () => orgApi.orgCreate(orgOption), 422) | ||
|
||
// await createTeam(orgApi) | ||
// create the org repo | ||
const repoOption = { ...new CreateRepoOption(), autoInit: false, name: repoName, _private: true } | ||
await doApiCall(errors, `Creating repo "${repoName}"`, () => userApi.createCurrentUserRepo(repoOption)) | ||
// await doApiCall(errors, `Creating org repo "${repoName}"`, () => orgApi.createOrgRepo(orgName, repoOption)) | ||
await doApiCall(errors, `Creating org repo "${repoName}"`, () => orgApi.createOrgRepo(orgName, repoOption)) | ||
// add the | ||
|
||
handleErrors(errors) | ||
if (errors.length) { | ||
console.error(`Errors found: ${JSON.stringify(errors, null, 2)}`) | ||
process.exit(1) | ||
} else { | ||
console.info('Success!') | ||
} | ||
} | ||
// Run main only on execution, not on import (like tests) | ||
|
||
if (typeof require !== 'undefined' && require.main === module) { | ||
main() | ||
} |
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