generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from ubq-test-jordan/max-assignments
- Loading branch information
Showing
10 changed files
with
133 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Context } from "../../types"; | ||
|
||
interface MatchingUserProps { | ||
role: string; | ||
limit: number; | ||
} | ||
|
||
export async function getUserRoleAndTaskLimit(context: Context, user: string): Promise<MatchingUserProps> { | ||
const orgLogin = context.payload.organization?.login; | ||
const { config, logger } = context; | ||
const { maxConcurrentTasks } = config; | ||
|
||
const smallestTask = Object.entries(maxConcurrentTasks).reduce((minTask, [role, limit]) => (limit < minTask.limit ? { role, limit } : minTask), { | ||
role: "", | ||
limit: Infinity, | ||
} as MatchingUserProps); | ||
|
||
try { | ||
// Validate the organization login | ||
if (typeof orgLogin !== "string" || orgLogin.trim() === "") { | ||
throw new Error("Invalid organization name"); | ||
} | ||
|
||
const response = await context.octokit.orgs.getMembershipForUser({ | ||
org: orgLogin, | ||
username: user, | ||
}); | ||
|
||
const role = response.data.role.toLowerCase(); | ||
const limit = maxConcurrentTasks[role]; | ||
|
||
return limit ? { role, limit } : smallestTask; | ||
} catch (err) { | ||
logger.error("Could not get user role", { err }); | ||
return smallestTask; | ||
} | ||
} |
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
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
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
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,14 +1,27 @@ | ||
[ | ||
{ | ||
"id": 1, | ||
"login": "ubiquity" | ||
"login": "ubiquity", | ||
"role": "admin" | ||
}, | ||
{ | ||
"id": 2, | ||
"login": "user2" | ||
"login": "user2", | ||
"role": "contributor" | ||
}, | ||
{ | ||
"id": 3, | ||
"login": "user3" | ||
"login": "user3", | ||
"role": "collaborator" | ||
}, | ||
{ | ||
"id": 4, | ||
"login": "user4", | ||
"role": "new-start" | ||
}, | ||
{ | ||
"id": 5, | ||
"login": "user5", | ||
"role": "member" | ||
} | ||
] |
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